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.
#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 |
Let us access the pod; curl http://10.233.84.91:5000
Delete Pod
#Delete Pod |
Points to note:
- We need not specify the node, kubernetes will automatically select a node from the cluster and deploy the pod
- When a pod is launched, Kubernetes will automatically assign an ip address to the pod. This ip address comes from cluster private network range
- 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 |
Scale Up
#Scale up the instances; Don’t delete existing instances |
Availability
#Manually delete a pod |
Roll out
#Update deployment.yaml |
Rollback Changes
#Get Rollout history |
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:
- 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.
- Load balancing: If your application is running on multiple pod how do you distribute load to all pods
- Access from External world: Your pod ip addresses are not reachable from outside the cluster.
- Access External World Services: Your pods want to access database which is outside kubernetes cluster.
Service Types:
- Cluster IP: Exposes the service on a cluster-internal IP. Reachable only within the cluster. Suited for backend services like databases, caches, etc.
- NodePort: This like a ClusterIP Service, but exposes the Service on the same port of each selected Node in the cluster using NAT
- 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
- 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 |
Create Service – Nodeport
Service manifest is below
#Create deployment |