我们在介绍service的章节中,了解了ingress网络插件,使用了mandatory.yaml文件,在该文件中使用到了ConfigMap。
mandatory.yaml下载地址
https://github.com/kubernetes/ingress-nginx/blob/nginx-0.20.0/deploy/mandatory.yaml
在mandatory.yaml文件可以看到有nginx-configuration、tcp-services等名称的ConfigMap,并且发现最后在容器的参数中也使用了这些ConfigMap.
---
kind: ConfigMap
apiVersion: v1
metadata:
name: nginx-configuration
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
---
kind: ConfigMap
apiVersion: v1
metadata:
name: tcp-services
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
---
containers:
- name: nginx-ingress-controller
image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.26.1
args:
- /nginx-ingress-controller
- --configmap=$(POD_NAMESPACE)/nginx-configuration
- --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services
- --udp-services-configmap=$(POD_NAMESPACE)/udp-services
- --publish-service=$(POD_NAMESPACE)/ingress-nginx
- --annotations-prefix=nginx.ingress.kubernetes.io
下面我们来看一下configMap在ingress中是应用
(1)查看nginx ingress controller的pod部署
`查看pod详情信息`
[root@henry001 network]# kubectl get pods -n ingress-nginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-ingress-controller-7c66dcdd6c-hdxz5 1/1 Running 0 82m 192.168.0.6 henry002 <none> <none>
(2)发现运行在henry002节点上,说明nginx-ingress-controller是部署在henry002,此时我们到henry002机器上进行 查看
[root@henry002 ~]# docker ps | grep ingress
49f9a2023661 quay.io/kubernetes-ingress-controller/nginx-ingress-controller "/usr/bin/dumb-init …" About an hour ago Up About an hour k8s_nginx-ingress-controller_nginx-ingress-controller-7c66dcdd6c-hdxz5_ingress-nginx_32e2a965-2d3e-11ea-8e59-00163e2c61c9_0
e1718c5f10e5 k8s.gcr.io/pause:3.1 "/pause" About an hour ago Up About an hour k8s_POD_nginx-ingress-controller-7c66dcdd6c-hdxz5_ingress-nginx_32e2a965-2d3e-11ea-8e59-00163e2c61c9_0
(3)进入容器看看
[root@henry001 network]# kubectl exec -it nginx-ingress-controller-7c66dcdd6c-hdxz5 -n ingress-nginx bash
www-data@henry002:/etc/nginx$ ls
fastcgi_params geoip lua mime.types modsecurity modules nginx.conf opentracing.json owasp-modsecurity-crs template
(4)可以发现,就是一个nginx嘛,而且里面还有nginx.conf文件
/etc/nginx/nginx.conf
(5)不妨打开nginx.conf文件看看
假如已经配置过ingress,不妨尝试搜索一下"server_name tomcat.henry.com"
`查找`
www-data@henry002:/etc/nginx$ grep 'server_name tomcat.henry.com' nginx.conf -C 4
## end server _
## start server tomcat.henry.com
server {
server_name tomcat.henry.com ;
listen 80 ;
listen 443 ssl http2
(6)此时我们可能会发现,原来nginx ingress controller就是一个nginx,而所谓的ingress.yaml文件中配置的内容像tomcat.henry.com就会对应到nginx.conf中。
但是,不可能每次都进入到容器里面来修改,而且还需要手动重启nginx,这样会很麻烦,
于是我们会想有没有一个更好的方法,比如在K8s中有对应的方式,修改了什么就能修改nginx.conf文件。
(8)下面先查看一下nginx.conf文件中的内容,比如找个属性:proxy_connect_timeout 5s
`查看proxy_connect_timeout`
www-data@henry002:/etc/nginx$ grep 'proxy_connect_timeout' nginx.conf
proxy_connect_timeout 5s;
下面我们使用configmap的方式将proxy_connect_timeout的值改为10s
1>创建一个nginx-config.yaml文件
vim nginx-config.yaml
kind: ConfigMap
apiVersion: v1
metadata:
name: nginx-configuration
namespace: ingress-nginx
labels:
app: ingress-nginx
data:
proxy-connect-timeout: "10" #这里有个坑,到家一定要注意是'-'不是‘_’
- 对于参数的修改可以参考
官网:
https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/
2>查看 ingress-nginx命名空间下的configmap
[root@henry001 network]# kubectl get cm -n ingress-nginx
NAME DATA AGE
ingress-controller-leader-nginx 0 103m
tcp-services 0 103m
udp-services 0 103m
3>执行yaml文件并查看
`创建configmap`
[root@henry001 network]# kubectl apply -f nginx-config.yaml
configmap/nginx-configuration created
`查看configmap`
[root@henry001 network]# kubectl get cm -n ingress-nginx
NAME DATA AGE
ingress-controller-leader-nginx 0 103m
nginx-configuration 0 34s
tcp-services 0 103m
udp-services 0 103m
(9)再次查看nginx.conf文件
`查看proxy_connect_timeout`
www-data@henry002:/etc/nginx$ grep 'proxy_connect_timeout' nginx.conf
proxy_connect_timeout 10s;
(10)最后大家可以到nginx ingress controller的官网中,做详细了解
https://kubernetes.github.io/ingress-nginx/
https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/