目录
实验:pv-pvc-nfs-service综合实验
实验环境
实验描述
实验拓扑图:
实验步骤:
1、修改nfs服务器的主机名:
2、搭建nfs服务器:(131条消息) 搭建NFS服务器_搭建nfs存储_Claylpf的博客-CSDN博客
3、测试k8s上所有节点服务器是否可以挂载到NFS服务器上去
4、在master机器上创建pv使用nfs服务器上的共享目录
5、在master机器上创建pvc使用pv
6、在master机器上创建pod使用pvc
7、测试去访问它(在node1上去访问)
8、使用deployment方式部署pod
9、创建一个service发布我们的Pod
10、测试访问
4台centos7虚拟机 (都已经搭建好k8s) 和一台准备搭建nfs服务器的centos7虚拟机
使用pv持久卷和pvc持久卷申请结合nfs将内网的数据发布出去,让外网的用户可以访问到内网的数据,更深刻的理解pv、pvc和nfs的使用流程,有更深刻的体会。
[root@nginx-lb1 ~]# hostnamectl set-hostname nfs
[root@nginx-lb1 ~]# su - root
上一次登录:一 7月 17 00:16:08 CST 2023从 192.168.2.43pts/1 上
[root@nfs ~]#
[root@nfs ~]# ps aux|grep nfs
root 3248 0.0 0.0 0 0 ? S< 00:36 0:00 [nfsd4_callbacks]
root 3254 0.0 0.0 0 0 ? S 00:36 0:00 [nfsd]
root 3255 0.0 0.0 0 0 ? S 00:36 0:00 [nfsd]
root 3256 0.0 0.0 0 0 ? S 00:36 0:00 [nfsd]
root 3257 0.0 0.0 0 0 ? S 00:36 0:00 [nfsd]
root 3258 0.0 0.0 0 0 ? S 00:36 0:00 [nfsd]
root 3259 0.0 0.0 0 0 ? S 00:36 0:00 [nfsd]
root 3260 0.0 0.0 0 0 ? S 00:36 0:00 [nfsd]
root 3261 0.0 0.0 0 0 ? S 00:36 0:00 [nfsd]
root 3271 0.0 0.0 112824 988 pts/1 R+ 00:36 0:00 grep --color=auto nfs
[root@nfs ~]# vim /etc/exports
[root@nfs ~]# mkdir /web
[root@nfs ~]# cd /web/
[root@nfs web]# ls
[root@nfs web]# exportfs -rv
exporting 192.168.2.0/24:/web
成功搭建NFS服务器
[root@master ~]# mkdir /sanchuang
[root@master ~]# mount 192.168.2.77:/web /sanchuang #挂载命令
[root@master ~]# df|grep web
192.168.2.77:/web 17811456 1769728 16041728 10% /sanchuang
[root@master ~]#
创建pv-nfs.yaml文件
[root@master pv]# vim pv-nfs.yaml
[root@master pv]# cat pv-nfs.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: sc-nginx-pv
labels:
type: sc-nginx-pv
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteMany
storageClassName: nfs #pv对应的名字
nfs:
path: "/web" #nfs共享的目录
server: 192.168.2.77 #nfs服务器的ip地址
readOnly: false #访问模式
[root@master pv]#
创建Pod
[root@master pv]# kubectl apply -f pv-nfs.yaml
persistentvolume/sc-nginx-pv created
[root@master pv]#
查看效果
[root@master pv]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
sc-nginx-pv 5Gi RWX Retain Available nfs 23s
task-pv-volume 10Gi RWO Retain Bound default/task-pv-claim manual 14h
[root@master pv]#
创建pvc使用pv
[root@master pv]# vim pvc-nfs.yaml
[root@master pv]# cat pvc-nfs.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: sc-nginx-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
storageClassName: nfs #使用nfs类型的pv
[root@master pv]#
创建Pod使用pvc
[root@master pv]# kubectl apply -f pvc-nfs.yaml
persistentvolumeclaim/sc-nginx-pvc created
[root@master pv]#
查看效果
[root@master pv]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
sc-nginx-pvc Bound sc-nginx-pv 5Gi RWX nfs 57s
task-pv-claim Bound task-pv-volume 10Gi RWO manual 13h
[root@master pv]#
创建pod使用pvc
[root@master pv]# vim pod-nfs.yaml
[root@master pv]# cat pod-nfs.yaml
apiVersion: v1
kind: Pod #这里使用pod方式去部署,下面也会介绍用deployment方式部署
metadata:
name: sc-pv-pod-nfs
spec:
volumes:
- name: sc-pv-storage-nfs
persistentVolumeClaim:
claimName: sc-nginx-pvc
containers:
- name: sc-pv-container-nfs
image: nginx
ports:
- containerPort: 80
name: "http-server"
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: sc-pv-storage-nfs
[root@master pv]#
应用和验证
[root@master pv]# kubectl apply -f pod-nfs.yaml
pod/sc-pv-pod-nfs created
[root@master pv]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-nginx-575db987b-d74hs 1/1 Running 0 12h 10.244.1.8 node1
my-nginx-575db987b-rmnvh 1/1 Running 0 16h 10.244.3.5 node3
my-nginx-575db987b-w2xvf 1/1 Running 0 16h 10.244.2.5 node2
sc-pv-pod-nfs 1/1 Running 0 13s 10.244.1.9 node1
test 1/1 Running 0 13h 10.244.2.7 node2
[root@master pv]#
[root@node1 sanchuang]# curl 10.244.1.12
Welcome tow sanchuang!
[root@node1 sanchuang]#
在nfs服务器上修改共享目录的内容,然后再重新在node机器上访问,观察是否变化,若跟nfs服务器上的共享目录一样,则证明成功
[root@master pv]# cat pod-nfs-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: claylpf-nginx
spec:
replicas: 5 #创建5个副本
selector:
matchLabels:
app: claylpf-nginx
template:
metadata:
labels:
app: claylpf-nginx
spec:
volumes:
- name: sc-pv-storage-nfs-2
persistentVolumeClaim:
claimName: sc-nginx-pvc
containers:
- name: sc-pv-container-nfs-2
image: nginx
ports:
- containerPort: 80
name: "http-server"
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: sc-pv-storage-nfs-2
[root@master pv]#
应用和查看
[root@master pv]# kubectl apply -f pod-nfs-deploy.yaml
deployment.apps/nginx-deployment created
[root@master pv]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-nginx-575db987b-d74hs 1/1 Running 0 13h 10.244.1.8 node1
my-nginx-575db987b-rmnvh 1/1 Running 0 17h 10.244.3.5 node3
my-nginx-575db987b-w2xvf 1/1 Running 0 17h 10.244.2.5 node2
nginx-deployment-849bdd77bb-5p6ms 1/1 Running 0 21s 10.244.1.14 node1
nginx-deployment-849bdd77bb-h5vvr 1/1 Running 0 22s 10.244.3.6 node3
nginx-deployment-849bdd77bb-r7cdk 1/1 Running 0 21s 10.244.3.7 node3
nginx-deployment-849bdd77bb-whcn2 1/1 Running 0 22s 10.244.1.13 node1
nginx-deployment-849bdd77bb-z6r52 1/1 Running 0 22s 10.244.2.8 node2
test 1/1 Running 0 14h 10.244.2.7 node2
[root@master pv]# cat pod-nfs-service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-nginx-nfs
labels:
run: my-nginx-nfs
spec:
type: NodePort
ports:
- port: 8080
targetPort: 80
protocol: TCP
name: http
selector:
app: claylpf-nginx #注意这里要用app的形式,跟前面的nfs-pod文件对应,有些使用方法是run,不要搞错了
[root@master pv]#
应用和查看
[root@master pv]# kubectl apply -f pod-nfs-service.yaml
[root@master pv]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 443/TCP 18h
my-nginx-nfs NodePort 10.10.6.200 8080:31058/TCP 72s
[root@master pv]#
随意访问一台主机的ip地址的31058端口,看是否出现画面