Thursday 16 July 2020

Kubernetes: Specify multiple containers in the same pod definition

A Pod can encapsulate more than one container. For example, following Pod definition encapsulate two containers.

a.   Container ‘user-service-terminal’ runs with the image busybox.

b.   Container ‘user-service’ runs with the image jboss/wildfly.

 

podWithTwoContainers.yml

apiVersion: v1
kind: Pod
metadata:
  name: container-with-two-pods
  labels:
    app: hello-world
    author: krishna
    serviceType: user-service
spec:
  containers:
    - name: user-service-terminal
      image: busybox
      command:
        - sleep 
        - "30"
    - name: user-service
      image: jboss/wildfly

Let’s create Pod using the above definition file.

$kubectl create -f podWithTwoContainers.yml 
pod/container-with-two-pods created

Let’s get more information about the pod using ‘kubectl describe’ command.

$kubectl describe pod container-with-two-pods
Name:         container-with-two-pods
Namespace:    default
Priority:     0
Node:         minikube/192.168.99.100
Start Time:   Sat, 06 Jun 2020 16:23:44 +0530
Labels:       app=hello-world
              author=krishna
              serviceType=user-service
Annotations:  <none>
Status:       Running
IP:           172.17.0.6
IPs:
  IP:  172.17.0.6
Containers:
  user-service-terminal:
    Container ID:  docker://6a116bacf83bd5d443ba13a3a838379c026ed5a43e2933cf6fdb244bb703eccd
    Image:         busybox
    Image ID:      docker-pullable://busybox@sha256:95cf004f559831017cdf4628aaf1bb30133677be8702a8c5f2994629f637a209
    Port:          <none>
    Host Port:     <none>
    Command:
      sleep
      30
    State:          Running
      Started:      Sat, 06 Jun 2020 16:23:50 +0530
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-w7rp7 (ro)
  user-service:
    Container ID:   docker://09b65ce085383ee69df6afe574341d0f7d22ee04f133431252e62c5d5a4176f2
    Image:          jboss/wildfly
    Image ID:       docker-pullable://jboss/wildfly@sha256:67a4f90b213bc2600d08d90e82df58be83813d118d163fbcc8765b6eeaade7e6
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Sat, 06 Jun 2020 16:23:54 +0530
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-w7rp7 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-w7rp7:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-w7rp7
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age        From               Message
  ----    ------     ----       ----               -------
  Normal  Scheduled  <unknown>  default-scheduler  Successfully assigned default/container-with-two-pods to minikube
  Normal  Pulling    22s        kubelet, minikube  Pulling image "busybox"
  Normal  Pulled     16s        kubelet, minikube  Successfully pulled image "busybox"
  Normal  Created    16s        kubelet, minikube  Created container user-service-terminal
  Normal  Started    16s        kubelet, minikube  Started container user-service-terminal
  Normal  Pulling    16s        kubelet, minikube  Pulling image "jboss/wildfly"
  Normal  Pulled     12s        kubelet, minikube  Successfully pulled image "jboss/wildfly"
  Normal  Created    12s        kubelet, minikube  Created container user-service
  Normal  Started    12s        kubelet, minikube  Started container user-service

You can confirm that this Pod is running with two containers from Containers:, Events: section.

 

You can delete the pod by executing the below command.

$kubectl delete pod container-with-two-pods
pod "container-with-two-pods" deleted

When multiple containers running in the same Pod, they can share the data using shared storage.


Previous                                                    Next                                                    Home

No comments:

Post a Comment