Kubernetes服务可以在服务定义中有一个targetPort和port:

kind: Service
apiVersion: v1
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9376

端口和targetPort有什么区别?


当前回答

目标器端口说明

# pod file
apiVersion: v1
kind: Pod
metadata: 
  name: pod_name
  labels: 
     name: pod_ref
spec: 
  containers:
  - name: docker-container
    image: python:3:11
    ports: 
    - containerPort: 5000 # this is the target port which we need to access (target) from service file

---
apiVersion: v1
kind: Service
metadata: 
   name: service_config
   labels: 
     name: service_config_ref
spec:
   type: NodePort
   selector:
     name: pod_ref  # same as pod metadata match labels 
     ports:
      - targetPort: 5000 # this is where the pod is listening 
        port: 80 # this is where this service (this file) is going to listen. 
                 # If you call the port 80, it will reach the service, then the service will forward the port to port:5000


其他回答

案例1:

让我们假设没有nodPort或port,现在你想运行你的应用程序并将其暴露给外部,你将需要:

一个Ingress控制器,它将使用servicePort根据路由重定向到我们想要的服务。 一个集群IP服务,其中定义了到应用程序端口的目标(也称为targetPort) 用于标识计算机上运行的应用程序或服务的网络端口(换句话说就是应用程序端口)。

所以,要从外部进入,我们需要三个端口。

servicePort(入口控制器) targetPort(集群Ip服务) networkPort(应用端口)

使一切正常工作: servicePort === targetPort === networkPort

案例2: 现在假设一个服务与集群中的另一个服务通信,或者假设一个服务从外部接收到一个请求,并发出一个事件,该事件触发了集群中的另一个服务。

假设服务X通过使用nodePort服务暴露在外部,在收到请求后,X服务希望与Y服务通信。

Y服务需要以下端口

ClusterIP端口,X服务通过该端口转发请求 一个ClusterIP targetPort, Y服务将通过该ClusterIP targetPort确定哪个端口应用程序正在运行。 应用端口

端口=== any

targetPort ===应用程序端口

内部服务X:

app.post('/posts/create', async (req, res) => {
  const id = randomBytes(4).toString('hex');
  const { title } = req.body;

  posts[id] = {
    id,
    title
  };

  await axios.post('http://event-bus-srv:4010/events', {
    type: 'PostCreated',
    data: {
      id,
      title
    }
  });

  res.status(201).send(posts[id]);
});

服务Y的配置和内部

apiVersion: v1
kind: Service
metadata:
  name: event-bus-srv
spec:
  selector:
    app: event-bus
  type: ClusterIP
  ports:
    - name: event-bus
      protocol: TCP
      port: 4010
      targetPort: 4009
app.listen(4009, () => {

  console.log('Listening on 4009');
});

目标器端口说明

# pod file
apiVersion: v1
kind: Pod
metadata: 
  name: pod_name
  labels: 
     name: pod_ref
spec: 
  containers:
  - name: docker-container
    image: python:3:11
    ports: 
    - containerPort: 5000 # this is the target port which we need to access (target) from service file

---
apiVersion: v1
kind: Service
metadata: 
   name: service_config
   labels: 
     name: service_config_ref
spec:
   type: NodePort
   selector:
     name: pod_ref  # same as pod metadata match labels 
     ports:
      - targetPort: 5000 # this is where the pod is listening 
        port: 80 # this is where this service (this file) is going to listen. 
                 # If you call the port 80, it will reach the service, then the service will forward the port to port:5000


“目标端口”是容器正在运行的端口。

端口:端口将流量从服务重定向到容器。

公开部署

  master $ kubectl get deployments
NAME         READY   UP-TO-DATE   AVAILABLE   AGE

nginx        1/1     1            1           31s
master $ kubectl expose deployment nginx --name=nginx-svc --port=8080 --target-port=80
service/nginx-svc exposed

master $ kubectl get svc

NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE

nginx-svc    ClusterIP   10.107.209.151   <none>        8080/TCP   5s

NodePort:服务对外访问的端口。

希望这是答案。

NodePort =将kubernetes服务对外公开到internet Port =将在集群中公开kubernetes服务,这样多个不同pod之间的通信就可以发生,并将请求重定向到TargetPort(因为不可能在同一个端口上运行多个pod, K8S引入了Port来处理可重用性) TargetPort =容器正在运行的实际端口

作为在docker-compose中指定的参考

ports:
  - 8080:80

从上面你可以比较8080是主机端口代表端口,80是目标端口

如果容器监听端口9376,那么 发送:9376

如果服务在端口80上侦听,则 端口:80

然后服务端口配置如下所示

ports:
 - protocol: TCP
   port: 80
   targetPort: 9376

最后,请求接收到服务的端口,并在pod的targetPort上转发。