Home  K8s   How to add ...

How to add multiple containers inside a single Pod template within a k8s Deployment manifest

You can have multiple containers inside a single Pod template within a Deployment manifest. This allows you to run tightly coupled applications together. The containers within a Pod share the same network namespace and can communicate with each other using localhost. They can also share storage volumes.

Here is an example of a Deployment manifest with multiple containers in a single Pod:

Example Deployment Manifest with Multiple Containers

apiVersion: apps/v1
kind: Deployment
metadata:
  name: multi-container-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: multi-container-app
  template:
    metadata:
      labels:
        app: multi-container-app
    spec:
      containers:
      - name: nginx-container
        image: nginx:latest
        ports:
        - containerPort: 80
      - name: sidecar-container
        image: busybox:latest
        command: ['sh', '-c', 'while true; do echo "Hello from the sidecar"; sleep 5; done']

Explanation:

Use Case:

Apply the Deployment:

  1. Save the YAML to a file, for example multi-container-deployment.yaml.

  2. Apply the deployment to your Kubernetes cluster:

    kubectl apply -f multi-container-deployment.yaml
    
  3. Verify the Deployment:

    kubectl get deployments
    
  4. Inspect the Pods:

    kubectl get pods
    
  5. Check the logs of each container:

    kubectl logs <pod-name> -c nginx-container
    kubectl logs <pod-name> -c sidecar-container
    
Published on: Jul 03, 2024, 06:20 AM  
 

Comments

Add your comment