This commit is contained in:
parent
879075474f
commit
ba1d0d53ba
|
@ -1,85 +0,0 @@
|
|||
import json
|
||||
|
||||
import yaml
|
||||
from kubernetes import client, config
|
||||
from flask import jsonify
|
||||
# Load in-cluster config
|
||||
config.load_incluster_config()
|
||||
v1 = client.CoreV1Api()
|
||||
namespace = open("/var/run/secrets/kubernetes.io/serviceaccount/namespace").read()
|
||||
IP = "36.138.114.105"
|
||||
def list_pods():
|
||||
ret = v1.list_namespaced_pod(namespace, watch=False)
|
||||
return {
|
||||
"list": [{
|
||||
"name": item.metadata.name.replace("database-dmserver-0", "student-2210502337-0-12"),
|
||||
"ip": item.status.pod_ip
|
||||
} for item in ret.items]
|
||||
}
|
||||
|
||||
def list_services():
|
||||
ret = v1.list_namespaced_service(namespace)
|
||||
return {
|
||||
"list": [
|
||||
# "name": item.metadata.name.replace("database-dmserver-0", "student-2210502337-0-12"),
|
||||
# "ip": item.spec.cluster_ip,
|
||||
# "ports": [[port.to_dict() for port in item.spec.ports]],
|
||||
#
|
||||
item.to_dict()
|
||||
# "node_port": item.spec.ports[0].node_port,
|
||||
for item in ret.items]
|
||||
}
|
||||
|
||||
def create_pod(type, name):
|
||||
# 读取 pod yaml 文件
|
||||
with open(f"assets/type{type}-pod.yaml") as f:
|
||||
pod_manifest = yaml.safe_load(f)
|
||||
pod_manifest["metadata"]["name"] = name
|
||||
pod_manifest["metadata"]["labels"]["app"] = name
|
||||
# 创建 Pod
|
||||
resp = v1.create_namespaced_pod(
|
||||
body=pod_manifest,
|
||||
namespace=namespace
|
||||
)
|
||||
print(f"pod '{resp.metadata.name}' 创建成功")
|
||||
# 创建 Service
|
||||
service_manifest = {
|
||||
"apiVersion": "v1",
|
||||
"kind": "Service",
|
||||
"metadata": {
|
||||
"name": name+ "-service"
|
||||
},
|
||||
"spec": {
|
||||
"selector": {
|
||||
"app": pod_manifest['metadata']['labels']['app']
|
||||
},
|
||||
"ports": [
|
||||
{
|
||||
"protocol": "TCP",
|
||||
"port": 22,
|
||||
"targetPort": 22
|
||||
},
|
||||
{
|
||||
"protocol": "TCP",
|
||||
"port": 5236,
|
||||
"targetPort": 5236
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
resp = v1.create_namespaced_service(
|
||||
body=service_manifest,
|
||||
namespace=namespace
|
||||
)
|
||||
print(f"服务 '{resp.metadata.name}' 创建成功")
|
||||
# 创建成功,再次查询,拿到端口
|
||||
service = v1.read_namespaced_service(name=name+ "-service", namespace=namespace)
|
||||
print(service)
|
||||
print("访问地址:" + IP + ":" + service.spec.ports[0].node_port)
|
||||
|
||||
|
||||
def delete_pod(name):
|
||||
pass
|
||||
|
||||
|
Loading…
Reference in New Issue