微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐