问题描述
在Ingress NGINX,如何通过在Header中添加字段,从而实现基于Header的的分发。
问题分析
Ingress NGINX提供了 nginx.ingress.kubernetes.io/canary-by-header 和 nginx.ingress.kubernetes.io/canary-by-header-value 用于自定义于Header,实现基于Header的分发。
问题解决
1.部署两个 echoserver 用于测试
$ kubectl get pod -n default
NAME READY STATUS RESTARTS AGE
canary-5978bccbf6-x44t8 1/1 Running 0 8s
production-8ffb86cb4-gvzc8 1/1 Running 0 5m24s
$ kubectl get svc -n default
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
canary ClusterIP 10.99.3.0 <none> 80/TCP 17s
production ClusterIP 10.99.224.239 <none> 80/TCP 5m33s
# 访问production svc
$ root@k8s-master01:/usr/local/src/canary# curl 10.99.224.239
Hostname: production-8ffb86cb4-gvzc8
Pod Information:
node name: 192.168.0.75
pod name: production-8ffb86cb4-gvzc8
pod namespace: default
pod IP: 10.233.87.35
Server values:
server_version=nginx: 1.13.3 - lua: 10008
Request Information:
client_address=10.233.15.89
method=GET
real path=/
query=
request_version=1.1
request_scheme=http
request_uri=http://10.99.224.239:8080/
Request Headers:
accept=*/*
host=10.99.224.239
user-agent=curl/7.68.0
Request Body:
-no body in request-
# 访问canary svc
$ curl 10.99.3.0
Hostname: canary-5978bccbf6-x44t8
Pod Information:
node name: 192.168.0.75
pod name: canary-5978bccbf6-x44t8
pod namespace: default
pod IP: 10.233.90.160
Server values:
server_version=nginx: 1.13.3 - lua: 10008
Request Information:
client_address=10.233.15.89
method=GET
real path=/
query=
request_version=1.1
request_scheme=http
request_uri=http://10.99.3.0:8080/
Request Headers:
accept=*/*
host=10.99.3.0
user-agent=curl/7.68.0
Request Body:
-no body in request-
2.创建ingress
$ cat production-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: production
spec:
ingressClassName: nginx
rules:
- host: "prod.xxx.com"
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: production
port:
number: 80
$ cat canary-header-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: canary-weight
annotations:
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-by-header: "canary"
nginx.ingress.kubernetes.io/canary-by-header-value: "ingress"
spec:
ingressClassName: nginx
rules:
- host: "prod.xxx.com"
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: canary
port:
number: 80
$ kubectl apply -f production-ingress.yaml
$ kubectl apply -f canary-header-ingress.yaml
$ kubectl get ingress -n default
NAME CLASS HOSTS ADDRESS PORTS AGE
canary-weight nginx prod.xxx.com 10.99.115.162 80 75s
production nginx prod.xxx.com 10.99.115.162 80 3m8s
3.验证
$ curl -s -H "canary:ingress" prod.xxx.com:32080 | grep Hostname
Hostname: canary-5978bccbf6-x44t8
$ curl -s -H "canary:test" prod.xxx.com:32080 | grep Hostname
Hostname: production-8ffb86cb4-gvzc8
$ curl -s -H "canary:always" prod.xxx.com:32080 | grep Hostname
Hostname: production-8ffb86cb4-gvzc8
$ curl -s -H "canary:never" prod.xxx.com:32080 | grep Hostname
Hostname: production-8ffb86cb4-gvzc8
参考链接
[1] https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#canary 如果您有其他问题,欢迎您联系火山引擎技术支持服务