kubernetes api微服務開發--jupyter模型建立
目標:完成模型開發功能中的Model建立,返回jupyter notebook訪問資訊
環境:IntelliJ IDEA
步驟:概述->Replication Controller功能修改->TensorFlow Jupyter Notebook應用建立->獲取notebook token->訪問jupyter notebook並測試
1.概述
人工智慧服務中模型開發功能的實現主要參考金山雲方案,提供一個tensorflow容器,並通過jupyter notebook編寫程式碼,實現模型開發。
本文使用容器映象為jupyter/tensorflow-notebook
2.Replication Controller功能修改
在原有rc建立程式碼的基礎上,增加cpu與memory設定的功能:
//建立Replication Controller public static ReplicationController createRC(String rcName, String nsName, String lbkey, String lbvalue, int replicas, String ctName, String imName, int cnPort, String cpuRes, String memRes, String cpuLim, String memLim){ Quantity cpuQn = new QuantityBuilder() .withAmount(cpuRes) .build(); Quantity memQn = new QuantityBuilder() .withAmount(memRes) .build(); Quantity cpuliQn = new QuantityBuilder() .withAmount(cpuLim) .build(); Quantity memliQn = new QuantityBuilder() .withAmount(memLim) .build(); ReplicationController rc = new ReplicationControllerBuilder() .withApiVersion("v1") .withKind("ReplicationController") .withNewMetadata() .withName(rcName) .withNamespace(nsName) .addToLabels(lbkey, lbvalue) .endMetadata() .withNewSpec() .withReplicas(replicas) .addToSelector(lbkey, lbvalue) .withNewTemplate() .withNewMetadata() .addToLabels(lbkey, lbvalue) .endMetadata() .withNewSpec() .addNewContainer() .withName(ctName) .withImage(imName) .addNewPort() .withContainerPort(cnPort) .endPort() .withNewResources() .addToRequests("cpu", cpuQn) .addToRequests("memory", memQn) .addToLimits("cpu", cpuliQn) .addToLimits("memory", memliQn) .endResources() .endContainer() .endSpec() .endTemplate() .endSpec() .build(); try { kubernetesClient.replicationControllers().create(rc); System.out.println("replication controller create success"); }catch (Exception e) { System.out.println("replication controller create failed"); } return rc; }
其中request為初始執行時佔用的資源值,limit為最大能使用的資源值。
將新增引數加入DevK8sApiController.java
//k8s rc create @ApiOperation(value = "CreateReplicationController", notes = "CreateReplicationController") @RequestMapping(value = "/createrc", method = RequestMethod.POST) public ReplicationController createk8src(@RequestParam(value = "ReplicationControllerName") String rcName, @RequestParam(value = "NamespaceName") String nsName, @RequestParam(value = "LabelKey") String lbkey, @RequestParam(value = "LabelValue") String lbvalue, @RequestParam(value = "Replicas") int replicas, @RequestParam(value = "ContainerName") String ctName, @RequestParam(value = "ImageName") String imName, @RequestParam(value = "ContainerPort") int cnPort, @RequestParam(value = "CpuR") String cpurName, @RequestParam(value = "MemR") String memrName, @RequestParam(value = "CpuL") String cpulName, @RequestParam(value = "MemL") String memlName){ return devK8sApiService.createRC(rcName, nsName, lbkey, lbvalue, replicas, ctName,imName, cnPort, cpurName, memrName, cpulName, memlName); }
3.TensorFlow Jupyter Notebook應用建立
修改完成後gradle build重新構建專案,啟動應用,訪問swagger-ui。
(1)建立replication controller
傳入引數:
檢視返回結果:
在master節點上檢視rc資訊:
(2)建立service
傳入引數:
返回結果:
在master節點上檢視service資訊:
需要獲取notebook的token才能訪問。
4.獲取notebook token
編寫token獲取方法:
//token查詢
public static Map<String, String> readToken(String nsName, String rcName, String lbKey, String lbValue){
Map<String, String> resourceInfo = new HashMap<String, String>();
try {
//ExecWatch tokenResult = kubernetesClient.pods().inNamespace(nsName).withName(rcName).exec("jupyter notebook list");
PodList podList = kubernetesClient.pods().inNamespace(nsName).withLabel(lbKey, lbValue).list();
for (Pod pod:podList.getItems()){
String podName = pod.getMetadata().getName();
String podLog = kubernetesClient.pods().inNamespace(nsName).withName(podName).getLog();
resourceInfo.put(podName+"token: ", podLog);
}
//resourceInfo.put("token: ", tokenResult.toString());
System.out.println("model token get success");
}catch (Exception e){
System.out.println("model token get failed");
}
return resourceInfo;
}
因執行exec會有認證的問題,此處使用getLog的方法獲取啟動日誌,在日誌中查詢token。
同時修改DevK8sApiController.java
//token read
@ApiOperation(value = "ReadToken", notes = "ReadToken")
@RequestMapping(value = "/readtoken", method = RequestMethod.GET)
public Map<String, String> readJNtoken(@RequestParam(value = "NamespaceName") String nsName,
@RequestParam(value = "ReplicationControllerName") String rcName,
@RequestParam(value = "LabelKey") String lbkey,
@RequestParam(value = "LabelValue") String lbvalue){
return devK8sApiService.readToken(nsName, rcName, lbkey, lbvalue);
}
因為rc建立的pod都會隨機生成5位字串,所以通過Label查詢pod名稱,並獲取日誌。
傳入引數:
結果查詢:
token資訊:token=1e90f0ce7fcc6f7a5805778da7be6c122e92a6adbeec9aba
5.訪問jupyter notebook並測試
輸入上一節中的token並登入:
編寫tensorflow測試程式:
import tensorflow as tf
hello = tf.Variable('Hello TF')
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
sess.run(hello)
執行:以上,開發模型建立完成。