Nginx为例
1.创建NFS共享目录
[ root@localhost ~ ]# yum -y install nfs-utils rpcbind
[ root@localhost ~ ]# mkdir -p /nfsdata
[ root@localhost ~ ]# echo "/nfsdata 192.168.3.0/24(rw,no_passwd_squash,sync)" >> /etc/export
[ root@localhost ~ ]# systemctl enable rpcbind nfs
#开启ipv6
[ root@localhost ~ ]# vim /etc/sysctl.conf
net.ipv6.conf.all.disable_ipv6 = 0
net.ipv6.conf.default.disable_ipv6 = 0
net.ipv6.conf.lo.disable_ipv6 = 0
[ root@localhost ~ ]# sysctl -p
[ root@localhost ~ ]# systemctl start rpcbind nfs
[ root@localhost ~ ]# showmount -e localhost
2.创建PV
[ root@localhost ~ ]# vim nginx.pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: nginx
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs
nfs:
path: /nfsdata
server: 192.168.3.2
3.创建PVC
[ root@localhost ~ ]# vim nginx.pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nginx
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
storageClassName: nfs
4.创建Deployment
[ root@localhost ~ ]# vim nginx.deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: ntp.wei.club/nginx/nginx:latest
ports:
- containerPort: 80
volumeMounts:
- mountPath: /usr/share/nginx/html
subPath: nginx
name: storage
volumes:
- name: storage
persistentVolumeClaim:
claimName: nginx
5.创建Service
[ root@localhost ~ ]# vim nginx.service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
selector:
app: nginx
type: NodePort
ports:
- nodePort: 8080
name: web
port: 80
targetPort: 80