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:
minReadySeconds: 30 #更新间隔
strategy: #定义滚动更新策略
rollingUpdate:
maxSurge: 1 #允许超出所需规模的最大Pod数量
maxUnavailable: 0 #允许最大不可用的Pod数量
type: RollingUpdate #更新方式滚动更新
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
version: v1
spec:
terminationGracePeriodSeconds: 10 #优雅停止pod,超过一定时间后旧Pod将会被强制杀死
imagePullSecrets:
- name: nginx_key
containers:
- name: nginx
image: linuxwei/nginx_test:v1
imagePullPolicy: IfNotPresent
env:
- name: PATH
value: /usr/local/tomcat/bin:/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
- name: LANG
value: zh_CN.UTF-8
- name: TZ
value: Asia/Shanghai
ports:
- containerPort: 80
- containerPort: 8080
volumeMounts:
- mountPath: /usr/share/nginx/html
subPath: nginx
name: storage
resources:
limits:
cpu: "1"
memory: 2Gi
requests:
cpu: 250m
memory: 1Gi
livenessProbe:
httpGet:
port: 80
path: /index.html
initialDelaySeconds: 30 #容器启动后30秒进行检测
periodSeconds: 3 #检查周期间隔
timeoutSeconds: 10 #超时时间
readinessProbe:
httpGet:
port: 80
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
timeoutSeconds: 10
volumes:
- name: storage
persistentVolumeClaim:
claimName: nginx
# - name: nginx-nfs
# nfs:
# path: /data/docker/logs
# server: 172.18.20.1
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