在kubernetes集群中运行nginx
时间:2018-05-09 11:56 来源:linux.it.net.cn 作者:IT
在完成前面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
6、重启nginx使配置文件
通过将rc的副本数重新设置为0后,再将副本数设置为2,达到重启nginx的效果。
当然这里也可以通过kubectl exec的方式进行。
7、验证
验证server_token已经成功关闭
验证在nfs server上能看到nginx的访问日志,证明配置文件修改成功。
在生产环境中,nginx服务的发布需要考虑使用ingress,nginx配置文件应当首先考虑使用configmap来实现。
(责任编辑:IT)
在完成前面kubernetes数据持久化的学习之后,本节我们开始尝试在k8s集群中部署nginx应用,对于nginx来说,需要持久化的数据主要有两块:
一、配置nginx网页文件持久化 # 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
# echo 'Hello, Welcome to my website...' > /home/index.html
# 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
(责任编辑:IT) |