【问题标题】:Ingress not binding to Load Balancer入口未绑定到负载均衡器
【发布时间】:2023-03-17 02:15:02
【问题描述】:

我将 Netlify 上的 A record 映射到 Digital Ocean 上的负载均衡器 IP 地址,它能够访问 nginx 服务器,但在尝试访问任何应用程序 API 时,我得到了 404。我注意到我的 Ingress 的状态并未显示它已绑定到负载均衡器。

有人知道我缺少什么来获得这个设置吗?

应用入口:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: d2d-server
spec:
  rules:
    - host: api.cloud.myhostname.com
      http:
        paths:
          - backend:
              service:
                name: d2d-server
                port:
                  number: 443
            path: /
            pathType: ImplementationSpecific

应用服务:

apiVersion: v1
kind: Service
metadata:
  name: d2d-server
spec:
  selector:
    app: d2d-server
  ports:
    - name: http-api
      protocol: TCP
      port: 443
      targetPort: 8080
  type: ClusterIP

入口控制器:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ingress-nginx-controller
  namespace: ingress-nginx
  uid: fc64d9f6-a935-49b2-9d7a-b862f660a4ea
  resourceVersion: '257931'
  generation: 1
  creationTimestamp: '2021-10-22T05:31:26Z'
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/version: 1.0.4
    helm.sh/chart: ingress-nginx-4.0.6
  annotations:
    deployment.kubernetes.io/revision: '1'
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/component: controller
      app.kubernetes.io/instance: ingress-nginx
      app.kubernetes.io/name: ingress-nginx
  template:
    metadata:
      creationTimestamp: null
      labels:
        app.kubernetes.io/component: controller
        app.kubernetes.io/instance: ingress-nginx
        app.kubernetes.io/name: ingress-nginx
    spec:
      volumes:
        - name: webhook-cert
          secret:
            secretName: ingress-nginx-admission
            defaultMode: 420
      containers:
        - name: controller
          image: >-
            k8s.gcr.io/ingress-nginx/controller:v1.0.4@sha256:545cff00370f28363dad31e3b59a94ba377854d3a11f18988f5f9e56841ef9ef
          args:
            - /nginx-ingress-controller
            - '--publish-service=$(POD_NAMESPACE)/ingress-nginx-controller'
            - '--election-id=ingress-controller-leader'
            - '--controller-class=k8s.io/ingress-nginx'
            - '--configmap=$(POD_NAMESPACE)/ingress-nginx-controller'
            - '--validating-webhook=:8443'
            - '--validating-webhook-certificate=/usr/local/certificates/cert'
            - '--validating-webhook-key=/usr/local/certificates/key'
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
            - name: https
              containerPort: 443
              protocol: TCP
            - name: webhook
              containerPort: 8443
              protocol: TCP
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.name
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.namespace
            - name: LD_PRELOAD
              value: /usr/local/lib/libmimalloc.so
          resources:
            requests:
              cpu: 100m
              memory: 90Mi
          volumeMounts:
            - name: webhook-cert
              readOnly: true
              mountPath: /usr/local/certificates/
          livenessProbe:
            httpGet:
              path: /healthz
              port: 10254
              scheme: HTTP
            initialDelaySeconds: 10
            timeoutSeconds: 1
            periodSeconds: 10
            successThreshold: 1
            failureThreshold: 5
          readinessProbe:
            httpGet:
              path: /healthz
              port: 10254
              scheme: HTTP
            initialDelaySeconds: 10
            timeoutSeconds: 1
            periodSeconds: 10
            successThreshold: 1
            failureThreshold: 3
          lifecycle:
            preStop:
              exec:
                command:
                  - /wait-shutdown
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent
          securityContext:
            capabilities:
              add:
                - NET_BIND_SERVICE
              drop:
                - ALL
            runAsUser: 101
            allowPrivilegeEscalation: true
      restartPolicy: Always
      terminationGracePeriodSeconds: 300
      dnsPolicy: ClusterFirst
      nodeSelector:
        kubernetes.io/os: linux
      serviceAccountName: ingress-nginx
      serviceAccount: ingress-nginx
      securityContext: {}
      schedulerName: default-scheduler
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25%
      maxSurge: 25%
  revisionHistoryLimit: 10
  progressDeadlineSeconds: 600

负载均衡器:

apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx-controller
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/version: 1.0.4
    helm.sh/chart: ingress-nginx-4.0.6
  annotations:
    kubernetes.digitalocean.com/load-balancer-id: <LB_ID>
    service.beta.kubernetes.io/do-loadbalancer-enable-proxy-protocol: 'true'
    service.beta.kubernetes.io/do-loadbalancer-name: ingress-nginx
    service.beta.kubernetes.io/do-loadbalancer-protocol: https
status:
  loadBalancer:
    ingress:
      - ip: <IP_HIDDEN>
spec:
  ports:
    - name: http
      protocol: TCP
      appProtocol: http
      port: 80
      targetPort: http
      nodePort: 31661
    - name: https
      protocol: TCP
      appProtocol: https
      port: 443
      targetPort: https
      nodePort: 32761
  selector:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
  clusterIP: <IP_HIDDEN>
  clusterIPs:
    - <IP_HIDDEN>
  type: LoadBalancer
  sessionAffinity: None
  externalTrafficPolicy: Local
  healthCheckNodePort: 30477
  ipFamilies:
    - IPv4
  ipFamilyPolicy: SingleStack

【问题讨论】:

    标签: nginx kubernetes digital-ocean


    【解决方案1】:

    kind: ingress 是关于管理从 入口控制器 到集群服务的流量的代理规则。但要实现这一点,外部流量需要到达 Ingress Controller。 https://kubernetes.io/docs/concepts/services-networking/ingress/#what-is-ingress

    假设“客户端”是我们的负载均衡器

    所以我假设您想要做的是将您的 LoadBalancer 指向 Ingress Controler,然后根据您的 Ingress 规则,它将流量路由给您(在这种情况下) d2d 服务。

    要将 LB 指向 pod,您需要创建一个带有 spec.type: Loadbalancer 字段的 Service 资源。我修改了一个来自数字海洋的示例,它应该符合您的需求。注意 Service 的注解可以修改 Loadbalancer 参数,更多信息可以在这里找到:https://github.com/digitalocean/digitalocean-cloud-controller-manager/blob/master/docs/controllers/services/annotations.md

    apiVersion: v1
    kind: Service
    metadata:
      name: ingress-nginx-controller
      namespace: ingress-nginx
      annotations:
        # @Edit set it to http since no certyficate is provided
        service.beta.kubernetes.io/do-loadbalancer-protocol: "http"
        service.beta.kubernetes.io/do-loadbalancer-name: "<YOUR_LB_NAME>"
    spec:
      type: LoadBalancer
      selector:
        app.kubernetes.io/component: controller
        app.kubernetes.io/instance: ingress-nginx
        app.kubernetes.io/managed-by: Helm
        app.kubernetes.io/name: ingress-nginx
        app.kubernetes.io/version: 1.0.4
        helm.sh/chart: ingress-nginx-4.0.6
      ports:
        - name: http
          protocol: TCP
          port: 80
          targetPort: 80
        - name: https
          protocol: TCP
          port: 443
          targetPort: 443
        - name: webhook
          protocol: TCP
          port: 8443
          targetPort: 8443
    

    【讨论】:

    • 你说得对,我希望流量通过 Ingress Controller。看起来我的 Digital Ocean 集群默认带有该 LoadBalancer 服务。我已经更新了我的问题以包含它。你有什么建议吗?
    • 您的 d2d pod 正在运行吗?是否有 type:ClisterIP 服务指向它?我从来没有使用过 DigitalOcean 很抱歉盲目地询问,但是,我们需要找出请求在哪里下降。你能检查一下 LB 健康检查是否通过了吗?您在尝试连接时使用的是 http 还是 https? spec.rules.host 字段是否与您的 DNS 规则匹配?您可以删除host 字段并再次检查。查找入口控制器日志以检查请求是否命中。使用 -f 参数获取实时日志并尝试通过 dns 以及 IP 地址(删除主机字段时的 IP)连接到您的 pod。
    • -d2d pod 正在运行。 -Service 具有 ClusterIP 类型。 -我在负载均衡器事件中看到这个错误:Error syncing load balancer: failed to ensure load balancer: failed to build load-balancer request: failed to build TLS part(s) of forwarding rule: must set certificate id or enable tls pass through
    • 好的,这是一个证书问题。我看到在你的入口服务上你只暴露了 443 端口。您可以在 d2d 服务和入口服务中额外添加端口 80。还可以尝试在 ingres-controler 服务上设置注释以强制使用 http 流量 service.beta.kubernetes.io/do-loadbalancer-protocol: "http"。查看此网站了解更多选项:github.com/digitalocean/digitalocean-cloud-controller-manager/….
    • 我按照说明操作,但仍然收到 404。我没有在 pod 日志中看到发送到入口控制器的请求,但我确实看到了 "successfully validated configuration, accepting" ingress="d2d-server/prod"
    【解决方案2】:

    我只需要将nginx 的字段ingressClassName 添加到入口规范。

    【讨论】:

      猜你喜欢
      • 2017-12-18
      • 1970-01-01
      • 2021-12-07
      • 2017-03-01
      • 2021-03-18
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多