Kubernetes進階之PersistentVolume 靜態供給實現NFS網絡存儲
網絡存儲html
NFS是一種很早的技術,單機的存儲在服務器方面仍是很是主流的,但nfs惟一的就是缺點比較大就是沒有集羣版,作集羣化仍是比較費勁的,文件系統作不了,這是一個很大的弊端,大規模的仍是須要選擇一些分佈式的存儲,nfs就是一個網絡文件存儲服務器,裝完nfs以後,共享一個目錄,其餘的服務器就能夠經過這個目錄掛載到本地了,在本地寫到這個目錄的文件,就會同步到遠程服務器上,實現一個共享存儲的功能,通常都是作數據的共享存儲,好比多臺web服務器,確定須要保證這些web服務器的數據一致性,那就會用到這個共享存儲了,要是將nfs掛載到多臺的web服務器上,網站根目錄下,網站程序就放在nfs服務器上,這樣的話。每一個網站,每一個web程序都能讀取到這個目錄,一致性的數據,這樣的話就能保證多個節點,提供一致性的程序了。node
單獨拿一臺服務器作nfs服務器,咱們這裏先搭建一臺NFS服務器用來存儲咱們的網頁根目錄nginx
[root@nfs ~]# yum install nfs-utils -y
暴露目錄,讓是讓其餘服務器能掛載這個目錄web
[root@nfs ~]# mkdir /opt/k8s [root@nfs ~]# vim /etc/exports /opt/k8s 192.168.30.0/24(rw,no_root_squash)
給這個網段加上權限,可讀可寫[root@nfs ~]# systemctl start nfs
vim
找個節點去掛載測試一下,只要去共享這個目錄就要都去安裝這個客戶端centos
[root@k8s-node2 ~]# yum install nfs-utils -y [root@k8s-node2 ~]# mount -t nfs 192.168.30.27:/opt/k8s /mnt [root@k8s-node2 ~]# cd /mnt [root@k8s-node2 mnt]# df -h 192.168.30.27:/opt/k8s 36G 5.8G 30G 17% /mnt [root@k8s-node2 mnt]# touch a.txt
去服務器端查看已經數據共享過來了api
[root@nfs ~]# cd /opt/k8s/ [root@nfs k8s]# ls a.txt
刪除nfs服務器的數據也會刪除
接下來怎麼將K8s進行使用
咱們把網頁目錄都放在這個目錄下bash
[root@nfs k8s]# mkdir wwwroot [root@k8s-master demo]# vim nfs.yaml apiVersion: apps/v1beta1 kind: Deployment metadata: name: nfs spec: replicas: 3 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx volumeMounts: - name: wwwroot mountPath: /usr/share/nginx/html ports: - containerPort: 80 volumes: - name: wwwroot nfs: server: 192.168.30.27 path: /opt/k8s/wwwroot
[root@k8s-master demo]# kubectl create -f nfs.yaml
服務器
[root@k8s-master demo]# kubectl get pod NAME READY STATUS RESTARTS AGE mypod 1/1 Running 0 6h6m mypod2 1/1 Running 0 6h nginx-5ddcc6cb74-lplxl 1/1 Running 0 6h43m nginx-deployment-744d977b46-8q97k 1/1 Running 0 48s nginx-deployment-744d977b46-ftjfk 1/1 Running 0 48s nginx-deployment-744d977b46-nksph 1/1 Running 0 48s web-67fcf9bf8-mrlhd 1/1 Running 0 103m
進入容器並查看掛載,肯定掛載上網絡
[root@k8s-master demo]# kubectl exec -it nginx-deployment-744d977b46-8q97k bash root@nginx-deployment-744d977b46-8q97k:/# df -h Filesystem Size Used Avail Use% Mounted on overlay 17G 5.6G 12G 33% / tmpfs 64M 0 64M 0% /dev tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/mapper/centos-root 17G 5.6G 12G 33% /etc/hosts shm 64M 0 64M 0% /dev/shm 192.168.30.27:/opt/k8s/wwwroot 36G 5.8G 30G 17% /usr/share/nginx/html tmpfs 2.0G 12K 2.0G 1% /run/secrets/kubernetes.io/serviceaccount tmpfs 2.0G 0 2.0G 0% /proc/acpi tmpfs 2.0G 0 2.0G 0% /proc/scsi tmpfs 2.0G 0 2.0G 0% /sys/firmware
咱們在源pod的網頁目錄下寫入數據,並查看咱們的nfs服務器目錄下也會共享
root@nginx-deployment-744d977b46-8q97k:/# cd /usr/share/nginx/html/ root@nginx-deployment-744d977b46-8q97k:/usr/share/nginx/html# ls root@nginx-deployment-744d977b46-8q97k:/usr/share/nginx/html# echo "hello world" > index.html root@nginx-deployment-744d977b46-8q97k:/usr/share/nginx/html# cat index.html hello world
測試查看
[root@nfs k8s]# cd wwwroot/ [root@nfs wwwroot]# ls [root@nfs wwwroot]# ls index.html [root@nfs wwwroot]# cat index.html hello world
K8s爲了作存儲的編排
數據持久卷PersistentVolume 簡稱pv/pvc主要作容器存儲的編排
• PersistentVolume(PV):對存儲資源建立和使用的抽象,使得存儲做爲集羣中的資源管理
pv都是運維去考慮,用來管理外部存儲的
• 靜態 :提早建立好pv,好比建立一個100G的pv,200G的pv,讓有須要的人拿去用,就是說pvc鏈接pv,就是知道pv建立的是多少,空間大小是多少,建立的名字是多少,有必定的可匹配性
• 動態
• PersistentVolumeClaim(PVC):讓用戶不須要關心具體的Volume實現細節
使用多少個容量來定義,好比開發要部署一個服務要使用10個G,那麼就可使用pvc這個資源對象來定義使用10個G,其餘的就不用考慮了
PersistentVolume 靜態供給
先建立一個容器應用
[root@k8s-master ~]# cd demo/ [root@k8s-master demo]# mkdir storage [root@k8s-master demo]# cd storage/ [root@k8s-master storage]# vim pod.yaml
apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80 volumeMounts: - name: www mountPath: /usr/share/nginx/html volumes: - name: www persistentVolumeClaim: claimName: my-pvc
卷需求yaml,這裏的名稱必定要對應,通常兩個文件都放在一塊
[root@k8s-master storage]# vim pvc.yaml apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-pvc spec: accessModes: - ReadWriteMany resources: requests: storage: 5Gi
接下來就是運維出場了,提早建立好pv
[root@k8s-master storage]# vim pv.yaml apiVersion: v1 kind: PersistentVolume metadata: name: zhaocheng spec: capacity: storage: 5Gi accessModes: - ReadWriteMany nfs: path: /opt/k8s/zhaocheng server: 192.168.30.27
提早建立好pv,以及掛載目錄
[root@k8s-master storage]# kubectl create -f pv.yaml persistentvolume/my-pv created [root@k8s-master storage]# kubectl get pv zhaocheng 5Gi RWX Retain Available 5s
我再建立一個pv,在nfs服務器提早把目錄建立好,名稱修改一下
[root@localhost ~]# cd /opt/k8s/ [root@localhost k8s]# mkdir zhaocheng [root@k8s-master storage]# vim pv2.yaml apiVersion: v1 kind: PersistentVolume metadata: name: zhaochengcheng spec: capacity: storage: 10Gi accessModes: - ReadWriteMany nfs: path: /opt/k8s/zhaochengcheng server: 192.168.30.27
[root@k8s-master storage]# kubectl get pv zhaocheng 5Gi RWX Retain Available 13s zhaochengcheng 10Gi RWX Retain Available 4s
而後如今建立一下咱們的pod和pvc,這裏我寫在一塊兒了
[root@k8s-master storage]# vim pod.yaml apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80 volumeMounts: - name: www mountPath: /usr/share/nginx/html volumes: - name: www persistentVolumeClaim: claimName: my-pvc --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-pvc spec: accessModes: - ReadWriteMany resources: requests: storage: 5Gi [root@k8s-master storage]# kubectl create -f pod.yaml
這裏會根據咱們pod的需求去匹配咱們靜態的pv,咱們是建立了一個5G,一個10G,根據自身大小去匹配
[root@k8s-master storage]# kubectl get pod,pvc pod/my-pod 1/1 Running 0 13s pod/nfs-744d977b46-dh9xj 1/1 Running 0 12m pod/nfs-744d977b46-kcx6h 1/1 Running 0 12m pod/nfs-744d977b46-wqhc6 1/1 Running 0 12m persistentvolumeclaim/my-pvc Bound zhaocheng 5Gi RWX 13s
進入容器查看咱們的使用內存大小
[root@k8s-master storage]# kubectl exec -it pod/my-pod bash root@my-pod:/# df -Th Filesystem Type Size Used Avail Use% Mounted on overlay overlay 17G 4.9G 13G 29% / tmpfs tmpfs 64M 0 64M 0% /dev tmpfs tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/mapper/centos-root xfs 17G 4.9G 13G 29% /etc/hosts shm tmpfs 64M 0 64M 0% /dev/shm 192.168.30.27:/opt/k8s/zhaocheng nfs4 36G 5.8G 30G 17% /usr/share/nginx/html tmpfs tmpfs 2.0G 12K 2.0G 1% /run/secrets/kubernetes.io/serviceaccount tmpfs tmpfs 2.0G 0 2.0G 0% /proc/acpi tmpfs tmpfs 2.0G 0 2.0G 0% /proc/scsi tmpfs tmpfs 2.0G 0 2.0G 0% /sys/firmware
去建立一個網頁測試
root@my-pod:/# cd /usr/share/nginx/html/ root@my-pod:/usr/share/nginx/html# ls root@my-pod:/usr/share/nginx/html# echo "5G ready" > index.html root@my-pod:/usr/share/nginx/html# cat index.html 5G ready
去咱們的nfs服務器查看
[root@localhost ~]# cd /opt/k8s/ [root@localhost k8s]# ls wwwroot zhaocheng zhaochengcheng [root@localhost k8s]# cd zhaocheng [root@localhost zhaocheng]# cat index.html 5G ready
綁定一塊兒了和咱們5G的
[root@k8s-master storage]# kubectl get pv zhaocheng 5Gi RWX Retain Bound default/my-pvc 8m52s zhaochengcheng 10Gi RWX Retain Available 7m51s