1. 程式人生 > 其它 >k8s emptyDir-臨時資料卷

k8s emptyDir-臨時資料卷

emptyDir-臨時資料卷

1. emptyDir-臨時資料卷

  • emptyDir卷:是一個臨時儲存卷,與Pod生命週期繫結一起,如果Pod刪除了卷也會被刪除。

  • 應用場景:Pod中容器之間資料共享

  • 示例:

    apiVersion: v1
    kind: Pod
    metadata:
      name: my-pod
    spec:
      containers:
      - name: write
        image: centos
        command: ["bash","-c","for i in {1..100};do echo $i >> /data/hello;sleep 1;done"]
        volumeMounts:
        - name: data
          mountPath: /data
      - name: read
        image: centos
        command: ["bash","-c","tail -f /data/hello"]
        volumeMounts:
        - name: data
          mountPath: /data
      volumes:
      - name: data
        emptyDir: {}
    

    ​ 示例:Pod內容器之前共享資料

2. 案例

2.1 編寫emptydir配置檔案

[root@k8s-master yaml]# mkdir -p emptyDir
[root@k8s-master yaml]# cd emptyDir/
[root@k8s-master emptyDir]# ll
總用量 0
[root@k8s-master emptyDir]# vim emptydir.yaml
[root@k8s-master emptyDir]# cat emptydir.yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: write
    image: centos
    command: ["bash","-c","for i in {1..100};do echo $i >> /data/hello;sleep 1;done"]
    volumeMounts:
    - name: data
      mountPath: /data
  - name: read
    image: centos
    command: ["bash","-c","tail -f /data/hello"]
    volumeMounts:
    - name: data
      mountPath: /data
  volumes:
  - name: data
    emptyDir: {}

2.2 啟動配置檔案

[root@k8s-master emptyDir]# kubectl apply -f emptydir.yaml 
pod/my-pod created

2.3 驗證服務

[root@k8s-master emptyDir]# kubectl get pod 
NAME                 READY   STATUS    RESTARTS   AGE
configmap-demo-pod   1/1     Running   0          45h
my-pod               2/2     Running   3          10m
secret-demo-pod      1/1     Running   0          38h

2.4 進入服務驗證資料

[root@k8s-master emptyDir]# kubectl exec -it my-pod -- /bin/bash
Defaulting container name to write.
Use 'kubectl describe pod/my-pod -n default' to see all of the containers in this pod.
[root@my-pod /]# ls -a /data/
.  ..  hello
[root@my-pod /]# cat /data/hello 
1
2
3
4
5
6
7
8