I am creating a kubernetes cluster using kubeadm. How can I deploy the pod on a specific node. below the code that've used to deploy the pod in a simple minikube cluster. Thanks
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);
How can we use fully the kubectl functionalities inside a kubeadm cluster using the K8S Client Api in Java?
You can use node selectors or node affinity or anti-affinity depending on what you are trying to do.
An example for using a node selector is below (assuming that you added the label named "nodeLabelKey" with value "nodeLabelValue" to your node):
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);