{"id":1442,"date":"2023-03-23T22:09:37","date_gmt":"2023-03-23T14:09:37","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1442"},"modified":"2023-04-28T20:51:40","modified_gmt":"2023-04-28T12:51:40","slug":"pod-creation-and-deletion-for-kubernetes-api-microservice-development","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/23\/pod-creation-and-deletion-for-kubernetes-api-microservice-development\/","title":{"rendered":"Kubernetes api\u5fae\u670d\u52a1\u5f00\u53d1\u4e4bpod\u521b\u5efa\u4e0e\u5220\u9664"},"content":{"rendered":"<p>\u76ee\u6807\uff1a\u5b8c\u6210Pod\u521b\u5efa\u5220\u9664\u670d\u52a1\u7684\u5f00\u53d1<\/p>\n<h2>Kubernetes Client\u7b80\u4ecb<\/h2>\n<p>kubernetes client\u4e3afabric8\u6846\u67b6\u4e0b\u7684kubernetes api\u5c01\u88c5\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u64cd\u4f5c\u7c7b\uff0c\u53ef\u8c03\u7528API\u00a0 Server\u5b8c\u6210\u76f8\u5e94\u529f\u80fd\u3002<\/p>\n<p><!-- more --><\/p>\n<p>Github\u5730\u5740\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/fabric8io\/kubernetes-client.git\">https:\/\/github.com\/fabric8io\/kubernetes-client.git<\/a><\/p>\n<h2>Kubernetes Pod YAML\u6587\u4ef6\u8bf4\u660e<\/h2>\n<p>\u9996\u5148\u5728kubernetes master\u4e0a\u7f16\u5199Pod\u7684YAML\u6587\u4ef6\uff0c\u6d4b\u8bd5Pod\u7684\u521b\u5efa\u5220\u9664\u529f\u80fd\u662f\u5426\u53ef\u7528\u3002<\/p>\n<p>vim Pod-test.yaml<\/p>\n<pre><code class=\"language-yml\">apiVersion: v1\nkind: Pod\nmetadata:\n  name: nginx-1.17.0\n  namespace: appblog\nspec:\n  containers:\n  - name: nginx-1170\n    image: nginx\n    ports:\n    - containerPort: 80\n      hostPort: 30011<\/code><\/pre>\n<p>\u5176\u4e2dkind\u8868\u793a\u9700\u8981\u4f7f\u7528\u7684\u7c7b\u522b\uff0cmetadata\u4e3a\u57fa\u672c\u4fe1\u606f\uff0cspec\u5305\u542b\u5bb9\u5668\u4fe1\u606f\u3002<\/p>\n<p>\u5728Master\u8282\u70b9\u4e0a\u6267\u884c\uff1a<\/p>\n<pre><code>kubectl create -f Pod-test.yaml  \/\/\u521b\u5efapod\nkubectl delete -f Pod-test.yaml  \/\/\u5220\u9664pod<\/code><\/pre>\n<h2>Pod\u670d\u52a1\u7f16\u5199<\/h2>\n<p>\u65b0\u589epod\u521b\u5efa\u4e0e\u5220\u9664\u4ee3\u7801\uff1a<\/p>\n<p>\uff081\uff09\u670d\u52a1\u7c7b-DevK8sApiService.java<\/p>\n<pre><code class=\"language-java\">\/\/\u521b\u5efaPod\npublic Pod createPod(String nameSpace, String podName, String containerName, String imageName, int cnPort, int htPort) {\n    \/\/ObjectMeta \u914d\u7f6e\n    ObjectMeta objectMeta = new ObjectMetaBuilder().\n            withName(podName).\n            withNamespace(nameSpace).\n            build();\n    \/\/Container \u7aef\u53e3\u914d\u7f6e\n    ContainerPort containerPort = new ContainerPortBuilder().\n            withContainerPort(cnPort).\n            withHostPort(htPort).\n            build();\n    \/\/Container \u914d\u7f6e\n    Container container = new ContainerBuilder().\n            withName(containerName).\n            withImage(imageName).\n            withPorts(containerPort).\n            build();\n    \/\/Spec \u914d\u7f6e\n    PodSpec podSpec = new PodSpecBuilder().\n            withContainers(container).\n            build();\n    \/\/Pod \u914d\u7f6e\n    Pod pod = new PodBuilder().\n            withApiVersion(&quot;v1&quot;).\n            withKind(&quot;Pod&quot;).\n            withMetadata(objectMeta).\n            withSpec(podSpec).\n            build();\n    try {\n        \/\/Pod \u521b\u5efa\n        kubernetesClient.pods().create(pod);\n        log.error(&quot;pod create success&quot;);\n    } catch (Exception e) {\n        log.error(&quot;pod create failed&quot;);\n        log.error(&quot;&quot;, e);\n    }\n    return pod;\n}\n\n\/\/\u5220\u9664pod\npublic Pod deletePod(String namespaceName, String podName) {\n    Pod pod = new Pod();\n    try {\n        \/\/\u83b7\u53d6\u8981\u5220\u9664\u7684pod\n        pod = kubernetesClient.pods().inNamespace(namespaceName).withName(podName).get();\n        \/\/Pod \u5220\u9664\n        kubernetesClient.pods().inNamespace(namespaceName).withName(podName).delete();\n        log.error(&quot;pod delete success&quot;);\n    } catch (Exception e) {\n        log.error(&quot;pod delete failed&quot;);\n        log.error(&quot;&quot;, e);\n    }\n    return pod;\n}<\/code><\/pre>\n<p>\uff082\uff09\u63a7\u5236\u7c7b-DevK8sApiController.java<\/p>\n<pre><code class=\"language-java\">\/\/k8s pod create\n@RequestMapping(value = &quot;\/createPod&quot;, method = RequestMethod.POST)\npublic Pod createK8sPod(@RequestParam(value = &quot;NameSpaceName&quot;) String nsName,\n                        @RequestParam(value = &quot;PodName&quot;) String pdName,\n                        @RequestParam(value = &quot;ContainerName&quot;) String ctName,\n                        @RequestParam(value = &quot;ImageName&quot;) String imName,\n                        @RequestParam(value = &quot;ContainerPort&quot;) int cnPort,\n                        @RequestParam(value = &quot;HostPort&quot;) int htPort){\n    return devK8sApiService.createPod(nsName,pdName,ctName,imName, cnPort, htPort);\n}\n\n\/\/k8s pod delete\n@RequestMapping(value = &quot;\/deletePod&quot;, method = RequestMethod.DELETE)\npublic Pod deleteK8sPod(@RequestParam(value = &quot;NameSpaceName&quot;) String nsName,\n                        @RequestParam(value = &quot;PodName&quot;) String pdName) {\n    return devK8sApiService.deletePod(nsName, pdName);\n}<\/code><\/pre>\n<h2>\u6d4b\u8bd5\u9a8c\u8bc1<\/h2>\n<p>\uff081\uff09\u6dfb\u52a0pod<\/p>\n<p><code>POST<\/code>: <code>http:\/\/127.0.0.1:8080\/k8s\/createPod<\/code><br \/>\n<code>Content-Type<\/code>: <code>application\/x-www-form-urlencoded<\/code><br \/>\n<code>Body<\/code>: {&quot;NameSpaceName&quot;:&quot;appblog&quot;, &quot;PodName&quot;:&quot;nginx-1.17.0&quot;, &quot;ContainerName&quot;:&quot;nginx-1170&quot;, &quot;ImageName&quot;:&quot;nginx&quot;, &quot;ContainerPort&quot;:&quot;80&quot;, &quot;HostPort&quot;:&quot;30080&quot;}<\/p>\n<pre><code class=\"language-json\">{\n    &quot;apiVersion&quot;: &quot;v1&quot;,\n    &quot;kind&quot;: &quot;Pod&quot;,\n    &quot;metadata&quot;: {\n        &quot;annotations&quot;: {},\n        &quot;labels&quot;: {},\n        &quot;name&quot;: &quot;nginx-1.17.0&quot;,\n        &quot;namespace&quot;: &quot;appblog&quot;\n    },\n    &quot;spec&quot;: {\n        &quot;containers&quot;: [\n            {\n                &quot;image&quot;: &quot;nginx&quot;,\n                &quot;name&quot;: &quot;nginx-1170&quot;,\n                &quot;ports&quot;: [\n                    {\n                        &quot;containerPort&quot;: 80,\n                        &quot;hostPort&quot;: 30080\n                    }\n                ]\n            }\n        ],\n        &quot;nodeSelector&quot;: {}\n    }\n}<\/code><\/pre>\n<p>\uff082\uff09<code>kubectl get pods -n appblog<\/code>\u67e5\u770b<\/p>\n<pre><code>[root@k8s-master ~]# kubectl get pods -n appblog\nNAME           READY   STATUS    RESTARTS   AGE\nnginx-1.17.0   1\/1     Running   0          4m34s\n[root@k8s-master ~]# kubectl describe pod nginx-1.17.0 -n appblog\nName:               nginx-1.17.0\nNamespace:          appblog\nPriority:           0\nPriorityClassName:  &lt;none&gt;\nNode:               k8s-node01\/192.168.0.10\nStart Time:         Thu, 13 Jun 2019 22:19:17 +0800\nLabels:             &lt;none&gt;\nAnnotations:        cni.projectcalico.org\/podIP: 172.16.1.9\/32\nStatus:             Running\nIP:                 172.16.1.9\nContainers:\n  nginx-1170:\n    Container ID:   docker:\/\/a37264ab1de6ed7b20859370d62b39b3854449145cf347dbbb7fe78faab33128\n    Image:          nginx\n    Image ID:       docker-pullable:\/\/nginx@sha256:bdbf36b7f1f77ffe7bd2a32e59235dff6ecf131e3b6b5b96061c652f30685f3a\n    Port:           80\/TCP\n    Host Port:      30080\/TCP\n    State:          Running\n      Started:      Thu, 13 Jun 2019 22:20:07 +0800\n    Ready:          True\n    Restart Count:  0\n    Environment:    &lt;none&gt;\n    Mounts:\n      \/var\/run\/secrets\/kubernetes.io\/serviceaccount from default-token-jrbkh (ro)\nConditions:\n  Type              Status\n  Initialized       True \n  Ready             True \n  ContainersReady   True \n  PodScheduled      True \nVolumes:\n  default-token-jrbkh:\n    Type:        Secret (a volume populated by a Secret)\n    SecretName:  default-token-jrbkh\n    Optional:    false\nQoS Class:       BestEffort\nNode-Selectors:  &lt;none&gt;\nTolerations:     node.kubernetes.io\/not-ready:NoExecute for 300s\n                 node.kubernetes.io\/unreachable:NoExecute for 300s\nEvents:\n  Type    Reason     Age    From                 Message\n  ----    ------     ----   ----                 -------\n  Normal  Scheduled  4m43s  default-scheduler    Successfully assigned appblog\/nginx-1.17.0 to k8s-node01\n  Normal  Pulling    4m40s  kubelet, k8s-node01  Pulling image &quot;nginx&quot;\n  Normal  Created    3m54s  kubelet, k8s-node01  Created container nginx-1170\n  Normal  Started    3m53s  kubelet, k8s-node01  Started container nginx-1170\n[root@k8s-master ~]# <\/code><\/pre>\n<p>\u4ee5NodePort\u65b9\u5f0f\u8bbf\u95ee\u670d\u52a1\uff1a<code>http:\/\/NodeIP:30080<\/code><\/p>\n<blockquote>\n<p>\u6ce8\uff1aNodeIP\u4e3a\u8282\u70b9<code>k8s-node01\u7684IP<\/code><\/p>\n<\/blockquote>\n<p>\uff083\uff09\u5220\u9664pod<\/p>\n<p><code>DELETE<\/code>: <code>http:\/\/127.0.0.1:8080\/k8s\/deletePod<\/code><br \/>\n<code>Content-Type<\/code>: <code>application\/x-www-form-urlencoded<\/code><br \/>\n<code>Body<\/code>: {&quot;NameSpaceName&quot;:&quot;appblog&quot;, &quot;PodName:nginx-1.17.0&quot;}<\/p>\n<pre><code class=\"language-json\">{\n    &quot;apiVersion&quot;: &quot;v1&quot;,\n    &quot;kind&quot;: &quot;Pod&quot;,\n    &quot;metadata&quot;: {\n        &quot;annotations&quot;: {\n            &quot;cni.projectcalico.org\/podIP&quot;: &quot;172.16.1.9\/32&quot;\n        },\n        &quot;creationTimestamp&quot;: &quot;2019-06-13T14:19:17Z&quot;,\n        &quot;name&quot;: &quot;nginx-1.17.0&quot;,\n        &quot;namespace&quot;: &quot;appblog&quot;,\n        &quot;resourceVersion&quot;: &quot;323516&quot;,\n        &quot;selfLink&quot;: &quot;\/api\/v1\/namespaces\/appblog\/pods\/nginx-1.17.0&quot;,\n        &quot;uid&quot;: &quot;3a84aba5-8de6-11e9-b4ff-525400a204f6&quot;\n    },\n    &quot;spec&quot;: {\n        &quot;containers&quot;: [\n            {\n                &quot;image&quot;: &quot;nginx&quot;,\n                &quot;imagePullPolicy&quot;: &quot;Always&quot;,\n                &quot;name&quot;: &quot;nginx-1170&quot;,\n                &quot;ports&quot;: [\n                    {\n                        &quot;containerPort&quot;: 80,\n                        &quot;hostPort&quot;: 30080,\n                        &quot;protocol&quot;: &quot;TCP&quot;\n                    }\n                ],\n                &quot;resources&quot;: {},\n                &quot;terminationMessagePath&quot;: &quot;\/dev\/termination-log&quot;,\n                &quot;terminationMessagePolicy&quot;: &quot;File&quot;,\n                &quot;volumeMounts&quot;: [\n                    {\n                        &quot;mountPath&quot;: &quot;\/var\/run\/secrets\/kubernetes.io\/serviceaccount&quot;,\n                        &quot;name&quot;: &quot;default-token-jrbkh&quot;,\n                        &quot;readOnly&quot;: true\n                    }\n                ]\n            }\n        ],\n        &quot;dnsPolicy&quot;: &quot;ClusterFirst&quot;,\n        &quot;enableServiceLinks&quot;: true,\n        &quot;nodeName&quot;: &quot;k8s-node01&quot;,\n        &quot;priority&quot;: 0,\n        &quot;restartPolicy&quot;: &quot;Always&quot;,\n        &quot;schedulerName&quot;: &quot;default-scheduler&quot;,\n        &quot;securityContext&quot;: {},\n        &quot;serviceAccount&quot;: &quot;default&quot;,\n        &quot;serviceAccountName&quot;: &quot;default&quot;,\n        &quot;terminationGracePeriodSeconds&quot;: 30,\n        &quot;tolerations&quot;: [\n            {\n                &quot;effect&quot;: &quot;NoExecute&quot;,\n                &quot;key&quot;: &quot;node.kubernetes.io\/not-ready&quot;,\n                &quot;operator&quot;: &quot;Exists&quot;,\n                &quot;tolerationSeconds&quot;: 300\n            },\n            {\n                &quot;effect&quot;: &quot;NoExecute&quot;,\n                &quot;key&quot;: &quot;node.kubernetes.io\/unreachable&quot;,\n                &quot;operator&quot;: &quot;Exists&quot;,\n                &quot;tolerationSeconds&quot;: 300\n            }\n        ],\n        &quot;volumes&quot;: [\n            {\n                &quot;name&quot;: &quot;default-token-jrbkh&quot;,\n                &quot;secret&quot;: {\n                    &quot;defaultMode&quot;: 420,\n                    &quot;secretName&quot;: &quot;default-token-jrbkh&quot;\n                }\n            }\n        ]\n    },\n    &quot;status&quot;: {\n        &quot;conditions&quot;: [\n            {\n                &quot;lastTransitionTime&quot;: &quot;2019-06-13T14:19:17Z&quot;,\n                &quot;status&quot;: &quot;True&quot;,\n                &quot;type&quot;: &quot;Initialized&quot;\n            },\n            {\n                &quot;lastTransitionTime&quot;: &quot;2019-06-13T14:20:08Z&quot;,\n                &quot;status&quot;: &quot;False&quot;,\n                &quot;type&quot;: &quot;Ready&quot;\n            },\n            {\n                &quot;lastTransitionTime&quot;: &quot;2019-06-13T14:20:08Z&quot;,\n                &quot;status&quot;: &quot;True&quot;,\n                &quot;type&quot;: &quot;ContainersReady&quot;\n            },\n            {\n                &quot;lastTransitionTime&quot;: &quot;2019-06-13T14:19:17Z&quot;,\n                &quot;status&quot;: &quot;True&quot;,\n                &quot;type&quot;: &quot;PodScheduled&quot;\n            }\n        ],\n        &quot;containerStatuses&quot;: [\n            {\n                &quot;containerID&quot;: &quot;docker:\/\/a37264ab1de6ed7b20859370d62b39b3854449145cf347dbbb7fe78faab33128&quot;,\n                &quot;image&quot;: &quot;nginx:latest&quot;,\n                &quot;imageID&quot;: &quot;docker-pullable:\/\/nginx@sha256:bdbf36b7f1f77ffe7bd2a32e59235dff6ecf131e3b6b5b96061c652f30685f3a&quot;,\n                &quot;lastState&quot;: {},\n                &quot;name&quot;: &quot;nginx-1170&quot;,\n                &quot;ready&quot;: true,\n                &quot;restartCount&quot;: 0,\n                &quot;state&quot;: {\n                    &quot;running&quot;: {\n                        &quot;startedAt&quot;: &quot;2019-06-13T14:20:07Z&quot;\n                    }\n                }\n            }\n        ],\n        &quot;hostIP&quot;: &quot;192.168.0.10&quot;,\n        &quot;phase&quot;: &quot;Running&quot;,\n        &quot;podIP&quot;: &quot;172.16.1.9&quot;,\n        &quot;qosClass&quot;: &quot;BestEffort&quot;,\n        &quot;startTime&quot;: &quot;2019-06-13T14:19:17Z&quot;\n    }\n}<\/code><\/pre>\n<p>\u67e5\u770b<\/p>\n<pre><code>[root@k8s-master ~]# kubectl get pods -n appblog\nNo resources found.<\/code><\/pre>\n<p>\u4ee5\u4e0a\uff0cpod\u521b\u5efa\u4e0e\u5220\u9664\u529f\u80fd\u5f00\u53d1\u5b8c\u6210\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u6807\uff1a\u5b8c\u6210Pod\u521b\u5efa\u5220\u9664\u670d\u52a1\u7684\u5f00\u53d1 Kubernetes Client\u7b80\u4ecb kubernetes client [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[356],"class_list":["post-1442","post","type-post","status-publish","format-standard","hentry","category-k8s","tag-kubernetes"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1442"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1442\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}