使用多个Ingress-nginx控制器时,Ingress资源从错误的Ingress Controller获取地址

时间:2019-10-21 09:44:56

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

我们在AWS(EKS)中拥有一个Kubernetes集群。在我们的设置中,我们需要有两个Ingress-nginx控制器,以便我们可以实施不同的安全策略。为此,我利用

  

kubernetes.io/ingress.class和-ingress-class

根据here的建议,我从ingress-nginx存储库中创建了一个默认的'mandatory.yaml'标准Ingress Controller。

为创建第二个入口控制器,我从'mandatory.yaml'中自定义了一点入口部署。我基本上添加了标签:

  

“ env:内部”

到部署定义。

我还相应地创建了另一个服务,指定“ env:internal”标签,以便将该新服务与新的入口控制器绑定。请看看我的Yaml定义:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-ingress-controller-internal
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    env: internal
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: ingress-nginx
      app.kubernetes.io/part-of: ingress-nginx
      env: internal
  template:
    metadata:
      labels:
        app.kubernetes.io/name: ingress-nginx
        app.kubernetes.io/part-of: ingress-nginx
        env: internal
      annotations:
        prometheus.io/port: "10254"
        prometheus.io/scrape: "true"
    spec:
      # wait up to five minutes for the drain of connections
      terminationGracePeriodSeconds: 300
      serviceAccountName: nginx-ingress-serviceaccount
      nodeSelector:
        kubernetes.io/os: linux
      containers:
        - name: nginx-ingress-controller-internal
          image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.26.1
          args:
            - /nginx-ingress-controller
            - --configmap=$(POD_NAMESPACE)/nginx-configuration
            - --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services
            - --udp-services-configmap=$(POD_NAMESPACE)/udp-services
            - --publish-service=$(POD_NAMESPACE)/ingress-nginx
            - --annotations-prefix=nginx.ingress.kubernetes.io
            - --ingress-class=nginx-internal
          securityContext:
            allowPrivilegeEscalation: true
            capabilities:
              drop:
                - ALL
              add:
                - NET_BIND_SERVICE
            # www-data -> 33
            runAsUser: 33
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
          ports:
            - name: http
              containerPort: 80
            - name: https
              containerPort: 443
          livenessProbe:
            failureThreshold: 3
            httpGet:
              path: /healthz
              port: 10254
              scheme: HTTP
            initialDelaySeconds: 10
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 10
          readinessProbe:
            failureThreshold: 3
            httpGet:
              path: /healthz
              port: 10254
              scheme: HTTP
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 10
          lifecycle:
            preStop:
              exec:
                command:
                  - /wait-shutdown

---

kind: Service
apiVersion: v1
metadata:
  name: ingress-nginx-internal
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    env: internal
spec:
  externalTrafficPolicy: Local
  type: LoadBalancer
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    env: internal
  ports:
    - name: http
      port: 80
      targetPort: http
    - name: https
      port: 443
      targetPort: https

应用此定义后,将创建我的Ingress控制器以及新的LoadBalancer服务:

$ kubectl get deployments -n ingress-nginx
NAME                                READY   UP-TO-DATE   AVAILABLE   AGE
nginx-ingress-controller            1/1     1            1           10d
nginx-ingress-controller-internal   1/1     1            1           95m

$ kubectl get service -n ingress-nginx
NAME                     TYPE           CLUSTER-IP       EXTERNAL-IP                                          PORT(S)                      AGE
ingress-nginx            LoadBalancer   172.20.6.67      xxxx.elb.amazonaws.com   80:30857/TCP,443:31863/TCP   10d
ingress-nginx-internal   LoadBalancer   172.20.115.244   yyyyy.elb.amazonaws.com   80:30036/TCP,443:30495/TCP   97m

到目前为止,一切都很好。

但是,当我创建两个入口资源时,这些资源中的每一个都绑定到不同的入口控制器(注意'kubernetes.io/ingress.class:'):

外部入口资源:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: accounting-ingress
  annotations:  
    kubernetes.io/ingress.class: nginx
 spec: ...

内部入口资源:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: internal-ingress
  annotations:  
    kubernetes.io/ingress.class: nginx-internal    
spec: ...

我看到它们都包含相同的地址,即第一个入口控制器的地址:

$ kg ingress
NAME                 HOSTS                  ADDRESS                                                                   PORTS     AGE
external-ingress   bbb.aaaa.com           xxxx.elb.amazonaws.com   80, 443   10d
internal-ingress   ccc.aaaa.com           xxxx.elb.amazonaws.com   80        88m

我希望绑定到“ ingress-class = nginx-internal”的入口将包含以下地址:“ yyyyy.elb.amazonaws.com”。虽然一切似乎都工作正常,但这让我很烦,我觉得有些地方不对。

我应该在哪里开始对其进行故障排除,以了解幕后发生的事情?

#### --- UPDATE --- ####

除了上述内容外,我还在manadatory.yaml中添加了行“ ” ingress-controller-leader-nginx-internal” ”,如下所示。我是根据我在essential.yaml文件中找到的一条评论来做到这一点的:

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: Role
metadata:
  name: nginx-ingress-role
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
rules:
  - apiGroups:
      - ""
    resources:
      - configmaps
      - pods
      - secrets
      - namespaces
    verbs:
      - get
  - apiGroups:
      - ""
    resources:
      - configmaps
    resourceNames:
      # Defaults to "<election-id>-<ingress-class>"
      # Here: "<ingress-controller-leader>-<nginx>"
      # This has to be adapted if you change either parameter
      # when launching the nginx-ingress-controller.
      - "ingress-controller-leader-nginx"
      - "ingress-controller-leader-nginx-internal"

不幸的是,nginx文档只提到了用于定义新控制器的'kubernetes.io/ingress.class和-ingress-class'。我有可能弄乱了一些小细节。

1 个答案:

答案 0 :(得分:1)

尝试更改此行:

- --configmap=$(POD_NAMESPACE)/nginx-configuration

在您的代码中,应该是这样的:

- --configmap=$(POD_NAMESPACE)/internal-nginx-configuration

这样,您将为每个nginx-controller具有不同的配置,否则,您将具有相同的配置,这似乎可以正常工作,但是在更新时会出现一些错误……(已经在那里。 )