List of Topics:
Location Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Create a Kubernetes Pod with Two Containers-One Running an NGINX Web Server and Another Acting as a Logging Sidecar?

Kubernetes

Condition for Create a Kubernetes Pod with Two Containers-One Running an NGINX Web Server and Another Acting as a Logging Sidecar

  • Description:
    In Kubernetes, a pod can run multiple containers that share the same network namespace and storage volumes. This is called the sidecar pattern, commonly used for:
     Logging
     Monitoring
     Proxying traffic
     In this exercise, you will create a sidecar pod with:
     NGINX container: Serves HTTP traffic on port 80.
     BusyBox container: Runs a simple logging command in a loop.
     You will also learn how to expose a pod via port-forwarding to access it in your browser.

Steps

  •  STEP 1 — Create YAML File
     Create a file named nginx-sidecar.yaml with the following content:
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx-sidecar
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
      - name: busybox
        image: busybox
        command: ["sh", "-c", "while true; do echo 'Logging sidecar'; sleep 5; done"]
                
  •  STEP 2 — Apply the Pod
     Run the following command to create the pod:
    kubectl apply -f nginx-sidecar.yaml
                
     Verify the pod is running:
    kubectl get pods
                
     Check logs of the sidecar container:
    kubectl logs nginx-sidecar -c busybox
                
  •  STEP 3 — Expose NGINX via Port-Forwarding
     Forward port 8080 on your local machine to port 80 of the pod:
    kubectl port-forward pod/nginx-sidecar 8086:80
                
  •  STEP 4 — Access NGINX in Browser
     Open your browser and visit:
    http://localhost:8080
                
     You should see the default NGINX welcome page.
  •  STEP 5 — Verify Logging Sidecar
     In another terminal, check the logs of the busybox container:
    kubectl logs nginx-sidecar -c busybox
                
     You should see repeated messages:
    Logging sidecar
    Logging sidecar
                
Screenshots
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469