1. 创建yaml
yaml
[root@kube-master nfs]# cat 1.nfs.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nfs-deployment
spec:
replicas: 1
selector:
matchLabels:
app: my-nfs-app
template:
metadata:
labels:
app: my-nfs-app
spec:
containers:
- name: my-nfs-container
image: nginx:latest
ports:
- containerPort: 80
volumeMounts:
- name: my-nfs-volume
mountPath: /usr/share/nginx/html
volumes:
- name: my-nfs-volume
nfs:
server: 192.168.1.100
path: /data/nfs #nfs中此文件夹必须事先存在(需要在每个节点上面创建,因为你不知道pod在那个节点上面创建),否则pod建立不了
[root@kube-master nfs]# cat 1.nfs.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nfs-deployment
spec:
replicas: 1
selector:
matchLabels:
app: my-nfs-app
template:
metadata:
labels:
app: my-nfs-app
spec:
containers:
- name: my-nfs-container
image: nginx:latest
ports:
- containerPort: 80
volumeMounts:
- name: my-nfs-volume
mountPath: /usr/share/nginx/html
volumes:
- name: my-nfs-volume
nfs:
server: 192.168.1.100
path: /data/nfs #nfs中此文件夹必须事先存在(需要在每个节点上面创建,因为你不知道pod在那个节点上面创建),否则pod建立不了
💡 说明
这就是原生的一个缺点,需要在每个节点上面创建这个目录
2. 执行apply
bash
[root@kube-master nfs]# kubectl apply -f 1.nfs.yaml
deployment.apps/my-nfs-deployment created
[root@kube-master nfs]# kubectl apply -f 1.nfs.yaml
deployment.apps/my-nfs-deployment created
3. 查看
bash
[root@kube-master nfs]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-nfs-deployment-59b75555dd-qwtbl 1/1 Running 0 4m35s 172.30.0.148 kube-node01 <none> <none>
[root@kube-master nfs]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-nfs-deployment-59b75555dd-qwtbl 1/1 Running 0 4m35s 172.30.0.148 kube-node01 <none> <none>
4. 在nfs上面创建文件
bash
#在server端执行
echo "hello nfs" > /data/nfs/index.html
#在server端执行
echo "hello nfs" > /data/nfs/index.html
- 验证效果
bash
[root@kube-master nfs]# curl 172.30.0.148
hello nfs
[root@kube-master nfs]# curl 172.30.0.148
hello nfs
5. redis案例
5.1 pv
yaml
[root@kube-master nfs]# cat 4.nfs-redis-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv
labels: #标签
release: nfs-redis
spec:
storageClassName: "nfs-storage" #资源类型表示
persistentVolumeReclaimPolicy: "Recycle" #回收策略为Recycle相当rm -rf /
capacity:
storage: 0.5Gi
accessModes:
- ReadWriteMany
nfs:
server: 10.103.236.199
path: /data/nfs/redis
[root@kube-master nfs]# cat 4.nfs-redis-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv
labels: #标签
release: nfs-redis
spec:
storageClassName: "nfs-storage" #资源类型表示
persistentVolumeReclaimPolicy: "Recycle" #回收策略为Recycle相当rm -rf /
capacity:
storage: 0.5Gi
accessModes:
- ReadWriteMany
nfs:
server: 10.103.236.199
path: /data/nfs/redis
5.2 pvc
yaml
[root@kube-master nfs]# cat 5.nfs-redis-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-pvc
labels: #首先标签须匹配,不然无法匹配,也可以称为强行绑定。
release: nfs-redis
spec:
storageClassName: "nfs-storage" #须同属一个
accessModes:
- ReadWriteMany
resources:
requests:
storage: 0.5Gi #指定大小。
[root@kube-master nfs]# cat 5.nfs-redis-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-pvc
labels: #首先标签须匹配,不然无法匹配,也可以称为强行绑定。
release: nfs-redis
spec:
storageClassName: "nfs-storage" #须同属一个
accessModes:
- ReadWriteMany
resources:
requests:
storage: 0.5Gi #指定大小。
- 查看
bash
[root@kube-master nfs]# kubectl get pvc,pv
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/nfs-pvc Bound nfs-pv 512Mi RWX nfs-storage 2m
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/nfs-pv 512Mi RWX Recycle Bound default/nfs-pvc nfs-storage 5m12s
[root@kube-master nfs]# kubectl get pvc,pv
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/nfs-pvc Bound nfs-pv 512Mi RWX nfs-storage 2m
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/nfs-pv 512Mi RWX Recycle Bound default/nfs-pvc nfs-storage 5m12s
bash
[root@kube-master nfs]# kubectl describe pv nfs-pv
Name: nfs-pv
Labels: release=nfs-redis # 所属标签
Annotations: pv.kubernetes.io/bound-by-controller: yes
Finalizers: [kubernetes.io/pv-protection]
StorageClass: nfs-storage # storageclass名称
Status: Bound # 绑定状态
Claim: default/nfs-pvc # 名称空间
Reclaim Policy: Recycle # 回收策略
Access Modes: RWX # 访问模式
VolumeMode: Filesystem
Capacity: 512Mi # 大小
Node Affinity: <none>
Message:
Source:
Type: NFS (an NFS mount that lasts the lifetime of a pod)
Server: 10.103.236.199 # 来自那个nfs服务器
Path: /data/nfs/redis # 共享数据目录
ReadOnly: false
Events: <none>
[root@kube-master nfs]# kubectl describe pv nfs-pv
Name: nfs-pv
Labels: release=nfs-redis # 所属标签
Annotations: pv.kubernetes.io/bound-by-controller: yes
Finalizers: [kubernetes.io/pv-protection]
StorageClass: nfs-storage # storageclass名称
Status: Bound # 绑定状态
Claim: default/nfs-pvc # 名称空间
Reclaim Policy: Recycle # 回收策略
Access Modes: RWX # 访问模式
VolumeMode: Filesystem
Capacity: 512Mi # 大小
Node Affinity: <none>
Message:
Source:
Type: NFS (an NFS mount that lasts the lifetime of a pod)
Server: 10.103.236.199 # 来自那个nfs服务器
Path: /data/nfs/redis # 共享数据目录
ReadOnly: false
Events: <none>
5.3 pod
yaml
[root@kube-master nfs]# cat 6.nfs-redis-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: redis
spec:
containers:
- name: redis
image: redis
volumeMounts:
- name: redis-data
mountPath: /data
volumes:
- name: redis-data
persistentVolumeClaim:
claimName: nfs-pvc
[root@kube-master nfs]# cat 6.nfs-redis-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: redis
spec:
containers:
- name: redis
image: redis
volumeMounts:
- name: redis-data
mountPath: /data
volumes:
- name: redis-data
persistentVolumeClaim:
claimName: nfs-pvc
- 执行apply
bash
[root@kube-master nfs]# kubectl apply -f 6.nfs-redis-pod.yaml
[root@kube-master nfs]# kubectl apply -f 6.nfs-redis-pod.yaml
- 查看pod地址
bash
[root@kube-master nfs]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
redis 1/1 Running 0 11m 172.30.0.170 kube-node01 <none> <none>
[root@kube-master nfs]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
redis 1/1 Running 0 11m 172.30.0.170 kube-node01 <none> <none>
- 进入到redis
bash
[root@kube-master nfs]# kubectl exec -it redis -- /bin/bash
root@redis:/data# redis-cli -h 127.0.0.1
127.0.0.1:6379> set kety han
OK
127.0.0.1:6379> bgsave
Background saving started
127.0.0.1:6379> exit
[root@kube-master nfs]# kubectl exec -it redis -- /bin/bash
root@redis:/data# redis-cli -h 127.0.0.1
127.0.0.1:6379> set kety han
OK
127.0.0.1:6379> bgsave
Background saving started
127.0.0.1:6379> exit
- 查看nfs端
bash
[root@Rocky redis]# ls
dump.rdb
[root@Rocky redis]# pwd
/data/nfs/redis
[root@Rocky redis]# ls
dump.rdb
[root@Rocky redis]# pwd
/data/nfs/redis