0%

k8s ingress-nginx的转发配置

转发方法

1、直接跳转 redirect

a. 域名转发 xxx.com 也指定转发到 www.xxx.com

1
2
3
4
nginx.ingress.kubernetes.io/configuration-snippet: |
if ($host != 'www.xxx.com') {
rewrite ^/(.*)$ https://www.xxx111.com/$1 permanent;
}

b. https://meta.xxx.com/ 转发到 https://meta.xxx.com/categories

1
2
nginx.ingress.kubernetes.io/configuration-snippet: |
rewrite ^/$ https://meta.xxx.com/categories permanent;

c. https://www.xxx.com/ 转发到 https://www.yyy.com/

1
2
3
4
5
nginx.ingress.kubernetes.io/configuration-snippet: |
#if ($host != 'www.aaa.com') {
# rewrite ^/(.*)$ https://www.bbb.com/$1 permanent;
#}
return 301 https://www.bbb.com$request_uri;

2、后台转发代理 类似proxy-pass

通过service的externalName类型进行转发

1
2
3
4
5
6
7
8
9
10
11
12
13
apiVersion: {{svcApiVersion}}
kind: Service
metadata:
name: {{svcName}}
namespace: {{namespace}}
spec:
externalName: "www.xxx.cn"
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
type: ExternalName

1、直接跳转 redirect

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
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name:ingress-nginx
namespace: default
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
kubernetes.io/ingress.class: nginx
cert-manager.io/cluster-issuer: letsencrypt-prod
nginx.ingress.kubernetes.io/ssl-redirect: 'true'
nginx.ingress.kubernetes.io/force-ssl-redirect: 'true'
nginx.ingress.kubernetes.io/configuration-snippet: |
if ($host != 'www.xxx.com') {
rewrite ^/(.*)$ https://www.xxx111.com/$1 permanent;
}
spec:
rules:
- host: www.xxx.com
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: nginx-svc
port:
number: 80
tls:
- hosts:
- www.xxx.com
secretName: www-xxx-com

2、后台转发代理 类似proxy-pass

** 通过externalName访问外部服务的方式进行转发 **

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
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-nginx
namespace: default
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
kubernetes.io/ingress.class: nginx
cert-manager.io/cluster-issuer: letsencrypt-prod
nginx.ingress.kubernetes.io/ssl-redirect: 'true'
nginx.ingress.kubernetes.io/force-ssl-redirect: 'true'
nginx.ingress.kubernetes.io/upstream-vhost: www.xxx.cn
nginx.ingress.kubernetes.io/backend-protocol: HTTP
spec:
rules:
- host: www.yyy.com
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: nginx-svc
port:
number: 80
tls:
- hosts:
- www.yyy.com
secretName: www-yyy-com

service配置

1
2
3
4
5
6
7
8
9
10
11
12
13
apiVersion: v1
kind: Service
metadata:
name: external—name
namespace: default
spec:
externalName: "www.xxx.cn"
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
type: ExternalName

3、后台转发代理

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

apiVersion: v1
kind: Endpoints
metadata:
name: datasensor-server
namespace: bigdata
subsets:
- addresses:
- ip: xxx.xxx.xx.xxx
ports:
- port: 80
protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
name: datasensor-server
namespace: bigdata
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
type: NodePort
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: datasensor-ingress
namespace: bigdata
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
cert-manager.io/cluster-issuer: 'letsencrypt-prod'
nginx.ingress.kubernetes.io/ssl-redirect: 'true'
nginx.ingress.kubernetes.io/force-ssl-redirect: 'true'
kubernetes.io/ingress.class: "nginx"

spec:
rules:
- host: "datasensor.xxxx.com"
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: datasensor-server
port:
number: 80
tls:
- hosts:
- "datasensor.xxxx.com"
secretName: "datasensor-xxxx-com"

参考文档

https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#proxy-redirect

nginx-ingress配置(注解)

NGINX Ingress Controller 配置代理外部服务

[k8s istio 配置域名转发到外部节点机器上]https://www.cnblogs.com/already/p/13125322.html

------------- 本文结束 感谢您的阅读-------------