{"id":1444,"date":"2023-03-23T22:12:09","date_gmt":"2023-03-23T14:12:09","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1444"},"modified":"2023-04-28T20:51:11","modified_gmt":"2023-04-28T12:51:11","slug":"jupyter-model-creation-for-kubernetes-api-microservice-development","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/23\/jupyter-model-creation-for-kubernetes-api-microservice-development\/","title":{"rendered":"Kubernetes api\u5fae\u670d\u52a1\u5f00\u53d1\u4e4bjupyter\u6a21\u578b\u521b\u5efa"},"content":{"rendered":"<p>\u76ee\u6807\uff1a\u5b8c\u6210\u6a21\u578b\u5f00\u53d1\u529f\u80fd\u4e2d\u7684Model\u521b\u5efa\uff0c\u8fd4\u56de<code>jupyter notebook<\/code>\u8bbf\u95ee\u4fe1\u606f<\/p>\n<h2>\u6982\u8ff0<\/h2>\n<p>\u4eba\u5de5\u667a\u80fd\u670d\u52a1\u4e2d\u6a21\u578b\u5f00\u53d1\u529f\u80fd\u7684\u5b9e\u73b0\u4e3b\u8981\u53c2\u8003\u91d1\u5c71\u4e91\u65b9\u6848\uff0c\u63d0\u4f9b\u4e00\u4e2a<code>tensorflow<\/code>\u5bb9\u5668\uff0c\u5e76\u901a\u8fc7<code>jupyter notebook<\/code>\u7f16\u5199\u4ee3\u7801\uff0c\u5b9e\u73b0\u6a21\u578b\u5f00\u53d1\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u672c\u6587\u4f7f\u7528\u5bb9\u5668\u955c\u50cf\u4e3a<code>jupyter\/tensorflow-notebook<\/code><\/p>\n<h2>Replication Controller\u529f\u80fd\u4fee\u6539<\/h2>\n<p>DevK8sApiService.java\u5728\u539f\u6709rc\u521b\u5efa\u4ee3\u7801\u7684\u57fa\u7840\u4e0a\uff0c\u589e\u52a0cpu\u4e0ememory\u8bbe\u5b9a\u7684\u529f\u80fd\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/\u521b\u5efaReplication Controller\npublic ReplicationController createRC(String rcName, String nsName, String lbkey, String lbvalue,\n                                      int replicas, String ctName, String imName, int cnPort,\n                                      String cpuRes, String memRes, String cpuLim, String memLim) {\n    Quantity cpuQn = new QuantityBuilder()\n            .withAmount(cpuRes)\n            .build();\n    Quantity memQn = new QuantityBuilder()\n            .withAmount(memRes)\n            .build();\n    Quantity cpuliQn = new QuantityBuilder()\n            .withAmount(cpuLim)\n            .build();\n    Quantity memliQn = new QuantityBuilder()\n            .withAmount(memLim)\n            .build();\n    ReplicationController rc = new ReplicationControllerBuilder()\n            .withApiVersion(&quot;v1&quot;)\n            .withKind(&quot;ReplicationController&quot;)\n            .withNewMetadata()\n            .withName(rcName)\n            .withNamespace(nsName)\n            .addToLabels(lbkey, lbvalue)\n            .endMetadata()\n            .withNewSpec()\n            .withReplicas(replicas)\n            .addToSelector(lbkey, lbvalue)\n            .withNewTemplate()\n            .withNewMetadata()\n            .addToLabels(lbkey, lbvalue)\n            .endMetadata()\n            .withNewSpec()\n            .addNewContainer()\n            .withName(ctName)\n            .withImage(imName)\n            .addNewPort()\n            .withContainerPort(cnPort)\n            .endPort()\n            .withNewResources()\n            .addToRequests(&quot;cpu&quot;, cpuQn)\n            .addToRequests(&quot;memory&quot;, memQn)\n            .addToLimits(&quot;cpu&quot;, cpuliQn)\n            .addToLimits(&quot;memory&quot;, memliQn)\n            .endResources()\n            .endContainer()\n            .endSpec()\n            .endTemplate()\n            .endSpec()\n            .build();\n    try {\n        kubernetesClient.replicationControllers().create(rc);\n        System.out.println(&quot;replication controller create success&quot;);\n    } catch (Exception e) {\n        System.out.println(&quot;replication controller create failed&quot;);\n    }\n    return rc;\n}<\/code><\/pre>\n<p>\u5176\u4e2drequest\u4e3a\u521d\u59cb\u8fd0\u884c\u65f6\u5360\u7528\u7684\u8d44\u6e90\u503c\uff0climit\u4e3a\u6700\u5927\u80fd\u4f7f\u7528\u7684\u8d44\u6e90\u503c\u3002<\/p>\n<p>\u5c06\u65b0\u589e\u53c2\u6570\u52a0\u5165DevK8sApiController.java<\/p>\n<pre><code class=\"language-java\">\/\/k8s rc create\n@RequestMapping(value = &quot;\/createRCWithinQuantity&quot;, method = RequestMethod.POST)\npublic ReplicationController createK8sRC(@RequestParam(value = &quot;ReplicationControllerName&quot;) String rcName,\n                                         @RequestParam(value = &quot;NamespaceName&quot;) String nsName,\n                                         @RequestParam(value = &quot;LabelKey&quot;) String lbkey,\n                                         @RequestParam(value = &quot;LabelValue&quot;) String lbvalue,\n                                         @RequestParam(value = &quot;Replicas&quot;) int replicas,\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;CpuR&quot;) String cpurName,\n                                         @RequestParam(value = &quot;MemR&quot;) String memrName,\n                                         @RequestParam(value = &quot;CpuL&quot;) String cpulName,\n                                         @RequestParam(value = &quot;MemL&quot;) String memlName) {\n    return devK8sApiService.createRC(rcName, nsName, lbkey, lbvalue, replicas, ctName, imName, cnPort, cpurName, memrName, cpulName, memlName);\n}<\/code><\/pre>\n<h2>TensorFlow Jupyter Notebook\u5e94\u7528\u521b\u5efa<\/h2>\n<p>\u4fee\u6539\u5b8c\u6210\u540e\u91cd\u65b0\u6784\u5efa\u9879\u76ee\uff0c\u542f\u52a8\u5e94\u7528<\/p>\n<h3>\u521b\u5efareplication controller<\/h3>\n<p><code>POST<\/code>: <code>http:\/\/127.0.0.1:8080\/k8s\/createRCWithinQuantity<\/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;ReplicationControllerName&quot;:&quot;tf-nb-rc&quot;, &quot;LabelKey&quot;:&quot;name&quot;, &quot;LabelValue&quot;:&quot;tf-nb&quot;, &quot;Replicas&quot;:&quot;1&quot;, &quot;ContainerName&quot;:&quot;tf-nb&quot;, &quot;ImageName&quot;:&quot;jupyter\/tensorflow-notebook&quot;, &quot;ContainerPort&quot;:&quot;8888&quot;, &quot;CpuR&quot;:&quot;1&quot;, &quot;MemR&quot;:&quot;1Gi&quot;, &quot;CpuL&quot;:&quot;1&quot;, &quot;MemL&quot;:&quot;2Gi&quot;}<\/p>\n<blockquote>\n<p>\u6ce8\uff1a<code>jupyter\/tensorflow-notebook<\/code>\u7684<code>ContainerPort<\/code>\u5fc5\u987b\u4e3a<code>8888<\/code><\/p>\n<\/blockquote>\n<pre><code class=\"language-json\">{\n    &quot;apiVersion&quot;: &quot;v1&quot;,\n    &quot;kind&quot;: &quot;ReplicationController&quot;,\n    &quot;metadata&quot;: {\n        &quot;annotations&quot;: {},\n        &quot;labels&quot;: {\n            &quot;name&quot;: &quot;tf-nb&quot;\n        },\n        &quot;name&quot;: &quot;tf-nb-rc&quot;,\n        &quot;namespace&quot;: &quot;appblog&quot;\n    },\n    &quot;spec&quot;: {\n        &quot;replicas&quot;: 1,\n        &quot;selector&quot;: {\n            &quot;name&quot;: &quot;tf-nb&quot;\n        },\n        &quot;template&quot;: {\n            &quot;metadata&quot;: {\n                &quot;annotations&quot;: {},\n                &quot;labels&quot;: {\n                    &quot;name&quot;: &quot;tf-nb&quot;\n                }\n            },\n            &quot;spec&quot;: {\n                &quot;containers&quot;: [\n                    {\n                        &quot;image&quot;: &quot;jupyter\/tensorflow-notebook&quot;,\n                        &quot;name&quot;: &quot;tf-nb&quot;,\n                        &quot;ports&quot;: [\n                            {\n                                &quot;containerPort&quot;: 8888\n                            }\n                        ],\n                        &quot;resources&quot;: {\n                            &quot;limits&quot;: {\n                                &quot;cpu&quot;: &quot;1&quot;,\n                                &quot;memory&quot;: &quot;2Gi&quot;\n                            },\n                            &quot;requests&quot;: {\n                                &quot;cpu&quot;: &quot;1&quot;,\n                                &quot;memory&quot;: &quot;1Gi&quot;\n                            }\n                        }\n                    }\n                ],\n                &quot;nodeSelector&quot;: {}\n            }\n        }\n    }\n}<\/code><\/pre>\n<p>\u5728master\u8282\u70b9\u4e0a\u67e5\u770brc\u4fe1\u606f\uff1a<\/p>\n<pre><code>[root@k8s-master ~]# kubectl get pods -n appblog\nNAME                    READY   STATUS    RESTARTS   AGE\ntf-nb-rc-vs52b          1\/1     Running   0          9m2s\n[root@k8s-master ~]# kubectl describe rc tf-nb-rc -n appblog\nName:         tf-nb-rc\nNamespace:    appblog\nSelector:     name=tf-nb\nLabels:       name=tf-nb\nAnnotations:  &lt;none&gt;\nReplicas:     1 current \/ 1 desired\nPods Status:  0 Running \/ 1 Waiting \/ 0 Succeeded \/ 0 Failed\nPod Template:\n  Labels:  name=tf-nb\n  Containers:\n   tf-nb:\n    Image:      jupyter\/tensorflow-notebook\n    Port:       8888\/TCP\n    Host Port:  0\/TCP\n    Limits:\n      cpu:     2\n      memory:  2Gi\n    Requests:\n      cpu:        1\n      memory:     1Gi\n    Environment:  &lt;none&gt;\n    Mounts:       &lt;none&gt;\n  Volumes:        &lt;none&gt;\nEvents:\n  Type    Reason            Age   From                    Message\n  ----    ------            ----  ----                    -------\n  Normal  SuccessfulCreate  10s   replication-controller  Created pod: tf-nb-rc-vs52b<\/code><\/pre>\n<h3>\u521b\u5efaservice<\/h3>\n<p><code>POST<\/code>: <code>http:\/\/127.0.0.1:8080\/k8s\/createService<\/code><br \/>\n<code>Content-Type<\/code>: <code>application\/x-www-form-urlencoded<\/code><br \/>\n<code>Body<\/code>: {&quot;ServiceName&quot;:&quot;tf-nb-service&quot;, &quot;NameSpaceName&quot;:&quot;appblog&quot;, &quot;LabelKey&quot;:&quot;name&quot;, &quot;LabelValue&quot;:&quot;tf-nb&quot;, &quot;ContainerPort&quot;:&quot;80&quot;, &quot;NodePort&quot;:&quot;30050&quot;}<\/p>\n<pre><code class=\"language-json\">{\n    &quot;apiVersion&quot;: &quot;v1&quot;,\n    &quot;kind&quot;: &quot;Service&quot;,\n    &quot;metadata&quot;: {\n        &quot;annotations&quot;: {},\n        &quot;labels&quot;: {\n            &quot;name&quot;: &quot;tf-nb&quot;\n        },\n        &quot;name&quot;: &quot;tf-nb-service&quot;,\n        &quot;namespace&quot;: &quot;appblog&quot;\n    },\n    &quot;spec&quot;: {\n        &quot;ports&quot;: [\n            {\n                &quot;nodePort&quot;: 30050,\n                &quot;port&quot;: 8888\n            }\n        ],\n        &quot;selector&quot;: {\n            &quot;name&quot;: &quot;tf-nb&quot;\n        },\n        &quot;type&quot;: &quot;NodePort&quot;\n    }\n}<\/code><\/pre>\n<p>\u5728master\u8282\u70b9\u4e0a\u67e5\u770bservice\u4fe1\u606f\uff1a<\/p>\n<pre><code>[root@k8s-master ~]# kubectl get service tf-nb-service -n appblog\nNAME            TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE\ntf-nb-service   NodePort   10.98.57.109   &lt;none&gt;        8888:30050\/TCP   24s<\/code><\/pre>\n<p>\u8bbf\u95ee\uff1a<code>http:\/\/NodeIP:30050<\/code><\/p>\n<p>\u9700\u8981\u83b7\u53d6notebook\u7684token\u624d\u80fd\u8bbf\u95ee\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u8fd0\u7ef4\/jupyter\u767b\u9646.png\" alt=\"jupyter\u767b\u9646\" \/><\/p>\n<h2>\u83b7\u53d6notebook token<\/h2>\n<p>\u7f16\u5199token\u83b7\u53d6\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/token\u67e5\u8be2\npublic Map&lt;String, String&gt; readToken(String nsName, String rcName, String lbKey, String lbValue) {\n    Map&lt;String, String&gt; resourceInfo = new HashMap&lt;String, String&gt;();\n\n    try {\n        \/\/ExecWatch tokenResult = kubernetesClient.pods().inNamespace(nsName).withName(rcName).exec(&quot;jupyter notebook list&quot;);\n        PodList podList = kubernetesClient.pods().inNamespace(nsName).withLabel(lbKey, lbValue).list();\n        for (Pod pod : podList.getItems()) {\n            String podName = pod.getMetadata().getName();\n            String podLog = kubernetesClient.pods().inNamespace(nsName).withName(podName).getLog();\n            resourceInfo.put(podName + &quot;token: &quot;, podLog);\n        }\n        \/\/resourceInfo.put(&quot;token: &quot;, tokenResult.toString());\n        log.error(&quot;model token get success&quot;);\n    } catch (Exception e) {\n        log.error(&quot;model token get failed&quot;);\n        log.error(&quot;&quot;, e);\n    }\n    return resourceInfo;\n}<\/code><\/pre>\n<p>\u56e0\u6267\u884cexec\u4f1a\u6709\u8ba4\u8bc1\u7684\u95ee\u9898\uff0c\u6b64\u5904\u4f7f\u7528getLog\u7684\u65b9\u6cd5\u83b7\u53d6\u542f\u52a8\u65e5\u5fd7\uff0c\u5728\u65e5\u5fd7\u4e2d\u67e5\u8be2token\u3002<\/p>\n<p>\u540c\u65f6\u4fee\u6539DevK8sApiController.java<\/p>\n<pre><code class=\"language-java\">\/\/token read\n@RequestMapping(value = &quot;\/readToken&quot;, method = RequestMethod.GET)\npublic Map&lt;String, String&gt; readJNtoken(@RequestParam(value = &quot;NamespaceName&quot;) String nsName,\n                                       @RequestParam(value = &quot;ReplicationControllerName&quot;) String rcName,\n                                       @RequestParam(value = &quot;LabelKey&quot;) String lbkey,\n                                       @RequestParam(value = &quot;LabelValue&quot;) String lbvalue) {\n    return devK8sApiService.readToken(nsName, rcName, lbkey, lbvalue);\n}<\/code><\/pre>\n<p>\u56e0\u4e3arc\u521b\u5efa\u7684pod\u90fd\u4f1a\u968f\u673a\u751f\u62105\u4f4d\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5\u901a\u8fc7Label\u67e5\u8be2pod\u540d\u79f0\uff0c\u5e76\u83b7\u53d6\u65e5\u5fd7\u3002<\/p>\n<p><code>URL<\/code>: <a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/127.0.0.1:8080\/k8s\/readToken?NamespaceName=appblog&amp;ReplicationControllerName=tf-nb-rc&amp;LabelKey=name&amp;LabelValue=tf-nb\">http:\/\/127.0.0.1:8080\/k8s\/readToken?NamespaceName=appblog&#038;ReplicationControllerName=tf-nb-rc&#038;LabelKey=name&#038;LabelValue=tf-nb<\/a><\/p>\n<pre><code class=\"language-json\">{\n    &quot;tf-nb-rc-gzz5jtoken: &quot;: &quot;Executing the command: jupyter notebook\\n[I 01:25:06.115 NotebookApp] Writing notebook server cookie secret to \/home\/jovyan\/.local\/share\/jupyter\/runtime\/notebook_cookie_secret\\n[I 01:25:07.998 NotebookApp] JupyterLab extension loaded from \/opt\/conda\/lib\/python3.7\/site-packages\/jupyterlab\\n[I 01:25:07.998 NotebookApp] JupyterLab application directory is \/opt\/conda\/share\/jupyter\/lab\\n[I 01:25:08.000 NotebookApp] Serving notebooks from local directory: \/home\/jovyan\\n[I 01:25:08.000 NotebookApp] The Jupyter Notebook is running at:\\n[I 01:25:08.000 NotebookApp] http:\/\/(tf-nb-rc-gzz5j or 127.0.0.1):8888\/?token=f94b3960fb6a1b50f06d07b1211f41b38c26a3a3ec3e9173\\n[I 01:25:08.000 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).\\n[C 01:25:08.005 NotebookApp] \\n    \\n    To access the notebook, open this file in a browser:\\n        file:\/\/\/home\/jovyan\/.local\/share\/jupyter\/runtime\/nbserver-6-open.html\\n    Or copy and paste one of these URLs:\\n        http:\/\/(tf-nb-rc-gzz5j or 127.0.0.1):8888\/?token=f94b3960fb6a1b50f06d07b1211f41b38c26a3a3ec3e9173\\n[I 01:28:48.071 NotebookApp] 302 GET \/ (192.168.0.10) 0.66ms\\n[I 01:28:48.124 NotebookApp] 302 GET \/tree? (192.168.0.10) 4.00ms\\n&quot;\n}<\/code><\/pre>\n<p>token\u4fe1\u606f\uff1a<code>token=f94b3960fb6a1b50f06d07b1211f41b38c26a3a3ec3e9173<\/code><\/p>\n<h2>\u8bbf\u95eejupyter notebook\u5e76\u6d4b\u8bd5<\/h2>\n<p>\u8f93\u5165\u4e0a\u4e00\u8282\u4e2d\u7684token\u5e76\u767b\u5f55\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u8fd0\u7ef4\/jupyter\u4e3b\u9875.png\" alt=\"jupyter\u4e3b\u9875\" \/><\/p>\n<p>\u7f16\u5199tensorflow\u6d4b\u8bd5\u7a0b\u5e8f\uff1a<\/p>\n<pre><code class=\"language-python\">import tensorflow as tf\n\nhello = tf.Variable(&#039;Hello TF&#039;)\n\nsess = tf.Session()\ninit = tf.global_variables_initializer()\n\nsess.run(init)\nsess.run(hello)<\/code><\/pre>\n<p>\u8fd0\u884c\u4ee3\u7801\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u8fd0\u7ef4\/jupyter\u7f16\u5199\u4ee3\u7801.png\" alt=\"jupyter\u7f16\u5199\u4ee3\u7801\" \/><\/p>\n<p>\u4ee5\u4e0a\uff0c\u5f00\u53d1\u6a21\u578b\u521b\u5efa\u5b8c\u6210\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u6807\uff1a\u5b8c\u6210\u6a21\u578b\u5f00\u53d1\u529f\u80fd\u4e2d\u7684Model\u521b\u5efa\uff0c\u8fd4\u56dejupyter notebook\u8bbf\u95ee\u4fe1\u606f \u6982\u8ff0 \u4eba\u5de5\u667a\u80fd\u670d\u52a1\u4e2d\u6a21 [&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-1444","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\/1444","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=1444"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1444\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}