$ mkdir nginx-operator
The Operator SDK includes options for generating an Operator project that leverages existing helm charts to deploy Kubernetes resources as a unified application, without having to write any Go code.
To demonstrate the basics of setting up and running an helm-based Operator using tools and libraries provided by the Operator SDK, Operator developers can build an example helm-based Operator for Nginx and deploy it to a cluster.
Logged into an OpenShift Container Platform 4.8 cluster with oc
with an account that has cluster-admin
permissions
To allow the cluster pull the image, the repository where you push your image must be set as public, or you must configure an image pull secret
You can build and deploy a simple helm-based Operator for Nginx by using the Operator SDK.
Create a project.
Create your project directory:
$ mkdir nginx-operator
Change into the project directory:
$ cd nginx-operator
Run the operator-sdk init
command
with the helm
plugin
to initialize the project:
$ operator-sdk init \
--plugins=helm
Create an API.
Create a simple Nginx API:
$ operator-sdk create api \
--group demo \
--version v1 \
--kind Nginx
This API uses the built-in helm chart boilerplate from the helm create
command.
Build and push the Operator image.
Use the default Makefile
targets to build and push your Operator. Set IMG
with a pull spec for your image that uses a registry you can push to:
$ make docker-build docker-push IMG=<registry>/<user>/<image_name>:<tag>
Run the Operator.
Install the CRD:
$ make install
Deploy the project to the cluster. Set IMG
to the image that you pushed:
$ make deploy IMG=<registry>/<user>/<image_name>:<tag>
Add a security context constraint (SCC).
The Nginx service account requires privileged access to run in OpenShift Container Platform. Add the following SCC to the service account for the nginx-sample
pod:
$ oc adm policy add-scc-to-user \
anyuid system:serviceaccount:nginx-operator-system:nginx-sample
Create a sample custom resource (CR).
Create a sample CR:
$ oc apply -f config/samples/demo_v1_nginx.yaml \
-n nginx-operator-system
Watch for the CR to reconcile the Operator:
$ oc logs deployment.apps/nginx-operator-controller-manager \
-c manager \
-n nginx-operator-system
Clean up.
Run the following command to clean up the resources that have been created as part of this procedure:
$ make undeploy
See Operator SDK tutorial for helm-based Operators for a more in-depth walkthrough on building a helm-based Operator.