Working with Kubernetes deployment, pods and service Part1

24 / Oct / 2016 by Rakesh Mahajan 3 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.

[shell]
kubectl run my-nginx –image=nginx –replicas=1 –port=80
[/shell]

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

[shell]
kubectl get deployment -o wide
[/shell]

deployment

[shell]
kubectl get pods -o wide
[/shell]

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).

[shell]
kubectl expose deployment my-nginx –port=80 –type=LoadBalancer
[/shell]

svc

Get the status of the service with the below command:

[shell]
kubectl get service -o wide
[/shell]

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:

[shell]

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

[/shell]

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

[shell]
kubectl apply -f nginx.yaml
[/shell]

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

[shell]
kubectl get nodes
[/shell]

  • Assign label to the node. Here I am assigning node=app label to the node:

[shell]
kubectl label nodes <node name> node=app
[/shell]

  • Update the yaml to add nodeSelector in pod spec:

[shell]
———
spec:
containers:
– name: my-nginx
image: nginx
ports:
– containerPort: 80
nodeSelector:
node: app
—-
[/shell]

  •  Run the kubectl apply command and get the pods status to verify it is running on the node which has the lable node=app:

[shell]
kuebctl apply -f nginx.yaml
kubectl get pods -o wide
[/shell]

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 (3)

  1. naveen

    Hi Rakesh,
    We are planning to run nginx only to particular node. Is this possible? If so can you kindly share the command to run that?

    Note : we have created 3 nodes and planning to run on particular VM out of those 3 VMs.

    Thanks in advance.

    Regards,
    Naveen

    Reply
  2. Pavel

    Hi Rakesh,
    Very good and informative post. I succeeded with everything except getting the ELB public address (I’m on AWS too). It shows as all the time. What is your setup with AWS? Did you create a separate VPC for your installation? I read somewhere that if you create your K8S cluster using kubeadm (like I did) then AWS load balancer won’t be accessible for you.
    Could you clarify, please?
    Thanks,
    Pavel

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *