kubernetes 如何在AWS ALB入口控制器中重写目标URL?

tvmytwxo  于 2023-01-25  发布在  Kubernetes
关注(0)|答案(3)|浏览(229)

在一个经典的Kubernetes Nginx入口中,我知道通过应用注解nginx.ingress.kubernetes.io/rewrite-target: /$1,可以基于特定的正则表达式重写目标url
但是这个注解在AWS ALB入口中不起作用,有没有人知道是否有可能用这种入口做重写工作?

yxyvkwin

yxyvkwin2#

添加操作注解:
alb.ingress.kubernetes.io/actions.redirect-home: {“类型”:“重定向”,“重定向配置”:{“主机”:“abc.example.com“,“路径”:“/我的上下文/其他路径”,“端口”:“443”,“协议”:“HTTPS”,“查询”:“#{查询}",“状态代码”:“HTTP_301”}}'
向操作添加路由:

- backend:
          service:
            name: redirect-home
            port:
              name: use-annotation
        path: /some-path
        pathType: ImplementationSpecific

这会将/some-path重定向到abc.example.com/mycontext/other-path
谢谢

ojsjcaue

ojsjcaue3#

曾经我的需求是重定向从coffee.xyz.comcoffee.abc.com的流量,所以为此,我使用alb重定向注解。

alb.ingress.kubernetes.io/actions.coffee-redirection: '{"Type":"redirect","RedirectConfig":{"Host":"coffee.xyz.com","Port":"443","Protocol":"HTTPS","Query":"#{query}","StatusCode":"HTTP_301"}}'

以下是同一问题的完整代码块。

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    alb.ingress.kubernetes.io/actions.ssl-redirect: '{"Type": "redirect", "RedirectConfig":{ "Protocol": "HTTPS", "Port": "443", "StatusCode": "HTTP_301"}}'
    alb.ingress.kubernetes.io/certificate-arn: arn:aws:acm:**********************************************
    alb.ingress.kubernetes.io/listen-ports: '[{"HTTP":80},{"HTTPS":443}]'
    alb.ingress.kubernetes.io/scheme: internet-facing
    alb.ingress.kubernetes.io/subnets: subnet-*******, subnet-********, subnet-**********,subnet-********
    service.beta.kubernetes.io/aws-load-balancer-name: coffee-alb-staging
    kubernetes.io/ingress.class: alb
    alb.ingress.kubernetes.io/ssl-redirect: '443'
    alb.ingress.kubernetes.io/group.name: coffee-staging
    alb.ingress.kubernetes.io/actions.coffee-redirection: '{"Type":"redirect","RedirectConfig":{"Host":"coffee.xyz.com","Port":"443","Protocol":"HTTPS","Query":"#{query}","StatusCode":"HTTP_301"}}'
  name: coffee-alb-staging
  namespace: NameSpace
spec:
rules:
- host: coffee.abc.com
http:
  paths:
    - path: /
      pathType: Prefix
      backend:
        service:
          name: coffee-fe
          port: 
            number: 80
    - path: /
      pathType: Exact
      backend:
        service:
          name: coffee-redirection
          port: 
            name: use-annotation

相关问题