概述
Kubernetes ServiceTypes 允许指定你所需要的 Service 类型,默认是 ClusterIP,还包含下面类型:
- ClusterIP:通过集群的内部 IP 暴露服务,选择该值时服务只能够在集群内部访问。 这也是默认的 ServiceType。
- NodePort:通过每个节点上的 IP 和静态端口(NodePort)暴露服务。 NodePort 服务会路由到自动创建的 ClusterIP 服务。 通过请求 <节点 IP>:<节点端口>,你可以从集群的外部访问一个 NodePort 服务。
- LoadBalancer:使用云提供商的负载均衡器向外部暴露服务。 外部负载均衡器可以将流量路由到自动创建的 NodePort 服务和 ClusterIP 服务上。
- ExternalName:通过返回 CNAME 和对应值,可以将服务映射到 externalName 字段的内容(例如,foo.bar.example.com)。 无需创建任何类型代理。
端口说明
- port:服务端口,暴露给集群内客户访问服务
- targetPort:容器端口即pod上的端口
- nodePort:节点端口,使用主机的端口,暴露给集群外客户访问服务
使用
ClusterIP
进入DCE,选择相应的集群,选择工作负载->创建工作负载->进入访问设置(第三步,前两部需要设置),也可以在应用负载->服务->新增服务。
上面创建对应的yaml内容apiVersion: v1kind: Servicemetadata:name: nginxspec:ports:- port: 80protocol: TCPtargetPort: 80selector:run: nginxtype: ClusterIP
NodePort
同上
上面创建对应的yaml内容apiVersion: v1kind: Servicemetadata:name: nginxspec:ports:- port: 80protocol: TCPtargetPort: 80nodePort: 80selector:run: nginxtype: NodePort
LoadBalancer
同上
上面创建对应的yaml内容
apiVersion: v1kind: Servicemetadata:name: nginxspec:ports:- port: 80protocol: TCPtargetPort: 80selector:run: nginxtype: LoadBalancer
ExternalName
同上
上面创建对应的yaml内容
具体可参考官方文档《使用Service暴露应用》apiVersion: v1kind: Servicemetadata:name: nginxspec:ports:- port: 80protocol: TCPtargetPort: 80type: ExternalNameexternalName: my.database.example.com
