Deploy the controller-manager in a Kubernetes cluster

Deploying the controller to a Kubernetes cluster involves following steps:

  • Building the docker image
  • Pushing the docker image to the container registry
  • Customizing the deployment manifests
  • Applying the manifests to deploy in the clusterKubebuilder generated Makefile supports all the above steps.

Prerequisites

Kubebuilder generated Makefile uses Kustomize for customizing the manifestsbefore deploying to the kubernetes cluster. Follow the instructions to install Kustomize andensure that is available in the PATH. Note that Kubebuilder requires Kustomize version 1.0.4 or higher for deploy to work.

  1. opsys=linux # or darwin, or windows
  2. curl -s https://api.github.com/repos/kubernetes-sigs/kustomize/releases/latest |\
  3. grep browser_download |\
  4. grep $opsys |\
  5. cut -d '"' -f 4 |\
  6. xargs curl -O -L
  7. mv kustomize_*_${opsys}_amd64 kustomize
  8. chmod u+x kustomize

The yaml configuration for the Manager is automatically created underconfig/manager.

Building the docker image and pushing it to a container registry

Makefile has following targets:

  • docker-build to build the docker image for the controller manager
  • docker-push to push it to the configured container registry.Both target support IMG variable. If IMG argument is not provided, it ispicked from the environment variable.
  1. # build the docker image
  2. make docker-build IMG=<image-name>
  3. # build the docker image
  4. make docker-push IMG=<image-name>

Customizing the controller manager manifests using Kustomize

Kubebuilder scaffolds a basic kustomization.yaml under config/default directory. Current customization:

  • Specifies all controller manager resources to be created under specified namespace
  • Adds a prefix (directory name of the project) for controller manager resources
  • Adds a patch config/default/manager_image_patch.yaml for override the image.Kustomize offers primitives for customizing namespace, nameprefix, labels, annotations etc., you can read more about it on Kustomize page.
  1. # examine the manifests before deploying
  2. kustomize build config/default

The above command will output the manifests on stdout so that it is easier to pipe it to kubectl apply

Customizing the controller manager manifests using Kustomize

deploy target in Makefile generates the base manifests, customizes the base manifests and then applies it to the configured Kubernetes cluster.

  1. # deploy the controller manager to the cluster
  2. make deploy

By now, you should have controller manager resources deployed in cluster. Youcan examine controller manager pod by running

  1. # deploy the controller manager to the cluster
  2. kubectl get pods -n <namespace>