如何使用 Kubernetes 端口名称? [英] How to make use of Kubernetes port names?
问题描述
在 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).
几个人所做的是拥有一个反向代理,可以将 ProxyPass 流量从一个端口传输到另一个端口(前提是我们谈论的是 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屋!