如何在 Kubernetes 中使用负载均衡器服务公开多个端口 [英] How to expose multiple port using a load balancer services in Kubernetes
问题描述
我已经使用谷歌云平台(容器引擎)创建了一个集群,并使用以下 YAML 文件部署了一个 pod:
I have created a cluster using the google cloud platform (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 the 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?
推荐答案
您有两个选择:
- 您可以拥有多个服务,每个端口一个.正如您所指出的,每个服务最终都会有一个不同的 IP 地址
- 您可以拥有一个具有多个端口的服务.在这种特殊情况下,您必须为所有端口命名.
在您的情况下,服务变为:
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屋!