kubernetes集群中的port、targetPort、nodePort和containerPort区别

port

port是暴露在cluster ip上的端口,port提供了集群内部客户端访问service的入口,即clusterIP:port

nodeport

nodePort 提供了集群外部客户端访问 Service 的一种方式,nodePort 提供了集群外部客户端访问 Service 的端口,通过 nodeIP:nodePort 提供了外部流量访问k8s集群中service的入口。

targetPort

targetPort是pod的端口,从port和nodePort来的流量经过kube-proxy流入到后端pod的targetPort上,最后进入容器。

containerPort

containerPort是pod内部容器的端口,targetPort映射到containerPort。

比如下面的nginx.yaml文件

[root@master1 ~]# cat nginx.yaml 
---
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: test
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.20
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 80
    volumeMounts:
    - name: nginx-data
      mountPath: /usr/share/nginx/html
    - name: nginx-conf
      mountPath: /etc/nginx/conf.d/

  volumes:
  - name: nginx-data
    hostPath:
      path: /www
      type: DirectoryOrCreate
  - name: nginx-conf
    nfs:
      server: 192.168.110.5 
      path: "/conf"

---
apiVersion: apps/v1
kind: Service
metadata:
  name: nginx
spec:
  type: NodePort
  ports:
  - name: nginx
    port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30015
  selector:
    app: nginx

 

posted @   水悦  阅读(224)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 提示词工程——AI应用必不可少的技术
· 地球OL攻略 —— 某应届生求职总结
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界
点击右上角即可分享
微信分享提示