k8s存储
一、部署NFS服务端 二选一
1.1 ubuntu 安装
sudo apt-get install nfs-kernel-server
在exports文件下添加以下内容:
sudo vim /etc/exports
/public *(rw,sync,no_root_squash)
1.2 centos 安装
yum install nfs-utils.x86_64
yum install rpcbind.x86_64
systemctl enable nfs
systemctl enable rpcbind
systemctl start nfs
systemctl start rpcbind
systemctl stop firewalld
systemctl disable firewalld
vim /etc/exports #添加如下内容
/public *(rw,async,no_root_squash,no_subtree_check)
安装完之后开机默认开启nfs和rpcbind服务:
systemctl enable nfs
systemctl enable rpcbind
systemctl start nfs
systemctl start rpcbind
systemctl stop firewalld
systemctl disable firewalld
二、使用客户端测试 二选一
2.1 ubuntu 安装
sudo apt-get install nfs-common
2.2 centos
开启服务后挂载进行挂载即可:
yum install nfs-utils.x86_64
yum install rpcbind.x86_64
挂载都是一样的,挂载后在客户端电脑就可以看见了。
mount -t nfs 192.168.77.146:/public /public #ip地址需要改成服务器的,
三、挂载到K8S集群测试
3.1 挂载pv
pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata: # PV建立不要加名称空间,因为PV属于集群级别的
name: nfs # PV名称
spec: # 这里的spec和volumes里面的一样
storageClassName: manual
accessModes: # 设置访问模型
- ReadWriteMany
capacity: # 设置存储空间大小
storage: 1Gi
persistentVolumeReclaimPolicy: Recycle # 回收策略
nfs:
path: /public
server: 192.168.77.146
终端查看:
kubectl apply -f pv.yaml
3.2 挂载pvc
pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs
spec:
accessModes:
- ReadWriteMany
storageClassName: manual
resources:
requests:
storage: 1Gi
kubectl apply -f pvc.yaml
3.3 挂载在pod上测试
tes_nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: my-nginx-pod
namespace: pv
labels:
name: my-nginx-pod
spec:
containers:
- name: my-nginx
image: nginx
ports:
- name: web
containerPort: 80
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: nfs
volumes:
- name: nfs
persistentVolumeClaim:
claimName: nfs #将/usr/share/nginx/html挂载到nfs
错误1:注意:在所有node节点都要安装 ,否则报错:Unable to attach or mount volumes: unmounted volumes
sudo apt-get install nfs-common
挂载成功:注意挂载不成功,检查一下 kubectl get pod -n kube-system 里面有没有问题。