备战CKA每日一题——第3天

2020-03-09  本文已影响0人  小E的私房菜

第2题:Init Container:


定义:

了解初始化容器:

一个Pod可以有多个运行应用程序的容器,但也可以有一个或多个初始化容器,它们在启动应用程序容器之前就已运行.初始化容器与常规容器完全一样,除了:

具体可参考:https://kubernetes.io/docs/concepts/workloads/pods/init-containers/


与常规容器的区别:

举例:
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']
  - name: init-mydb
    image: busybox:1.28
    command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']

可以运行以下指令启动该pod:

sudo kubectl apply -f initContainer.yaml

此时,这些init容器将等待发现名为mydb和myservice的服务 。我们就需要创建该服务:

---
apiVersion: v1
kind: Service
metadata:
  name: myservice
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9376
---
apiVersion: v1
kind: Service
metadata:
  name: mydb
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9377

可以运行以下指令启动该服务:

sudo kubectl apply -f initContainerService.yaml

最后我们可以看到,pod已经为完全启动:


image.png

我们来看下pod内部:

 sudo kubectl describe pod myapp-pod -n ns-ehj
Name:         myapp-pod
Namespace:    ns-ehj
Priority:     0
Node:         p-109-123-122-12/109.123.122.12
Start Time:   Tue, 10 Mar 2020 01:36:59 +0800
Labels:       run=myapp-pod
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"creationTimestamp":null,"labels":{"run":"myapp-pod"},"name":"myapp-pod","nam...
Status:       Running
IP:           10.244.1.23
IPs:
  IP:  10.244.1.23
Init Containers:
  init-myservice:
    Container ID:  docker://7f0c53e8569ca159bd3de8e415312f1f4f09a3c44e9e9b78beb32649fb94fc09
    Image:         busybox:1.28
    Image ID:      docker-pullable://busybox@sha256:141c253bc4c3fd0a201d32dc1f493bcf3fff003b6df416dea4f41046e0f37d47
    Port:          <none>
    Host Port:     <none>
    Command:
      sh
      -c
      until nslookup myservice; do echo waiting for myservice; sleep 2; done;
    State:          Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Tue, 10 Mar 2020 01:37:12 +0800
      Finished:     Tue, 10 Mar 2020 01:46:07 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-hfgl7 (ro)
  init-mydb:
    Container ID:  docker://8a35d51bc279f7e38e728767962c9c35a1a8a7fc9444fa3a069f79d55eb32efd
    Image:         busybox:1.28
    Image ID:      docker-pullable://busybox@sha256:141c253bc4c3fd0a201d32dc1f493bcf3fff003b6df416dea4f41046e0f37d47
    Port:          <none>
    Host Port:     <none>
    Command:
      sh
      -c
      until nslookup mydb; do echo waiting for mydb; sleep 2; done;
    State:          Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Tue, 10 Mar 2020 01:46:08 +0800
      Finished:     Tue, 10 Mar 2020 01:46:08 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-hfgl7 (ro)
Containers:
  myapp-container:
    Container ID:  docker://88470fe67d075866a14aef0b6e8ec566808fb711043e6ba1941af9a59e39fb73
    Image:         busybox:1.28
    Image ID:      docker-pullable://busybox@sha256:141c253bc4c3fd0a201d32dc1f493bcf3fff003b6df416dea4f41046e0f37d47
    Port:          <none>
    Host Port:     <none>
    Command:
      sh
      -c
      echo The app is running! && sleep 3600
    State:          Running
      Started:      Tue, 10 Mar 2020 01:46:09 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-hfgl7 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-hfgl7:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-hfgl7
    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  22m        default-scheduler          Successfully assigned ns-ehj/myapp-pod to p-109-123-122-12
  Normal  Pulling    <invalid>  kubelet, p-109-123-122-12  Pulling image "busybox:1.28"
  Normal  Pulled     <invalid>  kubelet, p-109-123-122-12  Successfully pulled image "busybox:1.28"
  Normal  Created    <invalid>  kubelet, p-109-123-122-12  Created container init-myservice
  Normal  Started    <invalid>  kubelet, p-109-123-122-12  Started container init-myservice
  Normal  Pulled     <invalid>  kubelet, p-109-123-122-12  Container image "busybox:1.28" already present on machine
  Normal  Created    <invalid>  kubelet, p-109-123-122-12  Created container init-mydb
  Normal  Started    <invalid>  kubelet, p-109-123-122-12  Started container init-mydb
  Normal  Pulled     <invalid>  kubelet, p-109-123-122-12  Container image "busybox:1.28" already present on machine
  Normal  Created    <invalid>  kubelet, p-109-123-122-12  Created container myapp-container
  Normal  Started    <invalid>  kubelet, p-109-123-122-12  Started container myapp-container
上一篇下一篇

猜你喜欢

热点阅读