V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
css3
V2EX  ›  程序员

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

  •  
  •   css3 · 2021-02-07 14:35:49 +08:00 · 1515 次点击
    这是一个创建于 1183 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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-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
        2
    YouLMAO  
       2021-02-07 14:46:12 +08:00
    暴露一个端口
    你的需求是 nodePort 不是 target-port

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

    但你能先简述你的需求吗,这很重要
    css3
        7
    css3  
    OP
       2021-02-07 15:05:04 +08:00
    @YouLMAO 就是要在本地 pc 上访问 k8s 的服务,默认 svc 的 type 都是 ClusterIP,本地 pc 和 k8s 服务不通,所以要重新新建个 svc, type 设置为 NodePort,就可以访问了,也是可行的,现在的问题就是这个端口是随机的,服务很多,不固定的话,调试起来不方便,所以想着能不能指定端口,才有了这个帖子
    twinsdestiny
        8
    twinsdestiny  
       2021-02-07 15:06:53 +08:00
    编写 yaml,指定 nodeport 端口是可以的,指令就不知道了
    basefas
        9
    basefas  
       2021-02-07 15:07:30 +08:00
    建议通过 ingress 访问
    YouLMAO
        10
    YouLMAO  
       2021-02-07 15:21:57 +08:00 via Android
    @YouLMAO 命令行换成这个命令
    dawnh
        11
    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
        12
    dreamusername  
       2021-02-08 09:47:35 +08:00
    思路错了。
    Kubernetes 作为遵从云原生的系统,推荐的是配置即代码,所以你要做的是先写一份 nodeport service 的 yaml,然后 kubectl apply,你这种做法仅用于 port forward 。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   742 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 21:36 · PVG 05:36 · LAX 14:36 · JFK 17:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.