k8s中许多关于port即端口的定义,端口在网络协议中是7层的概念也就是区分业务的编号。k8s的定义中也有很多关于端口的配置,下面就常见的几种加以说明。

环境说明

  • node主机IP: 172.10.220.201

containers定义中的containerPort

containerPort是容器需要暴露的端口,一般是容器主进程的监听端口。例如下面,iperf进程一般监听的是5201端口,所以该容器需要暴露5201端口。

    containers:
      - name: iperf
        args: ['-s']
        ports:
        - containerPort: 5201
        image: 192.168.1.2:1234/iperf:1.0

service中的端口定义

举一个nodeIP的Service的例子:

apiVersion: v1
kind: Service
metadata:
name: busybox-service
namespace: net
spec:
type: NodePort
selector:
  app: busybox 
ports:
    # By default and for convenience, the `targetPort` is set to the same value as the `port` field.
  - port: 80
    targetPort: 80
    # Optional field
    # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
    nodePort: 30007

#创建出来的service如下
kubectl get svc -o wide --all-namespaces
NAMESPACE   NAME                                         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE    SELECTOR
default     busybox-service                              NodePort    10.0.0.155   <none>        80:30007/TCP     15m    app=busybox

现就port、targetport和nodePort说明如下:

  • port: 是service暴露在cluster ip上的端口,:port 是提供给集群内部客户访问service的入口。也就是说可以在集群内部用10.0.0.155:80端口访问容器提供的服务;
  • nodePort: 是kubernetes提供给集群外部客户访问service入口的一种方式,:nodePort 是提供给集群外部客户访问service的入口。也就是说集群外部的客户可以通过172.10.220.201:3007访问pod提供的服务;
  • targetPort: 就pod上的真正提供服务的端口,最后的流量都会导到这里来,进入容器内部。

相关文章:

  • 2021-12-04
  • 2021-06-21
  • 2022-12-23
  • 2021-12-02
  • 2021-07-13
  • 2021-08-14
  • 2021-05-01
猜你喜欢
  • 2021-04-07
  • 2021-09-21
  • 2021-06-20
  • 2021-11-03
  • 2022-02-09
  • 2021-08-12
  • 2022-01-18
相关资源
相似解决方案