注:springBoot项目操作k8s api。

1.pom文件

增加以下依赖

        <!-- java操作k8s-->
        <dependency>
            <groupId>io.fabric8</groupId>
            <artifactId>kubernetes-client</artifactId>
            <version>4.0.0</version>
        </dependency>

2.yml文件

在这里插入图片描述

3. k8s初始化

import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * k8s初始化

 */
@Component
public class K8sInit {

    /**
     * 连接地址
     */
    @Value("${k8s.server}")
    private String apiHttps;

    /**
     * 安全证书相关
     */
    @Value("${k8s.certificate-authority-data}")
    private String caCert;

    @Value("${k8s.client-certificate-data}")
    private String clientCert;

    @Value("${k8s.client-key-data}")
    private String clientKey;

    /**
     *
     * k8s初始化
     * @return
     */
    public KubernetesClient getFabric8Connection() {
        Config config = new ConfigBuilder().withMasterUrl(apiHttps).withTrustCerts(true)
                .withCaCertData(caCert)
                .withClientCertData(clientCert)
                .withClientKeyData(clientKey)
                .build();
        KubernetesClient kubernetesClient = new DefaultKubernetesClient(config);
        return kubernetesClient;
    }
}

4. service&&serviceImpl

import io.fabric8.kubernetes.api.model.PodList;
/**
 * pod服务
 */
public interface PodService {
    /**
     * nameSpace不为空,获取指定命名空间下pod列表;nameSpace为空,获取所有
     * @param nameSpace
     * @return
     */
    public PodList getPodListByNamespace(String nameSpace);
}
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * pod服务实现
 */
@Service
public class PodServiceImpl implements PodService {

    @Autowired
    private K8sInit k8sInit;

    /**
     * nameSpace不为空,获取指定命名空间下pod列表;nameSpace为空,获取所有
     *
     * @param nameSpace
     * @return
     */
    @Override
    public PodList getPodListByNamespace(String nameSpace) {
        KubernetesClient kubernetesClient = k8sInit.getFabric8Connection();
        PodList podList = new PodList();
        if(StringUtils.isNotBlank(nameSpace)){
            podList = kubernetesClient.pods().inNamespace(nameSpace).list();
        }else {
            podList = kubernetesClient.pods().list();
        }
        return podList;
    }
}

5.comtroller

@RestController
public class PodController {

    @Autowired
    private PodService podService;

    @GetMapping("/getPodList")
    public ResultObject getPodList(String nameSpace) {
        return ResultObject.ok(podService.getPodListByNamespace(nameSpace));
    }
}
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐