kubectl expose svc 时指定的 target-port 为什么无效,仍是随机分配一个出来的?

2021-02-07 14:35:49 +08:00
 css3

k8s 里边需要对某个服务暴露一个端口出来,使用以下命令

kubectl expose svc myservice --type=NodePort --name=myservice-svc --port=9000 --target-port=31012

service "myservice-svc" exposed

创建成功,但端口是随机的 32378 不是我指定的 31012, 这是为什么啊, 要用指定的应该用什么指令呢?:

kubectl get svc|grep myservice-svc

default     myservcie-svc                                                  NodePort    10.213.22.105          <none>        9000:32378/TCP 
1532 次点击
所在节点    程序员
12 条回复
anubu
2021-02-07 14:44:49 +08:00
RTFM. https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#expose

你应该理解错了--target-port 的意思,据我了解 expose 应该不支持指定 nodePort 端口,可以考试使用 port-forward 。
YouLMAO
2021-02-07 14:46:12 +08:00
暴露一个端口
你的需求是 nodePort 不是 target-port

ip+nodePort 就能访问你的服务了
YouLMAO
2021-02-07 14:47:34 +08:00
type: NodePort
ports:
- nodePort: 31234
- port: 8080
- targetPort: 9999
css3
2021-02-07 14:54:02 +08:00
@anubu 应该是我的理解错了,那就是 NodePort 只支持随机的
css3
2021-02-07 14:54:37 +08:00
@YouLMAO yml 里边可以指定 NodePort,为啥命令行却不行
YouLMAO
2021-02-07 14:57:50 +08:00
感觉你要 kubectl create nodeport

但你能先简述你的需求吗,这很重要
css3
2021-02-07 15:05:04 +08:00
@YouLMAO 就是要在本地 pc 上访问 k8s 的服务,默认 svc 的 type 都是 ClusterIP,本地 pc 和 k8s 服务不通,所以要重新新建个 svc, type 设置为 NodePort,就可以访问了,也是可行的,现在的问题就是这个端口是随机的,服务很多,不固定的话,调试起来不方便,所以想着能不能指定端口,才有了这个帖子
twinsdestiny
2021-02-07 15:06:53 +08:00
编写 yaml,指定 nodeport 端口是可以的,指令就不知道了
basefas
2021-02-07 15:07:30 +08:00
建议通过 ingress 访问
YouLMAO
2021-02-07 15:21:57 +08:00
@YouLMAO 命令行换成这个命令
dawnh
2021-02-07 15:39:00 +08:00
确实是理解有误,很多人分不清 port,targetPort 和 nodePort 。参考这篇文章的解释就很容易理解了。

https://matthewpalmer.net/kubernetes-app-developer/articles/kubernetes-ports-targetport-nodeport-service.html
nodePort
This setting makes the service visible outside the Kubernetes cluster by the node’s IP address and the port number declared in this property. The service also has to be of type NodePort (if this field isn’t specified, Kubernetes will allocate a node port automatically).

port
Expose the service on the specified port internally within the cluster. That is, the service becomes visible on this port, and will send requests made to this port to the pods selected by the service.

targetPort
This is the port on the pod that the request gets sent to. Your application needs to be listening for network requests on this port for the service to work.


Learn Kube Faster.
Get the #1 guide.
Get my book on Kubernetes for software developers, used by engineers at Google, Microsoft, and IBM.

简单来说 targetPort 是指 pod 里服务在 podIP 上监听的端口,必须指定; port 是 expose 出来在 svc 上提供对外服务的端口,也必须指定;而 nodePort 是指定了 nodePort 方式后在 node IP 上提供服务的端口,这个不指定则随机分配。
dreamusername
2021-02-08 09:47:35 +08:00
思路错了。
Kubernetes 作为遵从云原生的系统,推荐的是配置即代码,所以你要做的是先写一份 nodeport service 的 yaml,然后 kubectl apply,你这种做法仅用于 port forward 。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/752064

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX