Can a pod have two containers with same image?

8/23/2019

For example two redis containers.

Can we do the following?

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webserver
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.16-alpine
        ports:
        - containerPort: 80
      - name: redis1
        image: redis
        ports:
        - containerPort: 891
      - name: redis2
        image: redis
        ports:
        - containerPort: 789
-- CriticalRebel
docker
kubernetes
yaml

1 Answer

8/23/2019

All the containers in a pod share same localhost hostname. All containers can be reached using pod's IP address. Like on your workstation, if you already have nginx running on port 80, you cannot use the same port for another nginx instance or any other application.
I don't get it why you'd use same image to create two containers. If it's for scaling up the application, you could just scale up using more replicas.

-- moazzem
Source: StackOverflow