How to do it...

The following are the steps to create a Pod has 2 containers:

  1. Log on to the Kubernetes machine (no need to log on if using minikube) and prepare the following YAML file. It defines the launch nginx container and the CentOS container. 
  2. The nginx container opens the HTTP port (TCP/80). On the other hand, the CentOS container attempts to access the 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 10; done"]
  1. Then, execute the 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 a few minutes, depending on the network bandwidth of the Docker Hub and Kubernetes node's spec.

  1. You can check 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 ContainerCreating 0 14s


//my-first-pod is running (2/2)
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
my-first-pod 2/2 Running 0 1m

Now both the nginx container (my-nginx) and the CentOS container (my-centos) are ready.

  1. Let's check whether the CentOS container can access nginx or not. You can run the kubectl exec command to run bash on the CentOS container, then run the curl command to access the nginx, as follows:
//run bash on my-centos container
//then access to TCP/80 using curl
$ kubectl exec my-first-pod -it -c my-centos -- /bin/bash
[root@my-first-pod /]#
[root@my-first-pod /]# curl -L http://localhost:80
<!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>

As you can see, the Pod links two different containers, nginx and CentOS, into the same Linux network namespace.

..................Content has been hidden....................

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