小言_互联网的博客

【云原生之kubernetes】kubernetes集群下初始化容器的使用方法

360人阅读  评论(0)

一、初始化容器介绍

1.初始化容器简介

Kubernetes 中 Init 容器,也叫做初始化容器,是 K8s 官方为我们提供的一个 可以用来判断我们的环境是否已经满足运行 Pod 应用前所需要的条件。

2.初始化容器特点

1.初始化容器用于帮助主容器执行初始化,它会先与主容器启动.
2.初始化容器可以有多个,这些初始化容器时顺序执行的,任意一个初始化容器执行失败,则整个pod退出。
3.初始化容器的任务不是长时任务,初始化容器任务执行完毕,且正常退出,主容器才会启动。

二、检查本地集群状态

1.检查工作节点状态

[root@k8s-master ~]# kubectl get nodes -owide
NAME         STATUS   ROLES                  AGE   VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION          CONTAINER-RUNTIME
k8s-master   Ready    control-plane,master   16d   v1.23.1   192.168.3.201   <none>        CentOS Linux 7 (Core)   3.10.0-957.el7.x86_64   containerd://1.6.6
k8s-node01   Ready    <none>                 16d   v1.23.1   192.168.3.202   <none>        CentOS Linux 7 (Core)   3.10.0-957.el7.x86_64   containerd://1.6.6
k8s-node02   Ready    <none>                 16d   v1.23.1   192.168.3.203   <none>        CentOS Linux 7 (Core)   3.10.0-957.el7.x86_64   containerd://1.6.6

2.检查系统pod状态

[root@k8s-master ~]# kubectl get pods -A
NAMESPACE     NAME                                       READY   STATUS    RESTARTS        AGE
default       nfs-client-provisioner-779b7f4dfd-tgmmc    1/1     Running   94 (5m4s ago)   30h
kube-system   calico-kube-controllers-7bc6547ffb-2nf66   1/1     Running   2 (3d6h ago)    16d
kube-system   calico-node-8c4pn                          0/1     Running   2 (3d6h ago)    16d
kube-system   calico-node-f28qq                          1/1     Running   2 (3d6h ago)    16d
kube-system   calico-node-wmc2j                          1/1     Running   2 (3d6h ago)    16d
kube-system   coredns-6d8c4cb4d-6gm4x                    1/1     Running   2 (3d6h ago)    16d
kube-system   coredns-6d8c4cb4d-7vxlz                    1/1     Running   2 (3d6h ago)    16d
kube-system   etcd-k8s-master                            1/1     Running   2 (3d6h ago)    16d
kube-system   kube-apiserver-k8s-master                  1/1     Running   2 (3d6h ago)    16d
kube-system   kube-controller-manager-k8s-master         1/1     Running   2 (3d6h ago)    16d
kube-system   kube-proxy-8dfw8                           1/1     Running   2 (3d6h ago)    16d
kube-system   kube-proxy-ghzrv                           1/1     Running   2 (3d6h ago)    16d
kube-system   kube-proxy-j867z                           1/1     Running   2 (3d6h ago)    16d
kube-system   kube-scheduler-k8s-master                  1/1     Running   2 (3d6h ago)    16d


 

三、运行一个初始化容器的pod

1.编辑web_init.yaml 文件

[root@k8s-master init]# cat web_init.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: web01   
spec:      
  restartPolicy: Always      #容器重启策略,Always总是重启,这是默认策略,OnFailure当pod异常退出时,则自动重启;Never不要重启
  dnsPolicy: Default
  volumes:
  - name: rootdir
#   hostPath:
#       path: /data/nginx
    emptyDir:
  - name: localtime
    hostPath:  #挂载宿主机本地目录
       path: /etc/localtime   
  initContainers:
  - name: busybox01
    image: busybox:1.28
    volumeMounts:
    - name: rootdir
      mountPath: /data/index/html
    command:
      - /bin/sh
      - -c
      - "echo 'hello world' > /data/index/html/index.html"
  containers:
  - name: busybox02
    image: busybox:1.28
    imagePullPolicy:  IfNotPresent   
    volumeMounts:
 #  - name: rootdir
 #    mountPath: /usr/share/nginx/html
    - name: localtime
      mountPath: /etc/localtime
      readOnly: true    #只读挂载
    env: 
    - name: test
      value: "123" 
    command:
    - /bin/sh
    - -c
    args:        #向Cmd传递参数
    - "sleep 3600"                
  - image: nginx:1.21  
    name: nginxtest   
    imagePullPolicy:  IfNotPresent   
    env:  
    - name: MYSQL_HOST
      value: "192.169.0.1"
    - name: MYSQL_PORT
      value: "3306"
    volumeMounts:
    - name: rootdir
      mountPath: /usr/share/nginx/html
    - name: localtime
      mountPath: /etc/localtime    
 #   command:
 #   - /bin/bash
 #   - -c
 #   args:        #向Cmd传递参数
 #   - "sleep 3600"                           
    ports:            #容器端口情况
    - name: http
      protocol: TCP
      containerPort: 80
      hostPort: 8080
 #  dnsPolicy: Default


 

2.运行pod

[root@k8s-master init]# kubectl apply -f web_init.yaml 

3.检查pod状态

[root@k8s-master init]# kubectl get pods
NAME    READY   STATUS     RESTARTS   AGE
web01   0/2     Init:0/1   0          11s
[root@k8s-master init]# kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
web01   2/2     Running   0          2m

四、测试初始化容器效果

1.查看pod的ip信息

[root@k8s-master ~]# kubectl get pods -owide
NAME        READY   STATUS    RESTARTS   AGE     IP              NODE         NOMINATED NODE   READINESS GATES
busybox03   1/1     Running   0          3m53s   10.244.85.202   k8s-node01   <none>           <none>
web01       2/2     Running   0          15m     10.244.58.209   k8s-node02   <none>           <none>

2.访问web内容

[root@k8s-master init]# kubectl exec -it web01 -c nginxtest -- /bin/bash
root@web01:/# curl 10.244.58.209
hello world

五、查看pod详细信息

[root@k8s-master ~]# kubectl  describe pod web01 
Name:         web01
Namespace:    default
Priority:     0
Node:         k8s-node02/192.168.3.203
Start Time:   Tue, 19 Jul 2022 17:46:07 +0800
Labels:       <none>
Annotations:  cni.projectcalico.org/containerID: 732320f6a7ffe9c7039b2b27de69c8773ecee304d2a20f98a4e9d243dfb7c8fc
              cni.projectcalico.org/podIP: 10.244.58.209/32
              cni.projectcalico.org/podIPs: 10.244.58.209/32
Status:       Running
IP:           10.244.58.209
IPs:
  IP:  10.244.58.209
Init Containers:
  busybox01:
    Container ID:  containerd://e6d9f1432c63a13271ce9d38f5d63c1fdefbadf0800fa1668c7059d9eaa7fc33
    Image:         busybox:1.28
    Image ID:      docker.io/library/busybox@sha256:141c253bc4c3fd0a201d32dc1f493bcf3fff003b6df416dea4f41046e0f37d47
    Port:          <none>
    Host Port:     <none>
    Command:
      /bin/sh
      -c
      echo 'hello world' > /data/index/html/index.html
    State:          Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Tue, 19 Jul 2022 17:46:25 +0800
      Finished:     Tue, 19 Jul 2022 17:46:25 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /data/index/html from rootdir (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-wzw77 (ro)
Containers:
  busybox02:
    Container ID:  containerd://104d483e7b9a3af176d7c9deb9c5c7da29215bd6a207317531f62f85fe5315bf
    Image:         busybox:1.28
    Image ID:      docker.io/library/busybox@sha256:141c253bc4c3fd0a201d32dc1f493bcf3fff003b6df416dea4f41046e0f37d47
    Port:          <none>
    Host Port:     <none>
    Command:
      /bin/sh
      -c
    Args:
      sleep 3600
    State:          Running
      Started:      Tue, 19 Jul 2022 17:46:25 +0800
    Ready:          True
    Restart Count:  0
    Environment:
      test:  123
    Mounts:
      /etc/localtime from localtime (ro)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-wzw77 (ro)
  nginxtest:
    Container ID:   containerd://2315c27ba2e0676bbb1e7ca0db764e96ca40af3f573ec1b536994f577332c911
    Image:          nginx:1.21
    Image ID:       docker.io/library/nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
    Port:           80/TCP
    Host Port:      8080/TCP
    State:          Running
      Started:      Tue, 19 Jul 2022 17:46:36 +0800
    Ready:          True
    Restart Count:  0
    Environment:
      MYSQL_HOST:  192.169.0.1
      MYSQL_PORT:  3306
    Mounts:
      /etc/localtime from localtime (rw)
      /usr/share/nginx/html from rootdir (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-wzw77 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  rootdir:
    Type:       EmptyDir (a temporary directory that shares a pod's lifetime)
    Medium:     
    SizeLimit:  <unset>
  localtime:
    Type:          HostPath (bare host directory volume)
    Path:          /etc/localtime
    HostPathType:  
  kube-api-access-wzw77:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Pulling    53m   kubelet            Pulling image "busybox:1.28"
  Normal  Scheduled  53m   default-scheduler  Successfully assigned default/web01 to k8s-node02
  Normal  Pulled     53m   kubelet            Successfully pulled image "busybox:1.28" in 17.313491259s
  Normal  Created    53m   kubelet            Created container busybox01
  Normal  Started    53m   kubelet            Started container busybox01
  Normal  Pulled     53m   kubelet            Container image "busybox:1.28" already present on machine
  Normal  Created    53m   kubelet            Created container busybox02
  Normal  Started    53m   kubelet            Started container busybox02
  Normal  Pulling    53m   kubelet            Pulling image "nginx:1.21"
  Normal  Pulled     53m   kubelet            Successfully pulled image "nginx:1.21" in 10.067013987s
  Normal  Created    53m   kubelet            Created container nginxtest
  Normal  Started    53m   kubelet            Started container nginxtest


 

六、查看pod日志信息

[root@k8s-master ~]# kubectl logs web01 busybox01
[root@k8s-master ~]# kubectl logs web01 busybox02
[root@k8s-master ~]# kubectl logs web01 nginxtest 
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/07/19 17:46:36 [notice] 1#1: using the "epoll" event method
2022/07/19 17:46:36 [notice] 1#1: nginx/1.21.5
2022/07/19 17:46:36 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2022/07/19 17:46:36 [notice] 1#1: OS: Linux 3.10.0-957.el7.x86_64
2022/07/19 17:46:36 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 65536:65536
2022/07/19 17:46:36 [notice] 1#1: start worker processes
2022/07/19 17:46:36 [notice] 1#1: start worker process 31
2022/07/19 17:46:36 [notice] 1#1: start worker process 32
10.244.58.209 - - [19/Jul/2022:18:09:17 +0800] "GET / HTTP/1.1" 200 12 "-" "curl/7.74.0" "-"


 

转载:https://blog.csdn.net/jks212454/article/details/125876135
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场