pod creation through kubernete api - kubernetes

I want to create pod using kubernete API from a code written in java. I went through the documentation available https://kubernetes.io/docs/api-reference/v1.6/#pod-v1-core but did not find it any helpful. I need to understand how the values are received to the respective API's and which fields are mandatory and optional while creation of pods.

From https://github.com/kubernetes/community/blob/master/contributors/devel/client-libraries.md
Java (OSGi)
Java (Fabric8, OSGi)

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

create kubernetes java client controller to watch pods

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

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.

Is there any API or go programming logic to get capacity of node in a kubernetes cluster?

Using kubectl describe nodes, i am able to get the capacity of resources(memory,cpu) of a node. I want to get the same via go client or kube API (if available). Can anyone help me out?
i am using minikube version: v1.7.2
kubectl version :
Client : GitVersion:"v1.16.3"
Server : GitVersion:"v1.16.2"
I am using metric-server to access the kubernetes resource.
Expected result:
Capacity of resources should be accessible through go program or kube API
There isn't any API call you could use to get kubectl describe nodes this is because this command is generating all the output.
Kubectl retrieves all relevant pods (every pod that isn't failed or succeeded) on a node and sums up all their resource definitions.
You can look into the code and find the function responsible for generating information about node here.
Same for collecting all requests and limits for pods, function is available here and it's called getPodsTotalRequestsAndLimits
Lastly the function what puts all that together can be seen here.
There is a really nice article about Kubernetes API: Allocatable Node Resources? The author is doing exactly what you are asking for but using Python.
Kubernetes Client libraries are the ones you need to look at https://kubernetes.io/docs/reference/using-api/client-libraries/#officially-supported-kubernetes-client-libraries
The following client libraries are officially maintained by Kubernetes SIG API Machinery.
Language Client Library
Go github.com/kubernetes/client-go/
Python github.com/kubernetes-client/python/
Java github.com/kubernetes-client/java
dotnet github.com/kubernetes-client/csharp
JavaScript github.com/kubernetes-client/javascript
Haskell github.com/kubernetes-client/haskell
Community-maintained client libraries
The following Kubernetes API client libraries are provided and maintained by their authors, not the Kubernetes team.
Language Client Library
Clojure github.com/yanatan16/clj-kubernetes-api
Go github.com/ericchiang/k8s
Java (OSGi) bitbucket.org/amdatulabs/amdatu-kubernetes
Java (Fabric8, OSGi) github.com/fabric8io/kubernetes-client
Lisp github.com/brendandburns/cl-k8s
Lisp github.com/xh4/cube
Node.js (TypeScript) github.com/Goyoo/node-k8s-client
Node.js github.com/tenxcloud/node-kubernetes-client
Node.js github.com/godaddy/kubernetes-client
Node.js github.com/ajpauwels/easy-k8s
Perl metacpan.org/pod/Net::Kubernetes
PHP github.com/maclof/kubernetes-client
PHP github.com/allansun/kubernetes-php-client
PHP github.com/travisghansen/kubernetes-client-php
Python github.com/eldarion-gondor/pykube
Python github.com/mnubo/kubernetes-py
Python github.com/tomplus/kubernetes_asyncio
Ruby github.com/Ch00k/kuber
Ruby github.com/abonas/kubeclient
Ruby github.com/kontena/k8s-client
Rust github.com/clux/kube-rs
Rust github.com/ynqa/kubernetes-rust
Scala github.com/doriordan/skuber
dotNet github.com/tonnyeremin/kubernetes_gen
DotNet (RestSharp) github.com/masroorhasan/Kubernetes.DotNet
Elixir github.com/obmarg/kazan
Elixir github.com/coryodaniel/k8s
Haskell github.com/kubernetes-client/haskell

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