Skip to main content

Command Palette

Search for a command to run...

24. Sidecar and Init Containers in Kubernetes

Updated
2 min read
24. Sidecar and Init Containers in Kubernetes
K

"Hello, I'm Kiran Pawar, a passionate Cloud and Devops Engineer with a strong background in cloud automation, configuration, and deployment. My journey in the world of technology has been a thrilling adventure, where I've had the privilege to work with cutting-edge tools and practices.

🚀 As a DevOps Engineer:

I specialize in automating, configuring, and deploying instances in cloud environments and data centers. My expertise extends to DevOps, GitOps, CI/CD pipeline management, HashiCorp Terraform, and containerization. I'm proficient in AWS and Linux/Unix administration, ensuring robust infrastructure and application performance.

🔧 My Tech Stack:

Front-end skills: HTML, CSS, SCSS, Tailwind CSS, Bootstrap, React, Material-UI, JavaScript DevOps toolbox: GIT, OWASP,Nexus,Trivy, Github, Gitlab, Terraform, Ansible, Docker, Kubernetes, Helm, Jenkins, Prometheus, Grafana, Argo CD, AWS EKS.

🌐 My Cloud Expertise:

I have hands-on experience managing AWS services, including EC2, S3, EBS, VPC, ELB, RDS, IAM, Route53, and more.

🔒 Networking and Security:

My skills include managing networking concepts such as TCP/IP protocols, security policies, and subnet interfacing. I have a strong understanding of infrastructure and networking, covering topics like firewalls, IP addressing, DNS, and more.

💡 What Sets Me Apart:

I bring a positive attitude, a strong work ethic, and a collaborative spirit to every project. I'm a self-starter, a fast learner, and an effective team player with strong interpersonal skills. In addition to my DevOps skills, I've developed shell scripts (Bash) for automating tasks and have proficiency in Python scripting. My ability to communicate and manage projects, along with a track record of resolving client issues, adds value to every team I work with. If you're looking for a DevOps engineer who is also well-versed in front-end technologies, feel free to connect with me. Let's explore new possibilities and create exceptional technical solutions together!"

In Kubernetes, Init Containers and Sidecar Containers enhance the functionality and modularity of applications by handling tasks like initialization, logging, or synchronization.


1. Init Containers

Init Containers run before the main application containers in a Pod. They perform initialization tasks required by the main containers to function correctly. These tasks could include setup operations, checking preconditions, or injecting configurations.


Example: Initialization Pod

File: init-container.yml

kind: Pod
apiVersion: v1
metadata:
  name: init-test
spec:
  initContainers:
  - name: init-container
    image: busybox:latest
    command: ["sh", "-c", "echo 'Initialization started ...'; sleep 10; echo 'Initialization completed.'"]
  containers:
  - name: main-container
    image: busybox:latest
    command: ["sh", "-c", "echo 'Main container started'"]

Deployment and Verification

  1. Deploy Pods:

     kubectl apply -f init-container.yml
    
  2. Check Pod Status:

     kubectl get pods
    
  3. View Logs:

     kubectl logs init-test -c init-container
    

Key Features

  1. Sequential Execution: The init-container runs before the main-container.

  2. Purpose:

    • Prepping resources.

    • Verifying dependencies.

  3. Behavior:

    • If the Init Container fails, the Pod retries until the initialization succeeds.

2. Sidecar Containers

Sidecar Containers are companion containers that augment the main application. They often handle auxiliary tasks like logging, proxying, or data synchronization.


Example: Sidecar Pattern Pod

File: sidecar-container.yml

kind: Pod
apiVersion: v1
metadata:
  name: sidecar-test
spec:
  volumes:
  - name: shared-logs
    emptyDir: {}

  containers:
  # Produce logs
  - name: main-container
    image: busybox
    command: ["sh", "-c", "while true; do echo 'Hello Dosto' >> /var/log/app.log; sleep 5; done"]
    volumeMounts:
    - name: shared-logs
      mountPath: /var/log/

  # Display logs
  - name: sidecar-container
    image: busybox
    command: ["sh", "-c", "tail -f /var/log/app.log"]
    volumeMounts:
    - name: shared-logs
      mountPath: /var/log/

Deployment and Verification

  1. Deploy Pods:

    
     kubectl apply -f sidecar-container.yml
    
  2. Check Pod Status:

     kubectl get pods
    
  3. View Logs:

     kubectl logs sidecar-test -c sidecar-container
    


Key Features

  1. Shared Volume: Both containers share a volume (shared-logs) for communication.

  2. Main Container:

    • Generates logs (app.log).
  3. Sidecar Container:

    • Continuously displays logs using the tail command.
  4. Purpose:

    • Enhances modularity by decoupling auxiliary tasks (e.g., logging, monitoring).

Benefits of Using Init and Sidecar Containers

Init Containers:

  • Simplify startup logic.

  • Guarantee that prerequisites are met before the main container starts.

Sidecar Containers:

  • Promote reusability and modular design.

  • Offload non-core functionalities from the main application.


More from this blog

Kiran Pawar's Blog

122 posts