如何使用Kubernetes端口名称? [英] How to make use of Kubernetes port names?

查看:121
本文介绍了如何使用Kubernetes端口名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在kubernetes 部署中,我这样指定一个端口:

In a kubernetes deployment I specify a port like so:

 containers:
 - name: nginx
   image: nginx:latest
   ports:
    - name: nginx-port
      containerPort: 80
      protocol: TCP

现在在服务中,我可以像这样引用该端口(允许我仅在服务中指定外部端口):

Now in a service I can reference that port like so (allows me to only specify the external port in the service):

spec:
  type: ClusterIP
  ports:
  - name: nginx-port
    port: 80
    targetPort: nginx-port
    protocol: TCP

现在是一个问题,我可以使用以下语法nginx-service.default.svc.cluster.local:nginx-port引用服务和端口吗?您知道我可以使用这些特殊名称来引用服务,但是我发现自己像nginx-service.default.svc.cluster.local:80这样对端口号进行硬编码.

Now the question, can I reference service and port elsewhere using the following syntax nginx-service.default.svc.cluster.local:nginx-port? You know I can make reference to services using this special names, but I find myself hardcoding the port number like so nginx-service.default.svc.cluster.local:80.

推荐答案

kube-dns是一种DNS服务,它使用与所有常规DNS服务器相同的协议.从这个意义上说,DNS并非旨在解析端口名",而是解析域名(映射到IP地址).

kube-dns is a DNS service that uses the same protocol as all the regular DNS servers. In this sense, DNS was not designed to resolve "port names", but domain names (which map to an IP address).

几个人要做的是拥有一个反向代理,该代理可以将流量从一个端口传递到另一个端口(假设我们正在谈论HTTP/HTTPS流量)https://serverfault.com/questions/85078/how-to-forward-dns-alias-to-hostnameport 或使用iptables规则 https://www .digitalocean.com/community/tutorials/how-to-forward-ports-through-a-linux-gateway-with-iptables .但是,这些假设您首先会将流量转发到特定端口(例如80)

What several people do is to have a reverse proxy that would ProxyPass traffic from one port to another (provided that we are talking about HTTP/HTTPS traffic) https://serverfault.com/questions/85078/how-to-forward-dns-alias-to-hostnameport or use iptables rules https://www.digitalocean.com/community/tutorials/how-to-forward-ports-through-a-linux-gateway-with-iptables. However, these assume that you would forward traffic to a specific port in the first place (for example, 80)

正如您在评论中所写,使用环境变量将是适合您情况的选择.

As you wrote in the comment, using environment variables would be the option that fits your case.

这篇关于如何使用Kubernetes端口名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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