一、ingress最常见的用法(基于域名的虚拟主机)
- 部署v2测试应用
和nginx一样,nginx-ingress也同样支持基于域名的虚拟主机,所谓基于域名的虚拟主机,就是配置不通的域名访问不通的服务。
1 | echo ' |
访问v2应用
部署v3测试应用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67echo '
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: todo-v3-ingress
namespace: zlx
annotations:
kubernetes.io/ingress.class: "public-nginx"
spec:
rules:
- host: v3.chulinx.com
http:
paths:
- path: /
backend:
serviceName: todo-v3-svc
servicePort: 80
---
kind: Service
apiVersion: v1
metadata:
name: todo-v3-svc
namespace: zlx
spec:
selector:
name: todo-v3
type: ClusterIP
ports:
- name: todo-v3-http
port: 80
targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-v3
namespace: zlx
labels:
name: todo-v3
spec:
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate
selector:
matchLabels:
name: todo-v3
template:
metadata:
labels:
name: todo-v3
spec:
containers:
- image: registry.chulinx.cn/docker/public-images/todo:v0.3
name: todo-v3
resources:
requests:
cpu: "20m"
memory: "55M"
env:
- name: ENVVARNAME
value: ENVVARVALUE
ports:
- containerPort: 80
name: todo-v3
restartPolicy: Always'|kubectl create -f -查看部署的应用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18$ [K8sDev] kubectl get all -n zlx
NAME READY STATUS RESTARTS AGE
pod/todo-v2-8599bb87fc-hm8mk 1/1 Running 0 8m22s
pod/todo-v3-7b65c96879-9tngw 1/1 Running 0 11s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/todo-v2-svc ClusterIP 10.96.75.231 <none> 80/TCP 10m
service/todo-v3-svc ClusterIP 10.97.76.114 <none> 80/TCP 11s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/todo-v2 1/1 1 1 10m
deployment.apps/todo-v3 1/1 1 1 11s
NAME DESIRED CURRENT READY AGE
replicaset.apps/todo-v2-69f5b79459 0 0 0 10m
replicaset.apps/todo-v2-6bf5fd9cf4 0 0 0 9m19s
replicaset.apps/todo-v2-8599bb87fc 1 1 1 8m22s
replicaset.apps/todo-v3-7b65c96879 1 1 1 11s访问测试v3应用
二、基于path的虚拟主机(同一域名指定不同的path访问不同的应用)
更改v2/v3的ingress
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: todo-v2-ingress
namespace: zlx
annotations:
kubernetes.io/ingress.class: "public-nginx"
nginx.ingress.kubernetes.io/rewrite-target: /$2
nginx.ingress.kubernetes.io/configuration-snippet: |
rewrite ^/static/(.*)$ http://$host:8080/v2/static/$1 redirect;
spec:
rules:
- host: www.chulinx.com
http:
paths:
- path: /v2(/|$)(.*)
backend:
serviceName: todo-v2-svc
servicePort: 80
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: todo-v3-ingress
namespace: zlx
annotations:
kubernetes.io/ingress.class: "public-nginx"
nginx.ingress.kubernetes.io/rewrite-target: /$2
nginx.ingress.kubernetes.io/configuration-snippet: |
rewrite ^/static/(.*)$ http://$host:8080/v3/static/$1 redirect;
spec:
rules:
- host: www.chulinx.com
http:
paths:
- path: /v3(/|$)(.*)
backend:
serviceName: todo-v3-svc
servicePort: 80访问测试
发现v2可以正常访问,但是v3却不行,查看chrome调试信息,发现js静态资源全部redirect到v2,这是为什么呢?原因是因为我们在配置v3的redirect之前已经配置了v2的redirect,所以遇到以^/static/开头的所有uri都被redirect到v2/static/$1了,这也证实了nginx-ingress不能通过/path路由到不同的应用
三、 通过ip访问v2应用
- 更改v2的ingress
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: todo-v2-ingress
namespace: zlx
annotations:
kubernetes.io/ingress.class: "public-nginx"
spec:
rules:
- http:
paths:
- path: /
backend:
serviceName: todo-v2-svc
servicePort: 80
发现是可以访问的,如法炮制v3ingress,访问测试依旧是v2,但是删除v2,再次访问就变成v3,这个过程证明,如果不指定hostname,那应同一个ingress通过ip路由应用只能路由一个应用。
- 本文作者: ChuLinx
- 本文链接: http://yoursite.com/2020/05/14/Kubernetes Nginx-Ingress 常见配置用法介绍/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!