k8s無腦系列-NFS存儲(chǔ)(簡單版本)
1.概念
-
搞清楚pv,pvc
pv = PersistentVolume 持久化存儲(chǔ)控制器舆吮,面向集群而不是namespace。
pvc = PersistentVolumeClaim 對接pod與pv晋涣, -
關(guān)系伙菊,官方說明
A PVC to PV binding is a one-to-one mapping, using a ClaimRef which is a bi-directional binding between the PersistentVolume and the PersistentVolumeClaim
大體意思如下:pvc與pv的綁定關(guān)系是一一對應(yīng)的迫像。注意!這里的一一對應(yīng)是每個(gè)pvc只有一個(gè)pv茄蚯,但pv可以對應(yīng)多個(gè)pvc压彭。這一點(diǎn)從Access Modes包含ReadWriteMany就可以看出來睦优。
這篇文章只針對簡單的“靜態(tài)存儲(chǔ)”,不包含“動(dòng)態(tài)存儲(chǔ)”部分哮塞。
pvc與pv對應(yīng)問題刨秆。pvc會(huì)搜索pv,找到?jīng)]有被綁定忆畅,資源要求又一致衡未,如果添加了label,pvc又設(shè)置了matchLabel屬性的時(shí)候家凯,會(huì)自動(dòng)綁定缓醋。 更具體的,會(huì)在NFS存儲(chǔ)(復(fù)雜版本)中介紹绊诲。
2. 部署
2.1 NFS安裝
- 服務(wù)端安裝
$yum install nfs-utils -y
$systemctl enable rpcbind nfs
$systemctl start rpcbind nfs
$mkdir /data/nfs
$chown -R nfsnobody.nfsnobody /data/nfs
$vim /etc/export
/data/nfs 192.168.56.0/24(rw,async,all_squash)
/data/nfs/mysql 192.168.56.0/24(rw,async,no_root_squash,fsid=0) # 這里很重要送粱,MYSQL會(huì)修改目錄權(quán)限所以必須放寬ROOT權(quán)限
$systemctl restart nfs
- 客戶端驗(yàn)證服務(wù)可用性
cd $HOME
mkdir nfs
sudo mount -t nfs 192.168.56.4:/data/nfs nfs
3. 實(shí)際操作
-
創(chuàng)建給MySQL用的存儲(chǔ)空間mysql-pv.yaml
apiVersion: v1 kind: PersistentVolume metadata: name: mysql-pv spec: capacity: storage: 5Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Recycle nfs: path: /data/nfs/mysql server: 192.168.56.4
$kubectl create -f mysql-pv.yaml $kubectl get pv mysql-pv NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM REASON AGE mysql-pv 5Gi RWO Recycle Available 3s
-
創(chuàng)建mysql-pvc.yaml
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mysql-pvc namespace: default spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi
$kubectl create -f mysql-pvc.yaml NAME STATUS VOLUME CAPACITY ACCESSMODES AGE pvc/mysql-pvc Bound mysql-pv 5Gi RWO 22s NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM REASON AGE pv/mysql-pv 5Gi RWO Recycle Bound default/mysql-pvc 2m
-
修改mysql-rc.yaml(參見無腦系列-最小集群搭建)
apiVersion: v1 kind: ReplicationController metadata: name: mysql-rc spec: replicas: 1 selector: app: mysql-pod template: metadata: labels: app: mysql-pod spec: containers: - name: mysql image: mysql:5.6 ports: - containerPort: 3306 env: - name: MYSQL_ROOT_PASSWORD value: "123" volumeMounts: - mountPath: "/var/lib/mysql" name: mysql-data volumes: - name: mysql-data persistentVolumeClaim: claimName: mysql-pvc
$kubectl create -f mysql-rc.yaml $kubectl get rc mysql-rc NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR mysql-rc 1 1 1 16s mysql mysql:5.6 app=mysql-pod
4. 出現(xiàn)的錯(cuò)誤
-
mysql-rc創(chuàng)建不成功,報(bào)告“changing ownership of '/var/lib/mysql/': Operation not permitted”
解答:請參考NFS的export部分掂之,掛載給MySQL的目錄抗俄,一定要加入no_root_squash,fsid=0。不然就會(huì)報(bào)告上面的錯(cuò)誤
如果已經(jīng)做了調(diào)整世舰,請重啟nfs服務(wù)动雹,刪除mysql-rc。重新創(chuàng)建
-
診斷技巧
mysql-rc創(chuàng)建不成功跟压,xxxx代表運(yùn)行時(shí)的編號
$kubectl describe pod mysql-rc-xxxx $kubectl log mysql-rc-xxxx
查看pod所在機(jī)器胰蝠、錯(cuò)誤日志
進(jìn)入pod所在Node,執(zhí)行df命令查看掛載是否成功