本文介绍了Google Kubernetes Engine:如何为多个命名空间定义一个入口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GKE上,K8s Inress是Compute Engine提供的LoadBalancer,有一定的开销。例如,2个月我支付了16.97欧元。

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

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

如有任何帮助,我们将不胜感激。

非常感谢

推荐答案

好的,这是我一直在做的事情。我只有一个入口和一个到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

通过这种方式,您可以利用入口功能(如由Google或cert-manager管理的TLS/SSL终端),如果您愿意,还可以在nginx中进行复杂的配置。

这篇关于Google Kubernetes Engine:如何为多个命名空间定义一个入口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-01 13:46