Building a Kubernetes Operator in Quarkus
Total Page:16
File Type:pdf, Size:1020Kb
D E M 3 5 - S Building a Kubernetes Operator in Quarkus Fabian Lange VP of Engineering Instana © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. What is… Kubernetes? Container orchestration system An Operator? Kubernetes software extension to manage applications and its components Quarkus? Java framework designed for Kubernetes © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. Getting Started mvn \ io.quarkus:quarkus-maven-plugin:0.19.1:create \ -DprojectGroupId=com.instana \ -DprojectArtifactId=operator-example \ -DclassName="com.instana.operator.HelloResource" \ -Dpath="/hello" mvn \ quarkus:add-extension \ -Dextensions=quarkus-kubernetes-client Creating a Kubernetes Client package com.instana.operator; public class ClientProducer { @Produces @Singleton KubernetesClient makeDefaultK8sClient(@Named("namespace") String namespace) { return new DefaultKubernetesClient().inNamespace(namespace); } @Produces @Singleton @Named("namespace") String findMyNamespace() throws IOException { return new String(Files.readAllBytes( Paths.get("/var/run/secrets/kubernetes.io/serviceaccount/namespace"))); } } Listing and Watching Pods package com.instana.operator; public class PodLister { @Inject KubernetesClient defaultClient; void onStartup(@Observes StartupEvent e) { defaultClient.pods().list().getItems().stream() .map(PodPrinter::toString) .forEach(System.out::println); defaultClient.pods().watch(new Watcher<Pod>() { public void eventReceived(Action action, Pod pod) { System.out.println(action + " on " + PodPrinter.toString(pod)); } }); } } Building and Running mvn \ clean package -DskipTests && \ docker build -f src/main/docker/Dockerfile.jvm -t instana/demo-operator:1 . && \ kind load docker-image instana/demo-operator:1 kubectl run --image=nginx --restart=Never nginx1 kubectl apply -f operator.yaml kubectl run --image=nginx --restart=Never nginx2 NAME READY STATUS RESTARTS AGE nginx1 1/1 Running 0 23s operator-example-6fb74d98c5-5zzj9 1/1 Running 0 19s nginx2 1/1 Running 0 14s Operator Output exec java -Dquarkus.http.host=0.0.0.0 - Djava.util.logging.manager=org.jboss.logmanager.LogManager -javaagent:/opt/agent-bond/agent- bond.jar=jmx_exporter{{9779:/opt/agent-bond/jmx_exporter_config.yml}} -XX:+UseParallelGC - XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:MinHeapFreeRatio=20 - XX:MaxHeapFreeRatio=40 -XX:+ExitOnOutOfMemoryError -cp . -jar /deployments/app.jar Pod nginx1 version 3143 Pod operator-example-6989bcf5f8-j4d2d version 3457 2019-10-14 14:35:02,320 INFO [io.quarkus] (main) Quarkus 0.19.1 started in 1.866s. Listening on: http://[::]:8080 2019-10-14 14:35:02,322 INFO [io.quarkus] (main) Installed features: [cdi, kubernetes- client, resteasy] ADDED on Pod nginx2 version 4512 MODIFIED on Pod nginx2 version 4513 MODIFIED on Pod nginx2 version 4514 MODIFIED on Pod nginx2 version 4526 © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. Thank you! © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved..