Introduction Init container
Pod can have a plurality of containers, applications running inside the container, but it may also have one or more applications prior to the start of the container vessel Init
Init vessel much like an ordinary container, except the following two points:
C Init container always run until successful completion of
2) Init each container must be completed before the next start a successful Init container
if the container Pod's Init failure, Kubernetes will continue to restart the Pod, Init container until it succeeds. However, if the corresponding restartPolicy Pod is Never.
Init role of the container
Because Init container has a separate application container alone mirroring, so they start the relevant code has the following advantages:
1) they can contain and run the utility, but for security reasons, it is not recommended to include these in the mirror in the application container utility
2) which may comprise the use of tools to install and custom code, but does not appear in the application image. For example, you do not need to create a mirror image FROM another, only need to use similar sed, awk, python or dig during the installation
of such tools.
3) the application image can be separated from the role created and deployed, but they do not need to jointly build a single image.
4) Init containers Linux Namespace, the relative container, the application has a different file system views. Therefore, they can have access to the Secret, and the application container can not.
5) They must be run to completion before starting the application container, the container and the application run in parallel, so that the container can be provided a method Init a simple delay block or start the application of the container until a set of prerequisites met.
test
Description: mainly in the start Pod, there are two initc, a start is not ready, so the phenomenon appears initialization 0/2
init.yaml
kind: Pod metadata: name: myapp-pod labels: app: myapp spec: containers: - name: myapp-container image: busybox command: ['sh', '-c', 'echo The app is running! && sleep 3600'] initContainers: - name: init-myservice image: busybox command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;'] - name: init-mydb image: busybox command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']
[root@k8s-master mnt]# kubectl create -f init.yaml pod/myapp-pod created [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:0/2 0 8s [root@k8s-master mnt]# kubectl describe myapp-pod error: the server doesn't have a resource type "myapp-pod" [root@k8s-master mnt]# kubectl describe pod myapp-pod Name: myapp-pod Namespace: default Priority: 0 Node: k8s-node02/192.168.180.134 Start Time: Wed, 18 Dec 2019 22:02:57 +0800 Labels: app=myapp Annotations: <none> Status: Pending IP: 10.244.1.9 IPs: IP: 10.244.1.9 Init Containers: init-myservice: Container ID: docker://3c0e850042efab506f95737adfd3dc6ef2da9218ce51eb5eb4e94573a657fd2b Image: busybox Image ID: docker-pullable://busybox@sha256:1828edd60c5efd34b2bf5dd3282ec0cc04d47b2ff9caa0b6d4f07a21d1c08084 Port: <none> Host Port: <none> Command: sh -c until nslookup myservice; do echo waiting for myservice; sleep 2; done; State: Running Started: Wed, 18 Dec 2019 22:03:03 +0800 Ready: False Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-gx2h8 (ro) init-mydb: Container ID: Image: busybox Image ID: Port: <none> Host Port: <none> Command: sh -c until nslookup mydb; do echo waiting for mydb; sleep 2; done; State: Waiting Reason: PodInitializing Ready: False Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-gx2h8 (ro) Containers: myapp-container: Container ID: Image: busybox Image ID: Port: <none> Host Port: <none> Command: sh -c echo The app is running! && sleep 3600 State: Waiting Reason: PodInitializing Ready: False Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-gx2h8 (ro) Conditions: Type Status Initialized False Ready False ContainersReady False PodScheduled True Volumes: default-token-gx2h8: Type: Secret (a volume populated by a Secret) SecretName: default-token-gx2h8 Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled <unknown> default-scheduler Successfully assigned default/myapp-pod to k8s-node02 Normal Pulling 22s kubelet, k8s-node02 Pulling image "busybox" Normal Pulled 18s kubelet, k8s-node02 Successfully pulled image "busybox" Normal Created 18s kubelet, k8s-node02 Created container init-myservice Normal Started 17s kubelet, k8s-node02 Started container init-myservice
View myservice
[root@k8s-master mnt]# kubectl logs myapp-pod -c init-myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find myservice.default.svc.cluster.local: NXDOMAIN *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer *** Can't find myservice.default.svc.cluster.local: No answer *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find myservice.default.svc.cluster.local: NXDOMAIN *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer *** Can't find myservice.default.svc.cluster.local: No answer *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find myservice.default.svc.cluster.local: NXDOMAIN *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer *** Can't find myservice.default.svc.cluster.local: No answer *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find myservice.default.svc.cluster.local: NXDOMAIN *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer *** Can't find myservice.default.svc.cluster.local: No answer *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find myservice.default.svc.cluster.local: NXDOMAIN *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer *** Can't find myservice.default.svc.cluster.local: No answer *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find myservice.default.svc.cluster.local: NXDOMAIN *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer *** Can't find myservice.default.svc.cluster.local: No answer *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find myservice.default.svc.cluster.local: NXDOMAIN *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer *** Can't find myservice.default.svc.cluster.local: No answer *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find myservice.default.svc.cluster.local: NXDOMAIN *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer *** Can't find myservice.default.svc.cluster.local: No answer *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find myservice.default.svc.cluster.local: NXDOMAIN *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer *** Can't find myservice.default.svc.cluster.local: No answer *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find myservice.default.svc.cluster.local: NXDOMAIN *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer *** Can't find myservice.default.svc.cluster.local: No answer *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find myservice.default.svc.cluster.local: NXDOMAIN *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer *** Can't find myservice.default.svc.cluster.local: No answer *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find myservice.default.svc.cluster.local: NXDOMAIN *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer *** Can't find myservice.default.svc.cluster.local: No answer *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find myservice.default.svc.cluster.local: NXDOMAIN *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer *** Can't find myservice.default.svc.cluster.local: No answer *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer waiting for myservice Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find myservice.default.svc.cluster.local: NXDOMAIN *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer *** Can't find myservice.default.svc.cluster.local: No answer *** Can't find myservice.svc.cluster.local: No answer *** Can't find myservice.cluster.local: No answer *** Can't find myservice.localdomain: No answer waiting for myservice
myservice.yaml
[root@k8s-master mnt]# cat myservice.yaml kind: Service apiVersion: v1 metadata: name: myservice spec: ports: - protocol: TCP port: 80 targetPort: 9376 [root@k8s-master mnt]#
[root@k8s-master mnt]# vim myservice.yaml [root@k8s-master mnt]# kubectl create -f myservice.yaml service/myservice created [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:0/2 0 4m23s [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:0/2 0 4m25s [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:0/2 0 4m27s [root@k8s-master mnt]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10h myservice ClusterIP 10.102.35.5 <none> 80/TCP 49s [root@k8s-master mnt]# kubectl get pod -n kube-system NAME READY STATUS RESTARTS AGE coredns-58cc8c89f4-pzbrd 1/1 Running 23 10h coredns-58cc8c89f4-vmhl2 1/1 Running 23 10h etcd-k8s-master 1/1 Running 4 10h kube-apiserver-k8s-master 1/1 Running 4 10h kube-controller-manager-k8s-master 1/1 Running 21 10h kube-flannel-ds-amd64-c4fs4 1/1 Running 2 9h kube-flannel-ds-amd64-ct6mc 1/1 Running 2 9h kube-flannel-ds-amd64-mtzz9 1/1 Running 5 9h kube-proxy-9bdql 1/1 Running 2 9h kube-proxy-cv8lk 1/1 Running 2 9h kube-proxy-h8jk8 1/1 Running 4 10h kube-scheduler-k8s-master 1/1 Running 21 10h [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:1/2 0 5m58s [root@k8s-master mnt]# vim myservice.yaml [root@k8s-master mnt]# kubectl create -f myservice.yaml service/myservice created [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:0/2 0 4m23s [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:0/2 0 4m25s [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:0/2 0 4m27s [root@k8s-master mnt]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10h myservice ClusterIP 10.102.35.5 <none> 80/TCP 49s [root@k8s-master mnt]# kubectl get pod -n kube-system NAME READY STATUS RESTARTS AGE coredns-58cc8c89f4-pzbrd 1/1 Running 23 10h coredns-58cc8c89f4-vmhl2 1/1 Running 23 10h etcd-k8s-master 1/1 Running 4 10h kube-apiserver-k8s-master 1/1 Running 4 10h kube-controller-manager-k8s-master 1/1 Running 21 10h kube-flannel-ds-amd64-c4fs4 1/1 Running 2 9h kube-flannel-ds-amd64-ct6mc 1/1 Running 2 9h kube-flannel-ds-amd64-mtzz9 1/1 Running 5 9h kube-proxy-9bdql 1/1 Running 2 9h kube-proxy-cv8lk 1/1 Running 2 9h kube-proxy-h8jk8 1/1 Running 4 10h kube-scheduler-k8s-master 1/1 Running 21 10h [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:1/2 0 5m58s
Phenomenon: the discovery to 1/2
[root@k8s-master mnt]# cat mydb.yaml kind: Service apiVersion: v1 metadata: name: mydb spec: ports: - protocol: TCP port: 80 targetPort: 9377 [root@k8s-master mnt]#
[root@k8s-master mnt]# vim mydb.yaml [root@k8s-master mnt]# kubectl create -f mydb.yaml service/mydb created [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:1/2 0 11m [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 0/1 Init:1/2 0 11m [root@k8s-master mnt]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp-pod 1/1 Running 0 12m [root@k8s-master mnt]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10h mydb ClusterIP 10.104.158.92 <none> 80/TCP 3m24s myservice ClusterIP 10.102.35.5 <none> 80/TCP 10m [root@k8s-master mnt]#
Phenomenon: myapp-pod up
Explanation
- Pod the startup process, Init vessel will sequentially starts after the network and the data volume initialization. Each container must quit before the next successful launch container
- If for running or fail to exit, will cause the container to start it fails, it will retry based on the Pod restartPolicy specified policy. However, if the restartPolicy Pod is set to Always, use RestartPolicy strategy failed when Init container
- Before all the Init container without success, Pod will not become Ready state. Init container port will not be gathered in the Service. Initializing the Pod in a Pending state, but will be set to true Initializing state
- If the Pod restart, all containers must be re-run Init
- Modifications to the Init container spec is limited to container image field, modify the other fields will not take effect. Change Init vessel image field, equivalent to restart the Pod
- Init container having all the fields of the application container. In addition readinessProbe, other states can not be defined as different from the completed container Init (Completion) Ready (Readiness) outside. This forces in the verification process
- Each app name and Init container in Pod must be unique; share the same name with any other container will throw an error when verifying