Home Explore Blog CI



kubernetes

2nd chunk of `content/en/blog/_posts/2016-01-00-Simple-Leader-Election-With-Kubernetes.md`
d39f2ec8d2028feaca8d90e6076cae476ddf1de9e38b7c350000000100000965
To see which pod was chosen as the leader, you can access the logs of one of the pods, substituting one of your own pod's names in place of

```  
${pod_name}, (e.g. leader-elector-inmr1 from the above)

$ kubectl logs -f ${name}
leader is (leader-pod-name)
```
… Alternately, you can inspect the endpoints object directly:


_'example' is the name of the candidate set from the above kubectl run … command_
```
$ kubectl get endpoints example -o yaml
```
Now to validate that leader election actually works, in a different terminal, run:

```  
$ kubectl delete pods (leader-pod-name)
```
This will delete the existing leader. Because the set of pods is being managed by a replication controller, a new pod replaces the one that was deleted, ensuring that the size of the replicated set is still three. Via leader election one of these three pods is selected as the new leader, and you should see the leader failover to a different pod. Because pods in Kubernetes have a _grace period_ before termination, this may take 30-40 seconds.

The leader-election container provides a simple webserver that can serve on any address (e.g. http://localhost:4040). You can test this out by deleting the existing leader election group and creating a new one where you additionally pass in a --http=(host):(port) specification to the leader-elector image. This causes each member of the set to serve information about the leader via a webhook.

```    
# delete the old leader elector group
$ kubectl delete rc leader-elector

# create the new group, note the --http=localhost:4040 flag
$ kubectl run leader-elector --image=gcr.io/google_containers/leader-elector:0.4 --replicas=3 -- --election=example --http=0.0.0.0:4040

# create a proxy to your Kubernetes api server
$ kubectl proxy
```

You can then access:


http://localhost:8001/api/v1/proxy/namespaces/default/pods/(leader-pod-name):4040/


And you will see:

```  
{"name":"(name-of-leader-here)"}
```
####  Leader election with sidecars


Ok, that's great, you can do leader election and find out the leader over HTTP, but how can you use it from your own application? This is where the notion of sidecars come in. In Kubernetes, Pods are made up of one or more containers. Often times, this means that you add sidecar containers to your main application to make up a Pod. (for a much more detailed treatment of this subject see my earlier blog post).

Title: Validating Leader Election and Using it with Sidecars
Summary
This section describes how to validate that leader election is working by deleting the current leader pod and observing the failover to a new leader. It also explains how to access the leader's information over HTTP using a webserver and how to use leader election with sidecar containers in Kubernetes, where pods are composed of one or more containers.