1. 程式人生 > 其它 >Linux - K8S - 儲存管理 - emptyDir, hostPath, NFS

Linux - K8S - 儲存管理 - emptyDir, hostPath, NFS

# EmptyDir

1:01:43 root@master1 storage]#cat 01-storage-empty.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx-volume
  labels:
    name: nginx-volume
spec:
  volumes:
  - name: empty-volume
    emptyDir: {}
  containers:
    - name: nginx-volume
      image: 10.0.0.55:80/mykubernetes/nginx:1.21.3
      volumeMounts:
       - name: empty-volume
         mountPath: /nginx/www/empty/
[11:02:00 root@master1 storage]#kubectl apply -f 01-storage-empty.yaml 
pod/nginx-volume created

[11:05:15 root@master1 storage]#kubectl get pod -o wide
NAME           READY   STATUS    RESTARTS   AGE    IP           NODE               NOMINATED NODE   READINESS GATES
nginx-volume   1/1     Running   0          111s   10.244.3.2   node1.noisedu.cn   <none>           <none>

# 進入container,建立一個測試檔案

[11:05:48 root@master1 storage]#kubectl exec -it nginx-volume -- bash
root@nginx-volume:/# cd /nginx/www/empty/
root@nginx-volume:/nginx/www/empty# echo "test nginx" > test.txt
root@nginx-volume:/nginx/www/empty# exit
exit

# node節點查詢

[11:12:01 root@node1 ~]#ll /var/lib/kubelet/pods/7db545a5-dd8c-4695-9002-9e371bcc450d/volumes/kubernetes.io~empty-dir/empty-volume/test.txt 
-rw-r--r-- 1 root root 11 Dec 12 11:08 /var/lib/kubelet/pods/7db545a5-dd8c-4695-9002-9e371bcc450d/volumes/kubernetes.io~empty-dir/empty-volume/test.txt
[11:12:15 root@node1 ~]#cat /var/lib/kubelet/pods/7db545a5-dd8c-4695-9002-9e371bcc450d/volumes/kubernetes.io~empty-dir/empty-volume/test.txt
test nginx
# 當刪除container時候,這個目錄自動刪除
[11:16:15 root@node1 ~]#cat /var/lib/kubelet/pods/
2e83ffac-3365-4a2f-a421-c9be10d38de8/ 4458f0e3-dbe8-4a5e-81be-779f555ab26b/

[11:12:18 root@node1 ~]#cat /var/lib/kubelet/pods/7db545a5-dd8c-4695-9002-9e371bcc450d/volumes/kubernetes.io~empty-dir/empty-volume/test.txt
cat: /var/lib/kubelet/pods/7db545a5-dd8c-4695-9002-9e371bcc450d/volumes/kubernetes.io~empty-dir/empty-volume/test.txt: No such file or directory


  

# hostPath

[12:08:52 root@master1 storage]#cat 03-storage-hostpath-redis.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: hostpath-redis
spec:
  nodeName: node2.noisedu.cn
  volumes:
  - name: redis-backup
    hostPath:
     path: /backup/redis
  containers:
  - name: hostpath-redis
    image: 10.0.0.55:80/mykubernetes/redis:6.2.5
    volumeMounts:
    - name: redis-backup
      mountPath: /data

[12:05:45 root@master1 storage]#kubectl apply -f 03-storage-hostpath-redis.yaml 
pod/hostpath-redis created
[12:05:52 root@master1 storage]#kubectl get pod -o wide
NAME             READY   STATUS    RESTARTS   AGE   IP           NODE               NOMINATED NODE   READINESS GATES
hostpath-redis   1/1     Running   0          5s    10.244.4.4   node2.noisedu.cn   <none>           <none>


# 進入redis容器,輸入測試資料
[12:21:11 root@master1 storage]#kubectl exec -it hostpath-redis -- bash
root@hostpath-redis:/data# redis-cli
127.0.0.1:6379> set volume hostpath
OK
127.0.0.1:6379> bgsave
Background saving started
127.0.0.1:6379> exit
root@hostpath-redis:/data# exit
exit



# 進入node2檢視資料
[12:20:47 root@node2 ~]#ll /backup/redis
total 12
drwxr-xr-x 2  999 root   4096 Dec 12 12:21 ./
drwxr-xr-x 3 root root   4096 Dec 12 11:45 ../
-rw-r--r-- 1  999 docker  114 Dec 12 12:21 dump.rdb

# 備份資料
[12:21:40 root@node2 ~]#cp /backup/redis/dump.rdb /data/


# 再次新增資料
[12:21:29 root@master1 storage]#kubectl exec -it hostpath-redis -- bash
root@hostpath-redis:/data# redis-cli
127.0.0.1:6379> set volume1 hostpath1
OK
127.0.0.1:6379> bgsave
Background saving started
127.0.0.1:6379> exit
root@hostpath-redis:/data# exit
exit

[12:22:03 root@node2 ~]#ll /backup/redis
total 12
drwxr-xr-x 2  999 root   4096 Dec 12 12:22 ./
drwxr-xr-x 3 root root   4096 Dec 12 11:45 ../
-rw-r--r-- 1  999 docker  133 Dec 12 12:22 dump.rdb


# 刪除容器,重新還原資料只有一個volume的資料卷
[12:22:32 root@master1 storage]#kubectl delete -f 03-storage-hostpath-redis.yaml 
pod "hostpath-redis" deleted
[12:22:41 root@node2 ~]#mv /data/dump.rdb /backup/redis/
[12:23:09 root@node2 ~]#ll /backup/redis
total 12
drwxr-xr-x 2  999 root 4096 Dec 12 12:23 ./
drwxr-xr-x 3 root root 4096 Dec 12 11:45 ../
-rw-r--r-- 1 root root  114 Dec 12 12:22 dump.rdb


# 重新啟動redis容器,檢視資料
[12:23:47 root@master1 storage]#kubectl exec -it hostpath-redis -- bash
root@hostpath-redis:/data# redis-cli
127.0.0.1:6379> get volume
"hostpath"
127.0.0.1:6379> get volume1
(nil)
127.0.0.1:6379> exit
root@hostpath-redis:/data# exit
exit

# 還原成功,只能找到第一次的值

  

# NFS

# 安裝nfs服務
[13:17:54 root@nfs ~]#apt install -y nfs-kernel-server

# 配置共享目錄
[13:19:04 root@nfs ~]#mkdir /nfs-data
[13:20:33 root@nfs ~]#echo '/nfs-data *(rw,no_root_squash,sync)' >> /etc/exports
[13:20:40 root@nfs ~]#systemctl restart nfs-kernel-server.service 
[13:20:48 root@nfs ~]#exportfs
/nfs-data     	<world>


# 所有master和node安裝客戶端
[13:12:08 root@master1 storage]#apt install nfs-common -y

# 測試
[13:22:22 root@nfs ~]#showmount -e 10.0.0.58
Export list for 10.0.0.58:
/nfs-data *
[13:24:21 root@nfs ~]#mount -t nfs 10.0.0.58:/nfs-data /tmp
[13:25:29 root@nfs ~]#mount | grep nfs
nfsd on /proc/fs/nfsd type nfsd (rw,relatime)
10.0.0.58:/nfs-data on /tmp type nfs4 (rw,relatime,vers=4.2,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=10.0.0.58,local_lock=none,addr=10.0.0.58)
[13:25:35 root@nfs ~]#umount /tmp
[13:25:47 root@nfs ~]#mount | grep nfs
nfsd on /proc/fs/nfsd type nfsd (rw,relatime)

  

# 遠端掛載redis資料到nfs

[13:29:11 root@master1 storage]#cat 04-storage-nfs.yml 
apiVersion: v1
kind: Pod
metadata:
  name: volumes-nfs
spec:
  nodeName: node2.noisedu.cn
  volumes:
    - name: redisdatapath
      nfs:
        server: 10.0.0.58
        path: /nfs-data
  containers:
  - name: redis
    image: 10.0.0.55:80/mykubernetes/redis:6.2.5
    volumeMounts:
    - mountPath: /data
      name: redisdatapath

[13:29:02 root@master1 storage]#kubectl apply -f 04-storage-nfs.yml 
pod/volumes-nfs created
[13:29:06 root@master1 storage]#kubectl get pod -o wide
NAME          READY   STATUS    RESTARTS   AGE   IP            NODE               NOMINATED NODE   READINESS GATES
volumes-nfs   1/1     Running   0          4s    10.244.4.10   node2.noisedu.cn   <none>           <none>

[13:29:27 root@master1 storage]#kubectl exec -it volumes-nfs -- bash
root@volumes-nfs:/data# redis-cli 
127.0.0.1:6379> set volume nfs-data
OK
127.0.0.1:6379> exit
root@volumes-nfs:/data# redis-cli 
127.0.0.1:6379> bgsave
Background saving started
127.0.0.1:6379> exit
root@volumes-nfs:/data# exit
exit

[13:30:25 root@nfs nfs-data]#ll
total 12
drwxr-xr-x  2  999 root   4096 Dec 12 13:30 ./
drwxr-xr-x 25 root root   4096 Dec 12 13:19 ../
-rw-r--r--  1  999 docker  114 Dec 12 13:30 dump.rdb