kubectl expose svc 时指定的 target-port 为什么无效,仍是随机分配一个出来的? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
css3

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

  •  
  •   css3 2021 年 2 月 7 日 1999 次点击
    这是一个创建于 1900 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 
    12 条回复    2021-02-08 09:47:35 +08:00
    anubu
        1
    anubu  
       2021 年 2 月 7 日
    RTFM. https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#expose

    你应该理解错了--target-port 的意思,据我了解 expose 应该不支持指定 nodePort 端口,可以考试使用 port-forward 。
    YouLMAO
        2
    YouLMAO  
       2021 年 2 月 7 日
    暴露一个端口
    你的需求是 nodePort 不是 target-port

    ip+nodePort 就能访问你的服务了
    YouLMAO
        3
    YouLMAO  
       2021 年 2 月 7 日
    type: NodePort
    ports:
    - nodePort: 31234
    - port: 8080
    - targetPort: 9999
    css3
        4
    css3  
    OP
       2021 年 2 月 7 日
    @anubu 应该是我的理解错了,那就是 NodePort 只支持随机的
    css3
        5
    css3  
    OP
       2021 年 2 月 7 日
    @YouLMAO yml 里边可以指定 NodePort,为啥命令行却不行
    YouLMAO
        6
    YouLMAO  
       2021 年 2 月 7 日
    感觉你要 kubectl create nodeport

    但你能先简述你的需求吗,这很重要
    css3
        7
    css3  
    OP
       2021 年 2 月 7 日
    @YouLMAO 就是要在本地 pc 上访问 k8s 的服务,默认 svc 的 type 都是 ClusterIP,本地 pc 和 k8s 服务不通,所以要重新新建个 svc, type 设置为 NodePort,就可以访问了,也是可行的,现在的问题就是这个端口是随机的,服务很多,不固定的话,调试起来不方便,所以想着能不能指定端口,才有了这个帖子
    twinsdestiny
        8
    twinsdestiny  
       2021 年 2 月 7 日
    编写 yaml,指定 nodeport 端口是可以的,指令就不知道了
    basefas
        9
    basefas  
       2021 年 2 月 7 日
    建议通过 ingress 访问
    YouLMAO
        10
    YouLMAO  
       2021 年 2 月 7 日 via Android
    @YouLMAO 命令行换成这个命令
    dawnh
        11
    dawnh  
       2021 年 2 月 7 日
    确实是理解有误,很多人分不清 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
        12
    dreamusername  
       2021 年 2 月 8 日
    思路错了。
    Kubernetes 作为遵从云原生的系统,推荐的是配置即代码,所以你要做的是先写一份 nodeport service 的 yaml,然后 kubectl apply,你这种做法仅用于 port forward 。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3523 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 42ms UTC 11:45 PVG 19:45 LAX 04:45 JFK 07:45
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86