在完成前面kubernetes数据持久化的学习之后,本节我们开始尝试在k8s集群中部署nginx应用,对于nginx来说,需要持久化的数据主要有两块:
1、nginx配置文件和日志文件
2、网页文件
一、配置nginx网页文件持久化
1、ReplicationController配置文件如下
# cat nginx-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx-test
labels:
name: nginx-test
spec:
replicas: 2
selector:
name: nginx-test
template:
metadata:
labels:
name: nginx-test
spec:
containers:
- name: nginx-test
image: docker.io/nginx
volumeMounts:
- mountPath: /usr/share/nginx/html
name: nginx-data
ports:
- containerPort: 80
volumes:
- name: nginx-data
persistentVolumeClaim:
claimName: nfs-data
2、sevice配置文件如下
# cat nginx-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-test
labels:
name: nginx-test
spec:
type: NodePort
ports:
- port: 80
protocol: TCP
targetPort: 80
name: http
nodePort: 30088
selector:
name: nginx-test
3、通过配置文件创建ReplicationController和service
# kubectl create -f nginx-rc.yaml
# kubectl create -f nginx-svc.yaml
4、在nfs服务器上创建测试html文件
# echo 'Hello, Welcome to my website...' > /home/index.html
二、配置nginx配置文件持久化
我们需要在前面网页文件持久化的基础上把nginx配置文件和日志文件也进行持久化
1、创建PVC,其中nfs-data存储网页文件,nfs-nginx-etc存储配置文件和日志文件
2、修改nginx-rc.yaml配置文件,挂载两个pvc
# cat nginx-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx-test
labels:
name: nginx-test
spec:
replicas: 2
selector:
name: nginx-test
template:
metadata:
labels:
name: nginx-test
spec:
containers:
- name: nginx-test
image: docker.io/nginx
volumeMounts:
- mountPath: /usr/share/nginx/html
name: nginx-data
- mountPath: /etc/nginx
name: nginx-etc
ports:
- containerPort: 80
volumes:
- name: nginx-data
persistentVolumeClaim:
claimName: nfs-data
- name: nginx-etc
persistentVolumeClaim:
claimName: nfs-nginx-etc
3、重新创建rc验证两个PVC是否成功挂载
# kubectl delete -f nginx-rc.yaml
# kubectl create -f nginx-rc.yaml
# kubectl get pod
# kubectl describe pod nginx-test-kllfw
通过在对应的node节点上inspect docker容器验证
4、通过访问网页,发现nginx的server_token为开启状态
5、这里我们通过修改error_log和access_log到nfs路径下,同时将server_token修改为off