Skip to content

This script is implemented mostly in GO , which about Kubernetes Custom EndPoint HealthCheck.

License

Notifications You must be signed in to change notification settings

ranzhendong/lecter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kubernetes-custom-endpoint-healthcheck

GitHub Workflow Status Travis (.org) branch GitHub release (latest by date including pre-releases) GitHub last commit (branch)

  以go语言为载体实现对于kubernetes集群当中自建外部访问endpoint资源的健康检查。

配置

  通过config.yaml进行配置文件管理,配置文件位置建议和脚本同级或者在同级目录下的config文件夹下。

脚本配置my_conf

  基础脚本运行配置。

interval-健康检查间隔

interval: 2000

pingtimeout-ping超时时间

  指的是通过net包对ip port进行4层访问测试的超时时间。

pingtimeout: 3000

k8s配置kubernetes_conf

​ 通过这个配置实现对k8sApiServer基本读写配置。

url-访问k8s集群ApiServer地址

  在pod当中运行,可以填写域名来实现内部访问,示例配置为外部访问。

url: https://172.16.0.60:6443

endpointapi-访问endpoint资源api

  根据k8s版本不同,api的路径上有所不同,需要自己进行配置。

  需要注意的是myNameSpacesmyEndPoints是后面会进行替换的字段,因此路径自定义但是保证这两个关键字存在。

endpointapi: /api/v1/namespaces/myNameSpaces/endpoints/myEndPoints

content_type-写入时数据包类型

  因为写在代码当中,因此不建议更改,保持application/yaml就好。

content_type: application/yaml

token_file-访问k8s集群ApiServer的token

  如果为空,不填:

  • linux系统下会默认访问如下路径:/var/run/secrets/kubernetes.io/serviceaccount/token

  • window系统下会默认访问如下路径:C:\Users\Administrator\go\my_script\src\lepai.token\token

  如果为default

  • linux和window下,会在脚本执行的相对路径./以及./conf/进行token文件寻找。

  指定文件路径:

  • 例如:C:\Users\Administrator\go\my_script\src\lepai.token\token

  因此建议在pod当中使用挂载的serveraccount文件来实现。也就是/var/run/secrets/kubernetes.io/serviceaccount/token

token_file: default

存储配置storage_conf

  对健康检查信息进行存储,方便迭代。

  后期计划加入ectd存储选项,保证多个相同脚本同时运行,分布式锁的实现。

redis-redis存储配置

  redis:
    ipport: 172.16.0.61:6379
    password:

ipport-redisIP端口配置

ipport: 172.16.0.61:6379

password-redis访问auth

  如果没有密码,保持空就可以

password: 123

service配置service_info

  这部分配置主要是为了保证endpoint对应的service信息一致,也就是需要通过/api/v1/namespaces/myNameSpaces/endpoints/myEndPoints来对某个endpoint资源实现访问,示例当中配置了redis集群,eqmx集群以及ceph集群。

  最后为了保证可用性,可以自行通过命令行访问k8sApiServer的endpoint接口:/api/v1/namespaces/myNameSpaces/endpoints/myEndPoints来验证,注意需要对myNameSpacesmyEndPoints进行替换。

redis-示例名称

  可自定义,但是需要和下面的保持一致。

service_info:
  redis:
  emqx:
  ceph:

namespace-endpoint命名空间

namespace: default

name-endpoint名称

name: redis

endpoint健康检查配置service_healthcheck_list

  这部分是配置是告诉脚本这些ip地址和port端口是正常的,如果检测到从k8sApiServer取出的数据和它不一致,那么就可以确定是有ip地址对应的节点down。

redis-示例名称

  可自定义,注意需要保持和上面的service_info当中的名称保持一致。

service_healthcheck_list:
  redis:
  emqx:
  ceph:

port-端口

  指定endpoint访问端口其实也就是健康检查端口。

port: 38080

iplist-ip列表

  指定一个服务的多个节点ip信息。

  需要通过name进行区分,方便后面的钉钉报警组件(还未开发)

  - iplist:
    - name: node-01
      ip: 172.16.0.61
    - name: node-02
      ip: 172.16.0.62
    - name: node-03
      ip: 172.16.0.63

endpoint.yaml配置endpoint_template

  模板文件,需要通过它将主要的subsets字段进行替换,对通过健康检查的iplist进行更新,然后写入到k8s,实现覆盖,保证service访问。

  这部分除非k8s版本不一致导致需要更改apiVersion字段之外,其他的不建议更改,如果因为k8s导致subsets字段以及结构不一致的,则不建议使用该脚本,因为脚本对于字符的处理是基于下面的模板进行的,也可以进行自己需求的二次开发。

endpoint_template:
  kind: Endpoints
  apiVersion: v1
  metadata:
    name: ceph
    namespace: default
  subsets:
    - addresses:
        - ip: 10.0.0.71
        - ip: 10.0.0.72
      ports:
        - port: 8080
          protocol: TCP

使用

  使用方式在前面有叙述

编译

$ go build ./src/...

运行

main.exe

2019/11/25 17:58:34 main.go:94: 检查次数:[ 1 ]
2019/11/25 17:58:36 main.go:94: 检查次数:[ 2 ]
2019/11/25 17:58:38 main.go:94: 检查次数:[ 3 ]
2019/11/25 17:58:39 healthcheck.go:55: {Name:redis} {Addresses:[map[ip:172.16.0.61]] != SuccessMapList: [map[ip:172.16.0.61] map[ip:172.16.0.62]]}
2019/11/25 17:58:40 main.go:94: 检查次数:[ 4 ]
2019/11/25 17:58:41 healthcheck.go:52: {Name:redis} {Addresses:[map[ip:172.16.0.61] map[ip:172.16.0.62]] == SuccessMapList: [map[ip:172.16.0.61] map[ip:172.16.0.62]]}
redis: nil
2019/11/25 17:58:41 healthcheck.go:41: RedisListGet CAN'T GET
2019/11/25 17:58:41 healthcheck.go:52: {Name:ceph} {Addresses:[map[ip:172.16.0.61] map[ip:172.16.0.62]] == SuccessMapList: [map[ip:172.16.0.61] map[ip:172.16.0.62]]}
2019/11/25 17:58:42 main.go:94: 检查次数:[ 5 ]
2019/11/25 17:58:43 healthcheck.go:52: {Name:redis} {Addresses:[map[ip:172.16.0.61] map[ip:172.16.0.62]] == SuccessMapList: [map[ip:172.16.0.61] map[ip:172.16.0.62]]}
redis: nil
2019/11/25 17:58:43 healthcheck.go:41: RedisListGet CAN'T GET
2019/11/25 17:58:43 healthcheck.go:52: {Name:ceph} {Addresses:[map[ip:172.16.0.61] map[ip:172.16.0.62]] == SuccessMapList: [map[ip:172.16.0.61] map[ip:172.16.0.62]]}

About

This script is implemented mostly in GO , which about Kubernetes Custom EndPoint HealthCheck.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages