我在集群上运行了一些服务,ALB运行良好。但是我有一个CloudFront发行版,由于一些内部因素,我想使用集群作为入口点。因此,我试图添加一个入口,根据默认规则或命名主机将请求重定向到CloudFront分发版,两者都可以。
我尝试了两种不同的方法,但没有骰子:
创建外部名称和入口:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test-ingress
namespace: default
annotations:
kubernetes.io/ingress.class: alb
alb.ingress.kubernetes.io/scheme: "internet-facing"
alb.ingress.kubernetes.io/listen-ports: '[{"HTTP":80,"HTTPS": 443}]'
alb.ingress.kubernetes.io/certificate-arn: <my-cert-arn>
alb.ingress.kubernetes.io/actions.ssl-redirect: '{"Type": "redirect", "RedirectConfig": {
"Protocol": "HTTPS", "Port": "443", "StatusCode": "HTTP_301"}}'
alb.ingress.kubernetes.io/group.name: <my-group-name>
spec:
rules:
- host: test.my-host.net
HTTP:
paths:
- backend:
serviceName: test
servicePort: use-annotation
path: /
---
apiVersion: v1
kind: Service
metadata:
name: test
spec:
type: ExternalName
externalName: test.my-host.net
我还尝试在ALB ingress v2上创建带有注释的重定向入口,就像文档一样:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test-ingress
annotations:
kubernetes.io/ingress.class: alb
alb.ingress.kubernetes.io/scheme: "internet-facing"
alb.ingress.kubernetes.io/listen-ports: '[{"HTTP":80,"HTTPS": 443}]'
alb.ingress.kubernetes.io/certificate-arn: <my-cert-arn>
alb.ingress.kubernetes.io/actions.ssl-redirect: '{"Type": "redirect", "RedirectConfig": {
"Protocol": "HTTPS", "Port": "443", "StatusCode": "HTTP_301"}}'
alb.ingress.kubernetes.io/actions.redirect-to-eks: >
{"type":"redirect","redirectConfig":{"host":"my-
dist.cloudfront.net","path":"/","port":"443",
"protocol":"HTTPS","query":"k=v","statusCode":"HTTP_302"}}
alb.ingress.kubernetes.io/group.name: <my-group-name>
spec:
rules:
- host: <my-host-name>
HTTP:
paths:
- backend:
serviceName: ssl-redirect
servicePort: use-annotation
它不能使用ExternalName
服务类型的原因是因为它没有端点。ALB只能将目标设置为instance
或ip
(文档(,因此ExternalName
不是此处的选项。
您可以使用注释创建重定向,但这有点棘手首先,至少需要两个子网具有alb.ingress.kubernetes.io/subnets
注释的公共访问权限。子网可以自动发现,但我不知道它是否可以从分配给EKS集群的所有子网中选择公共子网,所以最好显式设置第二个,您需要使用alb.ingress.kubernetes.io/actions.<ACTION NAME>
注释(文档(,其中<ACTION NAME>
必须与入口规则中的serviceName
匹配第三个,您需要在重定向配置中指定Host
,否则它将是来自ingressspec
的host
。
下面是一个工作示例:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: test-alb
annotations:
kubernetes.io/ingress.class: alb
alb.ingress.kubernetes.io/scheme: "internet-facing"
alb.ingress.kubernetes.io/subnets: "public-subnet-id1,public-subnet-id2"
alb.ingress.kubernetes.io/listen-ports: '[{"HTTP":80,"HTTPS": 443}]'
alb.ingress.kubernetes.io/actions.redirect: '{"Type": "redirect", "RedirectConfig": { "Protocol": "HTTPS", "Host":"example.com", "port":"443", "StatusCode": "HTTP_301"}}'
spec:
rules:
- host: alb-test.host.name
http:
paths:
- backend:
serviceName: redirect
servicePort: use-annotation
除了重定向到<my-host-name>
之外,您的ssl-redirect
注释几乎是正确的。我建议您使用web控制台(更改链接中的区域(https://console.aws.amazon.com/ec2/v2/home?region=eu-central-1#LoadBalancers:sort=loadBalancerName查看生成的重定向规则并调试其他选项。
还有其他选项可以解决这个问题,例如nginx
的专用部署。或者你可以使用nginxingress控制器,根据我的经验,用它设置重定向要容易得多