大网数据广告活动
服务器运维问答

Is it possible to expose 2 ports in Kubernetes pod?

· 52CE 我爱测

I have the following scenario - The position simulator will get its longitude and latitude data based on time from a gps tracker. So this position simulator has a connection to activemq, but to receive the raw data, is it possibl…

问题原文

原帖链接 · Nazia Jahan Trisha · CC BY-SA 4.0

I have the following scenario -

The position simulator will get its longitude and latitude data based on time from a gps tracker. So this position simulator has a connection to activemq, but to receive the raw data, is it possible to expose another port of this service to receive the live tracking data?

已采纳解答

原帖链接 · PjoterS · CC BY-SA 4.0

If you want to expose for example you deployment on two or more ports you can do it at least two ways.

  1. Proper configuration in YAML. This method is well described in Kubernetes documentation.

    For some Services, you need to expose more than one port. Kubernetes lets you configure multiple port definitions on a Service object. When using multiple ports for a Service, you must give all of your ports names so that these are unambiguous. For example:

Example:

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 9376
    - name: https
      protocol: TCP
      port: 443
      targetPort: 9377
    - name: <another-name>
      protocol: TCP
      port: XXX
      targetPort: XXX
  1. Using kubectl expose.

$ kubectl exposed <resource(deployment/Replicaset/etc)> <deployment-name> --port=XXX,XYZ,ABC --target-port=XXX

$ kubectl expose deployment nginx --port=80,8080,6000 --target-port=8080 service/nginx exposed

$ kubectl describe svc nginx
Name:              nginx
Namespace:         default
Labels:            run=nginx
Annotations:       <none>
Selector:          run=nginx
Type:              ClusterIP
IP:                10.0.74.75
Port:              port-1  80/TCP
TargetPort:        8080/TCP
Endpoints:         10.48.0.13:8080
Port:              port-2  8080/TCP
TargetPort:        8080/TCP
Endpoints:         10.48.0.13:8080
Port:              port-3  6000/TCP
TargetPort:        8080/TCP
Endpoints:         10.48.0.13:8080
Session Affinity:  None
Events:            <none>

As mentioned in Kubernetes docs

targetPort: is the port the container accepts traffic on, port: is the abstracted Service port, which can be any port other pods use to access the Service

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-04-21)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。