文档章节

Kubernetes Python API快速入门

openthings
 openthings
发布于 2018/07/03 11:11
字数 632
阅读 3502
收藏 0

Kubernetes Python API 快速入门

Kubernetes提供了多种语言的客户端SDK, 其中Python API可以使用python语言来操纵集群,实现管理的自动化,非常方便、功能强大。

  • 官方网站,https://github.com/kubernetes-client/python
  • 注意:这里介绍的方法可以在kubernetes里面的JupyterHub中运行,pod里没有~/.kube目录,直接使用官网教程的load_kubeconfig方法会失败,这里改用从文件中载入配置参数,可以从网上操作集群。

1、安装

pip install git+https://github.com/kubernetes-client/python.git

或者:

pip install kubernetes

2、初始化

首先引入SDK支持库。然后将 ~/.kube 的config文件的内容复制到本地目录,保存为文件kubeconfig.yaml,然后运行下面的python代码:

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

获得API的CoreV1Api版本对象:

v1 = client.CoreV1Api()

3、列出 namespaces

for ns in v1.list_namespace().items:
    print(ns.metadata.name)

结果如下:

blockchain
default
ingress
istio-system
jupyter
ks-dev
kube-public
kube-system
kubeflow-dev
kubeflow013
monitor
rook-ceph-system

4、列出所有的services

print("Listing All services with their info:\n")
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 ))

5、列出所有的pods

print("Listing pods with their IPs:")
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))

结果如下:

Listing pods with their IPs:
10.244.0.155	default	busybox-577868d55b-47pkx
10.244.0.164	default	busybox-deployment-6c975f66c6-ndlt2
10.244.0.154	default	nfs-server-nfs-server-provisioner-0
10.244.0.117	default	nginx-65899c769f-tgthn
10.244.0.145	istio-system	istio-citadel-7bdc7775c7-p4ww8
10.244.0.125	istio-system	istio-cleanup-old-ca-dkvcx
10.244.0.144	istio-system	istio-egressgateway-795fc9b47-plrjf
10.244.0.148	istio-system	istio-ingress-84659cf44c-ztgl7
10.244.0.143	istio-system	istio-ingressgateway-7d89dbf85f-mgm6h
10.244.0.126	istio-system	istio-mixer-post-install-rfgrh
10.244.0.137	istio-system	istio-pilot-66f4dd866c-qcntx
10.244.0.146	istio-system	istio-policy-76c8896799-88t7b
10.244.0.139	istio-system	istio-sidecar-injector-645c89bc64-d8mxc
10.244.0.159	istio-system	istio-statsd-prom-bridge-949999c4c-rcpgt
10.244.0.138	istio-system	istio-telemetry-6554768879-vqw86
10.244.0.158	istio-system	prometheus-86cb6dd77c-dgp7c
10.244.0.141	jupyter	hook-image-puller-1530505782-9lp9b
10.244.0.162	jupyter	hub-7b46b56bb8-9l7qh
10.244.0.167	jupyter	jupyter-supermap
10.244.0.129	jupyter	proxy-757b9676bf-wngcd
192.168.199.249	kube-system	etcd-supermap
10.244.0.140	kube-system	flux-helm-operator-68f9f586f4-f8p2v
192.168.199.249	kube-system	kube-apiserver-supermap
192.168.199.249	kube-system	kube-controller-manager-supermap
10.244.0.156	kube-system	kube-dns-58dd58c6c4-6hx59
192.168.199.249	kube-system	kube-flannel-ds-sg9p6
192.168.199.249	kube-system	kube-proxy-79l6g
192.168.199.249	kube-system	kube-scheduler-supermap
10.244.0.119	kube-system	kubernetes-dashboard-7d5dcdb6d9-xknlr
10.244.0.136	kube-system	nginx-ingress-controller-67b9bf4c56-64lpc
10.244.0.134	kube-system	nginx-ingress-default-backend-d676cbb5f-8q6t8
10.244.0.147	kube-system	nvidia-device-plugin-daemonset-gzhvn
10.244.0.128	kube-system	tiller-deploy-f44fbbb48-ff6qq
10.244.0.127	kubeflow-dev	ambassador-d74c99b6f-28xnf
10.244.0.115	kubeflow-dev	ambassador-d74c99b6f-92mkv
10.244.0.132	kubeflow-dev	ambassador-d74c99b6f-mvb79
10.244.0.124	kubeflow-dev	spartakus-volunteer-7976c7dd5c-rdd6q
10.244.0.130	kubeflow-dev	tf-hub-0
10.244.0.135	kubeflow-dev	tf-job-dashboard-7b57c549c8-h2sv2
10.244.0.122	kubeflow-dev	tf-job-operator-594d8c7ddd-b75jw
10.244.0.116	kubeflow013	ambassador-5bfc88cc87-4njpb
10.244.0.123	kubeflow013	ambassador-5bfc88cc87-4s7jf
10.244.0.161	kubeflow013	ambassador-5bfc88cc87-x7bvz
10.244.0.118	kubeflow013	spartakus-volunteer-7976c7dd5c-btwtw
10.244.0.126	kubeflow013	tf-hub-0
10.244.0.121	kubeflow013	tf-job-dashboard-7b57c549c8-zwq92
10.244.0.125	kubeflow013	tf-job-operator-594d8c7ddd-ddmqn
10.244.0.120	monitor	grafana-8698b89767-hd7sv
10.244.0.163	monitor	prometheus-alertmanager-6df98765f4-gkhrh
10.244.0.131	monitor	prometheus-kube-state-metrics-6584885ccf-2tlh4
192.168.199.249	monitor	prometheus-node-exporter-tcqlx
10.244.0.160	monitor	prometheus-pushgateway-5495f55cdf-qbf4v
10.244.0.165	monitor	prometheus-server-5959898967-snztn
192.168.199.249	rook-ceph-system	rook-ceph-agent-s5tjx
10.244.0.142	rook-ceph-system	rook-ceph-mgr0-77d9fb65c-skljh
10.244.0.151	rook-ceph-system	rook-ceph-mon3-sgqs8
10.244.0.157	rook-ceph-system	rook-ceph-mon5-4rftd
10.244.0.153	rook-ceph-system	rook-ceph-mon6-vx9ch
10.244.0.149	rook-ceph-system	rook-ceph-operator-6c7687c646-l6r2r
10.244.0.133	rook-ceph-system	rook-ceph-osd-frf6h
10.244.0.152	rook-ceph-system	rook-discover-ptfwp

© 著作权归作者所有

openthings
粉丝 320
博文 1129
码字总数 675031
作品 1
东城
架构师
私信 提问
加载中

评论(5)

背影轮廓
背影轮廓

引用来自“背影轮廓”的评论

kubectl version得到的信息如下:Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.2", GitCommit:"bb9ffb1654d4a729bb4cec18ff088eacc153c239", GitTreeState:"clean", BuildDate:"2018-08-07T23:17:28Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"9+", GitVersion:"v1.9.3-30+7770c66690383c-dirty", GitCommit:"7770c66690383c32a840942da3be35b95f5c42ca", GitTreeState:"dirty", BuildDate:"2018-08-30T04:09:28Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"linux/amd64"}
但是用python访问的时候,报OpenSSL.SSL.Error: [('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')]错误

引用来自“openthings”的评论

你的Server节点版本是中间版本,有可能存在bug。尝试升级到1.11.2与kubectl的版本一致,试一下看。

引用来自“背影轮廓”的评论

好的 我试试
还是不行😭
背影轮廓
背影轮廓

引用来自“背影轮廓”的评论

kubectl version得到的信息如下:Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.2", GitCommit:"bb9ffb1654d4a729bb4cec18ff088eacc153c239", GitTreeState:"clean", BuildDate:"2018-08-07T23:17:28Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"9+", GitVersion:"v1.9.3-30+7770c66690383c-dirty", GitCommit:"7770c66690383c32a840942da3be35b95f5c42ca", GitTreeState:"dirty", BuildDate:"2018-08-30T04:09:28Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"linux/amd64"}
但是用python访问的时候,报OpenSSL.SSL.Error: [('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')]错误

引用来自“openthings”的评论

你的Server节点版本是中间版本,有可能存在bug。尝试升级到1.11.2与kubectl的版本一致,试一下看。
好的 我试试
openthings
openthings 博主

引用来自“背影轮廓”的评论

kubectl version得到的信息如下:Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.2", GitCommit:"bb9ffb1654d4a729bb4cec18ff088eacc153c239", GitTreeState:"clean", BuildDate:"2018-08-07T23:17:28Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"9+", GitVersion:"v1.9.3-30+7770c66690383c-dirty", GitCommit:"7770c66690383c32a840942da3be35b95f5c42ca", GitTreeState:"dirty", BuildDate:"2018-08-30T04:09:28Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"linux/amd64"}
但是用python访问的时候,报OpenSSL.SSL.Error: [('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')]错误
你的Server节点版本是中间版本,有可能存在bug。尝试升级到1.11.2与kubectl的版本一致,试一下看。
背影轮廓
背影轮廓

引用来自“背影轮廓”的评论

kubectl version得到的信息如下:Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.2", GitCommit:"bb9ffb1654d4a729bb4cec18ff088eacc153c239", GitTreeState:"clean", BuildDate:"2018-08-07T23:17:28Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"9+", GitVersion:"v1.9.3-30+7770c66690383c-dirty", GitCommit:"7770c66690383c32a840942da3be35b95f5c42ca", GitTreeState:"dirty", BuildDate:"2018-08-30T04:09:28Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"linux/amd64"}
但是用python访问的时候,报OpenSSL.SSL.Error: [('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')]错误
我用的的Ubuntu18.04,也试过用Ubuntu16.04和centos7,都是一样的错误信息,我执行的代码如下:
from kubernetes import client, config, watch

# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config(config_file="kubeconfig.yaml")
v1 = client.CoreV1Api()
print("Listing All services with their info:\n")
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))
背影轮廓
背影轮廓
kubectl version得到的信息如下:Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.2", GitCommit:"bb9ffb1654d4a729bb4cec18ff088eacc153c239", GitTreeState:"clean", BuildDate:"2018-08-07T23:17:28Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"9+", GitVersion:"v1.9.3-30+7770c66690383c-dirty", GitCommit:"7770c66690383c32a840942da3be35b95f5c42ca", GitTreeState:"dirty", BuildDate:"2018-08-30T04:09:28Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"linux/amd64"}
但是用python访问的时候,报OpenSSL.SSL.Error: [('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')]错误
[雪峰磁针石博客]pyspark工具机器学习(自然语言处理和推荐系统)2数据处理1

本章介绍数据处理。数据处理是执行Machine Learning所需的关键步骤,因为我们需要清理,过滤,合并和转换我们的所需数据形式。 快速入门 读取 添加列 上面的False表示超过20个字符也不会截断...

书籍寻找
01/28
0
0
AirFlow/NiFi/MLFlow/KubeFlow进展

大数据分析中,进行流程化的批处理是必不可少的。传统的大数据处理大部分是基于关系数据库系统,难以实现大规模扩展;主流的基于Hadoop/Spark体系总体性能较强,但使用复杂、扩展能力弱。大数...

openthings
06/21
245
0
IPFS 服务的Python访问

IPFS 服务的Python访问 py-ipfs-api提供python api对IPFS服务通过http gateway进行访问,需要运行一个本地的IPFS daemon。https://github.com/ipfs/py-ipfs 是IPFS的纯Python语言实现,本身就...

openthings
2018/08/26
172
0
编程语言太难学?只因你还没试过Python!

Python语言可能是第一种即简单又功能强大的编程语言。它不仅适合于初学者,也适合于专业人员使用,更加重要的是,用Python编程是一种愉快的事。本身将帮助你学习这个奇妙的语言,并且向你展示...

极客学院
2015/05/18
2.1K
7
基于微服务库的可插拔RPC - go-micro

Go-Micro 是一个基于微服务库的可插拔 RPC,为编写分布式应用程序提供基本构件。它是 Micro 工具包的一部分,支持 Proto-RPC 和 JSON-RPC 的请求/响应协议,默认设置Consul为探索。 示例服务...

匿名
2016/03/22
4.8K
2

没有更多内容

加载失败,请刷新页面

加载更多

Taro 兼容 h5 踩坑指南

最近一周在做 Taro 适配 h5 端,过程中改改补补,好不酸爽。 本文记录📝遇到的问题,希望为有相同需求的哥们👬节约点时间。 Taro 版本:1.3.9。 解决跨域问题 h5 发请求会报跨域问题,需...

dkvirus
50分钟前
3
0
Spring boot 静态资源访问

0. 两个配置 spring.mvc.static-path-patternspring.resources.static-locations 1. application中需要先行的两个配置项 1.1 spring.mvc.static-path-pattern 这个配置项是告诉springboo......

moon888
今天
3
0
hash slot(虚拟桶)

在分布式集群中,如何保证相同请求落到相同的机器上,并且后面的集群机器可以尽可能的均分请求,并且当扩容或down机的情况下能对原有集群影响最小。 round robin算法:是把数据mod后直接映射...

李朝强
今天
4
0
Kafka 原理和实战

本文首发于 vivo互联网技术 微信公众号 https://mp.weixin.qq.com/s/bV8AhqAjQp4a_iXRfobkCQ 作者简介:郑志彬,毕业于华南理工大学计算机科学与技术(双语班)。先后从事过电子商务、开放平...

vivo互联网技术
今天
19
0
java数据类型

基本类型: 整型:Byte,short,int,long 浮点型:float,double 字符型:char 布尔型:boolean 引用类型: 类类型: 接口类型: 数组类型: Byte 1字节 八位 -128 -------- 127 short 2字节...

audience_1
今天
9
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部