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

Implementing Kubernetes operators in Java with Micronaut

Implementing Kubernetes operators in Java with Micronaut

Micronaut is a modern, JVM-based, full stack Java framework designed for building modular, easily testable JVM applications

Micronaut supports a wide range of technologies, one of which is Kubernetes, allowing application developers to write cloud-native applications that are to be deployed on a Kubernetes cluster, and can benefit from features such as distributed configuration and service discovery. One of the features of the Micronaut Kubernetes module is the built-in support for implementing Kubernetes operators.

In this talk, Micronaut committer and Micronaut Kubernetes author Álvaro Sánchez-Mariscal, will demonstrate how to implement Kubernetes operators in Java with Micronaut. There will be a demo that includes the following:

- An application written in Micronaut, whose purpose is being deployed with a Kubernetes operator itself.
- Another Micronaut application that contains the Kubernetes operator logic, and will create Kubernetes deployments of the above applications.
- A Kubernetes custom resource definition (CRD) that will be used to manage the deployment of the first application.

Álvaro Sánchez-Mariscal

October 05, 2022
Tweet

More Decks by Álvaro Sánchez-Mariscal

Other Decks in Programming

Transcript

  1. Implementing Kubernetes operators in Java with the Micronaut® framework Álvaro

    Sánchez-Mariscal Principal Member of Technical Staff Oracle @alvaro_sanchez
  2. Álvaro Sánchez-Mariscal Principal Member of Technical Staff, Oracle Labs •

    Coming from Madrid 🇪🇸 • Developer since 2001 (Java ☕ stack) • Micronaut core developer since its inception (2017). • Author: Maven Plugin, Object Storage, Kubernetes, Cache. • Others: Core, Gradle, AWS, GCP, Azure, Security, Test, etc. • Currently at Oracle Labs: • Micronaut and Graal Cloud Native (GCN). • GraalVM Native Build Tools. • Speaker at conferences: Devoxx, GeeCON, JavaLand, JavaZone, jPrime, Codemotion, Commit Conf, etc. 🇪🇸 🇩🇪 🇮🇹 🇬🇧 🇺🇸 🇨🇿 🇵🇱 🇩🇰 🇧🇪 🇳🇴 🇷🇴 🇫🇷 🇱🇺 🇲🇦 🇷🇸 🇨🇭 🇧🇬 @alvaro_sanchez
  3. https://github.com/alvarosanchez/micronaut-kubernetes-demo Demo code available on GitHub Copyright © 2021, Oracle

    and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 4
  4. Copyright © 2022, Oracle and/or its affiliates. Micronaut® is a

    registered trademark of Object Computing, Inc. Use is for referential purposes and does not imply any endorsement or affiliation with any third-party product. 5
  5. Introduction to Micronaut Copyright © 2022, Oracle and/or its affiliates

    6 Micronaut is a complete solution for any type of application: microservices, message-driven producers or consumers, command-line apps, serverless functions, etc. All application types Micronaut leverages Java annotation processors and other optimisations to compute the framework infrastructure at compile- time, drastically reducing startup time and memory consumption. Highly optimised Modern Java Framework Micronaut has been designed from scratch in 2017, focused on modern architectures like microservices and serverless, and with the cloud in mind. It is completely OSS with an Apache 2 license.
  6. Micronaut computes at build time: • All dependency and configuration

    injection. • Annotation metadata (meta annotations) • AOP proxies. • Bean introspections. • All other framework infrastructure. At runtime: • No reflection. • No proxy generation. • No dynamic classloading. • No classpath scanning. AOT: Ahead Of Time Copyright © 2022, Oracle and/or its affiliates 7
  7. 8 Any language, build tool, test framework, reactive library, cloud,

    … Copyright © 2022, Oracle and/or its affiliates
  8. History Copyright © 2022, Oracle and/or its affiliates 9 2020

    Q1 Q2 Q3 Q4 2.0 January 2020 2021 Q1 Q2 Q3 Q4 2022 Q1 Q2 Q3 Q4 2023 Q1 Q2 Q3 Q4 3.9.2 May 2023 3.0 August 2021 4.0 Q2 2023*
  9. Micronaut features 1. Dependency injection. • Can use JSR-330 (@Inject)

    or Spring (@Autowired) annotations. 2. Configuration. • Properties, YAML, TOML, Groovy, Config4K. 3. Validation support. • Built-in: reflection free, faster startup, smaller JAR, reduced memory. • Hibernate Validation: full Bean Validation API compliance. 4. AOP: Aspect-Oriented Programming. • Compile-time, reflection free. 10 Copyright © 2022, Oracle and/or its affiliates
  10. Micronaut features: HTTP Client 12 Copyright © 2022, Oracle and/or

    its affiliates Implemented at compile time
  11. Micronaut features: message-driven applications Kafka • Producers. • Consumers. •

    Kafka Streams. RabbitMQ • Producers. • Consumers. • RPC. MQTT • Publishers. • Subscribers. NATS • Producers. • Consumers. Copyright © 2022, Oracle and/or its affiliates 13
  12. Micronaut features: data access 15 Copyright © 2022, Oracle and/or

    its affiliates Interface implemented at compile time Query generated from the method name, compile- time validated Can have custom queries, inserts, etc
  13. Micronaut features: security 1. Authentication providers. • Bult-in support for

    LDAP and OAuth 2.0 password grant flow. 2. Security rules. • Configuration or annotation-based. 3. Authorization strategies. • Basic auth, session, JWT (JWKS, JWS), X.509. 4. OAuth 2.0. • Authorization code, client credentials and password grants. • OpenID Connect. • Ease of integration with providers such as Okta, Auth0, AWS Cognito, Keycloak and more. 16 Copyright © 2022, Oracle and/or its affiliates
  14. Micronaut features: misc 1. Distributed Tracing. • Zipkin, Jaegger. 2.

    Service Discovery and Distributed Configuration. • Consul, Eureka, Kubernetes. 3. Monitoring. • Micrometer, JMX, Elasticsearch. 4. API development. • GraphQL, GRPC, Open API. 5. Containers. • Docker, Kubernetes, Testcontainers. 6. Cache. • Jcache, Redis, Ehcache, Hazelcast, Infinispan, Oracle Coherence. 7. Email. 8. Error handling. 17 Copyright © 2022, Oracle and/or its affiliates
  15. Getting Started: Micronaut Launch 18 Copyright © 2022, Oracle and/or

    its affiliates https:!//launch.micronaut.io
  16. Micronaut 4 sneak peak 1. Java 17 required. • Virtual

    threads support (Project Loom). • New HTTP Virtual threads support (Project Loom). • @Client implementation based on java.net.http.HttpClient (JEP 321) • For older versions of Java use Micronaut 3.x. • Other languages • Kotlin 1.8, KSP support. • Apache Groovy 4. 2. Micronaut Data v4 • Hibernate 6. 22 Copyright © 2022, Oracle and/or its affiliates
  17. Micronaut 4 sneak peak 3. Smaller, lighter runtime. • Performance

    closer to raw Netty (~200ms startup). • Some modules spun off core: Service Discovery, Retry, Session, Validation, Websocket. • Some dependencies now optional: Jackson, SnakeYAML. 4. Other features: • HTTP client/server filter methods: annotation-based and without reactive APIs. • Annotation-based CORS configuration. • HTTP/3 support. • Use of GraalVM metadata repository. • Prefer Micronaut Serialization over Jackson. • Disabled cloud environment deduction by default. 23 Copyright © 2022, Oracle and/or its affiliates
  18. Micronaut 4 sneak peak 5. Compile-time expression language 24 Copyright

    © 2022, Oracle and/or its affiliates Checked at compile time Checked at compile time
  19. Service discovery 1. Allows Micronaut HTTP clients to discover Kubernetes

    services (kind: Service) 2. Micronaut will query the Kubernetes API to fetch the list of services and watch for changes. • By default, will search in the current namespace, but can be configured to look in different ones. • If the service is a Multi-port one, the client can be configured to use one specific port. • Available services can be filtered by using includes/excludes lists or labels. 3. Service discovery modes: • Endpoint (default): reads from kind: Endpoints objects to compute a list of instances that are load balanced by the Micronaut HTTP client. • Service: uses the ClusterIP from the service. Load balancing is handled by Kubernetes’ kube-proxy. 30 Copyright © 2022, Oracle and/or its affiliates
  20. Distributed configuration 1. Reads Kubernetes kind: ConfigMap and kind: Secret,

    and makes them available as PropertySource’s 2. Micronaut will query the Kubernetes API to fetch the list of config maps / secrets, and watch for changes. • By default, will search in the current namespace, but can be configured to look in different ones. • Available objects can be filtered by using includes/excludes lists or labels. 3. Config maps / secrets can also be read from mounted volumes (no Kubernetes API). 32 Copyright © 2022, Oracle and/or its affiliates
  21. Health checks 33 Copyright © 2022, Oracle and/or its affiliates

    1. Probes communication with the Kubernetes API. 2. Provides some additional information about the pod where the application is running from. 3. Lists all the services discovered and their resolved URL endpoints.
  22. Kubernetes Java Client 34 Copyright © 2022, Oracle and/or its

    affiliates io.kubernetes.client.openapi.apis.CoreV1Api dependency injected
  23. Kubernetes Java Client 1. Allows you to use dependency injection

    to get a ready-to-use client object from the Official Kubernetes Java client library. 2. Supports multiple authentication options. It works out-of-the-box. Can be programmatically customised. 3. Micronaut implements reactive variants with Reactor or RxJava 2/3. 4. The underlying HTTP client can be programmatically customised if needed. 35 Copyright © 2022, Oracle and/or its affiliates
  24. Kubernetes Operator support 1. Micronaut allows you to implement the

    following Kubernetes operators building blocks: • Informers. • Reconcilers. • Leader election. 2. Allows you to work with both core and custom resource definitions (CRD). 36 Copyright © 2022, Oracle and/or its affiliates
  25. Native Image Goals Low Resource Usage Start Fast Compact Packaging

    Minimize Vulnerability Copyright © 2023, Oracle and/or its affiliates 41
  26. Micronaut and GraalVM 6ms As low as startup time 18MB

    Consuming memory footprint >30% Increased throughput up to and reduced latency
  27. Micronaut and GraalVM 1. The best integration possibly, supported by

    teams working together at Oracle Labs. • GraalVM EE includes optimisations for Micronaut for increased performance and throughput. • GraalVM EE license included in Oracle Cloud. 2. Micronaut is ready for GraalVM Native Image since day 1. • No reflection, no runtime proxies, no bytecode generation, no dynamic classloading. 3. GraalVM Extenstion Pack for Visual Studio Code. 43 Copyright © 2022, Oracle and/or its affiliates
  28. Micronaut and GraalVM powering Disney+ 44 Copyright © 2022, Oracle

    and/or its affiliates https://aws.amazon.com/blogs/opensource/improving-developer-productivity-at-disney-with-serverless-and-open-source/
  29. ABCDB: A Better Cool DataBase 1. Simple Micronaut application that

    pretends being a database product. 2. We want our customers to install it with a Kubernetes operator. • Instances will be created with a Custom Resource Definition 3. Packaged as a native executable inside a Docker image. 4. Can read configuration from a ConfigMap, and has a @Refreshable endpoint. 47 Copyright © 2022, Oracle and/or its affiliates
  30. ABCDB Operator Deployed in the cluster, reconciles the state of

    the kind: AbcDb resources. 48 Copyright © 2022, Oracle and/or its affiliates