Kubernetes – Pod, Deployment & Service

In this post I want to introduce some kubernetes commands with examples.

Lab Environment

Docker Image

nxgcloud/simpleweb is a simple flask application which returns container ip address, container host name. If these environment variables (MY_NODE_NAME, MY_POD_NAME,MY_NODE_IP and MY_POD_IP) exist , it will show them. In the later section, we will see how to set the environment variables. The container exposes port 5000.

Sample output1:

Sample output2:

The commands and Yaml files can be seen here .

POD

We can launch pod either from image directly or from pod manifest (yaml file). It is recommended to use yaml file. xx

#Launch the pod without yaml file

kubectl run webpod01 –image=nxgcloud/simpleweb:0.0.2

#Launch the pod from yaml file. Download yaml from here

kubectl create -f pod.yaml

#Get the list of pods
kubectl get pods
kubectl get pods -o wide

Let us access the pod; curl http://10.233.84.91:5000

Delete Pod

#Delete Pod
kubectl delete -f pod.yaml

Points to note:

  1. We need not specify the node, kubernetes will automatically select a node from the cluster and deploy the pod
  2. When a pod is launched, Kubernetes will automatically assign an ip address to the pod. This ip address comes from cluster private network range
  3. The cluster network is not accessible from outside world

Deployment

Deployment is responsible for creating and updating instances of your applications ( a.k.a pods)

Deployment simplifies the tasks below

  • Multiple instances: I want to deploy ‘n’ instances of same pod. E.g. 5 instances of web pod
  • Scale up: Application is experiencing high load, increase the number of instances
  • Availability: What happens if a node is down? Deployment controller continuously monitor the pod instances and if an instance is down it will be automatically replaced
  • Rollout new changes: I am running 4 pods and want to change the docker image to next version.
  • Rollback: New update did not work, can you roll back?

Create Deployment

#Create Deployment. Download yaml from here
kubectl create -f deployment.yaml
kubectl get deployment
kubectl get rs # List the release set
kubectl get pods # Pod names contain the release set version

Scale Up

#Scale up the instances; Don’t delete existing instances
# Update deployment.yaml

replicas: 2

# Run the kubectl apply
kubectl apply -f deployment.yaml

Availability

#Manually delete a pod
kubectl get pods
kubectl delete pod simpleweb-deployment-78c5bf47f8-phf5n
#List the pods
kubectl get pods

Roll out

#Update deployment.yaml
Old deployment.yaml

image: nxgcloud/simpleweb:0.0.1

New deployment.yaml

image: nxgcloud/simpleweb:0.0.2

#Rollout new changes
kubectl apply -f deployment.yaml

Rollback Changes

#Get Rollout history
kubectl rollout history deploy/simpleweb-deployment
#Undo Rollout
kubectl rollout undo deploy/simpleweb-deployment –to-revision=1

Service

A Kubernetes Service is an abstraction which defines a logical set of Pods and a policy by which to access them.

Service solves the below problems:

  1. Abstraction: What happens when a pod is died? Deployment will replace it with another pod. With a new pod comes a new ip. How can other dependent applications talk to your application when ips keep changed.
  2. Load balancing: If your application is running on multiple pod how do you distribute load to all pods
  3. Access from External world: Your pod ip addresses are not reachable from outside the cluster.
  4. Access External World Services: Your pods want to access database which is outside kubernetes cluster.

Service Types:

  1. Cluster IP: Exposes the service on a cluster-internal IP. Reachable only within the cluster. Suited for backend services like databases, caches, etc.
  2. NodePort: This like a ClusterIP Service, but exposes the Service on the same port of each selected Node in the cluster using NAT
  3. Load balancer: Integrates a 3rd party load balancer with the service. Creates an external load balancer in the current cloud (if supported) and assigns a fixed, external IP to the Service
  4. External Name: Exposes the Service using an arbitrary name by returning a CNAME record with the name. Used to access external services from kubernetes cluster.

Create Service – Cluster IP

We use the below yaml files for creating this service. These files can be downloaded from here .

#Create deployment
kubectl apply -f deployment-2.yaml
#Create service
kubectl apply -f service-clusterip.yaml
#Get list of services
kubectl get service
#kubectl get pod ips
kubectl get pods -o wide
#curl service ip repeatedly and see the request is going to all pods
#clean up the deployment and service
kubectl delete service servicecip
kubectl delete deployment simpleweb-deployment

Create Service – Nodeport

Service manifest is below

#Create deployment
kubectl apply -f deployment-2.yaml
#Create service
kubectl apply -f service-clusterip.yaml
#Get list of services
kubectl get service
#kubectl get pod ips
kubectl get pods -o wide
#curl http://:30080
#clean up the deployment and service
kubectl delete service servicecnp
kubectl delete deployment simpleweb-deployment