溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Kubernetes進(jìn)階之PersistentVolume 靜態(tài)供給實(shí)現(xiàn)NFS網(wǎng)絡(luò)存儲(chǔ)

發(fā)布時(shí)間:2020-08-23 15:29:02 來源:網(wǎng)絡(luò) 閱讀:713 作者:wx5c1cfd6e22842 欄目:系統(tǒng)運(yùn)維

Kubernetes進(jìn)階之PersistentVolume 靜態(tài)供給實(shí)現(xiàn)NFS網(wǎng)絡(luò)存儲(chǔ)
網(wǎng)絡(luò)存儲(chǔ)

NFS是一種很早的技術(shù),單機(jī)的存儲(chǔ)在服務(wù)器方面還是非常主流的,但nfs唯一的就是缺點(diǎn)比較大就是沒有集群版,做集群化還是比較費(fèi)勁的,文件系統(tǒng)做不了,這是一個(gè)很大的弊端,大規(guī)模的還是需要選擇一些分布式的存儲(chǔ),nfs就是一個(gè)網(wǎng)絡(luò)文件存儲(chǔ)服務(wù)器,裝完nfs之后,共享一個(gè)目錄,其他的服務(wù)器就可以通過這個(gè)目錄掛載到本地了,在本地寫到這個(gè)目錄的文件,就會(huì)同步到遠(yuǎn)程服務(wù)器上,實(shí)現(xiàn)一個(gè)共享存儲(chǔ)的功能,一般都是做數(shù)據(jù)的共享存儲(chǔ),比如多臺(tái)web服務(wù)器,肯定需要保證這些web服務(wù)器的數(shù)據(jù)一致性,那就會(huì)用到這個(gè)共享存儲(chǔ)了,要是將nfs掛載到多臺(tái)的web服務(wù)器上,網(wǎng)站根目錄下,網(wǎng)站程序就放在nfs服務(wù)器上,這樣的話。每個(gè)網(wǎng)站,每個(gè)web程序都能讀取到這個(gè)目錄,一致性的數(shù)據(jù),這樣的話就能保證多個(gè)節(jié)點(diǎn),提供一致性的程序了。

單獨(dú)拿一臺(tái)服務(wù)器做nfs服務(wù)器,我們這里先搭建一臺(tái)NFS服務(wù)器用來存儲(chǔ)我們的網(wǎng)頁根目錄

[root@nfs ~]# yum install nfs-utils -y
暴露目錄,讓是讓其他服務(wù)器能掛載這個(gè)目錄

[root@nfs ~]# mkdir /opt/k8s
[root@nfs ~]# vim /etc/exports
/opt/k8s 192.168.30.0/24(rw,no_root_squash)

給這個(gè)網(wǎng)段加上權(quán)限,可讀可寫
[root@nfs ~]# systemctl start nfs

找個(gè)節(jié)點(diǎn)去掛載測(cè)試一下,只要去共享這個(gè)目錄就要都去安裝這個(gè)客戶端

[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ù)也會(huì)刪除
接下來怎么將K8s進(jìn)行使用
我們把網(wǎng)頁目錄都放在這個(gè)目錄下

[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

我們?cè)谠磒od的網(wǎng)頁目錄下寫入數(shù)據(jù),并查看我們的nfs服務(wù)器目錄下也會(huì)共享

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

測(cè)試查看

[root@nfs k8s]# cd wwwroot/
[root@nfs wwwroot]# ls
[root@nfs wwwroot]# ls
index.html
[root@nfs wwwroot]# cat index.html 
hello world

K8s為了做存儲(chǔ)的編排
數(shù)據(jù)持久卷PersistentVolume 簡(jiǎn)稱pv/pvc主要做容器存儲(chǔ)的編排

? PersistentVolume(PV):對(duì)存儲(chǔ)資源創(chuàng)建和使用的抽象,使得存儲(chǔ)作為集群中的資源管理
pv都是運(yùn)維去考慮,用來管理外部存儲(chǔ)的
? 靜態(tài) :提前創(chuàng)建好pv,比如創(chuàng)建一個(gè)100G的pv,200G的pv,讓有需要的人拿去用,就是說pvc連接pv,就是知道pv創(chuàng)建的是多少,空間大小是多少,創(chuàng)建的名字是多少,有一定的可匹配性
? 動(dòng)態(tài)
? PersistentVolumeClaim(PVC):讓用戶不需要關(guān)心具體的Volume實(shí)現(xiàn)細(xì)節(jié)
使用多少個(gè)容量來定義,比如開發(fā)要部署一個(gè)服務(wù)要使用10個(gè)G,那么就可以使用pvc這個(gè)資源對(duì)象來定義使用10個(gè)G,其他的就不用考慮了
PersistentVolume 靜態(tài)供給

Kubernetes進(jìn)階之PersistentVolume 靜態(tài)供給實(shí)現(xiàn)NFS網(wǎng)絡(luò)存儲(chǔ)
先創(chuàng)建一個(gè)容器應(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,這里的名稱一定要對(duì)應(yīng),一般兩個(gè)文件都放在一塊

[root@k8s-master storage]# vim pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi

接下來就是運(yùn)維出場(chǎng)了,提前創(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)建一個(gè)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 

這里會(huì)根據(jù)我們pod的需求去匹配我們靜態(tài)的pv,我們是創(chuàng)建了一個(gè)5G,一個(gè)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)建一個(gè)網(wǎng)頁測(cè)試

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
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI