How to Refresh Your Application's Service Account Tokens Following the Managed Kubernetes 2.2 Upgrade

If your application makes calls to the Kubernetes API server and uses a Kubernetes Service Account, please read on.

As part of the Managed Kubernetes 2.2 upgrade, we are enforcing TLS authentication on the Kubernetes API Server. As part of that change, we are generating new client and server certificates for the entire cluster. In particular, we replace the key used to generate Service Account tokens, and then re-generate the Secrets that contain the Service Account tokens.

If you application uses a Service Account, Kubernetes makes the token available via the file /var/run/secrets/ to your application's containers. However, this file is not updated if the token is re-generated via the API Server.

To ensure that your application's token is up to date, you must delete and recreate your application's pods. Please choose from the following:

You deployed your application using a Replication Controller and can tolerate momentary downtime

You can temporarily scale your application to zero pods using the following bash script:

#!/usr/bin/env bash
# Usage: myrc
current_replicas=$(kubectl get rc $replicationcontroller -o jsonpath="{.status.replicas}")
kubectl scale rc $replicationcontroller --replicas=0
kubectl scale rc $replicationcontroller --replicas=$current_replicas

Example usage: myrc

You deployed your application using a Replication Controller and cannot tolerate momentary downtime

If you cannot tolerate any downtime, you can simulate an in-place rolling update. We implement this by copying your existing Replication Controller definition, replacing its name, then performing a rolling update from the existing to the new Replication Controller.

#!/usr/bin/env bash
# Usage: myrc myrc_newname
kubectl patch rc/$replicationcontroller -p '{"spec":{"selector":{"secrets":"old"},"template":{"metadata":{"labels":{"secrets":"old"}}}}}'
kubectl get rc/$replicationcontroller -o yaml \
| sed -e 's/secrets\: old/secrets\: new/g' \
-e "0,/name\: ${replicationcontroller}/{s/name\: \
${replicationcontroller}/name\: ${replicationcontroller_newname}/}" \
-e 's/resourceVersion.*//' \
| kubectl rolling-update $replicationcontroller --update-period=10s -f -

Example usage: myrc myrc_newname

You deployed your application as one or more stand-alone Pods

If you deployed your application as one or more Pods that are not managed by a Replication Controller, you will need to delete and re-create each of the Pods:

#!/usr/bin/env bash
# Usage: mypod
kubectl get pod $pod -o yaml | kubectl replace -f -

Example usage: mypod

August 15, 2016