pod的存活性探测 java版

废话不多说直接上代码 下面 代码中 有详细注解
注:SDK版本号 7.0.0

    /**
     * 创建带有存活探测的pod 检测方式 http :通过访问特定的接口查看是否访问成功
     * 注:该例为测试 请不要直接操作pod 请通过 Deployment DaemonSet StatefulSet 来控制 pod
     */
    @Test
    public void createNamespacePodProbe() {
        //获取对象操作
        CoreV1Api apiInstance = new CoreV1Api(DeploymentTest.getApiClient());
        String name = "test-pod";
        String namespace = "test-namespace"; // namespace
        String pretty = null; // String | If 'true', then the output is pretty printed.
        Boolean exact = null; // Boolean | Should the export be exact.  Exact export maintains cluster-specific fields like 'Namespace'. Deprecated. Planned for removal in 1.18.
        Boolean export = null; // Boolean | Should this value be exported.  Export strips fields that a user can not specify. Deprecated. Planned for removal in 1.18.
        String dryRun = null; // String | When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed
        String fieldManager = null; // String | fieldManager is a name associated with the actor or entity that is making these changes. The value must be less than or 128 characters long, and only contain printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint.
        //插入 端口暴露服务的 对应的是 Service.Spec.Selector 下的值
        Map selectLabels = new HashMap<>();
        selectLabels.put("select-name", "test-deployment");

        V1Pod body = new V1PodBuilder()
                .withMetadata(new V1ObjectMetaBuilder()
                        .withName(name)
                        .withNamespace(namespace)
                        .withLabels(selectLabels)
                        .build())
                .withSpec(new V1PodSpecBuilder()
                        .withContainers(new V1ContainerBuilder()
                                .withName("user")
                                .withImage("microservice-provider-user")
                                .withPorts(new V1ContainerPortBuilder()
                                        .withContainerPort(8011)
                                        .build())
                                //就绪检测
                                .withReadinessProbe(new V1ProbeBuilder()
                                        .withHttpGet(new V1HTTPGetActionBuilder()
                                                //访问路径
                                                .withPath("/health")
                                                //访问端口
                                                .withPort(new IntOrString(8011))
                                                .build())
                                        //启动pod后的120s后执行 就绪检测
                                        .withInitialDelaySeconds(120)
                                        //执行时间 10s 后如果返回状态码 不在 200~300内,就绪检测失败
                                        .withTimeoutSeconds(10)
                                        .build())
                                //存活性检测 
                                .withLivenessProbe(new V1ProbeBuilder()
                                        .withHttpGet(new V1HTTPGetActionBuilder()
                                                //访问路径
                                                .withPath("/health")
                                                //访问端口
                                                .withPort(new IntOrString(8011))
                                                .build())
                                        //启动pod 后 60后执行
                                        .withInitialDelaySeconds(60)
                                        //执行时间 10s 后如果返回状态码 不在 200~300内,就绪检测失败
                                        .withTimeoutSeconds(10)
                                        //检测间隔 5s
                                        .withPeriodSeconds(5)
                                        //失败后 检测成功的最小连续成功次数。 默认1,活跃度为1,最小值1
                                        .withSuccessThreshold(1)
                                        //当pod 启动成功 且检查失败时,k8s将在放弃尝试前的失败次数,放弃尝试时pod 将重启 默认值3最小值1 重启后数值重置
                                        .withFailureThreshold(5)
                                        .build())
                                .build())
                        .build())
                .build();
        try {
            V1Pod result = apiInstance.createNamespacedPod(namespace, body, pretty, dryRun, fieldManager);
            System.out.println(result);
        } catch (ApiException e) {
            System.err.println("Exception when calling CoreV1Api#createNamespacedPod");
            System.err.println("Status code: " + e.getCode());
            System.err.println("Reason: " + e.getResponseBody());
            System.err.println("Response headers: " + e.getResponseHeaders());
            e.printStackTrace();
        }
    }

你可能感兴趣的:(pod的存活性探测 java版)