Getting Started with KubeOps
This guide will walk you through installing the necessary tools and creating your first basic Kubernetes operator using KubeOps.
Prerequisites
Before you begin, ensure you have the following installed:
- .NET SDK: KubeOps targets .NET 8.0 and later. You can check your version by running
dotnet --version
. - Docker: Required for building container images of your operator.
- Kubernetes Cluster: A running Kubernetes cluster is needed to deploy and test your operator. Local options include:
- kubectl: The Kubernetes command-line tool for interacting with your cluster.
1. Install KubeOps CLI
The KubeOps CLI is a .NET Tool that helps scaffold new operator projects and generate Kubernetes manifests.
It's recommended to install it as a local tool within your solution or project directory. This pins the tool version to your project, ensuring reproducible builds.
First, if you don't already have one, create a tool manifest file in your solution's root directory:
dotnet new tool-manifest
Then, install the KubeOps CLI tool:
dotnet tool install KubeOps.Cli
Verify the installation:
dotnet kubeops --version # You might need to be in a directory with the tool-manifest
Note: You can also install the CLI globally (
dotnet tool install -g KubeOps.Cli
), but local installation is generally preferred for better project dependency management.
2. Install KubeOps Templates
KubeOps provides project templates to quickly scaffold a new operator. Install them using:
dotnet new install KubeOps.Templates
3. Create Your First Operator Project
Now, use the KubeOps template to create a new operator project. Choose a directory for your project and run:
dotnet new operator -n MyFirstOperator
This command creates a new solution (MyFirstOperator.sln
) using the template, typically including:
MyFirstOperator.Operator
: The main operator project containing theProgram.cs
entry point, controllers, finalizers, and webhook handlers.MyFirstOperator.Entities
: A separate project to define your Custom Resource entities (CRDs).MyFirstOperator.Test
: A unit testing project.
4. Understand the Project Structure
Entities/V1DemoEntity.cs
: An example Custom Resource definition.Operator/Controller/V1DemoEntityController.cs
: An example controller that watches for changes toV1DemoEntity
resources.Operator/Program.cs
: The main entry point that configures and runs the operator using theOperatorBuilder
.
5. Run the Operator Locally
Navigate into the operator project directory:
cd MyFirstOperator/MyFirstOperator.Operator
Run the operator using the .NET CLI:
dotnet run
This command builds and runs your operator project directly. It will connect to the Kubernetes cluster configured in your current kubectl
context (usually found in ~/.kube/config
). The KubeOps runtime automatically attempts to install the V1DemoEntity
CRD (defined in MyFirstOperator.Entities
) if it doesn't already exist in the cluster.
The operator will start, connect to your currently configured Kubernetes cluster (check kubectl config current-context
), install the necessary CRD (V1DemoEntity
), and begin watching for resources.
Note: After making changes to your entities or adding RBAC requirements (using
[EntityRbac]
attributes), you can generate the corresponding Kubernetes YAML manifests using the CLI:
dotnet kubeops generate crd -o ./deployment
(Generates CRD files)dotnet kubeops generate operator -o ./deployment
(Generates RBAC - Role/ClusterRole, ServiceAccount, etc.)
Congratulations! You've created and run your first basic KubeOps operator.
Next Steps:
- Learn more about defining Custom Entities.
- Dive deeper into writing Controller Logic.
- Understand how to build a container image and Deploy your Operator to your cluster.
Full Example
For a complete, runnable example demonstrating these concepts, refer to the main example operator project in the GitHub repository:
examples/Operator