Hi, I am trying to use multi-container pod for nginx so that sidecar container c . . .

akshay deshpande:
Hi, I am trying to use multi-container pod for nginx so that sidecar container can overwrite the contents of Nginx html page and also I can access the same on browser so I created Service as well but it is still failing. Can someone help me understand what i am doing wrong?

apiVersion: v1
kind: Pod
metadata:
  name: multi-pod
  labels:
    name: app
spec:

  restartPolicy: Never

  volumes:
  - name: shared-data
    emptyDir: {}

  containers:
  - name: nginx-container
    image: nginx
    volumeMounts:
    - name: shared-data
      mountPath: /usr/share/nginx/html
    ports:
    - containerPort: 8080
      hostPort: 8080

  - name: ubuntu-container
    image: ubuntu
    volumeMounts:
    - name: shared-data
      mountPath: /usr/share/nginx/html
    command: ["/bin/sh"]
    args: ["-c", "echo Today is `TZ=':US/Eastern' date +'%m-%d-%Y'` and time is `TZ=':US/Eastern' date +'%I:%M:%S %p'` > /usr/share/nginx/html/index.html"]
---
apiVersion: v1
kind: Service
metadata:
  name: my-nginx
spec:
  type: NodePort
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080
    nodePort: 30007
  selector:
    name: app

Kedarnath Belavanaki:
There is no volume created to store the file created by sidecar container.

Kedarnath Belavanaki:
Please add :
volumes:

  • name : shared-data
    emptyDir: {}

Ashok Kumar:
Hey @akshay deshpande
Check the below

apiVersion: v1
kind: Pod
metadata:
  name: multi-pod
  labels:
    name: app
spec:
  restartPolicy: Never
  volumes:
  - name: shared-data
    emptyDir: {}
  containers:
  - name: nginx-container
    image: nginx
    volumeMounts:
    - name: shared-data
      mountPath: /usr/share/nginx/html
    ports:
    - containerPort: 8080
      hostPort: 8080
  - name: ubuntu-container
    image: ubuntu
    volumeMounts:
    - name: shared-data
      mountPath: /usr/share/nginx/html
    args:
    - /bin/sh
    - -c
    - while true; do echo Today is `TZ=':US/Eastern' date +'%m-%d-%Y'` and time is `TZ=':US/Eastern' date +'%I:%M:%S %p'` > /usr/share/nginx/html/index.html; sleep 5; done
---
apiVersion: v1
kind: Service
metadata:
  name: my-nginx
spec:
  type: NodePort
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 8080
    nodePort: 30007
  selector:
    name: app

You are using ubuntu container which means it will terminate once its job is over, therefore you will see only one container always running 1/2
Try to use only args it’s simple instead of command and then args
To make the best of output on the browser use LoadBalancer

Ashok Kumar:
Also important headsup
Though you change to Loadbalancer you will see nothing on the browser that’s because nginx listens only on port 80 For custom port you need to change the nginx.conf file or attach it during creation. For more https://gist.github.com/petitviolet/d36f33d145d0bbf4b54eb187b79d0244|info

Ashok Kumar:
Also check <https://collabnix.github.io/kubelabs/pods101/deploy-your-first-nginx-pod.html|this >for your exact use case on multi-container pod example

akshay deshpande:
Thanks @Ashok Kumar