在容器的部署过程中,有的时候需要在容器运行之前进行一些预配置的工作,比如下载配置,判断某些服务是否启动,修改配置等一些准备的工作,想要实现这些功能,在k8s中可以使用初始化容器,在应用容器运行之前进行一些预处理的工作。
使用方法以下的例子使用初始化容器,在nginx容器启动之前下载一个index.html文件
kubectl apply -f - <<EOF apiVersion: v1 kind: Pod metadata: name: initnginx spec: initContainers: - name: install image: busybox command: - wget - "-O" - "/var/index.html" - "https://www.baidu.com" containers: - name: nginx image: 172.20.45.174:81/base/nginx:1.15-alpine ports: - containerPort: 80 dnsPolicy: Default EOF查看pod的状态
[root@nccztsjb-node-11 ~]# kubectl get pod NAME READY STATUS RESTARTS AGE cm-test-app 1/1 Running 0 20h cm-test-nginx 1/1 Running 0 175m cm-test-pod 1/1 Running 0 20h initnginx 0/1 Init:0/1 0 16s #在初始化阶段 nginx-test-5c7f8fc697-lrvpm 1/1 Running 0 32d nginx-test-5c7f8fc697-whx4g 1/1 Running 0 32d web-0 1/1 Running 0 32d web-1 1/1 Running 0 32d web-2 1/1 Running 0 32d # 初始化容器运行完成后,pod状态变为Running [root@nccztsjb-node-11 ~]# kubectl get pod NAME READY STATUS RESTARTS AGE cm-test-app 1/1 Running 0 20h cm-test-nginx 1/1 Running 0 178m cm-test-pod 1/1 Running 0 20h initnginx 1/1 Running 0 9s nginx-test-5c7f8fc697-lrvpm 1/1 Running 0 32d nginx-test-5c7f8fc697-whx4g 1/1 Running 0 32d web-0 1/1 Running 0 32d web-1 1/1 Running 0 32d web-2 1/1 Running 0 32d [root@nccztsjb-node-11 ~]#查看初始化容器的日志
[root@nccztsjb-node-11 ~]# kubectl logs initnginx -c install Connecting to (103.235.46.39:443) wget: note: TLS certificate validation not implemented saving to '/var/index.html' index.html 100% |********************************| 2443 0:00:00 ETA '/var/index.html' saved [root@nccztsjb-node-11 ~]#说明执行命令是成功的。
进入容器中,查看是否存在该文件
[root@nccztsjb-node-11 ~]# kubectl exec -it initnginx -- bash [initnginx root:~]# cd /var/ [initnginx root:/var]# ls cache empty git lib local lock log opt run spool tmp [initnginx root:/var]#发现并没有该文件!
问题在于install容器和nginx容器不是同一个容器所以,下载的/var/index.html只是在install容器的/var目录下。
为了实现nginx可以访问初始化容器中的内容,可以挂载一个数据卷进行2个容器的共享,修改pod定义如下:
kubectl apply -f - <<EOF apiVersion: v1 kind: Pod metadata: name: initnginx spec: initContainers: - name: install image: busybox command: - wget - "-O" - "/work-dir/index.html" - "https://www.baidu.com" volumeMounts: - name: workdir mountPath: /work-dir containers: - name: nginx image: 172.20.45.174:81/base/nginx:1.15-alpine ports: - containerPort: 80 volumeMounts: - name: workdir mountPath: /usr/share/nginx/html dnsPolicy: Default volumes: - name: workdir emptyDir: {} EOF这样的化,2个容器中就都挂载了workdir这个数据卷,初始化容器中下载的文件,就可以被nginx应用容器来使用了。
创建pod后查看nginx容器中的文件
[root@nccztsjb-node-11 ~]# kubectl exec -it initnginx -- bash [initnginx root:~]# cd /usr/share/nginx/ [initnginx root:/usr/share/nginx]# ls html [initnginx root:/usr/share/nginx]# cd html/ [initnginx root:/usr/share/nginx/html]# ls index.html [initnginx root:/usr/share/nginx/html]# cat index.html <!DOCTYPE html> <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=http://www.likecs.com//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=http://www.likecs.com//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span><input type=submit id=su value=百度一下 autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2F%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '">登录</a>'); </script> <a href=http://www.likecs.com//www.baidu.com/more/ name=tj_briicon class=bri>更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=http://www.likecs.com//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html> [initnginx root:/usr/share/nginx/html]#发现index.html文件已经生成了,就是在初始化容器中创建的文件被挂载到nginx中来使用。