向外发布-NodePort
通过NodePort的方式向外发布,基于ClusterIP的方式,首先要生成一个虚拟IP,然后将虚拟IP和端口映射到集群中的节点上,这样的话我们就可以通过节点本身加端口的方式来进行访问Service。
因为ClusterIP本身就已经提供了负载均衡的功能,所以在NodePort模式下,不管访问的时集群中的哪个节点地址,实现的负载均衡效果都是一样的,这个过程会先由某台机器通过映射关系转发到ClusterIP,然后由ClusterIP通过比例随机算法转发到对应的Pod。
[root@master ~]# vi testnodeportservice.yaml
apiVersion: v1
kind: Service
metadata:
name: deploymenthttp
spec:
selector:
example: deploymenthttp
ports:
- port: 8080
targetPort: 80
protocol: TCP
nodePort: 30002 #将ClusterIP及Port属性,来映射到集群中的各个机器的30001端口,这样的话,就可以通过集群节点ip加映射端口访问,nodePort取值范围时30000至32767
type: NodePort
[root@master ~]# kubectl apply -f testnodeportservice.yaml
service/deploymenthttp configured
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
deploymenthttp NodePort 10.1.245.237 <none> 8080:30002/TCP 65m
[root@master ~]# curl 10.1.245.237:8080 //因为是基于ClusterIP的方式进行访问,所以通过集群地址的方式依然可以访问到服务
<p> The host is deploymenthttp-7fbc9958f5-jrf2n </p>
访问成功,我们便可以在物理节点加上端口的方式进行访问,集群中的每一个节点都可以进行访问
[root@master ~]# curl 192.168.200.11:30002
<p> The host is deploymenthttp-7fbc9958f5-w9b8g </p>
[root@master ~]# curl 192.168.200.12:30002
<p> The host is deploymenthttp-7fbc9958f5-s5fwq </p>
[root@master ~]# curl 192.168.200.13:30002
<p> The host is deploymenthttp-7fbc9958f5-w9b8g </p>