Can we have same kind of multiple containers in a Pod in Kubernetes?
Asked Answered
E

3

6

For instance can I have following yaml to produce a pod with multiple containers:

apiVersion: v1
kind: Pod
metadata:
name: lampapp
labels:
    app: app
spec:
  containers:
  - name: lampdb
    image: mysql_test
  - name: app
    image: php-app-db-url-env
    env:
     - name: DB_URL
      value: 127.0.0.1:3306
  - name: app2
    image: php-app-db-url-env
    env:
    - name: DB_URL
      value: 127.0.0.1:3306
Elisavetpol answered 23/10, 2015 at 11:22 Comment(3)
Check this to better understand how to write a spec file: cloud.google.com/container-engine/docs/spec-schemaRubie
@Rubie Link is now broken. Gotta love chasing Google and their ever-out-of-date docs!Aisha
The latest docs are here now: kubernetes.io/docs/api-reference/v1.5 . Change the "1.5" with the desired version of your kube cluster.Rubie
C
3

Yes, you can add multiple container with same image.

The containers object must contain:

  1. name: Name of the container. It must be a DNS_LABEL and be unique within the pod. Cannot be updated.
  2. image: Docker image name.

You have to make container name unique

You can do following:

- name: app
  image: php-app-db-url-env   ---
- name: app2                    |> same image
  image: php-app-db-url-env   ---

But not this one:

- name: app
  image: php-app-db-url-env
- name: app
  image: <any image>

Also the containers spec should include a unique port number within the Pod

Cockchafer answered 23/10, 2015 at 21:58 Comment(3)
Thanks. I tried above structure but what it did was : it ups the first container but terminates the second one in few seconds. And it keeps on restarting it.Elisavetpol
May be they are trying to listen same port.. Check thatCockchafer
kubectl describe should give you a clue as to what is going wrongInformer
E
1

Same of kind of containers can be there but then their port would be different.

Elisavetpol answered 22/6, 2017 at 10:30 Comment(1)
Yes, its a good remark. If you have 2 containers with the same image and different names be careful with the containers params. If containers would be have the same port options only the last one container would be running.Urion
R
0

Well, that's exactly what a pod is: multiple containers that share some namespaces and volumes.

Rubie answered 23/10, 2015 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.