Home > Article > Web Front-end > CKA Full Course Day Multi Container Pod Kubernetes - Sidecar vs Init Container
In this project, I wanted to apply Kubernetes concepts like init containers and sidecar containers, but I didn’t want to just follow along with a tutorial.
My goal was to build something memorable.
After some brainstorming, I present to you the Get Me App!
The Get Me App is designed to dynamically fetch and display content from various GitHub repositories. The application runs within a Kubernetes pod and comprises two primary components:
Nginx Container: This serves as the main application, hosting the dynamically fetched content. It's lightweight and efficient, making it ideal for serving static web pages.
Sidecar Container: This runs alongside the primary nginx container. It is responsible for refreshing the content every 5 seconds by fetching the latest HTML from a randomly selected GitHub page. This ensures that the content served by Nginx is always up to date.
Init Container: This initializes the environment before the main containers start. It checks for the availability of the Get Me App Service by resolving its DNS entry. This step ensures that the application is ready to interact with the service once it starts running. Read more here (I had to)
The nginx container then serves the updated HTML webpage, making it accessible through a browser via a NodePort service.
Before we start, ensure you have a configuration file to create the Kubernetes cluster. Refer to the Kind Quick Start guide for detailed instructions on setting up your Kind cluster.
Create a file named config.yml with the following content to define your Kind cluster:
kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 name: cka-cluster nodes: - role: control-plane extraPortMappings: - containerPort: 30001 # Change this to match the NodePort hostPort: 30001 listenAddress: "0.0.0.0" protocol: tcp - role: worker - role: worker
Run the following command to create the cluster:
kind create cluster --name kind-cka-cluster --config config.yml
Use the following command to set the context to the new cluster:
kubectl config use-context kind-kind-cka-cluster
First, set up the directory and file structure for the project.
mkdir get-me-app cd get-me-app nano get-me-app.yml
In the get-me-app.yml file, we’ll define a Kubernetes pod that includes the nginx container, a sidecar container for content refreshing, and an init container for initial data fetch.
kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 name: cka-cluster nodes: - role: control-plane extraPortMappings: - containerPort: 30001 # Change this to match the NodePort hostPort: 30001 listenAddress: "0.0.0.0" protocol: tcp - role: worker - role: worker
nginx container: This is the primary container, serving content on port 80. The volumeMount makes the /usr/share/nginx/html directory available in the pod’s workdir volume.
Sidecar container (content-refresher): This container runs a while true loop, downloading the latest version of the webpage every 5 seconds. This ensures that the content in the workdir volume stays updated.
Init container (init-myservice): This waits for the get-me-app-service to become available by continuously performing a DNS lookup. It runs only once during initialization and does not restart after completion.
Volumes: The workdir volume (an emptyDir type) is shared among the containers, allowing the init container, sidecar, and nginx to access and serve the same content.
To make the app accessible through a browser on your local machine, configure a NodePort service to expose the pod’s port 80.
Add this service definition in get-me-app-service.yml:
kind create cluster --name kind-cka-cluster --config config.yml
Deploy the setup with:
kubectl config use-context kind-kind-cka-cluster
Access it by visiting http://localhost:30001 in your browser, and you should the github page. Refresh the page after 5 seconds and you should see a different github page.
This project helped me understand how init containers can initialize shared resources and how sidecar containers keep those resources updated for the main application. It's an engaging way to experiment with and learn about real-time data handling in Kubernetes.
Here’s the formatted content for the section you provided:
End Notes: Please remember that this project is not part of the video tutorials. I wanted to build something on my own using the concepts from the tutorial as general guidance.
The above is the detailed content of CKA Full Course Day Multi Container Pod Kubernetes - Sidecar vs Init Container. For more information, please follow other related articles on the PHP Chinese website!