看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
他们的关系如下图
 



![使用 Sa-Token 实现 [记住我] 模式登录、七天免登录](https://img-blog.csdnimg.cn/img_convert/bb003babb43320635e7de7e6300ad907.gif)















