Sidecar Container
Copy pod.yaml to pod-sidecar.yaml and modify the file as here:
File: /home/student/pod-sidecar.yaml
apiVersion: v1
kind: Pod
metadata:
name: website
labels:
app: application01
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
- name: centos
image: centos:7
command: [ "/bin/bash", "-c", "--" ]
args: [ "while true; do sleep 30; done;" ]
Better to delete the POD create few minutes ago.
kubectl delete pod website
pod "website" deleted
Now you can apply using Declarative approach.
kubectl apply -f pod-sidecar.yaml
pod/website created
List POD and check if 2/2 are ready. 2 containers (nginx and CentOS) of 2 are correctly ready.
kubectl get pod
NAME READY STATUS RESTARTS AGE website 2/2 Running 0 94s
Try to execute the command env inside the POD
student@master:~$ kubectl exec website env
Defaulting container name to nginx. Use 'kubectl describe pod/website -n default' to see all of the containers in this pod. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=website KUBERNETES_SERVICE_HOST=10.96.0.1 KUBERNETES_SERVICE_PORT=443 KUBERNETES_SERVICE_PORT_HTTPS=443 KUBERNETES_PORT=tcp://10.96.0.1:443 KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443 KUBERNETES_PORT_443_TCP_PROTO=tcp KUBERNETES_PORT_443_TCP_PORT=443 KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1 NGINX_VERSION=1.17.1 NJS_VERSION=0.3.3 PKG_RELEASE=1~stretch HOME=/root
As you can see the hostname of this POD is website. You can try to execute command directly in the container passing the parameter -c name_of_container
student@master:~$ kubectl exec website -c centos env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=website KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443 KUBERNETES_PORT_443_TCP_PROTO=tcp KUBERNETES_PORT_443_TCP_PORT=443 KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1 KUBERNETES_SERVICE_HOST=10.96.0.1 KUBERNETES_SERVICE_PORT=443 KUBERNETES_SERVICE_PORT_HTTPS=443 KUBERNETES_PORT=tcp://10.96.0.1:443 HOME=/root
You can use the same command for connect in interactive mode and in tty on the container CentOS.
kubectl exec website -ti -c centos -- /bin/bash
[root@website /]#
You are inside the container CentOS.
Try now to use curl for connect on localhost website.
[root@website /]# curl localhost
<!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>
From CentOS you can contact other containers using localhost. Try to create a folder inside your containers.
[root@website /]# mkdir container-centos
[root@website /]# ls
anaconda-post.log container-centos etc lib media opt root sbin sys usr bin dev home lib64 mnt proc run srv tmp var
The folder container-centos are here. Check the process of the container CentOS:
[root@website /]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 11692 2736 ? Ss 10:34 0:00 /bin/bash -c -- while true; do sleep 30; done; root 74 0.0 0.0 4372 688 ? S 10:57 0:00 sleep 30 root 75 2.0 0.0 11832 3036 pts/0 Ss 10:57 0:00 /bin/bash root 88 0.0 0.0 51752 3520 pts/0 R+ 10:57 0:00 ps aux
As you can see, you can read only the process of this container, and not of nginx.
You can exit from CentOS container
[root@website /]# exit
exit
Now enter in nginx container
kubectl exec website -ti -c nginx -- /bin/bash
root@website:/#
root@website:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
The folder container-centos are not here. The image of nginx do not container the command ps, so you should install it.
root@website:/# apt-get update && apt-get install procps
<output_omitted>
Check the processes of the container nginx
root@website:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 32656 5160 ? Ss 10:34 0:00 nginx: master process nginx -g daemon off; nginx 6 0.0 0.0 33112 3016 ? S 10:34 0:00 nginx: worker process root 17 0.0 0.0 18188 3352 pts/0 Ss 10:48 0:00 /bin/bash root 284 0.0 0.0 36636 2872 pts/0 R+ 10:56 0:00 ps aux
The namespace pids and mounts are private for container, while net, IPC e UTS are shared namespace of the POD.
[root@website /]# exit
exit
Now you can delete the pod.
kubectl delete pod website
pod "website" deleted