K8s 中 port, targetPort, NodePort的区别

12 篇文章 0 订阅

看1个例子:

我们用下面命令去创建1个pod2, 里面运行的是1个nginx

kubectl create deployment pod2 --image=nginx

当这个POD被创建后, 其实并不能被外部访问, 因为端口映射并没有完成.

我们用下面这个命令去创建1个svc , 暴露端口

kubectl expose deployment pod2 --port=8300 --type=NodePort --target-port=80 --name=pod2-service  -o yaml> expose.yaml

生成了1个service, service name is pod2-service, nodePort是31382 (这里是随机生成)

gateman@k8smaster:~/yamls$ kubectl get svc
NAME           TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
kubernetes     ClusterIP   10.96.0.1     <none>        443/TCP          239d
pod2-service   NodePort    10.99.120.7   <none>        8300:31382/TCP   57m

这时pod2的nginx就可以被外部的机器访问了

我的k8s 集群中有5个node, 而上面创建的pod2 只部署在了其中1个node k8snode0 上

gateman@k8smaster:~/yamls$ kubectl get nodes -o wide
NAME         STATUS   ROLES                  AGE    VERSION    INTERNAL-IP   EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
amdeuc-vm1   Ready    <none>                 25h    v1.22.15   10.0.1.156    <none>        Ubuntu 22.04.1 LTS   5.15.0-56-generic   docker://20.10.21
amdeuc-vm2   Ready    <none>                 196d   v1.22.15   10.0.1.157    <none>        Ubuntu 22.04.1 LTS   5.15.0-56-generic   docker://20.10.21
amdeuc-vm3   Ready    <none>                 196d   v1.22.15   10.0.1.158    <none>        Ubuntu 22.04.1 LTS   5.15.0-75-generic   docker://20.10.21
k8smaster    Ready    control-plane,master   239d   v1.22.15   10.0.1.152    <none>        Ubuntu 22.04.1 LTS   5.15.0-56-generic   docker://20.10.18
k8snode0     Ready    <none>                 239d   v1.22.15   10.0.1.154    <none>        Ubuntu 22.04.1 LTS   5.15.0-53-generic   docker://20.10.20
gateman@k8smaster:~/yamls$ kubectl get pods -o wide
NAME                   READY   STATUS    RESTARTS   AGE    IP            NODE       NOMINATED NODE   READINESS GATES
pod2-8dbd8b8df-f6fhg   1/1     Running   0          100m   10.244.1.13   k8snode0   <none>           <none>
gateman@k8smaster:~/yamls$ 

但pod2是通过任何1个node 的ip 加上nodeport都能访问的

hostname
manjaro-x13
❯ curl 10.0.1.157:31382
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

回到这篇文章的主题

上面的例子中
NodePort 是 31382
port 是 8300
target port 是80

他们的关系?


NodePort

NodePort 是 K8S 为集群每个Node都会创建的端口, 用于被集群外部的service 访问, 由于每个Node 都有这个端口, 所以外部访问哪个Node的效果都是一样, 包括master node



port

port 是用于集群内部访问的端口,
下面是1个例子

gateman@amdeuc-vm1:~$ curl 10.99.120.7:8300
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

上面在集群内部的其中1个node 用 10.99.120.7:8300 一样可以访问pod2 的nginx服务

其中 10.99.120.7 并不是某个node的ip, 而是pod2-service 的ip
所以port就是用于k8s集群内部访问的



targetPort

target port就是真的是容器暴露的ip, ngnix镜像默认暴露的ip

他们的关系如下图
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nvd11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值