|
| 1 | +# Deploy the controller-manager in a Kubernetes cluster |
| 2 | + |
| 3 | +Deploying the controller to a Kubernetes cluster involves following steps: |
| 4 | + - Building the docker image |
| 5 | + - Pushing the docker image to the container registry |
| 6 | + - Customizing the deployment manifests |
| 7 | + - Applying the manifests to deploy in the cluster |
| 8 | + |
| 9 | +Kubebuilder generated `Makefile` supports all the above steps. |
| 10 | + |
| 11 | +{% panel style="info", title="Prerequisites" %} |
| 12 | +Kubebuilder generated `Makefile` uses [Kustomize](https://github.com/kubernetes-sigs/kustomize) for customizing the manifests |
| 13 | +before deploying to the kubernetes cluster. Follow the [instructions](https://github.com/kubernetes-sigs/kustomize/blob/master/INSTALL.md) to install `Kustomize` and |
| 14 | +ensure that is available in the PATH. Note that Kubebuilder requires `Kustomize` version `1.0.4` or higher for deploy to work. |
| 15 | + |
| 16 | +```bash |
| 17 | +opsys=linux # or darwin, or windows |
| 18 | +curl -s https://api.github.com/repos/kubernetes-sigs/kustomize/releases/latest |\ |
| 19 | + grep browser_download |\ |
| 20 | + grep $opsys |\ |
| 21 | + cut -d '"' -f 4 |\ |
| 22 | + xargs curl -O -L |
| 23 | +mv kustomize_*_${opsys}_amd64 kustomize |
| 24 | +chmod u+x kustomize |
| 25 | +``` |
| 26 | + |
| 27 | +The yaml configuration for the Manager is automatically created under |
| 28 | +`config/manager`. |
| 29 | +{% endpanel %} |
| 30 | + |
| 31 | +#### Building the docker image and pushing it to a container registry |
| 32 | + |
| 33 | +`Makefile` has following targets: |
| 34 | +- `docker-build` to build the docker image for the controller manager |
| 35 | +- `docker-push` to push it to the configured container registry. |
| 36 | + |
| 37 | +Both target support `IMG` variable. If IMG argument is not provided, it is |
| 38 | +picked from the environment variable. |
| 39 | + |
| 40 | +```bash |
| 41 | +# build the docker image |
| 42 | +make docker-build IMG=<image-name> |
| 43 | + |
| 44 | +# build the docker image |
| 45 | +make docker-push IMG=<image-name> |
| 46 | +``` |
| 47 | + |
| 48 | +#### Customizing the controller manager manifests using Kustomize |
| 49 | + |
| 50 | +Kubebuilder scaffolds a basic `kustomization.yaml` under `config/default` directory. Current customization: |
| 51 | + - Specifies all controller manager resources to be created under specified `namespace` |
| 52 | + - Adds a prefix (directory name of the project) for controller manager resources |
| 53 | + - Adds a patch `config/default/manager_image_patch.yaml` for override the image. |
| 54 | + |
| 55 | +Kustomize offers primitives for customizing namespace, nameprefix, labels, annotations etc., you can read more about it on [Kustomize](https://github.com/kubernetes-sigs/kustomize) page. |
| 56 | + |
| 57 | +```bash |
| 58 | +# examine the manifests before deploying |
| 59 | +kustomize build config/default |
| 60 | +``` |
| 61 | +The above command will output the manifests on stdout so that it is easier to pipe it to `kubectl apply` |
| 62 | + |
| 63 | +#### Customizing the controller manager manifests using Kustomize |
| 64 | + |
| 65 | +`deploy` target in `Makefile` generates the base manifests, customizes the base manifests and then applies it to the configured Kubernetes cluster. |
| 66 | + |
| 67 | +```bash |
| 68 | +# deploy the controller manager to the cluster |
| 69 | +make deploy |
| 70 | +``` |
| 71 | + |
| 72 | +By now, you should have controller manager resources deployed in cluster. You |
| 73 | +can examine controller manager pod by running |
| 74 | + |
| 75 | +```bash |
| 76 | +# deploy the controller manager to the cluster |
| 77 | +kubectl get pods -n <namespace> |
| 78 | +``` |
| 79 | + |
0 commit comments