这种Service会占用宿主机的端口,默认可用的端口范围在 30000-32767 之间,可以在安装apiserver时修改。
NodePort使用场景是将集群内部服务暴露给集群外面使用的一种方案,在生产集群中,其实对集群外暴露的服务非常少,并且向外暴露服务由两种方案:NodePort 和 Ingress。
NodePort主要用于Ingress不能满足的场景,比如TCP/UDP 服务暴露到集群,或者涉及复杂的http rewriete 规则,使用Ingress过于复杂。
# svc-node-port.yaml
apiVersion: v1
kind: Service
metadata:
name: slb-s3
namespace: default
spec:
selector:
app: nginx
release: stable
partition: website
tier: slb
type: NodePort
ports:
- name: http
port: 80
targetPort: 80
nodePort: 30012
[root@maxiaoke nginx-01]# kubectl get svc |grep slb-s3
slb-s3 NodePort 10.100.218.132 <none> 80:30012/TCP 48s
[root@maxiaoke nginx-01]# kubectl describe svc slb-s3
Name: slb-s3
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=nginx,partition=website,release=stable,tier=slb
Type: NodePort
IP Families: <none>
IP: 10.100.218.132
IPs: 10.100.218.132
Port: http 80/TCP
TargetPort: 80/TCP
NodePort: http 30012/TCP
Endpoints: 10.200.3.10:80,10.200.4.8:80,10.200.4.9:80 + 2 more...
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@master_01_vm_160_10 ~]# netstat -lntp | grep 30012 # 在宿主机上开启3080端口,实现了集群外访问,注意避免端口冲突
tcp 0 0 0.0.0.0:30012 0.0.0.0:* LISTEN 10808/kube-proxy
[root@maxiaoke nginx-01]# curl http://10.0.160.30:30012/info # 集群外访问测试
2021-10-13T07:51:09+00:00|v1.0.3|nginx-deploy-699fdd685d-8nb5w|ok