上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人
3.4 Pod容器共享Volume
同一个Pod中的多个容器能够共享Pod级别的存储卷Volume。Volume可以被定义为各种类型,多个容器各自进行挂载操作,将一个Volume挂载为容器内部需要的目录,如图3.2所示。
图3.2 Pod中多个容器共享Volume
在下面的例子中,在Pod内包含两个容器:tomcat和busybox,在Pod级别设置Volume“app-logs”,用于tomcat向其中写日志文件,busybox读日志文件。
配置文件pod-volume-applogs.yaml的内容如下:
apiVersion: v1 kind: Pod metadata: name: volume-pod spec: containers: - name: tomcat image: tomcat ports: - containerPort: 8080 volumeMounts: - name: app-logs mountPath: /usr/local/tomcat/logs - name: busybox image: busybox command: ["sh", "-c", "tail -f /logs/catalina*.log"] volumeMounts: - name: app-logs mountPath: /logs volumes: - name: app-logs emptyDir: {}
这里设置的Volume名为app-logs,类型为emptyDir(也可以设置为其他类型,详见第1章对Volume概念的说明),挂载到tomcat容器内的/usr/local/tomcat/logs目录,同时挂载到logreader容器内的/logs目录。tomcat容器在启动后会向/usr/local/tomcat/logs目录写文件,logreader容器就可以读取其中的文件了。
logreader容器的启动命令为tail -f /logs/catalina*.log,我们可以通过kubectl logs命令查看logreader容器的输出内容:
# kubectl logs volume-pod -c busybox
............
29-Jul-2016 12:55:59.626 INFO [localhost-startStop-1]
org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application
directory /usr/local/tomcat/webapps/manager
29-Jul-2016 12:55:59.722 INFO [localhost-startStop-1]
org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web
application directory /usr/local/tomcat/webapps/manager has finished in 96 ms
29-Jul-2016 12:55:59.740 INFO [main] org.apache.coyote.AbstractProtocol.start
Starting ProtocolHandler ["http-apr-8080"]
29-Jul-2016 12:55:59.794 INFO [main] org.apache.coyote.AbstractProtocol.start
Starting ProtocolHandler ["ajp-apr-8009"]
29-Jul-2016 12:56:00.604 INFO [main]
org.apache.catalina.startup.Catalina.start Server startup in 4052 ms
这个文件为tomcat生成的日志文件/usr/local/tomcat/logs/catalina.<date>.log的内容。登录tomcat容器进行查看:
# kubectl exec -ti volume-pod -c tomcat -- ls /usr/local/tomcat/logs catalina.2016-07-29.log localhost_access_log.2016-07-29.txt host-manager.2016-07-29.log manager.2016-07-29.log # kubectl exec -ti volume-pod -c tomcat -- tail /usr/local/tomcat/logs/catalina.2016-07-29.log ............ 29-Jul-2016 12:55:59.722 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/manager has finished in 96 ms 29-Jul-2016 12:55:59.740 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8080"] 29-Jul-2016 12:55:59.794 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-apr-8009"] 29-Jul-2016 12:56:00.604 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 4052 ms