Google Kubernetes Engine:如何为多个命名空间定义一个 Ingress?

aku*_*ma8 7 kubernetes google-kubernetes-engine kubernetes-ingress

在 GKE 上,K8s Ingress 是 Compute Engine 提供的负载均衡器,它们有一定的成本。例如 2 个月我支付了 16.97 欧元。

在我的集群中,我有 3 个命名空间(default,devprod),因此为了降低成本,我想避免产生 3 个 LoadBalancer。问题是如何配置当前的指向正确的命名空间?

GKE 要求入口的目标 Service 类型为NodePort,由于该限制,我被卡住了。

我想做类似的事情:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
 namespace: dev
 annotations: # activation certificat ssl
   kubernetes.io/ingress.global-static-ip-name: lb-ip-adress     
spec:
 hosts:
    - host: dev.domain.com 
      http:
        paths:
          - path: /*
            backend:
              serviceName: dev-service # This is the current case, 'dev-service' is a NodePort
              servicePort: http

    - host: domain.com 
      http:
        paths:
          - path: /*
            backend:
              serviceName: prod-service # This service lives in 'dev' namespace and is of type ExternalName. Its final purpose is to point to the real target service living in 'prod' namespace.
              servicePort: http

    - host: www.domain.com 
      http:
        paths:
          - path: /* 
            backend:
              serviceName: prod-service
              servicePort: http
Run Code Online (Sandbox Code Playgroud)

由于 GKE 需要服务,因此NodePort我坚持使用prod-service.

任何帮助将不胜感激。

非常感谢

Pra*_*sha 6

好吧,这就是我一直在做的事情。我只有一个入口和一个 nginx 后端服务。

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress
spec:
  backend:
    serviceName: nginx-svc
    servicePort: 80
Run Code Online (Sandbox Code Playgroud)

在您的 nginx 部署/控制器中,您可以使用典型的 nginx 配置定义配置映射。通过这种方式,您可以使用一个入口并定位多个命名空间。

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  default.conf: |
    server {
      listen 80;
      listen [::]:80;
      server_name  _;

      location /{
        add_header Content-Type text/plain;
        return 200 "OK.";
      }

      location /segmentation {
        proxy_pass http://myservice.mynamespace.svc.cluster.local:80;
      }
    }
Run Code Online (Sandbox Code Playgroud)

并且部署将通过 config-map 使用上述 nginx 配置

apiVersion: extensions/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      #podAntiAffinity will not let two nginx pods to run in a same node machine
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: app
                    operator: In
                    values:
                      - nginx
              topologyKey: kubernetes.io/hostname
      containers:
        - name: nginx
          image: nginx
          ports:
            - containerPort: 80
          volumeMounts:
            - name: nginx-configs
              mountPath: /etc/nginx/conf.d
          livenessProbe:
            httpGet:
              path: /
              port: 80
      # Load the configuration files for nginx
      volumes:
        - name: nginx-configs
          configMap:
            name: nginx-config

---

  apiVersion: v1
  kind: Service
  metadata:
    name: nginx-svc
  spec:
    selector:
      app: nginx
    type: NodePort
    ports:
      - protocol: "TCP"
        nodePort: 32111
        port: 80
Run Code Online (Sandbox Code Playgroud)

这样你就可以利用像 tls/ssl 终止这样的入口功能,比如由 google 或 cert-manager 管理,如果你愿意,你也可以在 nginx 中进行复杂的配置。

  • 谢谢,这是我采用的解决方案,但忘记回答我的问题,我还更新了我的 DNS 配置以指向所有域的相同 IP 地址,并让 nginx 转发到所请求的服务。我仍然认为 GKE 在 Ingress 管理方面比其他 Kubernetes 提供商更麻烦。 (2认同)