port是暴露在cluster ip上的端口,port提供了集群内部客户端访问service的入口,即clusterIP:port
。
nodePort 提供了集群外部客户端访问 Service 的一种方式,nodePort 提供了集群外部客户端访问 Service 的端口,通过 nodeIP:nodePort
提供了外部流量访问k8s集群中service的入口。
targetPort是pod的端口,从port和nodePort来的流量经过kube-proxy流入到后端pod的targetPort上,最后进入容器。
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