This scenario shows:
- how to create multicontainer in one pod,
- how the multicontainers in the same pod have same ethernet interface (IPs),
- how the multicontainers in the same pod can reach the shared volume area,
- how to make port-forwarding to host PC ports
-
Run minikube (in this scenario, K8s runs on WSL2- Ubuntu 20.04) ("minikube start")
-
Create Yaml file (multicontainer.yaml) in your directory and copy the below definition into the file.
-
File: https://github.com/omerbsezer/Fast-Kubernetes/blob/main/labs/pod/multicontainer.yaml
apiVersion: v1
kind: Pod
metadata:
name: multicontainer
spec:
containers:
- name: webcontainer # container name: webcontainer
image: nginx # image from nginx
ports: # opening-port: 80
- containerPort: 80
volumeMounts:
- name: sharedvolume
mountPath: /usr/share/nginx/html # path in the container
- name: sidecarcontainer
image: busybox # sidecar, second container image is busybox
command: ["/bin/sh"] # it pulls index.html file from github every 15 seconds
args: ["-c", "while true; do wget -O /var/log/index.html https://raw.githubusercontent.com/omerbsezer/Fast-Kubernetes/main/index.html; sleep 15; done"]
volumeMounts:
- name: sharedvolume
mountPath: /var/log
volumes: # define emptydir temporary volume, when the pod is deleted, volume also deleted
- name: sharedvolume # name of volume
emptyDir: {} # volume type emtpydir: creates empty directory where the pod is runnning
- Create multicontainer on the pod (webcontainer and sidecarcontainer):
- Connect (/bin/sh of the webcontainer) and install net-tools to show ethernet interface (IP: 172.17.0.3)
- Connect (/bin/sh of the sidecarcontainer) and show ethernet interface (IP: 172.17.0.3).
- Containers running on same pod have same ethernet interfaces and same IPs (172.17.0.3).
- Under the webcontainer, the shared volume with sidecarcontainer can be reachable:
- It can be seen from sidecarcontainer. Both of the container can reach same volume area.
- If the new file is created on this volume, other container can also reach same new file.
- When we look at the sidecarcontainer logs, it pulls index.html file from "https://raw.githubusercontent.com/omerbsezer/Fast-Kubernetes/main/index.html" every 15 seconds.
- We can forward the port of the pod to the host PC port (hostPort:containerPort, e.g: 8080:80):
- On the browser, goto http://127.0.0.1:8080/
- After updating the content of the index.html, new html page will be downloaded by the sidecarcontainer:
- Exit from the container shell and delete multicontainer in a one pod: