The pod is a group of one or more containers and the smallest deployable unit in Kubernetes. Pods are always co-located and co-scheduled, and run in a shared context. Each pod is isolated by the following Linux namespaces:
In a pre-container world, they would have been executed on the same physical or virtual machine.
It is useful to construct your own application stack pod (for example, web server and database) that are mixed by different Docker images.
You must have a Kubernetes cluster and make sure that the Kubernetes node has accessibility to the Docker Hub (https://hub.docker.com) in order to download Docker images. You can simulate downloading a Docker image by using the docker pull
command as follows:
//run as root on node machine # docker pull centos latest: Pulling from centos 47d44cb6f252: Pull complete 168a69b62202: Pull complete 812e9d9d677f: Pull complete 4234bfdd88f8: Pull complete ce20c473cd8a: Pull complete Digest: sha256:c96eeb93f2590858b9e1396e808d817fa0ba4076c68b59395445cb957b524408 Status: Downloaded newer image for centos:latest
localhost:80
every three seconds using the curl
command:# cat my-first-pod.yaml apiVersion: v1 kind: Pod metadata: name: my-first-pod spec: containers: - name: my-nginx image: nginx - name: my-centos image: centos command: ["/bin/sh", "-c", "while : ;do curl http://localhost:80/; sleep 3; done"]
kubectl create
command to launch my-first-pod
as follows:# kubectl create -f my-first-pod.yaml pod "my-first-pod" created
It takes between a few seconds and minutes, depending on the network bandwidth to the Docker Hub and Kubernetes nodes spec.
kubectl get pods
to see the status as follows://still downloading Docker images (0/2) # kubectl get pods NAME READY STATUS RESTARTS AGE my-first-pod 0/2 Running 0 6s //it also supports shorthand format as "po" # kubectl get po NAME READY STATUS RESTARTS AGE my-first-pod 0/2 Running 0 7s //my-first-pod is running (2/2) # kubectl get pods NAME READY STATUS RESTARTS AGE my-first-pod 2/2 Running 0 8s
Now both the nginx container (my-nginx
) and the CentOS container (my-centos
) are ready.
kubectl logs
command and specifying the CentOS container (my-centos
) as follows://it shows last 30 lines output (--tail=30) # kubectl logs my-first-pod -c my-centos --tail=30 </body> </html> % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> 100 612 100 612 0 0 4059 0 --:--:-- --:--:-- --:--:-- 4080
As you can see, the pod links two different containers, nginx and CentOS into the same Linux namespace.
When launching a pod, the Kubernetes scheduler dispatches to the kubelet process to handle all the operations to launch both nginx and CentOS containers.
If you have two or more nodes, you can check the -o wide
option to find a node which runs a pod:
//it indicates Node ip-10-96-219-25 runs my-first-pod # kubectl get pods -o wide NAME READY STATUS RESTARTS AGE NODE my-first-pod 2/2 Running 0 2m ip-10-96-219-25
Log in to that node, then you can check the docker ps
command to see the running containers as follows:
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b7eb8d0925b2 centos "/bin/sh -c 'while : 2 minutes ago Up 2 minutes k8s_my-centos.704bf394_my-first-pod_default_a3b78651-a061-11e5-a7fb-06676ae2a427_f8b61e2b 55d987322f53 nginx "nginx -g 'daemon of 2 minutes ago Up 2 minutes k8s_my-nginx.608bdf36_my-first-pod_default_a3b78651-a061-11e5-a7fb-06676ae2a427_10cc491a a90c8d2d40ee gcr.io/google_containers/pause:0.8.0 "/pause" 2 minutes ago Up 2 minutes k8s_POD.6d00e006_my-first-pod_default_a3b78651-a061-11e5-a7fb-06676ae2a427_dfaf502a
You may notice that three containers – CentOS, nginx and pause – are running instead of two. Because each pod we need to keep belongs to a particular Linux namespace, if both the CentOS and nginx containers die, the namespace will also destroyed. Therefore, the pause container just remains in the pod to maintain Linux namespaces.
Let's launch a second pod, rename it as my-second-pod
and run the kubectl create
command as follows:
//just replace the name from my-first-pod to my-second-pod # cat my-first-pod.yaml | sed -e 's/my-first-pod/my-second-pod/' > my-second.pod.yaml # cat my-second.pod.yaml apiVersion: v1 kind: Pod metadata: name: my-second-pod spec: containers: - name: my-nginx image: nginx - name: my-centos image: centos command: ["/bin/sh", "-c", "while : ;do curl http://localhost:80/; sleep 3; done"] # kubectl create -f my-second.pod.yaml pod "my-second-pod" created # kubectl get pods NAME READY STATUS RESTARTS AGE my-first-pod 2/2 Running 0 49m my-second-pod 2/2 Running 0 5m
If you have two or more nodes, my-second-pod
was probably launched by another node, because the Kubernetes scheduler chose the most suitable node.
After your testing, you can run the kubectl delete
command to delete your pod from the Kubernetes cluster:
//running both my-first-pod and my-second-pod # kubectl get pods NAME READY STATUS RESTARTS AGE my-first-pod 2/2 Running 0 49m my-second-pod 2/2 Running 0 5m //delete my-second-pod # kubectl delete pod my-second-pod pod "my-second-pod" deleted # kubectl get pods NAME READY STATUS RESTARTS AGE my-first-pod 2/2 Running 0 54m //delete my-first-pod # kubectl delete pod my-first-pod pod "my-first-pod" deleted # kubectl get pods NAME READY STATUS RESTARTS AGE
This recipe described how to control pods. It is the basic component and operation of Kubernetes. The following recipes will describe advanced operation of pods using a replication controller, services and so on: