create kubernetes java client controller to watch pods - kubernetes

I want to use the kubernetes java client to create a controller (using shared informer) to watch for create, update, and delete events for pods in a specific namespace. I've found some examples that watch deployments and list nodes...but cannot find examples for pods. are there any examples that are available?

Maybe you can try the following link to understand what all things are necessary to write a custom controller and try to write your own with JAVA.
https://developers.redhat.com/blog/2019/10/07/write-a-simple-kubernetes-operator-in-java-using-the-fabric8-kubernetes-client#writing_a_simple_podset_operator_in_java

Related

Spawning pods dynamically at runtime from another pod

Is it possible for a pod to act like a spawner? When someone calls the api service in the first pod, it should spawn a new pod. This seems like a very simple thing but I cant really figure out where to look in the docs. Someone already mentioned using operators but I dont really see how that would aid me.
Im currenlty migrating a project which uses docker as a spawner to create other dockers. I somehow need this principle to work in kubernetes pods.
Kind regards
Have you looked into Kubespawner part of JupyterHub ?
I have been trying to find alternatives to Kubespawner and Kubernetes Operators might be the answer. https://kubernetes.io/docs/concepts/extend-kubernetes/operator/
GL

See Every Configuration Field (a Schema?) for Kubernetes REST Objects

I'm new to Kubernetes (K8s). It's my understanding that in order to "do things" in a kubernetes cluster, we interact with a kuberentes REST API endpoint and create/update/delete objects. When these objects are created/updated/deleted K8s will see those changes and take steps to bring the system in-line with the state of your objects.
In other words, you tell K8s you want a "deployment object" with container image foo/bar and 10 replicas and K8s will create 10 running pods with the foo/bar image. If you update the deployment to say you want 20 replicas, K8s will start more pods.
My Question: Is there a canonical description of all the possible configuration fields for these objects? That is -- tutorials liks this one do a good job of describing the simplest possible configuration to get an object like a deployment working, but now I'm curious what else it's possible to do with deployments that go beyond these hello world examples.
Is there a canonical description of all the possible configuration fields for these objects?
Yes, there is the Kubernetes API reference e.g. for Deployment.
But when developing, the easiest way is to use kubectl explain <resource> and navigate deeper, e.g:
kubectl explain Deployment.spec
and then deeper, e.g:
kubectl explain Deployment.spec.template

Being notified for changes in namespace of a pod

I have an application running on gcp. I want to set up a mechanism to be notified if there's any change in the namespace. There is an option to use kubernetes Watch to monitor any changes in namespace. But I'm looking for something to create an event or get notification to java application for such a change in namespace. I searched but could not find anything relevant, are there any options to be notified on such namespace changes?
If you are looking for forwarding to use third party app you can use plugin : botkube
If you want to create application in java you can check for respetvice client library of it in official document
https://kubernetes.io/docs/reference/using-api/client-libraries/
Java official client library for Kubernetes : https://github.com/kubernetes-client/java
This is some good example or it you can also use default Kubernetes API and write custom code and run that contained in same Kubernetes cluster to monitor any changes in namespace.
In order to do it, what I would do is deploying an application that checks if there are changes. To do it, you can use kubernetes api. You just need to install curl, instead of kubectl and the rest is restful.
curl http://localhost:8080/api/v1/namespaces/default/pods
Depending on your configuration you may need to use ssl or provide client certificate.
You should do a script with kubernetes api calls in order to check if there are changes.
I would use watches, depends on your specific use case, you can start here:
https://kubernetes.io/docs/reference/using-api/api-concepts/#efficient-detection-of-changes
https://engineering.bitnami.com/articles/kubernetes-async-watches.html
Let me know if this doesn't solve your use case, I can suggest other solutions.

How to connect applications with services created through Kubernetes operators?

I am looking for best practices to connect applications with services. I have a database operator which creates a service, and there is an application pod which needs to connect to it. Is the following approach going to work?
The operator injects the access details into the pods as Secret and ConfigMap.
The operator identifies application pods through label selectors (e.g., connects-to: mysql).
The application pod receives service access details through environment variables.
The operator can document the environment variables and the label selectors.
If the above flow is going to work, how can I inject values into pods?
I can see a few mechanisms. Which one would be better?
PodPreset (alpha since 2017)
Initializer
MutatingAdmissionWebhook
This is the expected interaction between controllers and actors (PodPreset can be substituted with other choices):
your question is not completely clear.
if I understand your question , you can use helm with multiple values file to it.
helm install ./repo/ --name repo --values ./values.file
you can also --set command to the help to add values that are not in the values file for some reason
Looking into the question it seems to general.
For example you can find more information about "CONTAINERS & KUBERNETES - Best practices for building Kubernetes Operators and stateful apps" here.
As per documentation the most important are:
Operators exercise one of the most valuable capabilities of Kubernetes—its extensibility—through the use of CRDs and custom controllers. Operators extend the Kubernetes API to support the modernization of different categories of workloads and provide improved lifecycle management, scheduling.
Please refer to "Application management made easier with Kubernetes Operators on GCP Marketplace" As you can see - in GCP was published a set of Kubernetes operators to encapsulate best practices and end-to-end solutions for specific applications.
In stack overflow you can find discussion about CRD
As an example please see postgress opeartor with comparison between two methods to set the Postgres Operator configuration here.
In this case "The CRD-based configuration is more powerful than the one based on ConfigMaps and should be used unless there is a compatibility requirement to use an already existing configuration"
Here you can also find information "When to use configMap or a custom resource?"
Hope this help

Is there a way to hook into a Kubernetes Ingress Resource change event?

I'm creating a minimal custom ingress controller in dotnet core. I can access the k8s ingress resources by querying the api server, but polling doesn't seem like a good solution to update ingress rules since in my use case they aren't changed that often but if they change, the changes should be applied right away.
Is there a way to receive notifications on ingress resource changes? Like registering a webhook or something along the lines? Or is polling the only way?
What dotnet core library are you using? There is a Watch on every resource in the golang client (e.g. ingress). You can find our more about this under "Setup a Watch" section in this article
It looks like there is a Watch example in the kubernetes-client/csharp as well.