Python开发运维:Python调用K8S API实现资源管理

2023-12-13 21:02:44

目录

一、实验

1.Python操作K8S API获取资源

2.Python操作K8S API创建deployment资源

3.Python操作K8S API删除k8s资源

4.Python操作K8S API修改k8s资源

5.Python操作K8S API查看k8s资源

二、问题

1.Windows11安装kubernetes报错

2.Python通过调用哪些方法实现Pod和Deployment管理

3.? __name__ == '__main__' 有何作用


一、实验

1.Python操作K8S API获取资源

(1)在Windows上安装kubernetes模块

python -m pip install --upgrade pip

pip install --ignore-installed kubernetes

pip install --ignore-installed ipaddress

(2)认证

#把k8s集群的控制节点上的/root/.kube/config传到自己的电脑指定路径下
#下载到桌面
sz /root/.kube/config

下载到桌面传到C盘

(3)新建项目

(4)新建python file

(5)编写代码:python-k8s-api.py

import kubernetes
from kubernetes import client,config
config.kube_config.load_kube_config(config_file='C:\config')

#获取CoreV1API版本对象
v1 = client.CoreV1Api()

#列出来k8s中的所有名称空间
for namespace in v1.list_namespace().items:
    print(namespace.metadata.name)

(6)查看namespace

kubectl get ns

(7)?列举所有名称空间下的所有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))

(8) 查看svc

kubectl get svc

(9)?列举所有名称空间下的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))

(10) 查看pod

kubectl get pods

(11)client.AppsV1Api对象可以操作跟k8s中控制器相关资源对象,列举所有名称空间的deployment

v1_deploy=client.AppsV1Api()
deploys=v1_deploy.list_deployment_for_all_namespaces()
for i in deploys.items:
    print("%s\t%s\t%s"%(i.metadata.name,i.metadata.namespace,i.spec.replicas))

(12)查看deploy

2.Python操作K8S API创建deployment资源

(1) 编写代码:python-api-deploy.py

from os import path 
import yaml
from kubernetes import client,config
#引入要用的包
def main():
    config.load_kube_config(config_file='C:\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,name=%s'%(resp.metadata.name))

if __name__ == '__main__':
      main()

(2)编写deployment文件:?nginx-deploy.yaml

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
        ports :
        - containerPort : 80

(3) 目录

(4)运行代码

(5)K8S集群查看deployment

kubectl get deploy

(6)查看pod

kubectl get pod

3.Python操作K8S API删除k8s资源

(1)编写代码:api-delete-k8s.py

from os import path
import yaml
from kubernetes import client,config

def main():
    config.load_kube_config(config_file='C:\config')
    k8s_core_v1=client.CoreV1Api()
    resp=k8s_core_v1.delete_namespaced_pod(namespace='default',name='nginx-7f466444dc-w2cqq')
    print('delete pod')
main()

(2)查看pod

kubectl get pods

4.Python操作K8S API修改k8s资源

(1)查看pod


(2)?编写代码:api-modify-k8s.py

from os import path
import yaml
from kubernetes import client,config
def main():
    config.load_kube_config(config_file='C:\config')
    k8s_core_v1=client.CoreV1Api()
    old_resp=k8s_core_v1.read_namespaced_pod(namespace='default',name=' nginx-deployment-7848d4b86f-57rvf')
    old_resp.spec.containers[0].image='busybox'
    #修改镜像
    new_resp=k8s_core_v1.patch_namespaced_pod(namespace='default',name=' nginx-deployment-7848d4b86f-57rvf ',body=old_resp)
    print(new_resp.spec.containers[0].image)
if __name__=='__main__':
main()

(3)?操作pod: nginx-deployment-7848d4b86f-57rvf ,将pod镜像nginx修改为busybox

(4) 查看pod

kubectl get pods

(5)??查看pod详细信息,pod镜像nginx已修改为busybox

kubectl describe pod nginx-deployment-7848d4b86f-57rvf

5.Python操作K8S API查看k8s资源

(1)编写代码:api-read-k8s.py

from os import path
import yaml
from kubernetes import client,config
def main():
    config.load_kube_config(config_file='C:\config')
    k8s_core_v1=client.CoreV1Api()
    resp=k8s_core_v1.read_namespaced_pod(namespace='default',name=' nginx-deployment-7848d4b86f-57rvf')
    print('read pod')
#    print(resp)
    #读取指定的信息
    print(resp.spec.containers[0])
    print(resp.spec.containers[0].image)
if __name__=='__main__':
    main()

(2)?查看pod

kubectl get pods

(3)读取pod: nginx-deployment-7848d4b86f-57rvf 信息

二、问题

1.Windows11安装kubernetes报错

(1)报错

(2)原因分析

未更新pip

(3)解决方法

执行如下命令:

python -m pip install --upgrade pip

pip install --ignore-installed kubernetes

上面报错需要重装:

如下面报错需要安装ipaddress:


pip install --ignore-installed ipaddress

kubernetes模块安装成功:

2.Python通过调用哪些方法实现Pod和Deployment管理

(1)?Pod

Pod调用CoreV1Api方法

(2)?Deployment

Deployment调用AppsV1Api 方法

3.? __name__ == '__main__' 有何作用

(1)问题

希望保留末尾测试代码,即上面调用函数和类,但也不希望再导入模块的时候执行。

(2)作用

利用Python文件的一个内置属性__name__实现,如果直接运行Python文件,__name__的值是"__main__",如果import一个模块,那么模块的__name__的值是"文件名"。

文章来源:https://blog.csdn.net/cronaldo91/article/details/134911869
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。