python封装api_kubernetes中python api的二次封装

本文介绍了一个名为K8sApi的Python类,用于封装Kubernetes的API操作,包括部署、命名空间、节点、Pod、服务、Ingress和StatefulSet的增删改查功能。通过实例化该类并调用相应方法,可以便捷地进行Kubernetes资源管理。
摘要由CSDN通过智能技术生成

import urllib3

from pprint import pprint

from kubernetes import client

from os import path

import yaml

class K8sApi(object):

def __init__(self):

# self.config = config.kube_config.load_kube_config()

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

self.configuration = client.Configuration()

self.configuration.host = "https://192.168.100.111:6443"

self.configuration.api_key[

'authorization'] = 'Bearer  token'

self.configuration.verify_ssl = False

self.k8s_apps_v1 = client.AppsV1Api(client.ApiClient(self.configuration))

self.Api_Instance = client.CoreV1Api(client.ApiClient(self.configuration))

self.Api_Instance_Extensions = client.ExtensionsV1beta1Api(client.ApiClient(self.configuration))

####################################################################################################################

def list_deployment(self, namespace="default"):

api_response = self.k8s_apps_v1.list_namespaced_deployment(namespace)

return api_response

def read_deployment(self, name="nginx-deployment", namespace="default"):

api_response = self.k8s_apps_v1.read_namespaced_deployment(name, namespace)

return api_response

def create_deployment(self, file="deploy-nginx.yaml"):

with open(path.join(path.dirname(__file__), file)) as f:

dep = yaml.safe_load(f)

resp = self.k8s_apps_v1.create_namespaced_deployment(

body=dep, namespace="default")

return resp

def replace_deployment(self, file="deploy-nginx.yaml", name="nginx-deployment", namespace="default"):

with open(path.join(path.dirname(__file__), file)) as f:

dep = yaml.safe_load(f)

re

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值