Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Developing Kubernetes Operators with Java Operator SDK

Developing Kubernetes Operators with Java Operator SDK

Developing Kubernetes Operators with Java Operator SDK - JavaDay Istanbul 2023

Aykut Bulgu

May 07, 2023
Tweet

More Decks by Aykut Bulgu

Other Decks in Technology

Transcript

  1. @systemcraftsman Developing Kubernetes Operators with Java Operator SDK Aykut M.

    Bulgu Principal Software Engineer @ Red Hat @SystemCraftsman
  2. @systemcraftsman Schedule • What is Kubernetes? ◦ Container Orchestration ◦

    Kubernetes Architecture ◦ The Controller Pattern • Extending Kubernetes ◦ Controllers ◦ Operators • Developing Operators ◦ Kubebuilder ◦ Operator SDK • Developing Operators with Java ◦ Java Operator SDK ◦ Quarkus Extension • Demo
  3. @systemcraftsman About Me #oc apply -f aykutbulgu.yaml apiVersion: redhat/v5 kind:

    Principal Software Engineer metadata: name: Aykut Bulgu namespace: Red Hat Kafka Engineering annotations: twitter: @systemcraftsman email: - [email protected] - [email protected] organizer: Software Craftsmanship Turkey founder: System Craftsman labels: married: yes children: 2 interests: - tech (mostly kafka) - aikido - gamification - stoicism spec: replicas: 1 containers: - image: aykut:latest
  4. @systemcraftsman $ docker build -t app/frontend:v1 . $ docker build

    -t app/backend:v1 . $ docker build -t app/database:v1 . $ docker build -t app/cache:v1 . $ docker build -t app/messaging:v1 . Container Orchestration
  5. @systemcraftsman $ docker run app/frontend:v1 link-to-backend $ docker run app/frontend:v1

    link-to-backend $ docker run app/backend:v1 link-to-db-cache-messaging $ docker run app/backend:v1 link-to-db-cache-messaging $ docker run app/database:v1 $ docker run app/cache:v1 link-to-db $ docker run app/messaging:v1 Container Orchestration
  6. @systemcraftsman We need more than just containers kubernetes ? Scheduling

    Decide where to deploy containers Lifecycle and health Keep containers running despite failures Discovery Find other containers on the network Monitoring Visibility into running containers Security Control who can do what Scaling Scale containers up and down Persistence Survive data beyond container lifecycle Aggregation Compose apps from multiple containers
  7. @systemcraftsman What is Kubernetes? Kubernetes is an open-source system for

    automating deployment, operations, and scaling of containerized applications across multiple hosts kubernetes
  8. @systemcraftsman The Control Loop In robotics and automation, a control

    loop is a non-terminating loop that regulates the state of a system. In Kubernetes, controllers are control loops that watch the state of your cluster, then make or request changes where needed.
  9. @systemcraftsman The Controller Pattern Actual Desired Action 2 replicas 3

    replicas Scale up v1.0 v2.0 Update Object Deleted Delete Observe Analyze Act
  10. @systemcraftsman Extending Kubernetes 1. Kubectl plugins 2. API Access extensions

    3. API extensions 4. Scheduling extensions 5. Controllers and Operators 6. Network plugins 7. Device and Storage plugins https://kubernetes.io/docs/concepts/extend-kubernetes/
  11. @systemcraftsman Controllers apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels:

    app: nginx spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:stable-alpine ports: - containerPort: 80
  12. @systemcraftsman Developing Controllers You can develop Controllers with any language,

    such as: To manage Kubernetes resources by using: • Labels • Annotations • ConfigMaps
  13. @systemcraftsman A Controller Written in Bash apiVersion : apps/v1 kind:

    Deployment ... name: config-watcher-controller ... spec: serviceAccountName : config-watcher-controller containers : - name: kubeapi-proxy image: k8spatterns/kubeapi-proxy - name: config-watcher image: k8spatterns/curl-jq env: - name: WATCH_NAMESPACE valueFrom: fieldRef: fieldPath: metadata.namespace command: - "sh" - "/watcher/config-watcher-controller.sh" volumeMounts : ... https://k8spatterns.io/
  14. @systemcraftsman Operators apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels:

    app: nginx spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:stable-alpine ports: - containerPort: 80 Prometheus YAML ?
  15. @systemcraftsman Operators’ Controller apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment

    labels: app: nginx spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:stable-alpine ports: - containerPort: 80 Prometheus YAML Prometheus Controller
  16. @systemcraftsman Kubebuilder $ kubebuilder init … Kubebuilder is a framework

    for building Kubernetes APIs using custom resource definitions (CRDs).
  17. @systemcraftsman Operator SDK • Ansible • Helm • Go •

    Java (Quarkus) Java Operator SDK (with Quarkus Extension)
  18. @systemcraftsman Java Operator SDK https://javaoperatorsdk.io/ Java Operator SDK is a

    higher level framework and related tooling to support writing Kubernetes Operators in Java. It makes it easy to implement best practices and patterns for an Operator. It provides a controller runtime, support for testing operators, and related tooling. In addition to that implementing conversion hooks and dynamic admission controllers are supported as a separate projects.
  19. @systemcraftsman Java Operator SDK https://github.com/java-operator-sdk/java-operator-sdk • Wraps Fabric8 and configures

    it for listening to changes on the specified Custom Resources, thus hiding the boilerplate code required for this. • Provides a clean interface to implement the reconciliation loop for a particular resource type. • Schedules change events to be executed in an efficient manner. Filtering obsolete events and executing unrelated events in parallel. • Retry failed reconciliation attempts.
  20. @systemcraftsman Reconciliation in Java Operator SDK # Reconciliation execution is

    always triggered by an event. # Events typically come from a primary resource, most of the time a custom resource. Reconciler implementations are associated with a given resource type. # No concurrent reconciliation happens for any given resource. # If an exception is thrown during execution, then it schedules a retry. # If new events are received during the controller execution, then a new reconciliation is scheduled. # If the reconcilier instructed the SDK to reschedule a reconciliation at a later date, then a timer event with the specified delay is scheduled.
  21. @systemcraftsman Reconciliation in Java Operator SDK Controller Level Eventing Level

    Reconciliation Level An Operator is a set of independent controllers. The Controller class, however, is an internal class managed by the framework itself and usually shouldn’t interacted with directly by end users. It manages all the processing units involved with reconciling a single type of Kubernetes resource.
  22. @systemcraftsman Other Features of Java Operator SDK • Finalizer Support

    • Support for Well Known (non-custom) Kubernetes Resources • Max Interval Between Reconciliations • Automatic Retries on Error • Reconciliation Rate Limiting • Dynamically Changing Target Namespaces • Automatic Generation of CRDs • …and many more at https://javaoperatorsdk.io/docs/features
  23. @systemcraftsman Quarkus Operator SDK Features • Automatically generates a main

    class • Provides CDI for the Kubernetes client • Automatically generates CRDs for all CustomResource implementations used by reconcilers • Provides a bundle generator • All Quarkus features such as: ◦ The Quarkus Dev mode ◦ Native binary generation ◦ CDI ◦ All MicroProfile Spec benefits (SmallRye is the implementation) ▪ Health Check ▪ Reactive Messaging ▪ … • For more: https://quarkiverse.github.io/quarkiverse-docs/quarkus-operator-sdk
  24. @systemcraftsman Operator SDK for Java • Ansible • Helm •

    Go • Java (Quarkus) Java Operator SDK (with Quarkus Extension)
  25. @systemcraftsman Who is Using the Java Operator SDK? • Keycloak

    Operator - JOSDK+Quarkus - https://github.com/keycloak/keycloak • Strimzi Access Operator - JOSDK - https://github.com/strimzi/kafka-access-operator • Apache Flink Kubernetes Operator - JOSDK - https://github.com/apache/flink-kubernetes-operator • Airflow Dag Operator - JOSDK+Quarkus - https://github.com/cdmikechen/airflow-dag-operator
  26. @systemcraftsman Contact & Social • Twitter: @systemcraftsman • Email: [email protected]

    • Linkedin: https://www.linkedin.com/in/mabulgu/ • Website: https://www.systemcraftsman.com/