Cookies & Containers

Or how I learned to love the cloud.

Writing a simple K8s Operator in Java

Writing a simple K8s Operator in Java

2020-11-03-windows-kubevirt-k3s.html ยท 2020-08-31-simple-k8s-operator-java.html ยท 2020-06-25-adventures-telco-xos.html

August 31, 2020 โ€” Kubernetes Operator are often used to simplify the usage of applications or software in and outside a K8s cluster. They allow you to extend K8s by adding custom controllers for custom resources, allowing for example simpler database upgrades, application maintenance and automated creation of K8s resources.

There is a large number of Operators readily availabe, from OperatorHub or GitHub.

Why write your own?

You might be providing an application that needs certain maintenance that you want to automate. You might be providing a database to your developers that you don't want them to have to provision themselves. You might just wanna learn more about Kubernetes.

Getting started

<dependency> <groupId>com.github.containersolutions</groupId> <artifactId>operator-framework</artifactId> <version>{see https://search.maven.org/search?q=a:operator-framework for latest version}</version> </dependency> public class YourOperator { public static void main(String[] args) { Operator operator = new Operator(new DefaultKubernetesClient()); operator.registerController(new YourController()); } } FROM openjdk:12-alpine ARG JAR_FILE ADD target/${JAR_FILE} /usr/share/operator/your-operator.jar ENTRYPOINT ["java", "-jar", "/usr/share/operator/your-operator.jar"] kubectl apply -f CRD.yaml kubectl apply -f deployment.yaml

Further reading

  1. Operator Pattern: https://kubernetes.io/docs/concepts/extend-kubernetes/operator/
  2. More on CustomResources: https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/

View source

Gmail icon Twitter icon GitHub icon
Built with Scroll v31.4.1