kubernetes api微服务开发--pod创建与删除

目标:完成Pod创建删除服务的开发

环境: IntelliJ IDEA

步骤:Kubernetes Client简介->Kubernetes Pod YAML文件说明->Pod服务编写->运行测试

1.Kubernetes Client简介

kubernetes client为fabric8框架下的kubernetes api封装,提供了多种操作类,可调用API  Server完成相应功能。

Github地址:

https://github.com/fabric8io/kubernetes-client.git

2.Kubernetes Pod YAML文件说明

首先在kubernetes master上编写Pod的YAML文件,测试Pod的创建删除功能是否可用。

vim Pod-test.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx-2018067
  namespace: test-caoxin
spec:
  containers:
  - name: nginx-20180607
    image: nginx
    ports:
    - containerPort: 80
      hostPort: 30011

其中kind表示需要使用的类别,metadata为基本信息,spec包含容器信息。

在Master节点上执行:

kubectl create -f Pod-test.yaml

创建pod

kubectl delete -f Pod-test.yaml

删除pod

3.Pod服务编写

在上一节的项目中新增pod创建与删除代码:

(1)服务类-DevK8sApiService.java

//创建Pod
public static Pod createPod(String nameSpace, String podName, String containerName, String imageName, int cnPort, int htPort){
    //ObjectMeta 配置
    ObjectMeta objectMeta = new ObjectMetaBuilder().
            withName(podName).
            withNamespace(nameSpace).
            build();
    //Container 端口配置
    ContainerPort containerPort = new ContainerPortBuilder().
            withContainerPort(cnPort).
            withHostPort(htPort).
            build();
    //Container 配置
    Container container = new ContainerBuilder().
            withName(containerName).
            withImage(imageName).
            withPorts(containerPort).
            build();
    //Spec 配置
    PodSpec podSpec = new PodSpecBuilder().
            withContainers(container).
            build();
    //Pod 配置
    Pod pod = new PodBuilder().
            withApiVersion("v1").
            withKind("Pod").
            withMetadata(objectMeta).
            withSpec(podSpec).
            build();
    try {
        //Pod 创建
        kubernetesClient.pods().create(pod);
        System.out.println("pod create success");
    }catch (Exception e) {
        System.out.println("pod create failed");
    }
    return pod;
}
 
//删除pod
public static Pod deletePod(String namespaceName, String podName){
    Pod pod = new Pod();
    try {
        //获取要删除的pod
        pod = kubernetesClient.pods().inNamespace(namespaceName).withName(podName).get();
        //Pod 删除
        kubernetesClient.pods().inNamespace(namespaceName).withName(podName).delete();
        System.out.println("pod delete success");
    }catch (Exception e){
        System.out.println("pod create failed");
    }
    return pod;
}

(2)控制类-DevK8sApiController.java

//k8s pod create
@ApiOperation(value = "CreatePod", notes = "CreatePod")
@RequestMapping(value = "/createpod", method = RequestMethod.POST)
public Pod createk8spod(@RequestParam(value = "NameSpaceName") String nsName,
                        @RequestParam(value = "PodName") String pdName,
                        @RequestParam(value = "ContainerName") String ctName,
                        @RequestParam(value = "ImageName") String imName,
                        @RequestParam(value = "ContainerPort") int cnPort,
                        @RequestParam(value = "HostPort") int htPort){
    return devK8sApiService.createPod(nsName,pdName,ctName,imName, cnPort, htPort);
}
 
//k8s pod delete
@ApiOperation(value = "DeletePod", notes = "DeletePod")
@RequestMapping(value = "/deletepod", method = RequestMethod.DELETE)
public Pod deletek8spod(@RequestParam(value = "NameSpaceName") String nsName,
                        @RequestParam(value = "PodName") String pdName){
    return devK8sApiService.deletePod(nsName,pdName);
}

4.运行测试

访问http://MasterIP:10099/swagger-ui.html

(1)创建pod

传入参数:


执行:


kubectl get pods -n test-caoxin 查看


以NodePort方式访问服务:http://NodeIP:30011


(2)删除pod

传入参数


执行


查看


以上,pod创建与删除功能开发完成。


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值