k8s中创建包含多个path(location)的ingress的方法

1、概述

本文当介绍,如果创建一个ingress,里面包括多个path,也就是实际情况的location,要怎么进行配置。

接下去的这个文档,就会介绍基本的配置的过程

2、配置过程

2.1、创建一个常规的ingress(包含一个path)

kubectl apply -f - <<EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-test-ingress
  namespace: test01
spec:
  ingressClassName: nginx
  rules:
  - host: nginx-test.k8s.com
    http:
      paths:
      - backend:
          service:
            name: nginx-test
            port:
              number: 80
        path: /
        pathType: Prefix
EOF

那么,这个ingress只包含一个path "/"

查看创建的ingress

[root@nccztsjb-node-23 ~]# kubectl get ingress -n test01
NAME                 CLASS   HOSTS                ADDRESS        PORTS   AGE
nginx-test-ingress   nginx   nginx-test.k8s.com   172.20.58.18   80      151m
[root@nccztsjb-node-23 ~]# 

查看生成的ingress-nginx-controller中的配置

server {
		server_name nginx-test.k8s.com ;
		
		listen 80  ;
		listen [::]:80  ;
		listen 443  ssl http2 ;
		listen [::]:443  ssl http2 ;
		
		set $proxy_upstream_name "-";
		
		ssl_certificate_by_lua_block {
			certificate.call()
		}
		
		location / {
			
			set $namespace      "test01";
			set $ingress_name   "nginx-test-ingress";
			set $service_name   "nginx-test";
			set $service_port   "80";
			set $location_path  "/";
			set $global_rate_limit_exceeding n;
			
			rewrite_by_lua_block {
				lua_ingress.rewrite({
					force_ssl_redirect = false,
					ssl_redirect = true,
					force_no_ssl_redirect = false,
					preserve_trailing_slash = false,
					use_port_in_redirects = false,
					global_throttle = { namespace = "", limit = 0, window_size = 0, key = { }, ignored_cidrs = { } },
				})
				balancer.rewrite()
				plugins.run()
			}

2.2、创建多个path的ingress

通过以下的命令创建包含多path的ingress

kubectl apply -f - <<EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-test-ingress
  namespace: test01
spec:
  ingressClassName: nginx
  rules:
  - host: nginx-test.k8s.com
    http:
      paths:
      - backend:
          service:
            name: nginx-test
            port:
              number: 80
        path: /
        pathType: Prefix
      - backend:
          service:
            name: nginx-test-02
            port:
              number: 80
        path: /test02
        pathType: Prefix
EOF

注意:就是在paths中,增加path的部分

      - backend:
          service:
            name: nginx-test-02
            port:
              number: 80
        path: /test02
        pathType: Prefix

生成的ingress-nginx-controller中的nginx的配置

	server {
		server_name nginx-test.k8s.com ;
		
		listen 80  ;
		listen [::]:80  ;
		listen 443  ssl http2 ;
		listen [::]:443  ssl http2 ;
		
		set $proxy_upstream_name "-";
		
		ssl_certificate_by_lua_block {
			certificate.call()
		}
		
		location /test02/ {
			
			set $namespace      "test01";
			set $ingress_name   "nginx-test-ingress";
			set $service_name   "nginx-test-02";
			set $service_port   "80";
			set $location_path  "/test02";
			set $global_rate_limit_exceeding n;
			
...中间省略...
			
		}
		
		location = /test02 {
			
			set $namespace      "test01";
			set $ingress_name   "nginx-test-ingress";
			set $service_name   "nginx-test-02";
			set $service_port   "80";
			set $location_path  "/test02";
			set $global_rate_limit_exceeding n;
...中间省略...
			
		}
		
		location / {
			
			set $namespace      "test01";
			set $ingress_name   "nginx-test-ingress";
			set $service_name   "nginx-test";
			set $service_port   "80";
			set $location_path  "/";
			set $global_rate_limit_exceeding n;
			
			rewrite_by_lua_block {
				lua_ingress.rewrite({
					force_ssl_redirect = false,
					ssl_redirect = true,
					force_no_ssl_redirect = false,
					preserve_trailing_slash = false,
					use_port_in_redirects = false,
					global_throttle = { namespace = "", limit = 0, window_size = 0, key = { }, ignored_cidrs = { } },
				})
				balancer.rewrite()
				plugins.run()
			}
			

发现生成了包括多个location的nginx的配置

OK,以上的过程,就介绍了,如何配置包含多个path的ingress.

3、注意

如果在path中配置了多个"/",会怎么样

我们通过以下的配置做个实验

kubectl apply -f - <<EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-test-ingress
  namespace: test01
spec:
  ingressClassName: nginx
  rules:
  - host: nginx-test.k8s.com
    http:
      paths:
      - backend:
          service:
            name: nginx-test
            port:
              number: 80
        path: /
        pathType: Prefix
      - backend:
          service:
            name: nginx-test-02
            port:
              number: 80
        path: /
        pathType: Prefix
EOF

查看ingress-nginx-controller中的nginx.conf配置

	## start server nginx-test.k8s.com
	server {
		server_name nginx-test.k8s.com ;
		
		listen 80  ;
		listen [::]:80  ;
		listen 443  ssl http2 ;
		listen [::]:443  ssl http2 ;
		
		set $proxy_upstream_name "-";
		
		ssl_certificate_by_lua_block {
			certificate.call()
		}
		
		location / {
			
			set $namespace      "test01";
			set $ingress_name   "nginx-test-ingress";
			set $service_name   "nginx-test";
			set $service_port   "80";
			set $location_path  "/";
			set $global_rate_limit_exceeding n;
			
			rewrite_by_lua_block {
				lua_ingress.rewrite({
					force_ssl_redirect = false,
					ssl_redirect = true,
					force_no_ssl_redirect = false,
					preserve_trailing_slash = false,
					use_port_in_redirects = false,
					global_throttle = { namespace = "", limit = 0, window_size = 0, key = { }, ignored_cidrs = { } },
				})
				balancer.rewrite()
				plugins.run()
			}

发现只有上面的“/”是生效的。

OK,我们在更改下顺序

kubectl apply -f - <<EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-test-ingress
  namespace: test01
spec:
  ingressClassName: nginx
  rules:
  - host: nginx-test.k8s.com
    http:
      paths:
      - backend:
          service:
            name: nginx-test-02
            port:
              number: 80
        path: /
        pathType: Prefix
      - backend:
          service:
            name: nginx-test
            port:
              number: 80
        path: /
        pathType: Prefix        
EOF

查看ingress-nginx-controller中的nginx.conf配置

		location / {
			
			set $namespace      "test01";
			set $ingress_name   "nginx-test-ingress";
			set $service_name   "nginx-test-02";
			set $service_port   "80";
			set $location_path  "/";
			set $global_rate_limit_exceeding n;
			
			rewrite_by_lua_block {
				lua_ingress.rewrite({
					force_ssl_redirect = false,
					ssl_redirect = true,
					force_no_ssl_redirect = false,
					preserve_trailing_slash = false,
					use_port_in_redirects = false,
					global_throttle = { namespace = "", limit = 0, window_size = 0, key = { }, ignored_cidrs = { } },
				})
				balancer.rewrite()
				plugins.run()
			}

发现是上面的那个是起作用的。

简单来说,如果有path,也就是location重复的,那么上面的那个会生效,后面的不生效。

posted @ 2022-04-18 14:48  Zhai_David  阅读(3867)  评论(0编辑  收藏  举报