对于集群内访问集群外部服务场景使用固定的endpoint维护增加探活功能
也可以手动执行一下脚本,namespace为xxx
for cep in $(kubectl get cep -n xxx -o jsonpath={.items[*].metadata.name});do kubectl patch cep -n xxx --type='json' -p='[{"op": "replace", "path": "/metadata/finalizers", "value":[]}]' $cep;done
升级资源之前最好先备份一下cr删除之后再重新创建即可。
由此,新的想法诞生了: 写一个controller,维护一个CRD来自动创建需要访问的外部服务对应的Service和Endpoint,并对创建的Endpoint中的外部服务数据(IP:PORT列表)进行探活,探活失败则移除对应的外部服务数据。
endpoints-operator是一个云原生、高可靠性、高性能、面向K8s内部服务访问外部服务的具备探活功能的4层LB。
主要使用在集群内部的Pod需要访问外部服务的场景,比如数据库、中间件等,通过endpoints-operator的探活能力,可及时将有问题的后端服务剔除,避免受单个宕机副本影响,并可查看status获取后端服务健康状态和探活失败的服务。
VERSION="0.2.1"
wget https://github.com/labring/endpoints-operator/releases/download/v${VERSION}/endpoints-operator-${VERSION}.tgz
helm install -n kube-system endpoints-operator ./endpoints-operator-${VERSION}.tgz
sealos run labring/endpoints-operator:v0.2.1
apiVersion: sealos.io/v1beta1
kind: ClusterEndpoint
metadata:
name: wordpress
namespace: default
spec:
periodSeconds: 10
ports:
- name: wp-https
hosts:
## 端口相同的hosts
- 10.33.40.151
- 10.33.40.152
protocol: TCP
port: 38081
targetPort: 443
tcpSocket:
enable: true
timeoutSeconds: 1
failureThreshold: 3
successThreshold: 1
- name: wp-http
hosts:
## 端口相同的hosts
- 10.33.40.151
- 10.33.40.152
protocol: TCP
port: 38082
targetPort: 80
httpGet:
path: /healthz
scheme: http
timeoutSeconds: 1
failureThreshold: 3
successThreshold: 1
- name: wp-udp
hosts:
## 端口相同的hosts
- 10.33.40.151
- 10.33.40.152
protocol: UDP
port: 38003
targetPort: 1234
udpSocket:
enable: true
data: "This is flag data for UDP svc test"
timeoutSeconds: 1
failureThreshold: 3
successThreshold: 1
- name: wp-grpc
hosts:
## 端口相同的hosts
- 10.33.40.151
- 10.33.40.152
protocol: TCP
port: 38083
targetPort: 8080
grpc:
enable: true
timeoutSeconds: 1
failureThreshold: 3
successThreshold: 1
“endpoints-operator” 的引入,对产品无侵入以及云原生等特性解决了在集群内部访问外部服务等问题。这个思路将会成为以后开发或者运维的标配,也是一个比较完善的项目,从开发的角度换个思路更优雅的去解决一些问题。