创建共享存储目录
yum install -y rpcbind nfs-utils
创建共享目录
mkdir -p /home/sharedir/www
修改目录权限
chmod 755 sharedir
修改NFS配置文件
vi /etc/exports
/home/sharedir 192.168.2.0(rw,no_root_squash,no_all_squash,sync)
[目录(全目录)] [IP(可填范围,*代表全部)] ([OPTION])
启动NFS
systemctl start nfs
创建pv.yaml
cat > /root/httpd-pv.yaml <<EOF
apiVersion: v1
kind: PersistentVolume
metadata:
name: httpd-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
storageClassName: nfs #和pvc.yaml文件保持一致
nfs:
path: /home/sharedir/www
server: 192.168.2.120
EOF
创建pvc.yaml
cat > /root/httpd-pvc.yaml <<EOF
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: httpd-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: nfs #和pv.yaml文件保持一致
EOF
创建httpd.yaml
cat > /root/httpd.yaml <<EOF
apiVersion: v1
kind: Service
metadata:
name: httpd
spec:
type: NodePort
ports:
- port: 80
nodePort: 32222
targetPort: 80
selector:
app: httpd
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd
spec:
selector:
matchLabels:
app: httpd
template:
metadata:
labels:
app: httpd
spec:
containers:
- name: httpd
image: httpd
ports:
- name: httpd
containerPort: 80
volumeMounts:
- name: httpd-persistent-storage
mountPath: /usr/local/apache2/htdocs
volumes:
- name: httpd-persistent-storage
persistentVolumeClaim:
claimName: httpd-pvc
EOF
执行资源创建
kubectl apply -f httpd-pv.yaml
kubectl apply -f httpd-pvc.yaml
kubectl apply -f httpd.yaml
查看容器
[root@master mysql]# kubectl get pod
NAME READY STATUS RESTARTS AGE
httpd-65c5d64bd7-pmwwr 1/1 Running 0 2m19s
进入httpd容器
[root@master mysql]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
httpd NodePort 10.103.239.58 <none> 80:32222/TCP 22m
访问站点
http://192.168.174.10:32222/
修改副本数量
kubectl scale deployment httpd --replicas=2