如何在Kubernetes中使用负载均衡器服务公开多个端口 [英] How to expose multiple port using a load balancer services in kubernetes

查看:549
本文介绍了如何在Kubernetes中使用负载均衡器服务公开多个端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Google云平台(容器引擎)创建了一个集群,并使用以下yaml文件部署了一个pod:

I have created a cluster using the google cloud plateform (container engine) and deployed a pod using the following yaml file:

apiVersion: extensions/v1beta1
kind: Deployment
metadata: 
  name: deployment-name
spec:
  replicas: 1
  template:
    metadata:
      name: pod-name
      labels: 
        app: app-label
    spec:
      containers: 
      - name: container-name
      image: gcr.io/project-id/image-name
      resources:
        requests:
          cpu: 1
      ports:
      - name: port80
        containerPort: 80
      - name: port443
        containerPort: 443
      - name: port6001
        containerPort: 6001

然后,我想创建一个服务,以使Pod能够在所有这些端口上进行侦听.我知道他跟随yaml文件可以创建一个在一个端口上侦听的服务:

Then I want to create a service that enables the pod to listen on all these ports. I know that he following yaml file works to create a service that listens on one port:

apiVersion: v1
kind: Service
metadata: 
  name: service-name
spec:    
  ports:
  - port: 80
    targetPort: 80
  selector: 
    app: app-label
  type: LoadBalancer

但是,当我希望Pod在这样的多个端口上侦听时,它不起作用:

However when I want the pod to listen on multiple ports like this, it doesn't work:

apiVersion: v1
kind: Service
metadata: 
  name: service-name
spec:    
  ports:
  - port: 80
    targetPort: 80
  - port: 443
    targetPort: 443
  - port: 6001
    targetPort: 6001
  selector: 
    app: app-label
  type: LoadBalancer

如何使我的Pod监听多个端口?

How can I make my pod listen to multiple ports?

推荐答案

您有两个选择:

  1. 您可以有多个服务,每个端口一个.正如您所指出的,每个服务将以不同的IP地址结尾
  2. 您可以使用具有多个端口的一项服务.在这种情况下,必须为所有端口命名.

在您的情况下,该服务将变为:

In your case, the service becomes:

apiVersion: v1
kind: Service
metadata:
  name: service-name
spec:
  ports:
  - name: http
    port: 80
    targetPort: 80
  - name: https
    port: 443
    targetPort: 443
  - name: something
    port: 6001
    targetPort: 6001
  selector:
    app: app-label
  type: LoadBalancer

这是必要的,以便可以消除端点的歧义.

This is necessary so that endpoints can be disambiguated.

这篇关于如何在Kubernetes中使用负载均衡器服务公开多个端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆