Working with Kubernetes deployment, pods and service Part1

24 / Oct / 2016 by Rakesh Mahajan 1 comments

In my previous blog, Getting Started with Kubernetes, I have explained the basics of kubernetes. In this blog, I will be explaining how to create deployment, pods, and service.

Deployment and pods: Below command will create a deployment controller named a “my-nginx”, a pod with a single  container of an image nginx (base image of nginx in docker hub) which listens on port 80. Replicas will define how many pods do  you want to run, in this case, it is “1”, so deployment controller will ensure that there should always be one pod running.

kubectl run my-nginx --image=nginx --replicas=1 --port=80

Get the status of the deployment and pod with below commands:

kubectl get deployment -o wide

deployment

kubectl get pods -o wide

pods

Service: Below command will create a service named “my-nginx” with type Loadbalancer, it will create ELB an endpoint. This service listens on the port 80 and redirects it to the target port 80 (the port on which containers listens).

kubectl expose deployment my-nginx --port=80 --type=LoadBalancer

svc

Get the status of the service with the below command:

kubectl get service -o wide

svc1

Curl the ELB endpoint and you should see the Welcome Nginx Page response back.

Deployments, pods, and services can also be created using yaml/json file. Please find below a sample yaml file nginx.yaml:


apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: my-nginx
spec:
replicas: 1
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: my-nginx
spec:
ports:
- name: "www"
port: 80
targetPort: 80
selector:
run: my-nginx
type: LoadBalancer

Run the below command which uses the above yaml and creates deployment controller, a pod and service.

kubectl apply -f nginx.yaml

Assign pod to a specific node: Suppose there has been an application which requires to run on the same machine i.e on the same IP. Steps to achieve the same:

  • Get the nodes
kubectl get nodes
  • Assign label to the node. Here I am assigning node=app label to the node:
kubectl label nodes <node name> node=app
  • Update the yaml to add nodeSelector in pod spec:
---------
 spec:
      containers:
      - name: my-nginx
        image: nginx
        ports:
        - containerPort: 80
        nodeSelector:
         node: app
----
  •  Run the kubectl apply command and get the pods status to verify it is running on the node which has the lable node=app:
kuebctl apply -f nginx.yaml
kubectl get pods -o wide

Hope, now we know how to create deployment pods, services and how to assign pod to a specific node. In the next blog we will covering deployment strategy, multi-container pods, communication between pods.

FOUND THIS USEFUL? SHARE IT

comments (1 “Working with Kubernetes deployment, pods and service Part1”)

Leave a comment -