使用Kubernetes JavaScript客户端的createDeployment()
我正在尝试使用Kubernetes Java客户端创建部署或复制集。Kubernetes javascript客户端文档几乎不存在。
有什么方法可以做到这一点吗?
解决方案
假设由:
createDeployment()
- 您指的是:
createNamespacedDeployment()
您可以使用以下代码片断使用Java客户端库创建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!
You spawned: ' + amazingDeployment.metadata.name);
},
(err) => {
console.log('Oh no. Something went wrong :(');
// console.log(err) <-- Get the full output!
}
);
免责声明!
此代码假定您已经配置了
~/.kube/config
!
使用以下命令首次运行此代码:
$ 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 :(
其他资源:
- Github.com: Kubernetes-client: Javascript
相关文章