faith丶

导航

kubernetes nodePort、targetPort、port、containerPort图解【附加endpoints】

###

1、nodePort

外部机器可访问的端口。
比如一个Web应用需要被其他用户访问,那么需要配置type=NodePort,而且配置nodePort=30001,那么其他机器就可以通过浏览器访问scheme://node:30001访问到该服务,例如http://node:30001。
例如MySQL数据库可能不需要被外界访问,只需被内部服务访问,那么不必设置NodePort
Nodeport:node节点上开放的端口,用于接收集群外部的请求
 
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
ports:
- port: 30080
  targetPort: 80
  nodePort: 30001
selector:
  name: nginx-pod
apiVersion: v1
kind: Service
metadata:
name: mysql-service
spec:
ports:
- port: 33306
  targetPort: 3306
selector:
  name: mysql-pod

2、targetPort

容器的端口(最终的流量端口)。【指定 pod 的接收端口,pod 通过该端口接收请求。要求 pod 中的应用已经开放了该端口】
与制作容器时暴露的端口一致(使用DockerFile中的EXPOSE),例如官方的nginx(参考DockerFile)暴露80端口。 


对应的service.yaml如下: apiVersion: v1 kind: Service metadata: name: nginx
-service spec: type: NodePort // 有配置NodePort,外部流量可访问k8s中的服务 ports: - port: 30080 // 指定service的接收端口 targetPort: 80 // 容器端口 nodePort: 30001 // NodePort:node节点上开放的端口,用于接收集群外部的请求 selector: name: nginx-pod

3、port

kubernetes中的服务之间访问的端口,尽管mysql容器暴露了3306端口(参考https://github.com/docker-library/mysql/的DockerFile),但是集群内其他容器需要通过33306端口访问该服务,外部机器不能访问mysql服务,因为他没有配置NodePort类型
port: 指定service 的接收端口,service 在这个端口上监听请求

apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
ports:
- port: 30080
  targetPort: 80
  nodePort: 30001
selector:
  name: nginx-pod
apiVersion: v1
kind: Service
metadata:
name: mysql-service
spec:
ports:
- port: 33306
  targetPort: 3306
selector:
  name: mysql-pod

4、containerPort

containerPort:容器内部端口

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: tomcat-deployment
spec:
replicas: 3
template:
metadata:
labels:
app: tomcat
tier: frontend
spec:
containers:
    name: tomcat
    image: docker.cinyi.com:443/tomcat
    ports:
    containerPort: 80   #这里containerPort是容器内部的port

5、附加:endpoints

5.1、endpoints概念

Endpoints表示一个Service对应的所有Pod副本的访问地址。

Node上的Kube-proxy进程获取每个Service的Endpoints,实现Service的负载均衡功能。

 

 

5.2、Endpoints Controller

Endpoints Controller就是负责生成和维护所有Endpoints对象的控制器,它负责监听Service和对应的Pod副本的变化。

如果检测到Service被删除,则删除和该Service同名的Endpoints对象。

如果检测到新的Service被创建或者修改则根据该Service信息获得相关的Pod列表,然后创建或者更新Service对应的Endpoints对象。

5.3、检查服务的endpoints/查看endpoinds的yaml

###======查看node
[root@k8s-master ~]# kubectl get node
NAME         STATUS   ROLES    AGE    VERSION
k8s-master   Ready    master   647d   v1.16.9
k8s-node     Ready    <none>   647d   v1.16.9
###======查看deployment [root@k8s-master ~]# kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE alpha-abilitytest-pc 2/2 2 2 581d
###
======查看pod [root@k8s-master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES alpha-abilitytest-pc-5785bffbd8-k2qlr 1/1 Running 0 333d 172.20.4.80 k8s-node <none> <none> alpha-abilitytest-pc-575955d586-rntd8 1/1 Running 0 333d 172.20.4.74 k8s-node <none> <none> ###======查看endpoints [root@k8s-master ~]# kubectl get endpoints NAME ENDPOINTS AGE alpha-abilitytest-pc-svc 172.20.4.80:80,172.20.4.74:80 473d ###======查看endpoints,service [root@k8s-master ~]# kubectl get ep,svc alpha-abilitytest-pc-svc NAME ENDPOINTS AGE endpoints/alpha-abilitytest-pc-svc 172.20.4.80:80,172.20.4.74:80 473d NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/alpha-abilitytest-pc-svc ClusterIP 172.21.9.21 <none> 80/TCP 473d ###======查看endpoints的yaml [root@k8s-master ~]# kubectl edit endpoints alpha-abilitytest-pc-svc # Please edit the object below. Lines beginning with a '#' will be ignored, # and an empty file will abort the edit. If an error occurs while saving this file will be # reopened with the relevant failures. # apiVersion: v1 kind: Endpoints metadata: creationTimestamp: "2020-10-08T05:44:58Z" name: alpha-abilitytest-pc-svc namespace: default resourceVersion: "136883654" selfLink: /api/v1/namespaces/default/endpoints/alpha-abilitytest-pc-svc uid: b3f0cd7e-33b5-4a4f-bb1b-191c2165a8e9 subsets: - addresses: - ip: 172.20.4.80 nodeName: k8s-node targetRef: kind: Pod name: alpha-abilitytest-pc-5785bffbd8-k2qlr namespace: default resourceVersion: "136883646" uid: 02a557fd-7da5-4b18-ab1a-35039ec1264a - ip: 172.20.4.74 nodeName: k8s-node targetRef: kind: Pod name: alpha-abilitytest-pc-575955d586-rntd8 namespace: default resourceVersion: "84260157" uid: 0b7c6910-16fa-449d-bb71-221799ba2eb0 ports: - name: http port: 80 protocol: TCP ###======查看service的yaml [root@k8s-master ~]# kubectl edit svc alpha-abilitytest-pc-svc # Please edit the object below. Lines beginning with a '#' will be ignored, # and an empty file will abort the edit. If an error occurs while saving this file will be # reopened with the relevant failures. # apiVersion: v1 kind: Service metadata: creationTimestamp: "2020-10-08T05:44:58Z" name: alpha-abilitytest-pc-svc namespace: default resourceVersion: "39553818" selfLink: /api/v1/namespaces/default/services/alpha-abilitytest-pc-svc uid: 8203cb5c-f56d-4790-b122-5a582135724b spec: clusterIP: 172.21.9.21 ports: - name: http port: 80 protocol: TCP targetPort: 80 selector: app: alpha-abilitytest-pc sessionAffinity: None type: ClusterIP status: loadBalancer: {}

 

5.4、访问集群外部的服务

对于一个集群内部的 pod 来说,如果他想访问一个集群外部的服务该怎么办呢?例如一个网站的公共 api,或者是一个云数据库。我们就没办法使用svc+标签选择器的方式来获取这些服务了,因为标签选择器只能监测集群内部的 pod 。而无法放眼外部。那么我们应该怎么做呢?
当然可以通过自定义一个endpoint资源,用它指定外部服务的 ip 及端口,然后绑定到一个svc上,这样内部的 pod 不就通过完全一样的方式访问外部服务了么?

6.图解


 

###

posted on 2019-12-26 18:28  faith丶  阅读(6273)  评论(2编辑  收藏  举报