kubernetes进阶之路(十一)~~~ConfigMap在Ingress Controller中实战

我们在介绍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/

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值