網站首頁 Python教程 正文
前言:
K8s也提供API接口,提供這個接口的是管理節點的apiserver組件,apiserver服務負責提供HTTP API,以便用戶、其他組件相互通信。客戶端庫
安裝
pip install kubernetes -i https://pypi.douban.com/simple
k8s認證方式:
- HTTPS 證書認證:基于CA證書簽名的數字證書認證
- HTTP Token認證:通過一個Token來識別用戶
HTTPS證書認證(kubeconfig)
import os
from kubernetes import client, config
config.load_kube_config(file_path) ?# 指定kubeconfig配置文件
apps_api = client.AppsV1Api() ?# 資源接口類實例化
for dp in apps_api.list_deployment_for_all_namespaces().items:
? ? print(dp)
HTTP Token認證(ServiceAccount)
from kubernetes import client, config
configuration = client.Configuration()
configuration.host = "https://192.168.3.201:16443" ?# APISERVER地址
configuration.ssl_ca_cert="ca.crt" ?# CA證書 /etc/kubernetes/pki/ca.crt
configuration.verify_ssl = True ? # 啟用證書驗證
configuration.api_key = {"authorization": "Bearer " + token} ?# 指定Token字符串
client.Configuration.set_default(configuration)
apps_api = client.AppsV1Api()?
這2個認證,2選1
獲取Token字符串:創建service account并綁定默認cluster-admin管理員集群角色:
創建用戶:
$ kubectl create serviceaccount dashboard-admin -n kube-system
用戶授權:
$ kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
獲取用戶Token:
$ kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk ‘/dashboard-admin/{print $1}’)
其他常用資源接口類實例化:
core_api = client.CoreV1Api() ?# namespace,pod,service,pv,pvc
apps_api = client.AppsV1Api() ?# deployment
networking_api = client.NetworkingV1beta1Api() ?# ingress
storage_api = client.StorageV1Api() ?# storage_class
舉個例子
Deployment操作:
# 先得有上面的認證,下面的代碼才行
# 創建
namespace = "default"
name = "api-test"
replicas = 3
labels = {'nginx':'true'} ?# 不區分數據類型,都要加引號
image = "nginx"
body = client.V1Deployment(
? ? ? ? ? ? api_version="apps/v1",
? ? ? ? ? ? kind="Deployment",
? ? ? ? ? ? metadata=client.V1ObjectMeta(name=name),
? ? ? ? ? ? spec=client.V1DeploymentSpec(
? ? ? ? ? ? ? ? replicas=replicas,
? ? ? ? ? ? ? ? selector={'matchLabels': labels},
? ? ? ? ? ? ? ? template=client.V1PodTemplateSpec(
? ? ? ? ? ? ? ? ? ? metadata=client.V1ObjectMeta(labels=labels),
? ? ? ? ? ? ? ? ? ? spec=client.V1PodSpec(
? ? ? ? ? ? ? ? ? ? ? ? containers=[client.V1Container(
? ? ? ? ? ? ? ? ? ? ? ? ? ? name="web",
? ? ? ? ? ? ? ? ? ? ? ? ? ? image=image
? ? ? ? ? ? ? ? ? ? ? ? )]
? ? ? ? ? ? ? ? ? ? )
? ? ? ? ? ? ? ? ),
? ? ? ? ? ? )
? ? ? ? )
try:
? ? apps_api.create_namespaced_deployment(namespace=namespace, body=body)
except Exception as e:
? ? status = getattr(e, "status")
? ? if status == 400:
? ? ? ? print(e)
? ? ? ? print("格式錯誤")
? ? elif status == 403:
? ? ? ? print("沒權限")
# 刪除
name = "api-test"
apps_api.delete_namespaced_deployment(namespace=namespace, name=name)
但其實這個API挺繞的 ,一個創建deployment的,這里N多的類的對象。
原文鏈接:https://blog.csdn.net/hans99812345/article/details/124851132
相關推薦
- 2022-06-06 PowerShell yarn : 無法加載文件 C:\Users\Admin\AppData\Ro
- 2022-10-25 基于windows10下使用bat腳本設置自定義開機啟動項
- 2022-07-21 常見CSS樣式
- 2022-02-10 MongoDB數據庫安裝部署及警告優化_MongoDB
- 2022-04-27 一文教你如何封裝安全的go_Golang
- 2022-05-02 深入了解Python?中線程和進程區別_python
- 2022-05-17 ubuntu如何給 GoLand 設置桌面快捷圖標
- 2023-07-04 ES聚合查詢+條件搜索的實現
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支