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

在GKE上,K8s Ingress是Compute Engine提供的LoadBalancers,需要花费一些费用。 2个月的示例我支付16.97€。

在我的集群中,我有3个命名空间(defaultdevprod),因此为了降低成本,我希望避免产生3个LoadBalancers。问题是如何配置当前一个以指向正确的名称空间?

GKE要求入口的目标服务的类型为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

由于GKE要求提供服务NodePort,所以我坚持使用prod-service

任何帮助将不胜感激。

非常感谢

conqueror_nannan 回答:Google Kubernetes Engine:如何为多个名称空间定义一个Ingress?

您可以使用nginx-ingress控制器。它要灵活得多,并且仅对所有入口对象使用一个GCP负载平衡器。与cert-manager配对,您可以拥有实质上完全受管的免费SSL。

,

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

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress
spec:
  backend:
    serviceName: nginx-svc
    servicePort: 80

然后在您的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;
      }
    }

部署将通过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

通过这种方式,您可以利用tls / ssl终止之类的入口功能(例如由google或cert-manager管理的),也可以根据需要在nginx中使用复杂的配置。

,

使用 @Prata 方式但有一个更改,不要通过 prod 路由 nginx 流量,而是将其直接路由到从 loadbalancer 的服务,使用 nginx non-prod traffic 例如登台等

原因是谷歌 HTTPS 负载均衡器使用 container native LB (Link) 将流量直接路由到健康的 Pod,从而节省了跳数并且高效,为什么不将其用于生产。

,

http(s) 负载平衡的一种替代方案(可能也是最灵活的 GCP 原生)是使用 standalone NEGs。这需要您自己设置负载平衡器的所有部分(例如网址映射、健康检查等)

有多种好处,例如:

  1. 一个负载均衡器可以为多个命名空间提供服务
  2. 同一个负载均衡器也可以集成其他后端(如集群外的其他实例组)
  3. 您仍然可以使用容器原生负载平衡

这种方法的一个挑战是它不是“GKE native”,这意味着即使您删除了底层服务,路由仍然存在。因此,最好通过 terraform 等工具来维护这种方法,它允许您拥有 GCP 整体部署控制。

本文链接:https://www.f2er.com/3148535.html

大家都在问