Skip to content

Commit

Permalink
Update leader election sample (#1281)
Browse files Browse the repository at this point in the history
Co-authored-by: Ryan Baxter <[email protected]>
  • Loading branch information
Ryan Baxter and ryanjbaxter authored Mar 13, 2024
1 parent d21c5d5 commit ad09a89
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,18 @@ items:
- apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: namespace-reader
namespace: default
name: namespace-reader
rules:
- apiGroups: ["", "extensions", "apps"]
resources: ["configmaps", "pods", "services", "endpoints", "secrets"]
verbs: ["get", "list", "watch"]
- apiGroups: ["", "extensions", "apps"]
resources: ["configmaps", "pods", "services", "endpoints", "secrets"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
- apiVersion: apps/v1
kind: Deployment
metadata:
name: kubernetes-leader-election-example
spec:
replicas: 4
selector:
matchLabels:
app: kubernetes-leader-election-example
Expand Down Expand Up @@ -148,17 +149,10 @@ And check the leadership information again:
curl $SERVICE_URL
```

Now you should receive a message like this:
```
I am 'kubernetes-leader-election-example-1234567890-abcde' but I am not a leader of the 'world'
```

If you wouldn't do anything for a few seconds, the same instance will become a leader again because it only yielded its leadership but stayed in the cluster.
If another instance was able to acquire the leadership you should see a different instance is now the leadership. You may
have to try this a few times depending on how the service is load balanced.

Now scale the application to two instances and try all the steps again:
```
kubectl scale --replicas=2 deployment.apps/kubernetes-leader-election-example
```
`DEBUG` logging is enabled so you can view the logs of the instances in order to see which instance is acquiring leadership.

> Note: with multiple replicas in the cluster, `curl` command will access one of them depending on the Kubernetes load balancing configuration.
Thus, when trying to yield the leadership, request might go to a non-leader node first. Just execute command again until it reaches the correct node.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public LeaderController() throws UnknownHostException {
* Return a message whether this instance is a leader or not.
* @return info
*/
@GetMapping
@GetMapping("/")
public String getInfo() {
if (this.context == null) {
return String.format("I am '%s' but I am not a leader of the '%s'", this.host, this.role);
Expand All @@ -63,7 +63,7 @@ public String getInfo() {
* to give up the leadership.
* @return info about leadership
*/
@PutMapping
@PutMapping("/")
public ResponseEntity<String> revokeLeadership() {
if (this.context == null) {
String message = String.format("Cannot revoke leadership because '%s' is not a leader", this.host);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
spring.cloud.kubernetes.leader.role=world
# Configmap to which leader election metadata will be saved
spring.cloud.kubernetes.leader.config-map-name=leader
logging.level.org.springframework.cloud.kubernetes.fabric8.leader=DEBUG
logging.level.org.springframework.cloud.kubernetes.commons.leader=DEBUG

0 comments on commit ad09a89

Please sign in to comment.