How it works...

When launching a Pod, the Kubernetes scheduler dispatches to the kubelet process to handle all the operations to launch both nginx and CentOS containers on one Kubernetes node.

The following diagram illustrates these two containers and the Pod; these two containers can communicate via the localhost network, because within the Pod containers, it share the network interface:

A Pod has two containers, which can communicate via localhost

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 "minikube" runs my-first-pod 
$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
my-first-pod 2/2 Running 0 43m 172.17.0.2 minikube

Log in to that node, then you can check the docker ps | grep my-first-pod command to see the running containers as follows: 

List of containers that belong to my-first-pod

You may notice that my-first-pod contains 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


//metadata.name has been changed to my-second-pod
$ 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 10; done"]


//create second pod
$ kubectl create -f my-second-pod.yaml
pod "my-second-pod" created


//2 pods are running
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
my-first-pod 2/2 Running 0 1h
my-second-pod 2/2 Running 0 43s

Now you have two Pods; each Pod has two containers, centos and nginx. So a total of four containers are running on your Kubernetes cluster as in the following diagram:

Duplicate Pod from my-first-pod to my-second-pod
If you would like to deploy more of the same Pod, consider using a Deployment (ReplicaSet) instead. 

After your testing, you can run the kubectl delete command to delete your Pod from the Kubernetes cluster: 

//specify --all option to delete all pods
$ kubectl delete pods --all
pod "my-first-pod" deleted
pod "my-second-pod" deleted


//pods are terminating
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
my-first-pod 2/2 Terminating 0 1h
my-second-pod 2/2 Terminating 0 3m
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.21.47