Kubernetes中的nodePort,targetPort,port的区别和意义

1. nodePort
外部机器可访问的端口。
比如一个Web应用需要被其他用户访问,那么需要配置type=NodePort,而且配置nodePort=30001,那么其他机器就可以通过浏览器访问scheme://nodeIP:30001访问到该服务,例如http://nodeIP:30001。

通过nodeIP: nodePort可以从外部访问到某个service。

例如MySQL数据库可能不需要被外界访问,只需被内部服务访问,那么不必设置NodePort

2. Port

port是k8s集群内部访问service的端口,即通过clusterIP: port可以访问到某个service

<cluster ip>:port 是提供给集群内部客户访问service的入口。

3. targetPort

targetPort是pod的端口,从port和nodePort来的流量经过kube-proxy流入到后端pod的targetPort上,最后进入容器。

 

 举例:

1.准备镜像 编辑配置文件,这里使用的是httpd做测试,默认开80端口
[root@tensorflow1 serviceTest]# cat httpd.yaml

# -------------------  Deployment ------------------- #

kind: Deployment
apiVersion: apps/v1beta2
metadata:
  labels:
    k8s-app: httpd
  name: httpd
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: httpd
  template:
    metadata:
      labels:
        k8s-app: httpd
    spec:
      containers:
      - name: httpd
        image: httpd
---
# ------------------- Service ------------------- #

kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: httpd
  name: httpd
spec:
  type: NodePort
  ports:
    - port: 8081
      targetPort: 80
      nodePort: 30001
  selector:
    k8s-app: httpd

2.启动
[root@tensorflow1 serviceTest]# kubectl create -f httpd.yaml

3.查看启动情况
[root@tensorflow1 serviceTest]# kubectl get all -o wide --all-namespaces

NAMESPACE     NAME                                     READY     STATUS    RESTARTS   AGE       IP              NODE
default       po/httpd-68f9d7648d-tv9xv                1/1       Running   0          26m       10.244.1.10     tensorflow2

NAMESPACE     NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE       SELECTOR
default       svc/httpd        NodePort    10.100.205.181   <none>        8081:30001/TCP   35m       k8s-app=httpd

[root@tensorflow1 serviceTest]# kubectl describe svc/httpd
Name:                     httpd
Namespace:                default
Labels:                   k8s-app=httpd
Annotations:              kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"k8s-app":"httpd"},"name":"httpd","namespace":"default"},"spec":{"ports":[{"...
Selector:                 k8s-app=httpd
Type:                     NodePort
IP:                       10.100.205.181
Port:                     <unset>  8081/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  30001/TCP
Endpoints:                10.244.1.10:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>


4.访问

直接访问pod ip:TargetPort (即Endpoints) 10.244.1.10:80 ,生命周期短,一般不用
[root@tensorflow1 serviceTest]# curl "10.244.1.10:80"
<html><body><h1>It works!</h1></body></html>

访问service的clusterIP:port,集群内访问,集群外无法不能访问
[root@tensorflow1 serviceTest]# curl "10.100.205.181:8081"
<html><body><h1>It works!</h1></body></html>

访问service的MasterNode:nodePort,这里masterNode指的是任意一台k8s集群内物理机,在所有物理机上开端口,主要是为了给集群外机器访问的,如果不需要则不用配置。
[root@tensorflow1 serviceTest]# curl "192.168.1.139:30001"
<html><body><h1>It works!</h1></body></html>

posted @ 2020-05-19 10:34  Gdavid  阅读(1550)  评论(0编辑  收藏  举报