【问题标题】:Deploy a Pod and assign it a node using Java Kubernetes Client API使用 Java Kubernetes Client API 部署 Pod 并为其分配节点
【发布时间】:2019-11-26 14:04:04
【问题描述】:

我正在使用 kubeadm 创建一个 kubernetes 集群。如何在特定节点上部署 pod。在用于在简单的 minikube 集群中部署 pod 的代码下方。谢谢

        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 功能?

【问题讨论】:

  • 为什么要让 pod 在特定节点上运行?
  • V1PodSpec 有nodeName,但我没有测试过。请让我知道这是否有效。
  • @Thomas 我想使用 k8s 管理一个平台 ;),所以有特定的 Pod 在特定节点上具有特定的行为.. 即

标签: java kubernetes kubeadm


【解决方案1】:

您可以使用node selectors or node affinity or anti-affinity,具体取决于您要执行的操作。

  • Node Name,这是将 pod 调度到特定节点的最简单方法,由于其局限性,不建议使用。
  • 节点选择器将允许您使用label selectors 选择具有特定标签的节点
  • 节点亲和性与节点选择器的工作方式几乎相同,但允许您指定选择器中定义的规则是必需的还是首选的。即使不满足约束,也允许在任何其他节点中调度 pod。

使用节点选择器的示例如下(假设您将名为“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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 2020-11-13
    • 2018-11-02
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    相关资源
    最近更新 更多