Introduction to the use of Init containers in Kubernetes

Introduction to the use of Init containers in Kubernetes

1. Basic introduction

The Init container in Kubernetes, also called the initialization container, is officially provided by K8s and can be used to judge whether our environment has met the conditions required before running the Pod application. For example, we have an application that is Tomcat, but needs to be registered with ZooKeeper.

Just when we were running Tomcat, ZooKeeper failed, which caused our Tomcat to report an error because it could not register the data.

But when we configure the Init container for Tomcat, the effect is no longer the same; because we can let the Init container run untilthe command to judge whether it meets the requirements of our application running, and it will exit successfully, otherwise it will continue to loop. until the condition succeeds.


The role of using the Init container:

  • There is no need to install some auxiliary tools in the application container (such as: nslookup nc)
  • You can configure the Init container to determine whether the startup conditions of the application container are met, and the Init container can access the Secret permission that the application container cannot access.

2. Introduction to the use of the Init container

1) Create the Pod's yamlfile

[root@k8s-master01 ~]# vim busybox-init.yaml
apiVersion: v1
kind: Pod
metadata:
  name: busybox
  labels:
    app: test
spec:
  initContainers:
  - name: init-1
    image: busybox:1.28.4
    command: ['sh','-c','until nslookup kubernetes;do echo Waiting for K8s...;sleep 3;done;']
  - name: init-2
    image: busybox:1.28.4
    command: ['sh','-c','until nslookup tomcat;do echo Waiting for Tomcat...;sleep 3;done;']
  containers:
  - name: busybox
    image: busybox:1.28.4
    command: ['sh','-c','echo Hello World && sleep 3600']
  • When the initialization container we defined runs successfully, it will exit and then start the next container.

2) Start the Pod via the yamlfile

[root@k8s-master01 ~]# kubectl create -f busybox-init.yaml

insert image description here
3) Create Service service

[root@k8s-master01 ~]# kubectl create service clusterip tomcat --tcp=8080

insert image description here
4) Verify
insert image description here

Guess you like

Origin blog.csdn.net/weixin_46902396/article/details/123313017