# 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
  ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bohans.gitbook.io/devops/kubernetes/ji-chu-zhi-shi/pod/pod-rong-qi-gong-xiang-volume.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
