
Kubernetes Api | python简单调用k8s的api
python操作kubernetes的api
·
一、安装k8s Python客户端库,加载配置文件
pip install kubernetes
加载配置文件,引用模块等(config_file="path/to/config"是k8s的授权文件用于访问k8s)
from kubernetes import config
config.load_kube_config()
from kubernetes import config
config.load_kube_config(config_file="path/to/config")
二、创建API实例,访问api
import kubernetes
from kubernetes import client,config
config.kube_config.load_kube_config(config_file='G:\config')
v1 = client.CoreV1Api()#获取CoreV1API版本对象(创建API实例)
#列出类k8s中的所有名称空间
for namespace in v1.list_namespace().items:
print(namespace.metadata.name)
#列举所有名称空间下的所有service
services=v1.list_service_for_all_namespaces()
for svc in services.items:
print('%s \t%s \t%s \t%s \n' %(svc.metadata.namespace,svc.metadata.name,svc.spec.cluster_ip,svc.spec.ports))
#列举所有名称空间下的pod资源
pods=v1.list_pod_for_all_namespaces()
for i in pods.items:
print("%s\t%s\t%s" %(i.status.pod_ip,i.metadata.namespace,i.metadata.name))
三、创建deployment和pod
from os import path
import yaml
from kubernetes import client,config
def main():
config.load_kube_config(config_file='D:\config')
with open(path.join(path.dirname(__file__),"nginx-deploy.yaml")) as f:
dep=yaml.safe_load(f)
k8s_apps_v1=client.AppsV1Api()
resp=k8s_apps_v1.create_namespaced_deployment(body=dep,namespace='default')
print('Deployment created,status=%s'%(resp.metadata.name))
if __name__ == '__main__':
main()
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
#创建pod
from kubernetes import client, config
config.load_kube_config(config_file='D:\config')
v1 = client.CoreV1Api()
api_instance = client.CoreV1Api()
namespace = 'default'
# 创建 Pod
pod_manifest = {
'apiVersion': 'v1',
'kind': 'Pod',
'metadata': {
'name': 'mypod'
},
'spec': {
'containers': [{
'name': 'mycontainer',
'image': 'nginx:latest',
'ports': [{
'containerPort': 80,
'hostPort': 80
}]
}]
}
}
resp = v1.create_namespaced_pod(body=pod_manifest, namespace=namespace)
print("Pod created. status='%s'" % resp.status.phase)
# 创建 Service
service_manifest = {
'apiVersion': 'v1',
'kind': 'Service',
'metadata': {
'name': 'myservice'
},
'spec': {
'selector': {
'app': 'myapp'
},
'ports': [{
'protocol': 'TCP',
'port': 80,
'targetPort': 80,
'nodePort': 30001
}],
'type': 'NodePort'
}
}
resp = api_instance.create_namespaced_service(
body=service_manifest,
namespace=namespace)
print("Service created. status='%s'" % resp)
四、删除和修改pod
from os import path
import yaml
from kubernetes import client,config
def main():
config.load_kube_config(config_file='G:\config')
k8s_core_v1=client.CoreV1Api()
resp=k8s_core_v1.delete_namespaced_pod(namespace='default',name='busybox-test')
print('delete pod')
main()
from os import path
import yaml
from kubernetes import client,config
def main():
config.load_kube_config(config_file='G:\config')
k8s_core_v1=client.CoreV1Api()
old_resp=k8s_core_v1.read_namespaced_pod(namespace='default',name='busybox-test')
old_resp.spec.containers[0].image='nginx'
#修改镜像
new_resp=k8s_core_v1.patch_namespaced_pod(namespace='default',name='busybox-test',body=old_resp)
print(new_resp.spec.containers[0].image)
if __name__=='__main__':
main()
官方文档
https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md
更多推荐
所有评论(0)