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

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值