1. NFS服务器
1.1 安装(每个节点)
$ yum install -y nfs-utils
1.2 存储
$ mkdir /data/k8s/v{ 1 .. 3 } -p
$ cat /etc/exports
/data/k8s/v1 192.168 .119.0/24( rw,sync,no_root_squash)
/data/k8s/v2 192.168 .119.0/24( rw,sync,no_root_squash)
/data/k8s/v3 192.168 .119.0/24( rw,sync,no_root_squash)
$ showmount -e 192.168 .119.190
Export list for 192.168 .119.190:
/data/k8s/v3 192.168 .119.0/24
/data/k8s/v2 192.168 .119.0/24
/data/k8s/v1 192.168 .119.0/24
1.3 防火墙、selinux
$ systemctl stop firewalld
$ systemctl disable firewalld
$ sed -i '/^SELINUX=/s/enforcing/disabled/' /etc/selinux/config
$ setenforce 0
1.4 服务
$ systemctl start nfs-server
$ systemctl enable nfs-server
$ systemctl start nfs
$ systemctl enable nfs
$ showmount -e 192.168 .119.190
Export list for 192.168 .119.190:
/data/k8s/v3 192.168 .119.0/24
/data/k8s/v2 192.168 .119.0/24
/data/k8s/v1 192.168 .119.0/24
2. PV
2.1 编写yaml
apiVersion : v1
kind : PersistentVolume
metadata :
name : bzx- pv
spec :
capacity :
storage : 1Gi
accessModes :
- ReadWriteOnce
persistentVolumeReclaimPolicy : Recycle
nfs :
path : /data/k8s/v1
server : 192.168.119.190
2.2 创建
$ kubectl apply -f pv01.yaml
2.3 查看pv
$ kubectl describe pv bzx-pv
Name: bzx-pv
Labels: < none>
Annotations: < none>
Finalizers: [ kubernetes.io/pv-protection]
StorageClass:
Status: Available
Claim:
Reclaim Policy: Recycle
Access Modes: RWO
VolumeMode: Filesystem
Capacity: 1Gi
Node Affinity: < none>
Message:
Source:
Type: NFS ( an NFS mount that lasts the lifetime of a pod)
Server: 192.168 .119.190
Path: /data/k8s/v1
ReadOnly: false
Events: < none>
3. PVC
3.1 编写yaml文件
apiVersion : v1
kind : PersistentVolumeClaim
metadata :
name : bzx- pvc
spec :
accessModes :
- ReadWriteOnce
resources :
requests :
storage : 1Gi
3.2 创建
$ kubectl apply -f pvc01.yaml
3.3 查看PVC
$ kubectl describe pvc bzx-pvc
Name: bzx-pvc
Namespace: default
StorageClass:
Status: Bound
Volume: bzx-pv
Labels: < none>
Annotations: pv.kubernetes.io/bind-completed: yes
pv.kubernetes.io/bound-by-controller: yes
Finalizers: [ kubernetes.io/pvc-protection]
Capacity: 1Gi
Access Modes: RWO
VolumeMode: Filesystem
Used By: < none>
Events: < none>
4. deployment、service
4.1 yaml文件
apiVersion : apps/v1
kind : Deployment
metadata :
name : pvc- dep
spec :
replicas : 2
selector :
matchLabels :
app : nfs- pvc
template :
metadata :
labels :
app : nfs- pvc
spec :
containers :
- name : nginx
image : nginx: latest
imagePullPolicy : IfNotPresent
ports :
- name : web
containerPort : 80
volumeMounts :
- name : www
mountPath : /usr/share/nginx/html
volumes :
- name : www
persistentVolumeClaim :
claimName : bzx- pvc
---
apiVersion : v1
kind : Service
metadata :
name : pvc- svc
labels :
app : nfs- pvc
spec :
type : NodePort
ports :
- port : 80
targetPort : web
selector :
app : nfs- pvc
4.2 创建
$ kubectl apply -f deployment01.yaml
4.3 查看
$ kubectl get deployment -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
pvc-dep 2 /2 2 2 3m32s nginx nginx:latest app = nfs-pvc
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT( S) AGE
kubernetes ClusterIP 10.1 .0.1 < none> 443 /TCP 14d
pvc-svc NodePort 10.1 .133.233 < none> 80 :31504/TCP 3m54s
5. 访问测试
$ echo "This is a pvc test page!" > /data/k8s/v1/index.html
$ curl 10.1 .133.233
This is a pvc test page!
http://192.168.119.191:31504/
http://192.168.119.192:31504/
http://192.168.119.193:31504/