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

传入参数:

kubernetes api微服务开发--pod创建与删除_第1张图片

执行:

kubernetes api微服务开发--pod创建与删除_第2张图片

kubectl get pods -n test-caoxin 查看


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

kubernetes api微服务开发--pod创建与删除_第3张图片

(2)删除pod

传入参数

kubernetes api微服务开发--pod创建与删除_第4张图片

执行

kubernetes api微服务开发--pod创建与删除_第5张图片

查看


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


你可能感兴趣的:(Kubernetes)