This service provides integration with Kubernetes clusters using the Moleculer framework. It allows you to manage and interact with Kubernetes resources programmatically.
- Load Kubernetes configurations
- Execute commands in Kubernetes pods
- Watch Kubernetes resources
- Manage Kubernetes resources through Moleculer actions
The service reads Kubernetes configurations from the config
folder. Each configuration file should be named as <cluster-name>.kubeconfig
.
To run the services, install Docker and run the following command:
docker run -d --name kubernetes --restart always -v /path/to/data:/app/db -v /path/to/config:/app/config ghcr.io/flybyme/kubernetes-service:main
For Nats as the transporter
docker run -d --name kubernetes --restart always -e TRANSPORTER=nats://10.1.10.1:4222 -v /path/to/data:/app/db -v /path/to/config:/app/config ghcr.io/flybyme/kubernetes-service:main
Replace /path/to/data
with the path to your data directory.
Replace /path/to/config
with the path to your config directory.
Load a Kubernetes configuration.
- Params:
name
(string): The name of the configuration.kubeconfig
(string): The content of the kubeconfig file.
Execute a command in a Kubernetes pod.
- Params:
cluster
(string, optional): The name of the cluster. Default is 'default'.namespace
(string): The namespace of the pod.name
(string): The name of the pod.container
(string, optional): The name of the container. If not provided, the first container in the pod will be used.command
(array of strings): The command to execute.
Find one document in the database.
- Params: Query parameters to filter the documents.
Find documents in the database.
- Params: Query parameters to filter the documents.
Load a Kubernetes configuration from a kubeconfig string.
- Params:
name
(string): The name of the configuration.kubeconfig
(string): The content of the kubeconfig file.
Watch a Kubernetes API for changes.
- Params:
config
(object): The Kubernetes configuration.api
(string): The name of the API to watch.events
(array of strings): The events to watch (default: ['ADDED', 'MODIFIED', 'DELETED']).
Load a Kubernetes API client.
- Params:
api
(string): The name of the API.kc
(object): The Kubernetes configuration.
Watch all resources for a given configuration.
- Params:
config
(object): The Kubernetes configuration.
Watch a specific resource for changes.
- Params:
api
(string): The name of the API.client
(object): The Kubernetes API client.watch
(object): The Kubernetes watch client.config
(object): The Kubernetes configuration.
Load all Kubernetes configurations from the config folder.
Stop watching all resources.
Stop watching resources for a specific cluster.
- Params:
cluster
(string): The name of the cluster.
Get all methods of a class.
- Params:
className
(object): The class to get methods from.
Parse the arguments of a function.
- Params:
func
(function): The function to parse.
Flatten an object.
- Params:
ob
(object): The object to flatten.
Find one document in the database.
- Params:
query
(object): The query to filter the documents.
Find documents in the database.
- Params:
query
(object): The query to filter the documents.
Service created lifecycle event handler.
Service started lifecycle event handler.
Service stopped lifecycle event handler.
To use this service, include it in your Moleculer project and configure it according to your needs. You can then call the actions provided by the service to interact with your Kubernetes clusters.
const { ServiceBroker } = require("moleculer");
const KubernetesService = require("./services/kubernetes.service");
const broker = new ServiceBroker();
broker.createService(KubernetesService);
broker.start().then(() => {
// Load Kubernetes configuration
broker.call("kubernetes.loadKubeConfig", { name: "default", kubeconfig: "..." })
.then(() => {
console.log("Kubernetes configuration loaded");
// Execute a command in a pod
return broker.call("kubernetes.exec", {
cluster: "default",
namespace: "default",
name: "my-pod",
container: "my-container",
command: ["echo", "Hello, World!"]
});
})
.then(res => {
console.log("Command executed:", res);
// Find a document in the database
return broker.call("kubernetes.findOne", { query: { name: "my-pod" } });
})
.then(doc => {
console.log("Document found:", doc);
// Stop watching resources
return broker.call("kubernetes.stopWatching");
})
.then(() => {
console.log("Stopped watching resources");
})
.catch(err => {
console.error("Error:", err);
});
});
Ensure that your Kubernetes configurations are placed in the config
folder with the naming convention <cluster-name>.kubeconfig
. The service will automatically load these configurations on startup.
You can call the actions provided by the service to interact with your Kubernetes clusters. Here are some examples:
broker.call("kubernetes.loadKubeConfig", { name: "default", kubeconfig: "..." });
broker.call("kubernetes.exec", {
cluster: "default",
namespace: "default",
name: "my-pod",
container: "my-container",
command: ["echo", "Hello, World!"]
});
broker.call("kubernetes.findOne", { query: { name: "my-pod" } });
broker.call("kubernetes.stopWatching");