我正在使用kubeadm创建一个kubernetes集群。如何在特定节点上部署Pod。在用于将Pod部署到简单minikube集群中的代码下面。谢谢
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
CoreV1Api api = new CoreV1Api();
V1ObjectMeta meta = new V1ObjectMeta();
meta.name("ms2-pod");
Map<String, String> labels = new HashMap<>();
labels.put("app", "ms2-pod");
meta.labels(labels);
V1ContainerPort port = new V1ContainerPort();
port.containerPort(9090);
V1Container container = new V1Container();
container.name("ms2-container");
container.image("ms2");
container.imagePullPolicy("IfNotPresent");
container.ports(Arrays.asList(port));
V1PodSpec spec = new V1PodSpec();
spec.containers(Arrays.asList(container));
V1Pod podBody = new V1Pod();
podBody.apiVersion("v1");
podBody.kind("Pod");
podBody.metadata(meta);
podBody.spec(spec);
V1Pod pod = api.createNamespacedPod("default", podBody, null, null, null);
如何使用Java中的K8S Client Api充分利用kubeadm集群中的kubectl功能?
答案 0 :(得分:0)
您可以根据自己的尝试使用node selectors or node affinity or anti-affinity。
下面是一个使用节点选择器的示例(假设您将名为“ nodeLabelKey”的标签的值为“ nodeLabelValue”添加到您的节点):
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
CoreV1Api api = new CoreV1Api();
V1ObjectMeta meta = new V1ObjectMeta();
meta.name("ms2-pod");
Map<String, String> labels = new HashMap<>();
labels.put("app", "ms2-pod");
meta.labels(labels);
V1ContainerPort port = new V1ContainerPort();
port.containerPort(9090);
V1Container container = new V1Container();
container.name("ms2-container");
container.image("ms2");
container.imagePullPolicy("IfNotPresent");
container.ports(Arrays.asList(port));
V1PodSpec spec = new V1PodSpec();
spec.containers(Arrays.asList(container));
V1Pod podBody = new V1Pod();
podBody.apiVersion("v1");
podBody.kind("Pod");
podBody.metadata(meta);
podBody.spec(spec);
Map<String, String> nodeSelectorMap = new HashMap<>();
nodeSelectorMap.put("nodeLabelKey", "nodeLabelValue");
spec.nodeSelector(nodeSelectorMap);
V1Pod pod = api.createNamespacedPod("default", podBody, null, null, null);