在服务器搭建nfs
yum install -y nfs-utils
echo "/nfs/data/ *(insecure,rw,sync,no_root_squash)" > /etc/exports
mkdir -p /nfs/data
systemctl enable rpcbind --now
systemctl enable nfs-server --now
#配置生效
exportfs -r
测试一下 showmount -e localhost 看看nfs服务启动正常
[admin@localhost data]$ showmount -e localhost
Export list for localhost:
/nfs/data *
nfs服务启动成功
编写yml文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 2
selector:
matchLabels:
app: mynginx
template:
metadata:
labels:
app: mynginx
spec:
containers:
- name: nginx
image: nginx:1.25
imagePullPolicy: IfNotPresent
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
volumes:
- name: html
persistentVolumeClaim:
claimName: nginx-pvc
---
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
selector:
app: mynginx
ports:
- port: 8000
protocol: TCP
targetPort: 80
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv01
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
storageClassName: nfs
nfs:
path: /nfs/data
server: 192.168.123.67
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: nginx-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 200Mi
storageClassName: nfs
PVC在PV的基础上选择符合条件的PV,其实就是加了一层
kubectl get 一下确定nfs已经挂在到pv上,pvc选择了合适的pv,pod将pvc选好的pv进行挂载
[admin@localhost k8s]$ kubectl apply -f nginx.yaml
deployment.apps/nginx unchanged
service/nginx unchanged
persistentvolume/pv01 unchanged
persistentvolumeclaim/nginx-pvc created
[admin@localhost k8s]$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 14m
nginx ClusterIP 10.99.104.71 <none> 8000/TCP 7m30s
[admin@localhost k8s]$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS VOLUMEATTRIBUTESCLASS REASON AGE
pv01 10Gi RWX Retain Bound default/nginx-pvc nfs <unset> 2m10s
[admin@localhost k8s]$ kubectl get pv -o wide
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS VOLUMEATTRIBUTESCLASS REASON AGE VOLUMEMODE
pv01 10Gi RWX Retain Bound default/nginx-pvc nfs <unset> 2m18s Filesystem
[admin@localhost k8s]$ kubectl get pvc -o wide
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE VOLUMEMODE
nginx-pvc Bound pv01 10Gi RWX nfs <unset> 43s Filesystem
[admin@localhost k8s]$ kubectl get pv -o wide
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS VOLUMEATTRIBUTESCLASS REASON AGE VOLUMEMODE
pv01 10Gi RWX Retain Bound default/nginx-pvc nfs <unset> 2m36s Filesystem
[admin@localhost data]$ kubectl describe pv pv01
Name: pv01
Labels: <none>
Annotations: pv.kubernetes.io/bound-by-controller: yes
Finalizers: [kubernetes.io/pv-protection]
StorageClass: nfs
Status: Bound
Claim: default/nginx-pvc
Reclaim Policy: Retain
Access Modes: RWX
VolumeMode: Filesystem
Capacity: 10Gi
Node Affinity: <none>
Message:
Source:
Type: NFS (an NFS mount that lasts the lifetime of a pod)
Server: 192.168.123.67
Path: /nfs/data
ReadOnly: false
Events: <none>
[admin@localhost data]$ kubectl describe pvc nginx-pvc
Name: nginx-pvc
Namespace: default
StorageClass: nfs
Status: Bound
Volume: pv01
Labels: <none>
Annotations: pv.kubernetes.io/bind-completed: yes
pv.kubernetes.io/bound-by-controller: yes
Finalizers: [kubernetes.io/pvc-protection]
Capacity: 10Gi
Access Modes: RWX
VolumeMode: Filesystem
Used By: nginx-7d78c7fcdf-55z9w
nginx-7d78c7fcdf-c8xbq
Events: <none>
随便进入一个pod,向挂载目录写个文件
[admin@localhost root]$ kubectl get service -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 17m <none>
nginx ClusterIP 10.99.104.71 <none> 8000/TCP 9m44s app=mynginx
[admin@localhost root]$ kubectl run -it nginx-7d78c7fcdf-c8xbq -- /bin/bash
Error: required flag(s) "image" not set
[admin@localhost root]$ kubectl exec -it nginx-7d78c7fcdf-c8xbq -- /bin/bash
root@nginx-7d78c7fcdf-c8xbq:/# cd /usr/share/nginx/html
root@nginx-7d78c7fcdf-c8xbq:/usr/share/nginx/html# ls
root@nginx-7d78c7fcdf-c8xbq:/usr/share/nginx/html# echo 123 > 1.html
在服务器的挂载目录也能看到1.html
[admin@localhost data]$ ls
1.html
[admin@localhost data]$ cat 1.html
123
curl一下service ip
root@nginx-7d78c7fcdf-c8xbq:/usr/share/nginx/html# curl 10.99.104.71:8000/1.html
123
成功