Kubernetes之Pod初始化容器

Kubernetes之Pod初始化容器

概述

​ 初始化是很多编程语言普遍关注的问题,甚至有些编程语言直接支持模式构造来生成初始化程序,这些用于进行初始化的程序结构称为初始化器或初始化列表。初始化代码要首先运行,且只能运行一次,它们常用于验证前提条件、基于默认值或传入的参数初始化对象实例的字段等。Pod中的初始化容器(Init Container)功能与此类似,它们为那些有先决条件的应用容器完成必要的初始设置,例如设置特殊权限、生成必要的iptables规则、设置数据库模式,以及获取最新的必要数据等。

​ 有很多场景都需要在应用容器启动之前进行部分初始化操作,例如等待其他关联组件服务可用、基于环境变量或配置模板为应用程序生成配置文件、从配置中心获取配置等。初始化容器的典型应用需求有如下几种。

  • 用于运行需要管理权限的工具程序,例如iptables命令等,出于安全等方面的原因,应用容器不适合拥有运行这类程序的权限。
  • 提供主容器镜像中不具备的工具程序或自定义代码。
  • 为容器镜像的构建和部署人员提供了分离、独立工作的途径,部署人员使用专用的初始化容器完成特殊的部署逻辑,从而使得他们不必协同起来制作单个镜像文件。
  • 初始化容器和应用容器处于不同的文件系统视图中,因此可分别安全地使用敏感数据,例如Secrets资源等。
  • 初始化容器要先于应用容器串行启动并运行完成,因此可用于延后应用容器的启动直至其依赖的条件得以满足。

​ Pod对象中的所有初始化容器必须按定义的顺序串行运行,直到它们全部成功结束才能启动应用容器,因而初始化容器通常很小,以便它们能够以轻量的方式快速运行。某初始化容器运行失败将会导致整个Pod重新启动(重启策略为Never时例外),初始化容器也必将再次运行,因此需要确保所有初始化容器的操作具有幂等性,以避免无法预知的副作用。

​ Init 容器与普通的容器非常像,除了如下两点:

  • 它们总是运行到完成,即它的本身是有周期的,并不是像nginx,tomcat那样一直堵塞在那里。
  • 每个都必须在下一个启动之前成功完成,即在真正容器启动之前,初始化容器都要成功跑完。

应用

下面这个pod,会先在初始化容器中往挂载的路径的index.html写入12222222,然后nginx的挂载静态文件下,读取index.html

apiVersion: v1
kind: Pod
metadata:
  name: "pod-life"
  labels:
    app: "pod-life"
spec:
  volumes:
  - name: content-vol
    emptyDir: {}
  initContainers:  ## Pod在启动containers之前,先要【运行完】initContainers的所有容器,所以这些容器必须有终结,不能一直运行
  - name: init-c-01
    image: alpine  ### 必须有终结的那个时刻,一般不要用一直启动的镜像
    command: ["/bin/sh","-c","echo 12222222 > /app/index.html;sleep 30;echo success;"]
    volumeMounts: 
     - name: content-vol
       mountPath: /app
  containers:
  ### docker run alpine 没有在后台一直启动的程序
  - name: pod-life-01
    image: "nginx" #默认的启动命令是启动nginx。nginx启动在后台一直有了
    volumeMounts: 
     - name: content-vol
       mountPath: /usr/share/nginx/html
  - name: pod-life-02
    image: "alpine"  #pod里面的containers都必须能启动起来,Pod会不断的重启这个容器
    command: ["/bin/sh","-c","sleep 30"]

应用后,查看日志

[root@k8s-01 ~]# kubectl logs -f --tail 200 pod-life -c init-c-01
success
[root@k8s-01 ~]#

查看pod,发现请求nginx的首页已经变成12222222

[root@k8s-01 ~]# kubectl get pods -o wide
NAME                                      READY   STATUS    RESTARTS        AGE    IP               NODE     NOMINATED NODE   READINESS GATES
counter                                   1/1     Running   0               35h    10.244.165.198   k8s-03   <none>           <none>
nfs-client-provisioner-69b76b8dc6-ms4xg   1/1     Running   1 (6d18h ago)   18d    10.244.179.21    k8s-02   <none>           <none>
nginx-5759cb8dcc-t4sdn                    1/1     Running   0               32m    10.244.179.50    k8s-02   <none>           <none>
pod-life                                  2/2     Running   0               117s   10.244.179.52    k8s-02   <none>           <none>
[root@k8s-01 ~]# curl 10.244.179.52
12222222
[root@k8s-01 ~]#

如果这个时候将yaml的命令改错

image-20220620113326173

那么初始化容器就会一直报错,重试,真正的容器也不会运行

Every 1.0s: kubectl get pods                                                                                                  Mon Jun 20 11:34:12 2022

NAME                                      READY   STATUS                  RESTARTS        AGE
counter                                   1/1     Running                 0               36h
nfs-client-provisioner-69b76b8dc6-ms4xg   1/1     Running                 1 (6d19h ago)   18d
nginx-5759cb8dcc-t4sdn                    1/1     Running                 0               47m
pod-life                                  0/2     Init:CrashLoopBackOff   3 (18s ago)     104s

实际应用

下面为kong的官方提供的yaml,就有一段初始化容器

      initContainers:
      - command:
        - /bin/sh
        - -c
        - while true; do kong migrations list; if [[ 0 -eq $? ]]; then exit 0; fi;
          sleep 2;  done;
        env:
        - name: KONG_PG_HOST
          value: postgres
        - name: KONG_PG_PASSWORD
          value: kong
        image: kong:2.8
        name: wait-for-migrations

主要是查看是否对数据库进行初始化数据,外面嵌套一层循环,一直等待初始化完成,则退出循环,再启动kong,把命令拿出来到源码安装环境下执行:

[root@localhost ~]#  kong migrations list
2022/06/20 11:48:08 [warn] ulimit is currently set to "1024". For better performance set it to at least "4096" using "ulimit -n"
Executed migrations:
                                    core: 000_base, 003_100_to_110, 004_110_to_120, 005_120_to_130, 006_130_to_140, 007_140_to_150, 008_150_to_200, 009_200_to_210, 010_210_to_211, 011_212_to_213, 012_213_to_220, 013_220_to_230, 014_230_to_260, 015_260_to_270, 016_270_to_280
                                     acl: 000_base_acl, 002_130_to_140, 003_200_to_210, 004_212_to_213
                                    acme: 000_base_acme
                              basic-auth: 000_base_basic_auth, 002_130_to_140, 003_200_to_210
                           bot-detection: 001_200_to_210
                                  canary: 001_200_to_210
                               degraphql: 000_base
          graphql-rate-limiting-advanced: 000_base_gql_rate_limiting
                               hmac-auth: 000_base_hmac_auth, 002_130_to_140, 003_200_to_210
                          ip-restriction: 001_200_to_210
                                     jwt: 000_base_jwt, 002_130_to_140, 003_200_to_210
                              jwt-signer: 000_base_jwt_signer, 001_200_to_210
                                key-auth: 000_base_key_auth, 002_130_to_140, 003_200_to_210
                            key-auth-enc: 000_base_key_auth_enc, 001_200_to_210
                               mtls-auth: 000_base_mtls_auth, 001_200_to_210, 002_2200_to_2300
                                  oauth2: 000_base_oauth2, 003_130_to_140, 004_200_to_210, 005_210_to_211
                          openid-connect: 000_base_openid_connect, 001_14_to_15, 002_200_to_210
                    proxy-cache-advanced: 001_035_to_050
                           rate-limiting: 000_base_rate_limiting, 003_10_to_112, 004_200_to_210
                   response-ratelimiting: 000_base_response_rate_limiting
                                 session: 000_base_session, 001_add_ttl_index
                              vault-auth: 000_base_vault_auth
                              enterprise: 000_base, 006_1301_to_1500, 006_1301_to_1302, 010_1500_to_2100, 007_1500_to_1504, 008_1504_to_1505, 007_1500_to_2100, 009_1506_to_1507, 009_2100_to_2200, 010_2200_to_2211, 010_2200_to_2300, 010_2200_to_2300_1, 011_2300_to_2600, 012_2600_to_2700, 012_2600_to_2700_1, 013_2700_to_2800
                          enterprise.acl: 001_1500_to_2100
                   enterprise.basic-auth: 001_1500_to_2100
                    enterprise.hmac-auth: 001_1500_to_2100
                          enterprise.jwt: 001_1500_to_2100
                     enterprise.key-auth: 001_1500_to_2100
                 enterprise.key-auth-enc: 001_1500_to_2100
                    enterprise.mtls-auth: 001_1500_to_2100, 002_2200_to_2300
                       enterprise.oauth2: 001_1500_to_2100, 002_2200_to_2211
 enterprise.request-transformer-advanced: 001_1500_to_2100
enterprise.response-transformer-advanced: 001_1500_to_2100
[root@localhost ~]# echo  $?
0
[root@localhost ~]#
posted @ 2022-11-29 10:26  天宇轩-王  阅读(185)  评论(0编辑  收藏  举报