Skip to content

Latest commit

 

History

History
94 lines (63 loc) · 4.42 KB

README.adoc

File metadata and controls

94 lines (63 loc) · 4.42 KB

Controller

This example demonstrates a simple controller that evaluates ConfigMaps and restarts associated pods if a ConfigMap changes.

The controller is inspired by configmap-controller but is more limited. It watches all ConfigMaps in the namespace where it’s deployed and reacts to the annotation k8spatterns.io/podDeleteSelector. If this annotation is present on a ConfigMap that has changed, the value of the annotation is used as a label selector to find Pods to kill. Assuming these pods are managed by a backend controller like Deployment, the Pods will be respawned, potentially picking up the changed configuration.

Refer to the "Controller" pattern in our book for a full explanation of how this controller works.

Warning
This example is for educational purposes only and is not suitable for general-purpose usage.

The following steps assume you are using minikube. More options for running the example are described in the installation instructions.

Create a ConfigMap containing the controller script:

kubectl create configmap config-watcher-controller --from-file=./config-watcher-controller.sh

To deploy the controller, a Deployment creates a pod with two containers:

  • A Kubernetes API proxy (k8spatterns/kubeapi-proxy) that exposes the Kubernetes API on localhost with port 8001. See the Dockerfile.

  • The main container (k8spattern/curl-jq) that executes the script from the configmap. It is based on an Alpine base image with curl and jq included. See the Dockerfile.

Both images are available on Docker Hub: k8spatterns/kubeapi-proxy and k8spatterns/curl-jq.

Create the deployment in the current namespace:

kubectl apply -f https://k8spatterns.io/Controller/config-watcher-controller.yml

Examine the descriptor file for useful comments and security setup information.

You now have a controller that watches ConfigMaps and restarts Pods when updates occur.

To test the controller, use the simple web application that exposes an environment variable as content. The image uses nc for content delivery and is available on Docker Hub as k8spatterns/mini-http-server.

Monitor the controller’s log (e.g., kubectl logs -f config-watcher-controller-…​.) to see incoming events:

kubectl logs -f $(kubectl get pods -l role=controller -o name) config-watcher

Run this command in the background to track future events.

Create the web application:

kubectl apply -f https://k8spatterns.io/Controller/web-app.yml

The descriptor includes a Deployment using the HTTP server, which references the content environment variable via a ConfigMap. The ConfigMap is annotated with a pod selector k8spatterns.io/podDeleteSelector: "app=webapp" that directly selects the webapp Pod.

The resource file also defines a Service that exposes the Pod via a nodePort.

With minikube, access the application in your browser:

minikube service webapp

Stop the tunnel created by minikube service (with CTRL-C), update the content of ConfigMap, and watch the controller’s log:

kubectl patch configmap webapp-config -p '{"data":{"message":"Take this update!"}}'

Finally, access the URL again to verify the content update:

minikube service webapp