简体   繁体   English

Kubernetes、LoadBalancer 和 Ingress 用于一个域上的多个服务

[英]Kubernetes, LoadBalancer and Ingress for more than one service on one domain

I may misunderstand this concept:我可能会误解这个概念:

I have two services (Frontend and API).我有两个服务(前端和 API)。 Both should to use one subdomain (my.domain.com):两者都应该使用一个子域 (my.domain.com):

  • domain.com => for FRONTEND ('hello-world')
  • domain.com/api => for API

I have a K8s-cloud running at Hetzner.我有一个在 Hetzner 运行的 K8s-cloud。 Hetzner also provides loadbalancers. Hetzner 还提供负载均衡器。 The cluster was created with Rancher -UI集群是用Rancher -UI 创建的

This is how I configure the loadbalancer Service :这是我配置loadbalancer Service

---
apiVersion: "v1"
kind: Service
metadata:
  name: "nginx-hello-world"
  labels:
    app: "hello-world"
  annotations:
    load-balancer.hetzner.cloud/name: lb-development
    load-balancer.hetzner.cloud/hostname: my.domain.com
    load-balancer.hetzner.cloud/protocol: http
spec:
  type: LoadBalancer
  selector:
    app: "hello-world"
  ports:
    - name: "http"
      port: 80
      targetPort: 80

And this is my Deployment for the frontend hello-world :这是我的前端hello-world Deployment

apiVersion: "apps/v1"
kind: "Deployment"
metadata:
  name: "nginx-hello-world"
  labels:
    app: "hello-world"
spec:
  selector:
    matchLabels:
      app: "hello-world"
  strategy:
    type: "Recreate"
  template:
    metadata:
      labels:
        app: "hello-world"
    spec:
      containers:
        - image: "rancher/hello-world"
          name: "nginx-hello-world"
          imagePullPolicy: "Always"
          ports:
            - containerPort: 80
              name: "http"

The two manifests above are WORKING!上面的两个清单正在工作! I can reach out to 'hello-world' by visiting my.domain.com/我可以通过访问my.domain.com/ “hello-world”

Now I wanna hook my backend API into that setup.现在我想将我的后端 API 连接到该设置中。 I thought I can do this by using an ingress manifest:我想我可以通过使用ingress清单来做到这一点:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  labels:
    app: my-ingress
  name: my-ingress
spec:
  rules:
  - host: my.domain.com
    http:
      paths:
      - backend:
          serviceName: hello-api
          servicePort: 80
        path: "/app"
        pathType: Prefix
      - backend:
          serviceName: hello-world
          servicePort: 80
        path: "/"
        pathType: Prefix

How can I achieve to connect another workload with my.domain.com/api for the API and keep the use of the Hetzner loadbalancer?我怎样才能实现将另一个workloadmy.domain.com/api for the API连接my.domain.com/api for the API并继续使用 Hetzner 负载均衡器?

I thought I can do that, by simply changing the selector in the Loadbalancer Service to point to my-ingress , but nothing I tried, worked so far.我以为我可以做到这一点,只需将Loadbalancer Service的选择器更改为指向my-ingress ,但我没有尝试过,到目前为止都有效。

┌─────────────────┐  ┌──────────────────┐   ┌──────────────────┐
│                 │  │  Ingress         │   │                  │
│ Loadbalancer Svc├─►│my.domain.com/    ├──►│ Frontend deploym.│
│                 │  │my.domain.com/api ├──►│ backend API depl │
└─────────────────┘  └──────────────────┘   └──────────────────┘

PS: it may be interesting: When I deployed Rancher, it has also created a nginx-ingress-controller PS:可能很有趣:我部署Rancher的时候,还创建了一个nginx-ingress-controller

Thank you in advance先感谢您

You need to create a service of type: LoadBalancer and use the the ingress controller app's label in the selector as explained in the link below.您需要创建一个类型为:LoadBalancer 的服务,并在选择器中使用入口控制器应用程序的标签,如下面的链接中所述。 Although, the link talks about using Traefik, the process should be same for the Nginx ingress controller.尽管该链接谈到了使用 Traefik,但 Nginx 入口控制器的过程应该是相同的。

https://community.hetzner.com/tutorials/howto-k8s-traefik-certmanager https://community.hetzner.com/tutorials/howto-k8s-traefik-certmanager

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM