【问题标题】:AWS Ingress Controller seems to be ignoring host name rulesAWS Ingress Controller 似乎忽略了主机名规则
【发布时间】:2022-01-26 18:02:53
【问题描述】:

我正在尝试将前端应用程序部署到 Amazon EKS。这个概念是,将有两个部署和两个服务(frontend-servicestg-frontend-service),一个用于生产,一个用于暂存。

除此之外,还有一个入口 ALB,它将根据主机名重定向流量。即如果主机名是www.project.io,流量将被路由到frontend-service,如果主机名是stg-project.io,流量将被路由到stg-frontend-service

这是我的部署和入口配置

stg-frontend-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: stg-frontend-deployment
  namespace: project
spec:
  replicas: 3
  selector:
    matchLabels:
      app: stg-frontend
  template:
    metadata:
      labels:
        app: stg-frontend
    spec:
      containers:
        - name: stg-frontend
          image: STAGING_IMAGE
          imagePullPolicy: Always
          ports:
            - name: web
              containerPort: 3000
      imagePullSecrets:
        - name: project-ecr

---

apiVersion: v1
kind: Service
metadata:
  name: stg-frontend-service
  namespace: project
spec:
  selector:
    app: stg-frontend
  ports:
  - protocol: TCP
    port: 80
    targetPort: 3000

stg-prod-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend-deployment
  namespace: project
spec:
  replicas: 3
  selector:
    matchLabels:
      app: frontend
  template:
    metadata:
      labels:
        app: frontend
    spec:
      containers:
        - name: frontend
          image: PRODUCTION_IMAGE
          imagePullPolicy: Always
          ports:
            - name: web
              containerPort: 3000
      imagePullSecrets:
        - name: project-ecr

---

apiVersion: v1
kind: Service
metadata:
  name: frontend-service
  namespace: project
spec:
  selector:
    app: frontend
  ports:
  - protocol: TCP
    port: 80
    targetPort: 3000

ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: project-ingress
  namespace: project
  annotations:
    kubernetes.io/ingress.class: alb
    alb.ingress.kubernetes.io/scheme: internet-facing
    alb.ingress.kubernetes.io/target-type: ip
spec:
  rules:
  - host: www.project.io
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: frontend-service
            port:
              number: 80
  - host: stg.project.io
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: stg-frontend-service
            port:
              number: 80

后来,我使用 Route 53 将流量从两个域路由到 ALB。

+----------------+------+---------+-----------------------------------------------------+
|  Record Name   | Type | Routing |               Value/Route traffic to                |
+----------------+------+---------+-----------------------------------------------------+
| www.project.io | A    | Simple  | dualstack.k8s-********.us-west-1.elb.amazonaws.com. |
| stg.project.io | A    | Simple  | dualstack.k8s-********.us-west-1.elb.amazonaws.com. |
+----------------+------+---------+-----------------------------------------------------+

问题是,ALB 入口总是将流量路由到第一个规范规则。在上面的配置中,第一条规则是主机www.project.io,它指的是frontend-service。每当我尝试访问www.project.iostg.project.io 时,它都会显示来自frontend-service 的回复。

后来,我切换了规则,先放了暂存规则,然后在两个域上都显示暂存服务。

我什至创建了一个像 junk.project.io 这样的虚拟记录并指向负载平衡器,它仍然有效并显示相同的响应,即使 junk.project.io 未包含在我的入口配置中。

在我看来,Ingress Config 完全忽略了主机名,总是从第一条规则返回响应。

【问题讨论】:

    标签: amazon-web-services kubernetes-ingress amazon-eks


    【解决方案1】:

    您的主机和 http 值在列表中定义为单独的项目,请尝试删除 http 节点前面的 -(连字符):

      - host: www.project.io
        http: # I removed the hyphen here
          paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: frontend-service
                port:
                  number: 80
      - host: stg.project.io
        http: # I removed the hyphen here
          paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: stg-frontend-service
                port:
                  number: 80
    

    【讨论】:

    • 成功了。我简直不敢相信我已经为这个连字符花了一整天 -_-
    • @HowardScott 也发生在我身上,兄弟!花了我很多时间挠头和破水(4 小时):D 我们应该为这些东西构建一个验证器哈哈
    猜你喜欢
    • 1970-01-01
    • 2016-10-31
    • 2020-01-27
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    相关资源
    最近更新 更多