Kubernetes進(jìn)階之PersistentVolume 靜態(tài)供給實(shí)現(xiàn)NFS網(wǎng)絡(luò)存儲
網(wǎng)絡(luò)存儲
成都創(chuàng)新互聯(lián)公司云計(jì)算的互聯(lián)網(wǎng)服務(wù)提供商,擁有超過13年的服務(wù)器租用、資陽主機(jī)托管、云服務(wù)器、虛擬空間、網(wǎng)站系統(tǒng)開發(fā)經(jīng)驗(yàn),已先后獲得國家工業(yè)和信息化部頒發(fā)的互聯(lián)網(wǎng)數(shù)據(jù)中心業(yè)務(wù)許可證。專業(yè)提供云主機(jī)、虛擬空間、國際域名空間、VPS主機(jī)、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。
NFS是一種很早的技術(shù),單機(jī)的存儲在服務(wù)器方面還是非常主流的,但nfs唯一的就是缺點(diǎn)比較大就是沒有集群版,做集群化還是比較費(fèi)勁的,文件系統(tǒng)做不了,這是一個很大的弊端,大規(guī)模的還是需要選擇一些分布式的存儲,nfs就是一個網(wǎng)絡(luò)文件存儲服務(wù)器,裝完nfs之后,共享一個目錄,其他的服務(wù)器就可以通過這個目錄掛載到本地了,在本地寫到這個目錄的文件,就會同步到遠(yuǎn)程服務(wù)器上,實(shí)現(xiàn)一個共享存儲的功能,一般都是做數(shù)據(jù)的共享存儲,比如多臺web服務(wù)器,肯定需要保證這些web服務(wù)器的數(shù)據(jù)一致性,那就會用到這個共享存儲了,要是將nfs掛載到多臺的web服務(wù)器上,網(wǎng)站根目錄下,網(wǎng)站程序就放在nfs服務(wù)器上,這樣的話。每個網(wǎng)站,每個web程序都能讀取到這個目錄,一致性的數(shù)據(jù),這樣的話就能保證多個節(jié)點(diǎn),提供一致性的程序了。
單獨(dú)拿一臺服務(wù)器做nfs服務(wù)器,我們這里先搭建一臺NFS服務(wù)器用來存儲我們的網(wǎng)頁根目錄
[root@nfs ~]# yum install nfs-utils -y
暴露目錄,讓是讓其他服務(wù)器能掛載這個目錄
[root@nfs ~]# mkdir /opt/k8s
[root@nfs ~]# vim /etc/exports
/opt/k8s 192.168.30.0/24(rw,no_root_squash)
給這個網(wǎng)段加上權(quán)限,可讀可寫[root@nfs ~]# systemctl start nfs
找個節(jié)點(diǎn)去掛載測試一下,只要去共享這個目錄就要都去安裝這個客戶端
[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
去服務(wù)器端查看已經(jīng)數(shù)據(jù)共享過來了
[root@nfs ~]# cd /opt/k8s/
[root@nfs k8s]# ls
a.txt
刪除nfs服務(wù)器的數(shù)據(jù)也會刪除
接下來怎么將K8s進(jìn)行使用
我們把網(wǎng)頁目錄都放在這個目錄下
[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 6h7m
mypod2 1/1 Running 0 6h
nginx-5ddcc6cb74-lplxl 1/1 Running 0 6h53m
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
進(jìn)入容器并查看掛載,確定掛載上
[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的網(wǎng)頁目錄下寫入數(shù)據(jù),并查看我們的nfs服務(wù)器目錄下也會共享
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為了做存儲的編排
數(shù)據(jù)持久卷PersistentVolume 簡稱pv/pvc主要做容器存儲的編排
? PersistentVolume(PV):對存儲資源創(chuàng)建和使用的抽象,使得存儲作為集群中的資源管理
pv都是運(yùn)維去考慮,用來管理外部存儲的
? 靜態(tài) :提前創(chuàng)建好pv,比如創(chuàng)建一個100G的pv,200G的pv,讓有需要的人拿去用,就是說pvc連接pv,就是知道pv創(chuàng)建的是多少,空間大小是多少,創(chuàng)建的名字是多少,有一定的可匹配性
? 動態(tài)
? PersistentVolumeClaim(PVC):讓用戶不需要關(guān)心具體的Volume實(shí)現(xiàn)細(xì)節(jié)
使用多少個容量來定義,比如開發(fā)要部署一個服務(wù)要使用10個G,那么就可以使用pvc這個資源對象來定義使用10個G,其他的就不用考慮了
PersistentVolume 靜態(tài)供給
先創(chuàng)建一個容器應(yīng)用
[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,這里的名稱一定要對應(yīng),一般兩個文件都放在一塊
[root@k8s-master storage]# vim pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
接下來就是運(yùn)維出場了,提前創(chuàng)建好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
提前創(chuàng)建好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
我再創(chuàng)建一個pv,在nfs服務(wù)器提前把目錄創(chuàng)建好,名稱修改一下
[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
然后現(xiàn)在創(chuàng)建一下我們的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
這里會根據(jù)我們pod的需求去匹配我們靜態(tài)的pv,我們是創(chuàng)建了一個5G,一個10G,根據(jù)自身大小去匹配
[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
進(jìn)入容器查看我們的使用內(nèi)存大小
[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
去創(chuàng)建一個網(wǎng)頁測試
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服務(wù)器查看
[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
新聞名稱:Kubernetes進(jìn)階之PersistentVolume靜態(tài)供給實(shí)現(xiàn)NFS網(wǎng)絡(luò)存儲
標(biāo)題鏈接:http://aaarwkj.com/article42/jeigec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、云服務(wù)器、小程序開發(fā)、自適應(yīng)網(wǎng)站、關(guān)鍵詞優(yōu)化、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)