Skip to content

Latest commit

 

History

History
84 lines (57 loc) · 4.06 KB

K8s-Multicontainer-Sidecar.md

File metadata and controls

84 lines (57 loc) · 4.06 KB

LAB: K8s Multicontainer - Sidecar - Emptydir Volume - Port-Forwarding

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

Steps

  • Run minikube (in this scenario, K8s runs on WSL2- Ubuntu 20.04) ("minikube start")

    image

  • Create Yaml file (multicontainer.yaml) in your directory and copy the below definition into the file:

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):

image

  • Connect (/bin/sh of the webcontainer) and install net-tools to show ethernet interface (IP: 172.17.0.3)

image

  • 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).

image

  • Under the webcontainer, the shared volume with sidecarcontainer can be reachable:

image

  • 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.

image

image

  • We can forward the port of the pod to the host PC port (hostPort:containerPort, e.g: 8080:80):

image

image

  • After updating the content of the index.html, new html page will be downloaded by the sidecarcontainer:

image

  • Exit from the container shell and delete multicontainer in a one pod:

image