我正在尝试使用Kubernetes Javascript客户端创建一个部署或副本集。 Kubernetes javascript客户端文档实际上不存在。
有什么办法可以做到这一点?
最佳答案
假设通过:
createDeployment()
createNamespacedDeployment()
您可以使用以下代码段通过Javascript客户端库创建
Deployment
:const k8s = require('@kubernetes/client-node');
const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const k8sApi = kc.makeApiClient(k8s.AppsV1Api); // <-- notice the AppsV1Api
// Definition of the deployment
var amazingDeployment = {
metadata: {
name: 'nginx-deployment'
},
spec: {
selector: {
matchLabels: {
app: 'nginx'
}
},
replicas: 3,
template: {
metadata: {
labels: {
app: 'nginx'
}
},
spec: {
containers: [
{
name: 'nginx',
image: 'nginx'
} ]
}
}
}
};
// Sending the request to the API
k8sApi.createNamespacedDeployment('default', amazingDeployment).then(
(response) => {
console.log('Yay! \nYou spawned: ' + amazingDeployment.metadata.name);
},
(err) => {
console.log('Oh no. Something went wrong :(');
// console.log(err) <-- Get the full output!
}
);
Disclaimer!
This code assumes that you have your
~/.kube/config
already configured!
使用以下命令首次运行此代码:
$ node deploy.js
应该输出:
Yay!
You spawned: nginx-deployment
您可以通过以下方式检查Deployment
是否存在:$ kubectl get deployment nginx-deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 6m57s
再次运行此代码将输出(部署已存在!):Oh no. Something went wrong :(
其他资源:
关于javascript - 使用Kubernetes JavaScript客户端的createDeployment(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64297139/