--- vssueId: 170 # layout: StepLayout sharingTitle: CKA备考打卡 - 每日一题 - Day 7 description: CKA备考打卡 - 每日一题 - Day 7 meta: - name: keywords content: Kubernetes,K8S,CKA,Certified Kubernetes Administrator --- # CKA每日一题 --- Day 7 ::: tip 考题 提供一个pod的yaml,要求添加Init Container,Init Container的作用是创建一个空文件,pod的Containers判断文件是否存在,不存在则退出 > 注意:附带Pod完整yaml ::: 答案及解析 ## 答案 ```yaml apiVersion: v1 kind: Pod metadata: labels: run: cka-1126 name: cka-1126 spec: initContainers: - image: busybox name: init-c command: ['sh', '-c', 'touch /tmp/cka-1126'] volumeMounts: - name: workdir mountPath: "/tmp" containers: - image: busybox name: cka-1126 command: ['sh', '-c', 'ls /tmp/cka-1126 && sleep 3600 || exit 1'] volumeMounts: - name: workdir mountPath: "/tmp" volumes: - name: workdir emptyDir: {} ``` 主Container的command就是判断文件是否存在,存在则不退出,不存在则退出;也可以用以下if判断: ``` sh command: ['sh', '-c', 'if [ -e /tmp/cka-1126 ];then echo "file exits";else echo "file not exits" && exit 1;fi'] ``` ## 解析 本题的关键点是init容器与主容器需要共同挂载一个名为workdir的目录,init容器在里面创建一个空文件,主容器去检验文件是否存在,检验主要用的是shell的语法; ``` command: ['sh', '-c', 'ls /tmp/cka-1126 && sleep 3600 || exit 1'] ``` 这句话意思是:如果`ls /tmp/cka-1126`返回码为0,即文件存在,将sleep 3600秒;否则exit 1退出; 也可以用`shell`的`if`语法判断。 **官方文档地址:** https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-initialization/ https://kubernetes.io/docs/concepts/workloads/pods/init-containers/ 中文文档: [https://kuboard.cn/learning/k8s-intermediate/workload/init-container.html](/learning/k8s-intermediate/workload/init-container.html) ### 梳理概念 初始化容器,顾名思义容器启动的时候,会先启动可一个或多个容器,如果有多个,那么这几个Init Container按照定义的顺序依次执行,一个执行成功,才能执行下一个,只有所有的Init Container执行完后,主容器才会启动。由于一个Pod里的存储卷是共享的,所以Init Container里产生的数据可以被主容器使用到。 Init Container可以在多种K8S资源里被使用到如Deployment、Daemon Set、StatefulSet、Job等,但归根结底都是在Pod启动时,在主容器启动前执行,做初始化工作。 Init 容器支持应用容器的全部字段和特性,包括资源限制、数据卷和安全设置。然而,Init 容器不支持 Readiness Probe,因为它们必须在 Pod 就绪之前运行完成;在资源限制、调度方面也会略有不同。 ### 应用场景 **等待其它模块Ready**:比如有一个应用里面有两个容器化的服务,一个是Web Server,另一个是数据库。其中Web Server需要访问数据库。但是当我们启动这个应用的时候,并不能保证数据库服务先启动起来,所以可能出现在一段时间内Web Server连接数据库错误。为了解决这个问题,我们可以在运行Web Server服务的Pod里使用一个InitContainer,去检查数据库是否准备好,直到数据库可以连接,Init Container才结束退出,然后Web Server容器被启动,发起正式的数据库连接请求。 **初始化配置**:比如集群里检测所有已经存在的成员节点,为主容器准备好集群的配置信息,这样主容器起来后就能用这个配置信息加入集群;目前在容器化,初始化集群配置文件时经常用到; **提供一种阻塞容器启动的方式**:必须在initContainer容器启动成功后,才会运行下一个容器,保证了一组条件运行成功的方式; **其它使用场景**:将pod注册到一个中央数据库、下载应用依赖等。 Kubernetes 1.5 版本 开始支持在annotations下用pod.beta.kubernetes.io/init-containers申明initContainer,像以下这样。 ```yaml apiVersion: v1 kind: Pod metadata: name: myapp-pod labels: app: myapp annotations: pod.beta.kubernetes.io/init-containers: '[ { "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;"] } ]' spec: containers: - name: myapp-container image: busybox command: ['sh', '-c', 'echo The app is running! && sleep 3600'] ``` Kubernetes 1.6 版本的新语法将 Init 容器的声明移到 spec 下,但是老的 annotation 语法仍然可以使用。 > CKA 考试每日一题系列,全部内容由 [我的小碗汤](https://mp.weixin.qq.com/s/5tYgb_eSzHz_TMsi0U32gw) 创作,本站仅做转载