Deploy a Pod and assign it a node using Java Kubernetes Client API

7/17/2019

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?

-- Smaillns
java
kubeadm
kubernetes

1 Answer

7/17/2019

You can use node selectors or node affinity or anti-affinity depending on what you are trying to do.

  • Node Name, this is the simplest way of scheduling a pod to a specific node, its usage is not recommended due to its limitations.
  • Node selectors will allow you to choose a node that has specific labels using label selectors
  • Node affinity works almost the same as node selectors but allows you to specify if the rule defined in the selector is required or preferred. Allowing the pod to be scheduled in any other node even if the constraint is not met.

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);
-- jlmayorga
Source: StackOverflow