当前位置: 首页 > 工具软件 > fabric8 > 使用案例 >

利用io.fabric8 java客户端操作k8s

宰父阳焱
2023-12-01

1、导入jar包

        <!-- https://mvnrepository.com/artifact/io.fabric8/kubernetes-client -->
        <dependency>
            <groupId>io.fabric8</groupId>
            <artifactId>kubernetes-client</artifactId>
            <version>4.2.1</version>
        </dependency>

2、创建k8s客户端

由于是多集群的k8s客户端,利用集群id做key,缓存客户端

package com.cn21.mec.meao.common.cache;

import com.cn21.mec.core.entity.api.model.Cluster;
import com.cn21.mec.core.exception.ResourceNotFoundException;
import com.cn21.mec.core.utils.BaseCache;
import com.cn21.mec.meao.dao.ClusterDao;
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.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.cn21.mec.core.constants.Constants.MEC_CLUSTER;

@Component
public class KubeClientCache extends BaseCache<Cluster, KubernetesClient> {
    private Map<Integer, KubernetesClient> kubernetesClientMap;

    @Resource
    private ClusterDao clusterDao;

    public KubeClientCache() {
        kubernetesClientMap = new HashMap<>();
    }

    @Override
    public void refresh() {
        List<Cluster> clusters = loadFromDB();

        for (Cluster cluster: clusters) {
            Config config = new ConfigBuilder()
                    .withMasterUrl(cluster.getCcpAccessAddress())
                    .withCaCertData(cluster.getAccessCaData())
                    .withClientCertData(cluster.getAccessCrtData())
                    .withClientKeyData(cluster.getAccessKeyData())
                    .build();

            this.kubernetesClientMap.put(cluster.getClusterId(), new DefaultKubernetesClient(config));
        }
    }

    @Override
    protected List<Cluster> loadFromDB() {
        return clusterDao.getAll();
    }

    @Override
    public KubernetesClient get(Object key) {
        KubernetesClient kubernetesClient = kubernetesClientMap.get((Integer) key);
        if (kubernetesClient == null) {
            throw new ResourceNotFoundException(MEC_CLUSTER, key);
        }
        return kubernetesClient;
    }

    @Override
    public void set(Object key, KubernetesClient kubernetesClient) {
        this.kubernetesClientMap.put((Integer) key, kubernetesClient);
    }

    @Override
    public void delete(Object key) {
        this.kubernetesClientMap.remove((Integer) key);
    }

    @Override
    public void setWithExpireTime(Object key, KubernetesClient kubernetesClient, long seconds) {

    }
}
package com.cn21.mec.core.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;

import java.util.List;

/**
 * Base Redis Cache
 * @author zoup
 * @version 2018-09-27
 */
public abstract class BaseCache<T, V> implements CommandLineRunner {
    @Autowired
    private CacheCenter cacheCenter;

    /**
     * 初始化
     * @author zoup
     */
    public void init() {
        cacheCenter.newCache(this);
        refresh();
    }

    /**
     * 刷新
     * @author zoup
     */
    public abstract void refresh();

    /**
     * 加载表数据
     * @author zoup
     * @return List<T> 数据库实体
     */
    protected abstract List<T> loadFromDB();

    /**
     * 读取缓存
     * @author zoup
     * @param key 缓存key
     * @return V 缓存实体
     */
    public abstract V get(Object key);

    /**
     * 写入缓存
     * @author zoup
     * @param key 缓存key
     * @param v 缓存实体
     */
    public abstract void set(Object key, V v);

    /**
     * 删除缓存
     * @param key 缓存key
     */
    public abstract void delete(Object key);

    /**
     * 写入缓存并设置过期时间,默认为String类型,时间单位为秒
     * @author zoup
     * @param key 缓存key
     * @param v 缓存实体
     */
    public abstract void setWithExpireTime(Object key, V v, long seconds);

    @Override
    public void run(String... args) throws Exception {
        init();
    }
}

3、对deployment进行操作


/* 1、设置deployment的唯一LabelSelector
*  2、设置pods的selector:orgId/serviceSetId/departmentId/deploymentName
*  3、创建,由缓存获取客户端,之后代码类似
*/
//设置deployment的唯一LabelSelector
pDeployment.getDeployment().getSpec().getSelector().getMatchLabels().put(Constants.ORG_ID,pDeployment.getDpOrgId().toString());

pDeployment.getDeployment().getSpec().getSelector().getMatchLabels().put(Constants.DEPARTMENT_ID,pDeployment.getDpDepartmentId().toString());

pDeployment.getDeployment().getSpec().getSelector().getMatchLabels().put(Constants.SERVICE_SET_ID,pDeployment.getServiceSetId().toString());

pDeployment.getDeployment().getSpec().getSelector().getMatchLabels().put(Constants.DEPLOYMENT_NAME,pDeployment.getName());


// 设置pods的selector:orgId/serviceSetId/departmentId/deploymentName

pDeployment.getDeployment().getSpec().getTemplate().getMetadata().getLabels().put(ORG_ID,pDeployment.getDpOrgId().toString());

pDeployment.getDeployment().getSpec().getTemplate().getMetadata().getLabels().put(Constants.DEPARTMENT_ID,pDeployment.getDpDepartmentId().toString());

pDeployment.getDeployment().getSpec().getTemplate().getMetadata().getLabels().put(Constants.SERVICE_SET_ID,pDeployment.getServiceSetId().toString());

pDeployment.getDeployment().getSpec().getTemplate().getMetadata().getLabels().put(Constants.DEPLOYMENT_NAME,pDeployment.getName());


// 创建
kubeClientCache.get(clusterId).apps().deployments().inNamespace(pDeployment.getNamespace()).create(deployment);

// 修改deployment
kubeClientCache.get(clusterId).apps().deployments().inNamespace(pDeployment.getNamespace()).withName(pDeployment.getName()).replace(deployment);

//获取详情
kubeClientCache.get(clusterId).apps().deployments().inNamespace(pDeployment.getNamespace()).withName(pDeploy.getName()).get();


// k8s删除 deployment rs pods,其中pods的labels为deployment中LabelSelector设置

//删除deployment,需要删除rs以及对应Labels的pods
Map<String, String> map = new HashMap<>();
map.put(ORG_ID, pDeploy.getDpOrgId().toString());
map.put(DEPARTMENT_ID, pDeploy.getDpDepartmentId().toString());
map.put(SERVICE_SET_ID, pDeploy.getServiceSetId().toString());
map.put(DEPLOYMENT_NAME, pDeploy.getName());

//删除deployment
kubeClientCache.get(clusterId).apps().deployments().inNamespace(pDeploy.getNamespace()).withName(pDeploy.getName()).delete();

//删除rs kubeClientCache.get(clusterId).apps().replicaSets().inNamespace(pDeploy.getNamespace()).withLabels(map).delete();

//删除pods
kubeClientCache.get(clusterId).pods().inNamespace(pDeploy.getNamespace()).withLabels(map).delete();

4、对service进行操作

//创建service
// 设置pods的selector :ServiceSetId作为service选择pods的唯一选择器   pService.getService().getSpec().getSelector().put(Constants.SERVICE_SET_ID,pService.getServiceSetId().toString());

pService.getService().getSpec().getSelector().put(Constants.ORG_ID,pService.getDpOrgId().toString());

pService.getService().getSpec().getSelector().put(Constants.DEPARTMENT_ID,pService.getDpDepartmentId().toString());

// 设置service的labels:orgId/departmentId/serviceSetId

pService.getService().getMetadata().getLabels().put(Constants.ORG_ID,pService.getDpOrgId().toString());

pService.getService().getMetadata().getLabels().put(Constants.DEPARTMENT_ID,pService.getDpDepartmentId().toString());

pService.getService().getMetadata().getLabels().put(Constants.SERVICE_SET_ID,pService.getServiceSetId().toString());


// 创建service
kubeClientCache.get(clusterId).services().inNamespace(pService.getNamespace()).create(service);


// 修改k8s service
kubeClientCache.get(clusterId).services().inNamespace(pService.getNamespace()).withName(redisPService.getName()).replace(service);


//获取service详情
kubeClientCache.get(clusterId).services().inNamespace(pService.getNamespace()).withName(redisPService.getName()).get();


//删除service
kubeClientCache.get(clusterId).services().inNamespace(redisPService.getNamespace()).withName(redisPService.getName()).delete();

5、对命名空间进行操作

// 创建命名空间
kubeClientCache.get(kubeBaseBean.getClusterId()).namespaces().create(namespace);

// 删除命名空间
 kubeClientCache.get(kubeBaseBean.getClusterId()).namespaces().withName(kubeBaseBean.getNamespace()).delete();

6、configMap进行操作

// 创建 configMap   其他操作与上类似
kubeClientCache.get(pConfigMap.getClusterId())
                .configMaps()
                .inNamespace(pConfigMap.getNamespace())
                .create(pConfigMap.getConfigMap());

 

 类似资料: