kubernetes 存储-基于NFS的PV创建应用
1. 安装nfs
yum install -y nfs-utils rpcbind
2. 创建共享目录
mkdir /data
3. 更改目录属主
chown -R nfsnobody:nfsnobody /data
4. 修改配置文件
vim /etc/exports
/nfs/data/ 10.10.10.0/24(rw,sync,all_squash) ## 可读写,同步到本地,压缩为nfsnobody权限
5. 启动nfs
systemctl start nfs
systemctl enable nfs
6. 创建 pv 资源
vim nginx-html-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: nginx-html-pv
spec:
capacity:
storage: 1Gi ## 申请储存空间
accessModes:
- ReadWriteMany ## 允许多个pod读写
nfs:
path: /nfs/data/nginx/html ## 需要申请的nfs目录
server: 10.10.10.10 ## nfs服务器地址
7. 创建 pvc 资源
vim nginx-html-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nginx-html-pvc
labels:
app: nginx
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi ## 需要的大小,匹配pv
8. 创建 Deploymnet 资源
vim nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 2 ## pod 附本数
selector:
matchLabels:
app: nginx
strategy:
type: Recreate ## 升级方式
template:
metadata:
labels:
app: nginx
spec:
imagePullSecrets:
- name: registry-secret # 下载私有仓库的镜像时需要设置Secret
containers:
- image: hub.ko.com/k8s-harbor/nginx:v1
name: nginx
ports:
- containerPort: 80
volumeMounts:
- name: nginx-html-storage # 使用 pvc 并挂载
mountPath: /usr/share/nginx/html
volumes:
- name: nginx-html-storage ## 申明 pvc
persistentVolumeClaim:
claimName: nginx-html-pvc