欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python3 kubernetes api的使用示例

 更新時(shí)間:2021年01月12日 10:53:42   作者:巽逸  
這篇文章主要介紹了python3 kubernetes api的使用示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下

一、安裝

github:https://github.com/kubernetes-client/python

安裝

pip install kubernetes

二、認(rèn)證

1、kubeconfig文件認(rèn)證

首先引入SDK支持庫。然后將 ~/.kube 的config文件的內(nèi)容復(fù)制到本地目錄,保存為文件kubeconfig.yaml,然后運(yùn)行下面的python代碼。

[root@k8s-m ~]# cp .kube/config  kubeconfig.yaml

#使用
from kubernetes import client, config
config.kube_config.load_kube_config(config_file="/root/kubeconfig.yaml")

三、api使用

1、列出資源信息

from kubernetes import client, config
config.kube_config.load_kube_config(config_file="/root/kubeconfig.yaml")

#獲取API的CoreV1Api版本對(duì)象
v1 = client.CoreV1Api()

#列出 namespaces
for ns in v1.list_namespace().items:
  print(ns.metadata.name)
  
#列出所有的services
ret = v1.list_service_for_all_namespaces(watch=False)
for i in ret.items:
  print("%s \t%s \t%s \t%s \t%s \n" % (i.kind, i.metadata.namespace, i.metadata.name, i.spec.cluster_ip, i.spec.ports ))
  
#列出所有的pod
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
  print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))

#列出所有deploy
ret = v1.list_deployments_for_all_namespaces(watch=False)
for i in ret.items:
  print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
  
##列出其他資源和以上類似,不懂可以查看(kubectl api-resources)

2、創(chuàng)建k8s資源對(duì)象

github:https://github.com/kubernetes-client/python/tree/master/examples

創(chuàng)建資源(提前寫好yaml資源清單)

#創(chuàng)建deploy
[root@k8s-m ~]# cat create_deploy.py
from os import path
import yaml
from kubernetes import client, config

def main():
  config.load_kube_config()

  with open(path.join(path.dirname(__file__), "/root/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)
main()

[root@k8s-m ~]# kubectl get pod 
NAME            READY  STATUS  RESTARTS  AGE
mydeploy-6946c867dc-bgcld  1/1   Running  0     40s
mydeploy-6946c867dc-rdnvj  1/1   Running  0     40s
[root@k8s-m ~]# kubectl get deploy
NAME    READY  UP-TO-DATE  AVAILABLE  AGE
mydeploy  2/2   2      2      44s


#創(chuàng)建pod例子(其它資源得自己查源碼自己找對(duì)應(yīng)的API)
[root@k8s-m ~]# cat create_pod.py 
from os import path

import yaml

from kubernetes import client, config


def main():
  config.load_kube_config()

  with open(path.join(path.dirname(__file__), "/root/pod.yaml")) as f:
    dep = yaml.safe_load(f)
    k8s_core_v1 = client.CoreV1Api()
    resp = k8s_core_v1.create_namespaced_pod(
      body=dep, namespace="default")
    print("Pod created. status='%s'" % resp.metadata.name)


if __name__ == '__main__':
  main()

##
[root@k8s-m ~]# python3 create_pod.py 
Pod created. status='nginx-pod'
[root@k8s-m ~]# kubectl get pod nginx-pod
NAME    READY  STATUS  RESTARTS  AGE
nginx-pod  1/1   Running  0     8s

3、刪除資源(我這里展示pod例子,其它資源刪除差不多)

參考地址:/usr/local/python3/lib/python3.6/site-packages/kubernetes/client/

[root@k8s-m ~]# cat dp.py
from os import path
import yaml
from kubernetes import client, config

def main():
  config.load_kube_config()
  k8s_core_v1 = client.CoreV1Api()
  resp = k8s_core_v1.delete_namespaced_pod(namespace="default",name='nginx-pod')
  print("delete Pod ")

[root@k8s-m ~]# python3 dp.py
delete Pod

4、查看資源(類似kubectl get pod xxx -o json)

#查看(read)

[root@k8s-m ~]# cat rp.py 
from os import path
import yaml
from kubernetes import client, config

def main():
  config.load_kube_config()
  k8s_core_v1 = client.CoreV1Api()
  resp = k8s_core_v1.read_namespaced_pod(namespace="default",name='nginx-pod')
  print("read Pod ")
  #詳細(xì)信息
  print(resp)
  #指定信息
  print(resp.spec.containers[0].image)

if __name__ == '__main__':
  main()

[root@k8s-m ~]# python3  rp.py |tail
      'host_ip': '172.31.250.229',
      'init_container_statuses': None,
      'message': None,
      'nominated_node_name': None,
      'phase': 'Running',
      'pod_ip': '10.244.167.134',
      'qos_class': 'BestEffort',
      'reason': None,
      'start_time': datetime.datetime(2019, 8, 30, 9, 13, 49, tzinfo=tzutc())}}
nginx

5、修改

[root@k8s-m ~]# cat pp.py 
from os import path
import yaml
from kubernetes import client, config

def main():
  config.load_kube_config()
  k8s_core_v1 = client.CoreV1Api()
  old_resp = k8s_core_v1.read_namespaced_pod(namespace="default",name='nginx-pod')
  old_resp.spec.containers[0].image = "nginx:alpine"
  #修改鏡像
  new_resp = k8s_core_v1.patch_namespaced_pod(namespace="default",name='nginx-pod',body=old_resp)
  print(new_resp.spec.containers[0].image)
if __name__ == '__main__':
  main()

[root@k8s-m ~]# python3 pp.py 
nginx:alpine

以上就是python3 kubernetes api的使用示例的詳細(xì)內(nèi)容,更多關(guān)于python3 kubernetes api的使用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python元類與迭代器生成器案例詳解

    Python元類與迭代器生成器案例詳解

    這篇文章主要介紹了Python元類與迭代器生成器案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Python學(xué)習(xí)筆記之lambda表達(dá)式用法詳解

    Python學(xué)習(xí)筆記之lambda表達(dá)式用法詳解

    這篇文章主要介紹了Python學(xué)習(xí)筆記之lambda表達(dá)式用法,結(jié)合實(shí)例形式詳細(xì)分析了lambda表達(dá)式的概念、功能、原理、組成及相關(guān)使用技巧,需要的朋友可以參考下
    2019-08-08
  • Python中逗號(hào)的三種作用實(shí)例分析

    Python中逗號(hào)的三種作用實(shí)例分析

    這篇文章主要介紹了Python中逗號(hào)的三種作用,實(shí)例分析了Python中的逗號(hào)在類型轉(zhuǎn)換與打印輸出時(shí)的使用技巧,需要的朋友可以參考下
    2015-06-06
  • Python實(shí)現(xiàn)找到同名文件并復(fù)制到其他文件夾中

    Python實(shí)現(xiàn)找到同名文件并復(fù)制到其他文件夾中

    這篇文章主要為大家介紹了如何基于Python語言,實(shí)現(xiàn)依據(jù)某一文件夾中大量文件的名稱復(fù)制另一文件夾中的同名文件,文中的示例代碼簡(jiǎn)潔易懂,需要的可以參考一下
    2023-05-05
  • Python3爬蟲ChromeDriver的安裝實(shí)例

    Python3爬蟲ChromeDriver的安裝實(shí)例

    在本篇文章里小編給大家整理的是一篇關(guān)于Python3爬蟲ChromeDriver的安裝實(shí)例內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。
    2021-02-02
  • Python 中l(wèi)ist ,set,dict的大規(guī)模查找效率對(duì)比詳解

    Python 中l(wèi)ist ,set,dict的大規(guī)模查找效率對(duì)比詳解

    這篇文章主要介紹了Python 中l(wèi)ist ,set,dict的大規(guī)模查找效率對(duì)比詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Django Channel實(shí)時(shí)推送與聊天的示例代碼

    Django Channel實(shí)時(shí)推送與聊天的示例代碼

    這篇文章主要介紹了Django Channel實(shí)時(shí)推送與聊天的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • 對(duì)Python 語音識(shí)別框架詳解

    對(duì)Python 語音識(shí)別框架詳解

    今天小編就為大家分享一篇對(duì)Python 語音識(shí)別框架詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • python使用rstrip函數(shù)刪除字符串末位字符

    python使用rstrip函數(shù)刪除字符串末位字符

    rstrip函數(shù)用于刪除字符串末位指定字符,默認(rèn)為空白符,這篇文章主要介紹了python使用rstrip函數(shù)刪除字符串末位字符的方法,需要的朋友可以參考下
    2023-04-04
  • Python爬取百度地圖POI數(shù)據(jù)代碼的步驟

    Python爬取百度地圖POI數(shù)據(jù)代碼的步驟

    爬取百度地圖的POI數(shù)據(jù)涉及法律和道德問題,因?yàn)檫@類數(shù)據(jù)受到版權(quán)保護(hù),且大多數(shù)在線地圖服務(wù)都有嚴(yán)格的反爬蟲措施,這篇文章主要介紹了Python爬取百度地圖POI數(shù)據(jù)代碼,需要的朋友可以參考下
    2024-08-08

最新評(píng)論