Kubernetes Learning — Init Containers
Tharindu Panagoda
Site Reliability Engineer | AWS Solution Architect | Terraform | Ansible | Python | RHCE | RHCSA |
Init containers are special containers that run before main application container start in a pod. It will be run as a preparation for the main container like initialization, configuration or setup dependencies.
There are few advantages of using init containers
Init containers will be useful in various scenarios.
We can create a simple Nginx server that will display its pod id address on the index page using init containers.
Pre-requisites
You can use your own Kubernetes cluster of use free sandbox like Redhat Open shift Developer Sandbox
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-server-deployment
spec:
replicas: 1
selector:
matchLabels:
app: web-server
template:
metadata:
labels:
app: web-server
spec:
volumes:
- name: web-content
emptyDir: {}
initContainers:
- name: get-ip
image: busybox
command: ['sh', '-c', 'echo $POD_IP > /web-content/ip.txt']
env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
volumeMounts:
- name: web-content
mountPath: /web-content
- name: create-index
image: busybox
command: ['sh', '-c', 'echo "<h1>Pod IP: $(cat /web-content/ip.txt)</h1>" > /web-content/index.html']
volumeMounts:
- name: web-content
mountPath: /web-content
containers:
- name: nginx
image: nginxinc/nginx-unprivileged:stable-alpine #I am using the unprivileged image due to a limitation in Openshift cluster, You can use any nginx image.
ports:
- containerPort: 8080
volumeMounts:
- name: web-content
mountPath: /usr/share/nginx/html
There are two init containers defined here,
领英推荐
one init container “get-ip” will get the node IP Address using field reference values, save it to environment variable and then save that content to a file in volume mount.
The other init container “create-index” will get that value from the file and save it to index.html file in the same volume mount.
Finally, our application container “nginx” will read the index file.
2. Apply the configuration
kubectl apply -f pod-with-init-containers.yaml
3. Deploy a service to expose the port of the application container.
apiVersion: v1
kind: Service
metadata:
name: web-server-service
spec:
type: NodePort # You can change this to ClusterIP or LoadBalancer as needed
selector:
app: web-server # This should match the pod label
ports:
- port: 8080 # Port that the service will expose
targetPort: 8080 # Port on the container to forward requests to
4. Wait for the pod to be ready:
kubectl get pods -w
5. Go to your load balancer or cluster dns to check whether the application is working.
Example: https://localhost:8080