项目作者: vapor-ware

项目描述 :
A minimal kubernetes-native sidecar elector service.
高级语言: Go
项目地址: git://github.com/vapor-ware/k8s-elector.git
创建时间: 2019-04-26T14:11:23Z
项目社区:https://github.com/vapor-ware/k8s-elector

开源协议:GNU General Public License v3.0

下载


k8s-elector

k8s-elector is a minimal Kubernetes-native sidecar elector service, allowing you to
perform simple leader election within your Kubernetes deployment.

Internally, it uses "k8s.io/client-go/tools/leaderelection" to run the election.

Usage

The k8s-elector is intended to be run as a sidecar to other services to provide
them leader election capabilities, but it can run on its own as well. For an example,
you can run just the elector containers with

  1. $ kubectl run k8s-elector --image=vaporio/k8s-elector --replicas=3 -- -election=example

Note By default, k8s-elector tries to use a Kubernetes LeaseLock. If running a
version of Kubernetes which does not support this, you can change the lock type with
the -lock-type flag. (valid values: leases, endpoints, configmaps)

This will run 3 instances of the k8s-elector. You can observe their logs to verify
a leader is chosen among them.

Looking through the logs for leadership is not tenable for a deployment to identify
leadership. k8s-elector exposes a basic HTTP API to provide leadership status information.
You can enable and conifgure it with the -http flag.

  1. $ kubectl run k8s-elector --image=vaporio/k8s-elector --replicas=3 -- -election=example -lock-type=configmaps -http=0.0.0.0:5002

You can look through the logs to see a leader was elected and you can verify from within
the cluster

that each pod reports its leadership status correctly.

  1. / # curl 10.1.0.180:5002/
  2. {"is_leader":false,"leader":"k8s-elector-74c54b485f-hgf9z","node":"k8s-elector-74c54b485f-564ht","timestamp":"2019-05-02T18:28:51Z"}
  3. / # curl 10.1.0.181:5002/
  4. {"is_leader":false,"leader":"k8s-elector-74c54b485f-hgf9z","node":"k8s-elector-74c54b485f-qztgk","timestamp":"2019-05-02T18:29:21Z"}
  5. / # curl 10.1.0.179:5002/
  6. {"is_leader":true,"leader":"k8s-elector-74c54b485f-hgf9z","node":"k8s-elector-74c54b485f-hgf9z","timestamp":"2019-05-02T18:29:26Z"}

As the above run command specified the lock type as “configmaps”, you should also expect to
see a ConfigMap with the same name as the election.

  1. $ kubectl get cm
  2. NAME DATA AGE
  3. example 0 21m

Configuration

For a full list of configuration options, you can run the elector with the -h flags. These
will include both the flags for the elector itself as well as flags for configuring logging.

  1. Usage of ./elector:
  2. -election string
  3. The name of the election. This is required.
  4. -http string
  5. The HTTP address (host:port) which leader state will be reported on.
  6. -id string
  7. The ID of the election participant. If not set, the hostname, as reported by the kernel, is used.
  8. -kubeconfig string
  9. The kubeconfig file to use. If not set, in-cluster config will be used.
  10. -lock-type string
  11. The type of Kubernetes object to use for the lock (leases, endpoints, configmaps) (default "leases")
  12. -namespace string
  13. The Kubernetes namespace to run the election in. If not set, elections will run in the default namespace. (default "default")
  14. -ttl duration
  15. The TTL for the election. (default 10s)

API

When enabled, the exposed HTTP API consists of a single endpoint at the URL root.

/

Method: GET

Example response:

  1. {
  2. "is_leader": false,
  3. "leader": "k8s-elector-74c54b485f-hgf9z",
  4. "node": "k8s-elector-74c54b485f-564ht",
  5. "timestamp": "2019-05-02T18:28:51Z"
  6. }

Fields

Field Description
is_leader A boolean describing whether the node being queried is the leader node.
leader The ID of the node which is currently the leader.
node The ID of the node being queried for leadership status.
timestamp The RFC3339-formatted UTC timestamp for when the response was returned.