使用Service将集群外服务引入集群内
业务场景
某个服务使用二进制部署,需要封装成kubernetes的Service来对集群内开放,例如mysql exporter
创建Service
apiVersion: v1
kind: Service
metadata:
name: mysql-sql-exporter-service
spec:
ports:
- protocol: TCP
port: 80 # service port
targetPort: 9104 # 对应endpoints端口
由于此服务没有选择算符,因此不会自动创建相应的EndpointSlices(之前叫Endpoints) 对象。 你可以通过手动添加 Endpoints 对象,将服务手动映射到运行该服务的网络地址和端口。
EndpointSlices是一个新API,它提供了Endpoints API的可扩展和可扩张的替代方案。
当一个Service对应Pod数量比较大的时候,例如一个service对应5000个pod endpoints, 控制器要使用EndpointSlices,避免单个Endpoint过大导致整个集群同步数据时出现问题。
EndpointSlices版本示例代码,k8s v1.21以上版本才支持
apiVersion: discovery.k8s.io/v1
kind: EndpointSlice
metadata:
name: mysql-sql-exporter-service-1 # by convention, use the name of the Service
# as a prefix for the name of the EndpointSlice
labels:
# You should set the "kubernetes.io/service-name" label.
# Set its value to match the name of the Service
kubernetes.io/service-name: mysql-sql-exporter-service
addressType: IPv4
ports:
- name: '' # empty because port 9376 is not assigned as a well-known
# port (by IANA)
appProtocol: http
protocol: TCP
port: 9104
endpoints:
- addresses:
- "10.7.224.5" # the IP addresses in this list can appear in any order
endpoints版本示例代码, kubernetes各版本都支持
apiVersion: v1
kind: Endpoints
metadata:
# 这里的 name 要与 Service 的名字相同
name: mysql-sql-exporter-service
subsets:
- addresses:
- ip: 10.7.224.5
ports:
- port: 9104
查看的服务信息
% k get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql-sql-exporter-service ClusterIP 172.16.254.22 <none> 80/TCP 12m
k node-shell 进入节点,使用curl 172.16.254.22/metrics 即可看到 对应返回的接口
参考文章:
https://cloud.tencent.com/developer/article/1739701 利用EndpointSlices扩展Kubernetes网络,提供更强的可伸缩性和功能