0
votes

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?

1
Why do you want to have the pod running on a specific node? - Thomas
V1PodSpec has nodeName, but I have not tested it. Let me know if that works, please. - cj rogers
@Thomas I want to manage a platform using k8s ;), so there are specific Pods that have certain behavior on a specific node.. that is - Smaillns

1 Answers

0
votes

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);