nfs搭建
# 关闭防火墙 systemctl stop firewalld.service systemctl disable firewalld.service # 1.在线安装 yum -y install nfs-utils rpcbind # 共享目录设置权限: chmod 755 /data/k8s/ # 配置 nfs,nfs 的默认配置文件在 /etc/exports 文件下 vi /etc/exports /data/k8s *(rw,sync,no_root_squash) #/data/k8s:是共享的数据目录 #*:表示任何人都有权限连接,当然也可以是一个网段,一个 IP,也可以是域名 #rw:读写的权限 #sync:表示文件同时写入硬盘和内存 #no_root_squash:当登录 NFS 主机使用共享目录的使用者是 root 时,其权限将被转换成为匿名使用者,通常它的 UID 与 GID,都会变成 nobody 身份 systemctl start rpcbind.service systemctl enable rpcbind.service systemctl status rpcbind.service systemctl start nfs.service systemctl enable nfs.service systemctl status nfs.service # 查看具体目录挂载权限: cat /var/lib/nfs/etab # 子节点查看 showmount -e slave1 #Export list for 10.151.30.57: # /data/k8s * # 在客户端上新建目录: mkdir -p /data/k8s #将 nfs 共享目录挂载到上面的目录: mount -t nfs slave1:/data/k8s /data/k8s # 客户端新建个文件测试 touch /data/k8s/hello.txt # 2.离线安装 cd /home/nfs # 安装nfs 需要提前把 包 准备好 rpm -ivh *.rpm --force --nodeps
pv和pvc入门
先来测试一下 nfs
apiVersion: apps/v1 kind: Deployment metadata: name: nginx spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: 10.140.113.5:82/k3s/nginx volumeMounts: - name: wwwroot mountPath: /usr/local/nginx/html ports: - containerPort: 80 volumes: - name: wwwroot nfs: server: 10.140.60.16 path: /data/k8s
pv.yaml
apiVersion: v1 kind: PersistentVolume metadata: name: my-pv spec: capacity: storage: 5Gi accessModes: - ReadWriteOnce nfs: server: 10.140.60.16 path: /data/k8s
pvc.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-dep1 spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: 10.140.113.5:82/k3s/nginx volumeMounts: - name: wwwroot mountPath: /usr/share/nginx/html ports: - containerPort: 80 volumes: - name: wwwroot persistentVolumeClaim: claimName: my-pvc --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-pvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 5Gi
注:这里的accessModes只能选用ReadWriteOnce,选用 ReadWriteMany 会报错