# Pod 容器共享 Volume

{% hint style="info" %}

#### <mark style="color:blue;">**同一个 Pod 中的多个容器能够共享 Pod 级别的存储卷 Volume**</mark>。Volume 可以被定义为各种类型，多个容器各自进行挂载操作，将一个 Volume 挂载为容器内部需要的目录。

{% endhint %}

在下面的例子中，**在 Pod 内包含两个容器：tomcat 和 busybox，在 Pod 级别设置 Volume “app-logs”，用于 tomcat 向其中写日志文件，busybox 读日志文件：**

<details>

<summary><mark style="color:purple;"><strong>Example</strong></mark></summary>

```yaml
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: {}

```

</details>

* 这里设置的 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 容器的输出内容**：

  ```properties
  kubectl logs pod volume-pod -c busybox
  ```
