k8s之Pod容器共享Volume

在实际生产中,经常会出现同一pod内的多个容器共享Pod级别的存储卷Volum情况。。Volume可以被定义为各种类型,多个容器各自进行挂载操作,将一个Volume挂载为容器内部需要的目录即可。
例如:
Pod内包含两个容器:tomcat和busybox,在Pod级别设置Volume “app-logs”,用于tomcat向其中写日志,busybox读取日志文件。

kubectl create -f pod-volume.yaml

pod-volume.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: {}

查看pod是否启动

[root@master manifests]# kubectl  get pods volume-pod
NAME         READY   STATUS    RESTARTS   AGE
volume-pod   2/2     Running   0          13h

busybox容器的启动命令为 tail -f /logs/catalia*.log, 查看busybox输出

[root@master manifests]# kubectl  logs volume-pod -c busybox
28-Oct-2020 06:34:54.718 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.25] using APR version [1.6.5].
28-Oct-2020 06:34:54.718 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
28-Oct-2020 06:34:54.718 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
28-Oct-2020 06:34:54.728 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1d  10 Sep 2019]
28-Oct-2020 06:34:56.466 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
28-Oct-2020 06:34:56.663 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [2927] milliseconds
28-Oct-2020 06:34:56.921 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
28-Oct-2020 06:34:56.929 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/9.0.39]
28-Oct-2020 06:34:56.950 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
28-Oct-2020 06:34:56.998 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [334] milliseconds

查看tomcat日志

root@volume-pod:/usr/local/tomcat# tail -f /usr/local/tomcat/logs/catalina.2020-10-28.log
28-Oct-2020 06:34:54.718 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.25] using APR version [1.6.5].
28-Oct-2020 06:34:54.718 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
28-Oct-2020 06:34:54.718 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
28-Oct-2020 06:34:54.728 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1d  10 Sep 2019]
28-Oct-2020 06:34:56.466 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
28-Oct-2020 06:34:56.663 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [2927] milliseconds
28-Oct-2020 06:34:56.921 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
28-Oct-2020 06:34:56.929 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/9.0.39]
28-Oct-2020 06:34:56.950 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
28-Oct-2020 06:34:56.998 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [334] milliseconds

我们对日志进行对比,发现日志是一致的。

已标记关键词 清除标记