在下面的例子中,在 Pod 内包含两个容器:tomcat 和 busybox,在 Pod 级别设置 Volume “app-logs”,用于 tomcat 向其中写日志文件,busybox 读日志文件:
apiVersion: v1 kind: Pod metadata: name: volume-pod spec: containers: # tocat 容器 - name: tomcat image: tomcat:9.0.83-jdk8-corretto-al2 ports: - containerPort: 8080 volumeMounts: - name: app-logs mountPath: /usr/local/tomcat/logs resources: limits: memory: "128Mi" cpu: "500m" # busybox 容器 - name: logreader image: busybox:1.36 command: ["sh", "-c", "tail -f /logs/catalina*.log"] volumeMounts: - name: app-logs mountPath: /logs resources: limits: memory: "128Mi" cpu: "500m" # 卷 volumes: - name: app-logs emptyDir: {}
这里设置的 Volume 名为 app-logs,类型为 emptyDir。挂载到 tomcat 容器内的 /usr/local/tomcat/logs 目录,同时挂载到 logreader 容器内的 /logs 目录。
tomcat 容器在启动后会向 /usr/local/tomcat/logs 目录写文件,logreader 容器就可以读取其中的文件了。
logreader 容器的启动命令为 tail -f /logs/catalina*.log,可以通过 kubectl logs 命令查看 logreader 容器的输出内容:
kubectl logs pod volume-pod -c busybox
最后更新于1年前