On parle des Operator Kubernetes, mais de quoi s’agit-il ? Comment peut-on programmer son cluster Kubernetes et surtout, est-il possible de les écrire en Java ?
C’est ce que nous allons présenter au cours de 3 sessions dont celle-ci est la première. Dans cette session, nous allons présenter les différentes ressources de l’api REST de Kubernetes, les CRD (Custom Resource Definition), la bibliothèque fabric8 kubernetes-client et le projet exemple Hypnos.
par Charles Sabourdin
1. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
DevOps
Container Image :
“Kubernetes Operator with Java”
_
-
2. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Charles Sabourdin
https://github.com/shyrkaio - https://www.shyrka.io/
@kanedafromparis
Javaiste
Linuxien
Devoxx France
ParisJUG
OpenSource
Architect
Dev/Ops
https://github.com/kanedafromparis/
- 45:00
3. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
DevOps
Container Image :
“Kubernetes Operator with Java”
Episode I : What’s an operator
2021/10/12
4. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Kubernetes Operator with Java
Part I : just watch
Part II : java operator sdk
5. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Charles Sabourdin
https://github.com/shyrkaio - https://www.shyrka.io/
@kanedafromparis
Javaiste
Linuxien
Devoxx France
ParisJUG
OpenSource
Architect
Dev/Ops
https://github.com/kanedafromparis/
- 45:00
6. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Operators
sources : https://operatorhub.io
I. Introduction
7. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Operators
sources : https://operatorhub.io/getting-started
I. Introduction
8. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
● Agile application creation and deployment: Increased ease and efficiency
of container image creation compared to VM image use.
● Continuous development, integration, and deployment
● Dev and Ops separation of concerns
● Environmental consistency across development, testing, and production
● Application-centric management: Raises the level of abstraction from
running an OS on virtual hardware to running an application on an OS
using logical resources.
● Loosely coupled, distributed, elastic, liberated micro-services
● Resource isolation: Predictable application performance.
● Resource utilization: High efficiency and density.
Kubernetes is a portable, extensible open-source platform for managing containerized workloads and services, that facilitates both
declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely
available.
Kubernetes
sources : https://kubernetes.io/docs/concepts/architecture/cloud-controller/
https://kubernetes.io/blog/2018/07/18/11-ways-not-to-get-hacked/
I. Introduction
10. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Operators & Controller : Reconciliation loop
sources : https://speakerdeck.com/govargo/inside-of-kubernetes-controller
Actual state
.status
Desired state
.spec
Control
loop
I. Introduction
11. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Kubernetes ressources & API
sources : https://kubernetes.io/docs/reference/using-api/api-concepts/
I. Introduction
● Cluster-scoped resources:
○ GET /apis/GROUP/VERSION/RESOURCETYPE
return the collection of resources of the resource type
○ GET /apis/GROUP/VERSION/RESOURCETYPE/NAME
return the resource with NAME under the resource type
● Namespace-scoped resources:
○ GET /apis/GROUP/VERSION/RESOURCETYPE
return the collection of all instances of the resource type across all namespaces
○ GET /apis/GROUP/VERSION/namespaces/NAMESPACE/RESOURCETYPE
return collection of all instances of the resource type in NAMESPACE
○ GET /apis/GROUP/VERSION/namespaces/NAMESPACE/RESOURCETYPE/NAME
return the instance of the resource type with NAME in NAMESPACE
NAME SHORTNAMES APIVERSION NAMESPACED KIND
bindings v1 true Binding
componentstatuses cs v1 false ComponentStatus
configmaps cm v1 true ConfigMap
services svc v1 true Service
ingressclasses networking.k8s.io/v1 false IngressClass
ingresses ing networking.k8s.io/v1 true Ingress
deployments deploy apps/v1 true Deployment
…
...
12. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Kubernetes ressources & API
sources : https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/
I. Introduction
$ kubectl api-resources
# Check all possible clusters, as your .KUBECONFIG may have multiple contexts:
kubectl config view -o jsonpath='{"Cluster nametServern"}{range .clusters[*]}{.name}{"t"}{.cluster.server}{"n"}{end}'
# Select name of cluster you want to interact with from above output:
export CLUSTER_NAME="op01"
# Point to the API server referring the cluster name
APISERVER=$(kubectl config view -o jsonpath="{.clusters[?(@.name=="$CLUSTER_NAME")].cluster.server}")
# Gets the token value
TOKEN=$(kubectl get secrets -o
jsonpath="{.items[?(@.metadata.annotations['kubernetes.io/service-account.name']=='default')].data.token}"|base64 --decode)
# Explore the API with TOKEN
curl -X GET $APISERVER/api --header "Authorization: Bearer $TOKEN" --insecure
13. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
$ kubectl get ns
# Select name of cluster you want to interact with from above output:
export CLUSTER_NAME="op01"
# Point to the API server referring the cluster name
APISERVER=$(kubectl config view -o jsonpath="{.clusters[?(@.name=="$CLUSTER_NAME")].cluster.server}")
# Check all possible clusters, as your .KUBECONFIG may have multiple contexts:
curl -X GET $APISERVER/api/v1/namespaces?limit=500
--cert ${HOME}/.minikube/profiles/$CLUSTER_NAME/client.crt
--key ${HOME}/.minikube/profiles/$CLUSTER_NAME/client.key
--cacert ${HOME}/.minikube/ca.crt
| jq .items[].metadata.name
Kubernetes ressources & API
sources : https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/
I. Introduction
14. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
$ kubectl get deploy
# Create deployment resources:
kubectl apply -f src/main/hypnos-scenarios/usecase-012/012-sample-deploy.yaml
# Select name of cluster you want to interact with from above output:
export CLUSTER_NAME="op01"
# Point to the API server referring the cluster name
APISERVER=$(kubectl config view -o jsonpath="{.clusters[?(@.name=="$CLUSTER_NAME")].cluster.server}")
# Check all possible clusters, as your .KUBECONFIG may have multiple contexts:
curl -X GET $APISERVER/apis/apps/v1/namespaces/hypnos-smp-012/deployments?limit=500
--cert ${HOME}/.minikube/profiles/$CLUSTER_NAME/client.crt
--key ${HOME}/.minikube/profiles/$CLUSTER_NAME/client.key
--cacert ${HOME}/.minikube/ca.crt
| jq .items[].metadata.name
Kubernetes ressources & API
sources : https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/
I. Introduction
15. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Kubernetes Java library
I. Introduction
https://github.com/fabric8io/
kubernetes-client
https://github.com/
kubernetes-client/java
16. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
try (KubernetesClient client = new DefaultKubernetesClient()) {
...
ServiceAccount fabric8 = new ServiceAccountBuilder().withNewMetadata().withName("fabric8").endMetadata().build();
client.serviceAccounts().inNamespace(NAMESPACE).createOrReplace(fabric8);
try {
Deployment deployment = new DeploymentBuilder()
.withNewMetadata()
.withName("nginx")
.endMetadata()
….
.endSpec()
.build();
deployment = client.apps().deployments().inNamespace(NAMESPACE).create(deployment);
...
client.apps().deployments().inNamespace(NAMESPACE).withName(”nginx").scale(2, true);
...
client.resource(deployment).delete();
...
Kubernetes ressources & API
sources : https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples
/src/main/java/io/fabric8/kubernetes/examples/DeploymentExamples.java#L46
I. Introduction
17. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Operators & Controller : Reconciliation loop
sources : https://speakerdeck.com/govargo/inside-of-kubernetes-controller
Actual state
.status
Desired state
.spec
Control
loop
II. Operator and CRD
18. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Kubernetes CustomResourceDefinition (CRD)
CustomResourceDefinition represents a resource that should be exposed on the API server.
Let’ introduce Hypnos.
The purpose of `shyrka-hypnos-operator` is to create an operator that will scale up and down resources (deployment,
statefullsate, deploymentConfig).
It is written in “pure” java using fabric8, quarkus, quartz
sources : kubectl explain crd
II. Operator and CRD
19. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
En pratique (et Yaml)
II. Operator and CRD
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
...
name: hypnox.shyrkaio.github.io
spec:
group: shyrkaio.github.io
names:
kind: Hypnos
listKind: HypnosList
plural: hypnox
singular: hypnos
scope: Cluster
versions:
- ...
- name: v1alpha3
# Each version can be enabled/disabled by Served flag.
served: true
# One and only one version must be marked as the storage version.
storage: true
schema:
openAPIV3Schema:
#https://swagger.io/docs/specification/data-models/data-types/
type: object
properties:
spec:
type: object
properties:
namespaceTargetedLabel:
type: string
nullable: false
#@TODO pattern: ... or switch to a match selector
targetedLabel:
type: string
nullable: true
#@TODO pattern: ... or switch to a match selector
resourceType:
type: array
nullable: false
items:
type: string
enum: [Deployment, StatefulSet, DeploymentConfig]
...
sources : https://github.com/shyrkaio/shyrka-hypnos-operator/blob/main/src/main/jkube/hypnos.crd.yaml
20. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
En pratique (et Yaml)
II. Operator and CRD
sources : https://github.com/shyrkaio/shyrka-hypnos-operator/blob/main/src/main/jkube/hypnos.crd.yaml
21. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
En pratique (et Yaml)
II. Operator and CRD
- name: v1alpha3
# Each version can be enabled/disabled by Served flag.
served: true
# One and only one version must be marked as the storage version.
storage: true
schema:
openAPIV3Schema:
...
additionalPrinterColumns:
- name: NameSpace Targeted Label
type: string
description: The label used to select targeted namespace
jsonPath: .spec.namespaceTargetedLabel
- name: Targeted Label
type: string
description: The label used to select resource to touch
jsonPath: .spec.targetedLabel
- name: Resource
type: string
description: The resource type to be scale up / scale down
jsonPath: .spec.resourceType
- name: WakeUp
type: string
description: The Cron Definition for the wake-up call
jsonPath: .spec.wakeup-cron
- name: comments
type: string
description: some comments on this hypnos definition
jsonPath: .spec.comments
sources : https://github.com/shyrkaio/shyrka-hypnos-operator/blob/main/src/main/jkube/hypnos.crd.yaml
22. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
$ kubectl get hypox
# Create hypnox resources definition:
kubectl apply -f src/main/jkube/hypnos.crd.yaml
# Select name of cluster you want to interact with from above output:
export CLUSTER_NAME="op1"
# Point to the API server referring the cluster name
APISERVER=$(kubectl config view -o jsonpath="{.clusters[?(@.name=="$CLUSTER_NAME")].cluster.server}")
# Check all possible clusters, as your .KUBECONFIG may have multiple contexts:
curl -X GET $APISERVER/apis/shyrkaio.github.io/v1alpha3/hypnox?limit=500
--cert ${HOME}/.minikube/profiles/$CLUSTER_NAME/client.crt
--key ${HOME}/.minikube/profiles/$CLUSTER_NAME/client.key
--cacert ${HOME}/.minikube/ca.crt
| jq .items[].metadata.name
Kubernetes get new resource type
sources : https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/
I. Introduction
23. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
$ kubectl get hypox
# start quarkus in dev mode
mvn quarkus:dev
MVN quarkus:dev
sources : https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/
I. Introduction
24. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
$ mvn package
# Create shyrka-erebus-operators namespace
kubectl create ns shyrka-erebus-operators
kubens shyrka-erebus-operators
# Create local build
mvn package k8s:build -Pkubernetes
# apply server resources definition to the cluster
mvn k8s:resource -Pkubernetes
# apply server resources definition to the cluster
mvn k8s:apply -Pkubernetes
MVN create build
sources : https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/
I. Introduction
25. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Thank you
● Questions ?
- 00:00
26. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
DevOps
Container Image :
“Kubernetes Operator with Java”
Episode II : java operator sdk
2021/10/12
27. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Kubernetes Operator with Java
Part I : just watch
Part II : java operator sdk
28. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Charles Sabourdin
https://github.com/shyrkaio - https://www.shyrka.io/
@kanedafromparis
Javaiste
Linuxien
Devoxx France
ParisJUG
OpenSource
Architect
Dev/Ops
https://github.com/kanedafromparis/
- 45:00
29. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Operators
sources : https://operatorhub.io/getting-started
I. Introduction - reminder
Operators are software extensions to Kubernetes that make use of custom resources to manage applications and their components.
Operators follow Kubernetes principles, notably the control loop.
Les opérateurs sont des programmes utilisant les mécanismes d’extensions de Kubernetes (custom resources)
pour automatiser la gestion d’applications et leurs composants.
Les opérateurs respectent les principes de développement de Kubernetes, notamment la boucle de contrôle
(control loop).
Actual state
.status
Desired state
.spec
Control
loop
30. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Operators
sources : https://operatorhub.io/getting-started
I. Introduction - reminder
31. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Kubernetes resources & API
sources : https://kubernetes.io/docs/reference/using-api/api-concepts/
I. Introduction - reminder
A resource is an endpoint in the Kubernetes API that stores a collection of API objects of a certain kind;
● Cluster-scoped resources:
○ GET /apis/GROUP/VERSION/RESOURCETYPE
return the collection of resources of the resource type
○ GET /apis/GROUP/VERSION/RESOURCETYPE/NAME
return the resource with NAME under the resource type
● Namespace-scoped resources:
○ GET /apis/GROUP/VERSION/RESOURCETYPE
return the collection of all instances of the resource type across all namespaces
○ GET /apis/GROUP/VERSION/namespaces/NAMESPACE/RESOURCETYPE
return collection of all instances of the resource type in NAMESPACE
○ GET /apis/GROUP/VERSION/namespaces/NAMESPACE/RESOURCETYPE/NAME
return the instance of the resource type with NAME in NAMESPACE
Custom resources are extensions of the Kubernetes API
NAME SHORTNAMES APIVERSION NAMESPACED KIND
bindings v1 true Binding
componentstatuses cs v1 false ComponentStatus
configmaps cm v1 true ConfigMap
services svc v1 true Service
ingressclasses networking.k8s.io/v1 false IngressClass
ingresses ing networking.k8s.io/v1 true Ingress
deployments deploy apps/v1 true Deployment
…
...
32. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Java Operator SDK
sources : https://javaoperatorsdk.io/
II. Java Operator SDK
33. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Java Operator SDK
sources : https://javaoperatorsdk.io/
II. Java Operator SDK
34. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Java Operator SDK
sources : https://github.com/java-operator-sdk/java-operator-sdk
II. Java Operator SDK
35. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Tom Operator
sources : https://operatorhub.io/getting-started
I. Introduction
● Create a tomcat deployment
● Use CRD to limit configuration
● Keep the deployment event when deleted
● Create IT test
● Create GithubAction IT test (local via act)
36. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
$ mvn archetype
# create project folder
mkdir sample-operator &&
cd sample-operator
# localy install jk8ps archetype
git clone https://github.com/shyrkaio/jk8ps-mvn-archetype.git &&
cd jk8ps-mvn-archetypedefault &&
mvn clean install archetype:update-local-catalog
Maven Archetype : jk8ps-mvn-archetype
sources : https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/
I. Introduction
37. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
$ mvn archetype
# create project folder
mkdir sample-operator &&
cd sample-operator
# localy install jk8ps archetype
git clone https://github.com/shyrkaio/jk8ps-mvn-archetype.git &&
cd jk8ps-mvn-archetypedefault &&
mvn clean install archetype:update-local-catalog
Maven Archetype : jk8ps-mvn-archetype
I. Introduction
38. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Maven Archetype : jk8ps-mvn-archetype
$ mvn archetype
# create project folder
cd ../
mkdir tomop &&
cd tomop
# localy install jk8ps archetype
mvn archetype:generate -DarchetypeGroupId=io.github.shyrkaio.archetypes
-DarchetypeArtifactId=default
-DarchetypeVersion=0.0.1-SNAPSHOT
-DgroupId=io.github.shyrkaio.sample
-DartifactId=tom
-Dversion=1.0-SNAPSHOT
-Dcrd-name=Tom
-Dcrd-name-lowercase=tom
I. Introduction
https://github.com/kanedafromparis/khool-simpleoperator
39. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Tom Operator
I. Introduction
● create a tomcat deployment and it’s service
● only take tomcat version in it’s CRD
● keep the deployment
https://github.com/kanedafromparis/khool-simpleoperator
40. Kubernetes Operator with Java
(operators in java)
2021/11/18
#J2K
Thank you
● Questions ?
- 00:00