我的入口目前看起来像这样:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress
annotations:
kubernetes.io/ingress.class: nginx
cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
tls:
- hosts:
- example.org
- app.example.org
secretName: prod-tls
rules:
- host: example.org
http:
paths:
- path: /
backend:
serviceName: app-service
servicePort: 80
- host: app.example.org
http:
paths:
- path: /
backend:
serviceName: app-service
servicePort: 80
但现在我想重定向app.example.org
到example.org
。我该怎么做?
我发现这个例子使用ingress.kubernetes.io/configuration-snippet
但我不知道它适用于哪些领域?
我正在使用 Helm nginx-ingress-1.37.0
;应用程序版本 0.32.0。
答案1
对于 Kubernetes 1.19 及更高版本,此入口将把所有流量从 app.example.org 重定向到 example.org:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/server-snippet: |
return 301 $scheme://example.org$request_uri;
name: ingress-redirect
spec:
ingressClassName: nginx
tls:
- hosts:
- app.example.org
secretName: prod-tls
rules:
- host: app.example.org
答案2
Ingress 控制器版本 0.22.0 或更高版本使用nginx.ingress.kubernetes.io/rewrite-target
而不是ingress.kubernetes.io/configuration-snippet
。因此,对于 @mdaniel 答案来说,这将是新的:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress
annotations:
kubernetes.io/ingress.class: nginx
cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
tls:
- hosts:
- example.org
secretName: prod-tls
rules:
- host: example.org
http:
paths:
- path: /
backend:
serviceName: app-service
servicePort: 80
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-redirect
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: "https://example.org/$1"
spec:
tls:
- hosts:
- app.example.org
secretName: prod-tls
rules:
- host: app.example.org
http:
paths:
- path: /
backend:
serviceName: app-service
servicePort: 80
答案3
我发现这个例子很有用
ingress.kubernetes.io/configuration-snippet:
,但我不知道它适用于哪些领域?
当你有两个 Ingress 时,推理该配置会更容易一些,一个仅用于“托管”,configuration-snippet:
另一个用于执行实际工作:
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress
annotations:
kubernetes.io/ingress.class: nginx
cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
tls:
- hosts:
- example.org
secretName: prod-tls
rules:
- host: example.org
http:
paths:
- path: /
backend:
serviceName: app-service
servicePort: 80
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-redirect
annotations:
kubernetes.io/ingress.class: nginx
# this should be covered by the annotation on the other ingress
# since it will renew the same certificate
# cert-manager.io/cluster-issuer: letsencrypt-prod
ingress.kubernetes.io/configuration-snippet: |
rewrite ^/(.*)$ https://example.org/$1 permanent;
spec:
tls:
- hosts:
- app.example.org
secretName: prod-tls
rules:
- host: app.example.org
http:
paths:
- path: /
backend:
serviceName: app-service
servicePort: 80