0
0
Kubernetesdevops~10 mins

Operator SDK basics in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Creating and managing Kubernetes operators can be complex. Operator SDK helps you build operators easily by providing tools and libraries to automate application management on Kubernetes.
When you want to automate deployment and lifecycle management of your custom application on Kubernetes.
When you need to extend Kubernetes with your own custom resources and controllers.
When you want to package your operational knowledge into code for repeatable management.
When you want to manage complex stateful applications that require custom logic beyond standard Kubernetes resources.
When you want to build operators using Go language with scaffolding and testing support.
Config File - PROJECT
PROJECT
domain: example.com
layout: go.kubebuilder.io
projectName: my-operator
version: 3

This file defines the Operator SDK project settings.

domain: Your organization domain used for API group naming.

layout: Project layout style, here using Go with kubebuilder conventions.

projectName: The name of your operator project.

version: Operator SDK project version.

Commands
Initialize a new Operator SDK project with your domain and Go module path. This sets up the basic project structure.
Terminal
operator-sdk init --domain example.com --repo github.com/example/my-operator
Expected OutputExpected
INFO[0000] Creating new Go operator 'my-operator' version 'v1' INFO[0000] Created new project version '3' with domain 'example.com' and repo 'github.com/example/my-operator'
--domain - Sets the API group domain for your custom resources
--repo - Defines the Go module path for your operator code
Create a new API and controller for a custom resource named MyApp in group apps and version v1. This scaffolds the resource and controller code.
Terminal
operator-sdk create api --group apps --version v1 --kind MyApp --resource --controller
Expected OutputExpected
INFO[0000] Created API version apps/v1 INFO[0000] Created controller for MyApp
--group - Specifies the API group for the custom resource
--version - Specifies the API version
--kind - Defines the kind name of the custom resource
--resource - Generates the resource manifests
--controller - Generates the controller code
Install the CustomResourceDefinition (CRD) into your Kubernetes cluster so the new resource type is recognized.
Terminal
make install
Expected OutputExpected
kubectl apply -f config/crd/bases/apps.example.com_myapps.yaml customresourcedefinition.apiextensions.k8s.io/myapps.apps.example.com created
Run the operator locally to test your controller logic against a Kubernetes cluster.
Terminal
make run
Expected OutputExpected
INFO[0000] Starting the Cmd. INFO[0001] Starting controller INFO[0002] Controller is running
Verify that the CustomResourceDefinition for your operator is installed and available in the cluster.
Terminal
kubectl get crd
Expected OutputExpected
NAME CREATED AT myapps.apps.example.com 2024-06-01T12:00:00Z
Key Concept

If you remember nothing else from this pattern, remember: Operator SDK scaffolds your operator project and automates creating APIs and controllers to manage custom Kubernetes resources.

Common Mistakes
Not running 'make install' before using the custom resource
The Kubernetes cluster does not recognize the new resource type without the CRD installed, so your operator cannot manage it.
Always run 'make install' to apply the CRD before creating or managing custom resources.
Forgetting to specify --resource and --controller flags when creating the API
Without these flags, the resource manifests and controller code are not generated, so the operator lacks the logic to manage resources.
Include both --resource and --controller flags to generate all necessary code and manifests.
Running 'make run' without a configured Kubernetes cluster
The operator needs access to a Kubernetes cluster to watch and manage resources; without it, it will fail to start properly.
Ensure your kubeconfig is set and points to a running cluster before running the operator locally.
Summary
Initialize an Operator SDK project with 'operator-sdk init' to set up the structure.
Create APIs and controllers with 'operator-sdk create api' including resource and controller flags.
Install the CRD into the cluster using 'make install' so Kubernetes knows your custom resource.
Run the operator locally with 'make run' to test your controller logic.
Verify CRD installation with 'kubectl get crd' to confirm your resource is registered.