使用 Java 操作 Kubernetes API_kubernetes-client-程序员宅基地

技术标签: kubernetes  java  kubernetes-云原生的掌舵人  kubernetes-client/java  客户端  

目录

本文目标

k8s-client-java选型

kubernetes-client/java和fabric8io/kubernetes-client对比

kubernetes-client/java的使用

REST API

REST API版本说明

Alpha、Beta、RC、GA版本的区别

kubectl api-versions

REST API 实例:     

API Object 整体划分图 (红星符号代表常用资源)

kubernetes-client/java客户端API接口识别

ApiClient初始化&认证

CRD资源增删改查

操作示例

Namespaces增删改查

Node增删改查

Pod增删改查

优先级

Services增删改查

操作示例

Deployment增删改查

kubernetes-client/java 使用注意(很重要):

 Kubernetes资源内容查询:JSONPath

 kubernetes-client: delete操作异常 Expected a string but was BEGIN_OBJECT at line 1 column 1912

 kubernetes:CPU 和内存单位解释

 kubenetes: patch更新和替换、删除资源内容

kubernetes:字段选择器(field-selector)标签选择器(labels-selector)和筛选 Kubernetes 资源

Kubernetes参数:dryRun理解

kubernetes-client/java:Scale报错400 BadRequest 或 500 cannot unmarshal object

Kubectl :--v 接口调试,以及设置日志输出详细程度


关注【云原生百宝箱】公众号,获取更多云原生消息

本文目标

基于官方kubernetes-client/java类库,实现通过java完成对kubenetes原生资源对象(pod、node、namespace、servcie、deployment)和自定义资源对象(如:cluster)的增删改查或事件监听(watch)

k8s-client-java选型

目前通过java操作k8s,开源版本共有两个:

kubernetes-client/javafabric8io/kubernetes-client对比

       和官网API一致性   社区活跃度                                          代码生成
kubernetes-client/java 根据k8s-openapi随之更新,一致性和更新频率高 目前不活跃 kubernetes-client/java提供了生成代码的通用跨语言工具,该工具托管在 kubernetes-client / gen存储库中
fabric8io/kubernetes-client 一致性低,更新慢;其中不支持k8s1.8和1.13 社区活跃,目前使用者多 暂无

鉴于kubernetes-client/java官网API一致性好,本文决定采用它

kubernetes-client/java的使用

REST API

API 资源使用REST模式。

kube-apiserver 支持同时提供 https(默认监听在 6443 端口)和 http API(默认监听在 127.0.0.1 的 8080 端口),其中 http API 是非安全接口,不做任何认证授权机制,不建议生产环境启用。两个接口提供的 REST API 格式相同

图片来自  OpenShift Blog

  1. GET /<资源名的复数格式>:获得某一类型的资源列表,例如GET /pods 返回一个Pod资源列表。

  2. POST /<资源名的复数格式>:创建一个资源,该资源来自用户提供的JSON对象。

  3. GET /<资源名复数格式>/<名字>:通过给出的名称(Name)获得单个资源,例如GET /pods/first 返回一个名称为“first”的Pod。

  4. DELETE /<资源名复数格式>/<名字>:通过给出的名字删除单个资源,删除选项(DeleteOptions)中可以指定的优雅删除(Grace Deletion)的时间(GracePeriodSeconds),该可选项表明了从服务端接收到删除请求到资源被删除的时间间隔(单位为秒)。

  5. PUT /<资源名复数格式>/<名字>:通过给出的资源名和客户端提供的JSON对象来更新或创建资源。

  6. PATCH /<资源名复数格式>/<名字>:选择修改资源详细指定的域。

  7. GET /watch/<资源名复数格式>:随时间变化,不断接收一连串的JSON对象,这些JSON对象记录了给定资源类别内所有资源对象的变化情况。

  8. GET /watch/<资源名复数格式>/:随时间变化,不断接收一连串的JSON对象,这些JSON对象记录了某个给定资源对象的变化情况。

REST API版本说明

为了在兼容旧版本的同时不断升级新的API,Kubernetes支持多种API版本,每种API版本都有不同的API路径,例如/api/v1或 /apis/extensions/v1beta1

Alpha级别:

  • 包含alpha名称的版本(例如v1alpha1)。
  • 该软件可能包含错误。启用一个功能可能会导致bug。默认情况下,功能可能会被禁用。

Beta级别:

  • 包含beta名称的版本(例如v2beta3)。
  • 该软件经过很好的测试。启用功能被认为是安全的。默认情况下功能是开启的。
  • 大家使用过的Beta版本后,可以多给社区反馈,如果此版本在后续更新后将不会有太大变化。

Stable级别:

  • 该版本名称命名方式:vX这里X是一个整数。
  • Stable版本的功能特性,将出现在后续发布的软件版本中。

Alpha、Beta、RC、GA版本的区别

  • Alpha:是内部测试版,一般不向外部发布,会有很多Bug.一般只有测试人员使用。
  • Beta:也是测试版,这个阶段的版本会一直加入新的功能。在Alpha版之后推出。
  • RC:(Release Candidate) 顾名思义么 ! 用在软件上就是候选版本。系统平台上就是发行候选版本。RC版不会再加入新的功能了,主要着重于除错。
  • GA:General Availability,正式发布的版本,在国外都是用GA来说明release版本的。

kubectl api-versions

查看 apiserver暴露的接口

kubectl api-versions

或者

curl -H'Authorization: Bearer token' https://192.168.1.122:6443/apis --insecure

[root@fly]# kubectl api-versions
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1beta1
apps/v1
apps/v1beta1
apps/v1beta2
authentication.istio.io/v1alpha1
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
autoscaling/v1
autoscaling/v2beta1
batch/v1
batch/v1beta1
certificates.k8s.io/v1beta1
config.istio.io/v1alpha2
events.k8s.io/v1beta1
extensions/v1beta1
networking.istio.io/v1alpha3
networking.k8s.io/v1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1beta1
rbac.istio.io/v1alpha1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1

REST API 实例:     

  https://10.10.124.199:6443/apis/apps/v1/deployment

  1. https  : kube-apiserver 支持同时提供https(默认监听在 6443 端口)和 http API(默认监听在 127.0.0.1 的 8080 端口),由于http api 是不会经过认证授权的,在生产环境中,为了安全性考虑,推荐是不启用。
  2. 10.10.124.199  : 该ip地址为kubernetes master 地址,在集群为高可用设置的场景下,该ip地址为 vip 地址。
  3. apis :kubernetes在这一层级只有api 与 apis ,api只有初步核心资源,包含pod,node,namespace等基本资源
  4. apps : 为api group 名,api group 是kubernetes 对资源类型特性相近的整合。
  5. v1: 是作为api group  version, 在新资源类型的加入到kubernetes,会经历版本变迁: v1alpha1-->v1alpha2-->....-->v1alphaN-->v1beta1-->v1beta2-->v1 。
  6. deployment : 是kubernetes 的资源名

API Object 整体划分图 (红星符号代表常用资源)

kubernetes-client/java客户端API接口识别

打开kubernetes-client/java,只要是以Api结尾,一般就是我们可以调用的Api接口

API接口识别

ApiClient初始化&认证

ApiClient client = new ClientBuilder().setBasePath("ApiServer地址").setVerifyingSsl(false)
                .setAuthentication(new AccessTokenAuthentication("Token")).build();
        Configuration.setDefaultApiClient(client);

在生产环境,建议放在 程序启动前的初始化方法

CRD资源增删改查

使用 CustomObjectsApi apiInstance = new CustomObjectsApi(); 操作

Method HTTP request Description
createClusterCustomObject POST /apis/{group}/{version}/{plural} 创建集群范围CRD资源对象
createNamespacedCustomObject POST /apis/{group}/{version}/namespaces/{namespace}/{plural} 创建分区范围CRD资源对象
deleteClusterCustomObject DELETE /apis/{group}/{version}/{plural}/{name} 删除集群范围CRD资源对象
deleteNamespacedCustomObject DELETE/apis/{group}/{version}/namespaces/{namespace}/{plural}/{name} 删除分区范围CRD资源对象
getClusterCustomObject GET /apis/{group}/{version}/{plural}/{name} 获取集群范围CRD资源对象
getClusterCustomObjectScale GET /apis/{group}/{version}/{plural}/{name}/scale 获取集群范围CRD资源对象-scale
getClusterCustomObjectStatus GET /apis/{group}/{version}/{plural}/{name}/status 获取集群范围CRD资源对象-状态
getNamespacedCustomObject GET /apis/{group}/{version}/namespaces/{namespace}/{plural}/{name} 获取分区范围CRD资源对象
getNamespacedCustomObjectScale GET/apis/{group}/{version}/namespaces/{namespace}/{plural}/{name}/scale 获取分区范围CRD资源对象-scale
getNamespacedCustomObjectStatus GET/apis/{group}/{version}/namespaces/{namespace}/{plural}/{name}/status 获取分区范围CRD资源对象-状态
listClusterCustomObject GET /apis/{group}/{version}/{plural} 集群范围CRD资源对象列表
listNamespacedCustomObject GET /apis/{group}/{version}/namespaces/{namespace}/{plural} 分区范围CRD资源对象列表
patchClusterCustomObject PATCH /apis/{group}/{version}/{plural}/{name} 更新集群范围CRD资源对象
patchClusterCustomObjectScale PATCH /apis/{group}/{version}/{plural}/{name}/scale 更新集群范围CRD资源对象-scale
patchClusterCustomObjectStatus PATCH /apis/{group}/{version}/{plural}/{name}/status 更新集群范围CRD资源对象-状态
patchNamespacedCustomObject PATCH/apis/{group}/{version}/namespaces/{namespace}/{plural}/{name} 更新分区范围CRD资源对象
patchNamespacedCustomObjectScale PATCH/apis/{group}/{version}/namespaces/{namespace}/{plural}/{name}/scale 更新分区范围CRD资源对象-scale
patchNamespacedCustomObjectStatus PATCH/apis/{group}/{version}/namespaces/{namespace}/{plural}/{name}/status 更新分区范围CRD资源对象-状态
replaceClusterCustomObject PUT /apis/{group}/{version}/{plural}/{name} 替换集群范围CRD资源对象
replaceClusterCustomObjectScale PUT /apis/{group}/{version}/{plural}/{name}/scale 替换集群范围CRD资源对象-scale
replaceClusterCustomObjectStatus PUT /apis/{group}/{version}/{plural}/{name}/status 替换集群范围CRD资源对象-状态
replaceNamespacedCustomObject PUT /apis/{group}/{version}/namespaces/{namespace}/{plural}/{name} 替换分区范围CRD资源对象
replaceNamespacedCustomObjectScale PUT/apis/{group}/{version}/namespaces/{namespace}/{plural}/{name}/scale 替换分区范围CRD资源对象-scale
replaceNamespacedCustomObjectStatus PUT/apis/{group}/{version}/namespaces/{namespace}/{plural}/{name}/status 替换分区范围CRD资源对象-状态

操作示例

简要描述:

请求方式:

  • GET

请求URL:

  • /apis/{group}/{version}/{plural}

请求URL示例:

  • /apis/flycloud.cn/v1/clusters

请求java示例:

        CustomObjectsApi apiInstance = new CustomObjectsApi();
        String group = "flycloud.cn"; 
        String version = "v1";
        String plural = "clusters";
        String pretty = "ture";
        try {
            Object result = apiInstance.listClusterCustomObject(group,version,plural,pretty,null,null,null,null);
            String listCluster = JSON.toJSONString(result);
            System.out.println(listCluster);
        } catch (ApiException e) {
            System.err.println("Exception when calling CustomObjectsApi#listClusterCustomObject");
            e.printStackTrace();
        }

返回结果:

 {
	"apiVersion": "flycloud.cn/v1",
	"items": [{
		"apiVersion": "flycloud.cn/v1",
		"kind": "Cluster",
		"metadata": {
			"annotations": {
				"name": "top"
			},
			"creationTimestamp": "2019-08-12T07:03:23Z",
			"generation": 1.0,
			"labels": {
				"template": "platform"
			},
			"name": "top",
			"namespace": "cluster-top",
			"resourceVersion": "277020",
			"selfLink": "/apis/flycloud.cn/v1/namespaces/cluster-top/clusters/top",
			"uid": "46528941-bccf-11e9-bfeb-005056bc7cff"
		},
		"spec": {
			"info": {
				"address": "192.168.103.60",
				"harbor": {
					"address": "192.168.103.65",
					"password": "123456",
					"port": 443.0,
					"protocol": "https",
					"user": "admin"
				},
				"jenkins": {
					"password": "admin",
					"type": "jenkins",
					"username": "admin"
				},
				"mysql": {
					"connectionProperties": "druid.stat.mergeSql=true druid.stat.slowSqlMillis=5000",
					"driverClass": "com.mysql.jdbc.Driver",
					"filters": "stat",
					"initialSize": 0.0,
					"logAbandoned": true,
					"maxActive": 100.0,
					"maxOpenPreparedStatements": 50.0,
					"maxWait": 60000.0,
					"minIdle": 0.0,
					"minPoolSize": 2.0,
					"password": "123456",
					"poolPreparedStatements": false,
					"removeAbandoned": true,
					"removeAbandonedTimeout": 900.0,
					"type": "api-mysql",
					"username": "root"
				},
				"network": {
					"networkFlag": "calico",
					"version": 1.0
				},
				"nfs": [{
					"capacity": "1",
					"ip": "192.168.103.65",
					"name": "nfs",
					"path": "/nfs/top",
					"type": "nfs"
				}],
				"port": 6443.0,
				"prometheusPort": 30003.0,
				"protocol": "https",
				"redis": {
					"maxTotal": 500.0,
					"maxWaitMillis": 15000.0,
					"minIdle": 10.0,
					"password": "123456",
					"testOnBorrow": true,
					"testWhileIdle": true,
					"timeBetweenEvictionRunsMillis": 600000.0,
					"type": "api-redis"
				}
			},
			"template": [{
				"namespace": "kube-system",
				"serviceName": "heapster",
				"servicePort": [{
					"port": 80.0,
					"protocol": "TCP",
					"type": "check"
				}],
				"type": "heapster"
			}, {
				"namespace": "kube-system",
				"serviceName": "influxdb",
				"servicePort": [{
					"port": 80.0,
					"protocol": "TCP",
					"type": "web"
				}, {
					"port": 8086.0,
					"protocol": "TCP",
					"type": "api"
				}],
				"type": "influxdb"
			}, {
				"namespace": "kube-system",
				"serviceName": "elasticsearch-logging-v1",
				"servicePort": [{
					"port": 9200.0,
					"protocol": "TCP",
					"type": "web"
				}, {
					"port": 9300.0,
					"protocol": "TCP",
					"type": "api"
				}],
				"type": "es"
			}, {
				"namespace": "kube-system",
				"serviceName": "oam-api-service",
				"servicePort": [{
					"port": 8081.0,
					"protocol": "TCP",
					"type": "api"
				}],
				"type": "oam-api"
			}, {
				"namespace": "kube-system",
				"serviceName": "oam-task-service",
				"type": "oma-task"
			}, {
				"namespace": "kube-system",
				"serviceName": "webapi-service",
				"servicePort": [{
					"port": 8080.0,
					"protocol": "TCP",
					"type": "api"
				}],
				"type": "webapi"
			}, {
				"namespace": "kube-system",
				"serviceName": "webpage-service",
				"servicePort": [{
					"port": 8887.0,
					"protocol": "TCP",
					"type": "web"
				}],
				"type": "webpage"
			}, {
				"namespace": "kube-system",
				"serviceName": "terminal-service",
				"servicePort": [{
					"port": 8888.0,
					"protocol": "TCP",
					"type": "api"
				}],
				"type": "terminal"
			}, {
				"namespace": "kube-system",
				"serviceName": "api-mysql-service",
				"servicePort": [{
					"nodePort": 30306.0,
					"port": 3306.0,
					"protocol": "TCP",
					"type": "api"
				}],
				"type": "api-mysql"
			}, {
				"namespace": "kube-system",
				"serviceName": "api-redis-service",
				"servicePort": [{
					"nodePort": 30379.0,
					"port": 6379.0,
					"protocol": "TCP",
					"type": "api"
				}],
				"type": "api-redis"
			}, {
				"namespace": "kube-system",
				"serviceName": "jenkins",
				"servicePort": [{
					"nodePort": 30080.0,
					"port": 8080.0,
					"protocol": "TCP",
					"type": "api"
				}],
				"type": "jenkins"
			}, {
				"namespace": "kube-system",
				"serviceName": "nfs-controller",
				"type": "nfs-controller"
			}, {
				"namespace": "kube-system",
				"serviceName": "auto-scale",
				"type": "auto-scale"
			}, {
				"namespace": "kube-system",
				"serviceName": "node-up-down",
				"type": "node-up-down"
			}, {
				"namespace": "kube-system",
				"serviceName": "calico-node",
				"type": "calico-node"
			}, {
				"namespace": "kube-system",
				"serviceName": "calico-kube-controller",
				"type": "calico-cotnroller"
			}, {
				"namespace": "kube-system",
				"serviceName": "kube-apiserver",
				"type": "kube-apiserver"
			}, {
				"namespace": "kube-system",
				"serviceName": "kube-controller-manager",
				"type": "kube-controller-manager"
			}, {
				"namespace": "kube-system",
				"serviceName": "kube-scheduler",
				"type": "kube-scheduler"
			}, {
				"namespace": "kube-system",
				"serviceName": "kube-proxy",
				"type": "kube-proxy"
			}, {
				"namespace": "kube-system",
				"serviceName": "etcd",
				"type": "etcd"
			}, {
				"namespace": "kube-system",
				"serviceName": "cluster-controller",
				"type": "cluster-controller"
			}, {
				"namespace": "kube-system",
				"serviceName": "kube-dns",
				"servicePort": [{
					"port": 53.0,
					"protocol": "TCP",
					"type": "check"
				}, {
					"port": 53.0,
					"protocol": "UDP",
					"type": "dns"
				}],
				"type": "kube-dns"
			}]
		},
		"status": {
			"conditions": [{
				"status": false,
				"type": "Ready"
			}]
		}
	}, {
		"apiVersion": "flycloud.cn/v1",
		"kind": "Cluster",
		"metadata": {
			"annotations": {
				"name": "test"
			},
			"creationTimestamp": "2019-09-13T15:54:57Z",
			"generation": 1.0,
			"labels": {
				"template": "dev"
			},
			"name": "test",
			"namespace": "flycloud",
			"resourceVersion": "7687403",
			"selfLink": "/apis/flycloud.cn/v1/namespaces/flycloud/clusters/test",
			"uid": "d5bddb21-d63e-11e9-b5a7-005056bc7cff"
		},
		"spec": {
			"info": {
				"address": "192.168.103.60",
				"domain": [],
				"external": [{
					"labels": {
						"lb": "nginx"
					},
					"maxPort": 35000.0,
					"minPort": 33000.0,
					"tcpConfig": "system-expose-nginx-config-tcp",
					"topLb": "192.168.103.61",
					"type": "nginx",
					"udpConfig": "system-expose-nginx-config-udp"
				}],
				"harbor": {
					"address": "192.168.103.59",
					"password": "Harbor12345",
					"port": 443.0,
					"protocol": "https",
					"user": "admin"
				},
				"network": {
					"networkFlag": "calico",
					"version": "1"
				},
				"nfs": [{
					"capacity": "1",
					"ip": "192.168.103.65",
					"name": "nfs",
					"path": "/nfs/top",
					"type": "nfs"
				}],
				"port": 6443.0,
				"prometheusPort": 30003.0,
				"protocol": "https",
				"storages": []
			},
			"template": [{
				"namespace": "kube-system",
				"serviceName": "auto-scale",
				"type": "auto-scale"
			}, {
				"namespace": "kube-system",
				"serviceName": "calico-kube-controller",
				"type": "calico-cotnroller"
			}, {
				"namespace": "kube-system",
				"serviceName": "calico-node",
				"type": "calico-node"
			}, {
				"namespace": "kube-system",
				"serviceName": "default-http-backend",
				"servicePort": [{
					"port": 80.0,
					"protocol": "TCP",
					"type": "web"
				}],
				"type": "default-http-backend"
			}, {
				"namespace": "kube-system",
				"serviceName": "elasticsearch-logging-v1",
				"servicePort": [{
					"port": 9200.0,
					"protocol": "TCP",
					"type": "web"
				}, {
					"port": 9300.0,
					"protocol": "TCP",
					"type": "api"
				}],
				"type": "es"
			}, {
				"namespace": "kube-system",
				"serviceName": "etcd",
				"type": "etcd"
			}, {
				"namespace": "kube-system",
				"serviceName": "heapster",
				"servicePort": [{
					"port": 80.0,
					"protocol": "TCP",
					"type": "check"
				}],
				"type": "heapster"
			}, {
				"namespace": "kube-system",
				"serviceName": "influxdb",
				"servicePort": [{
					"port": 80.0,
					"protocol": "TCP",
					"type": "web"
				}, {
					"port": 8086.0,
					"protocol": "TCP",
					"type": "api"
				}],
				"type": "influxdb"
			}, {
				"namespace": "kube-system",
				"serviceName": "kube-apiserver",
				"type": "kube-apiserver"
			}, {
				"namespace": "kube-system",
				"serviceName": "kube-controller-manager",
				"type": "kube-controller-manager"
			}, {
				"namespace": "kube-system",
				"serviceName": "kube-dns",
				"servicePort": [{
					"port": 54.0,
					"protocol": "TCP",
					"type": "check"
				}, {
					"port": 53.0,
					"protocol": "UDP",
					"type": "dns"
				}],
				"type": "kube-dns"
			}, {
				"namespace": "kube-system",
				"serviceName": "kube-proxy",
				"type": "kube-proxy"
			}, {
				"namespace": "kube-system",
				"serviceName": "kube-scheduler",
				"type": "kube-scheduler"
			}, {
				"namespace": "kube-system",
				"serviceName": "nfs-controller",
				"type": "nfs-controller"
			}, {
				"namespace": "kube-system",
				"serviceName": "nginx-controller",
				"servicePort": [{
					"port": 80.0,
					"protocol": "TCP",
					"type": "web"
				}],
				"type": "nginx-controller"
			}, {
				"namespace": "kube-system",
				"serviceName": "node-up-down",
				"type": "node-up-down"
			}, {
				"namespace": "kube-system",
				"serviceName": "oam-api-service",
				"servicePort": [{
					"port": 8081.0,
					"protocol": "TCP",
					"type": "api"
				}],
				"type": "oam-api"
			}, {
				"namespace": "kube-system",
				"serviceName": "oam-task-service",
				"type": "oma-task"
			}, {
				"namespace": "kube-system",
				"serviceName": "terminal-service",
				"servicePort": [{
					"port": 8888.0,
					"protocol": "TCP",
					"type": "api"
				}],
				"type": "terminal"
			}]
		},
		"status": {
			"conditions": [{
				"status": true,
				"type": "Ready"
			}]
		}
	}],
	"kind": "ClusterList",
	"metadata": {
		"continue": "",
		"resourceVersion": "7758294",
		"selfLink": "/apis/flycloud.cn/v1/clusters"
	}
}

Namespaces增删改查

使用 CoreV1Api apiInstance = new CoreV1Api(); 操作

Method HTTP request Description
createNamespace POST /api/v1/namespaces 创建分区
deleteNamespace DELETE/api/v1/namespaces/{name} 删除分区
listNamespace GET /api/v1/namespaces 分区列表
patchNamespace PATCH/api/v1/namespaces/{name} 更新分区内容
readNamespace GET/api/v1/namespaces/{name} 查询指定分区详情
replaceNamespace PUT/api/v1/namespaces/{name} 替换分区内容

Node增删改查

使用 CoreV1Api apiInstance = new CoreV1Api(); 操作

Method HTTP request Description
createNode POST /api/v1/nodes 创建节点
deleteCollectionNode DELETE /api/v1/nodes 删除多个节点
deleteNode DELETE/api/v1/nodes/{name} 删除节点
listNode GET /api/v1/nodes 节点列表
patchNode PATCH/api/v1/nodes/{name} 更新节点
readNode GET /api/v1/nodes/{name} 查询指定节点
replaceNode PUT /api/v1/nodes/{name} 替换指定节点内容
replaceNodeStatus PUT/api/v1/nodes/{name}/status 修改节点状态

Pod增删改查

使用 CoreV1Api apiInstance = new CoreV1Api(); 操作

Method HTTP request Description
createNamespacedPod POST /api/v1/namespaces/{namespace}/pods 创建pod
deleteCollectionNamespacedPod DELETE/api/v1/namespaces/{namespace}/pods 删除多个pod
deleteNamespacedPod DELETE/api/v1/namespaces/{namespace}/pods/{name} 删除pod
listNamespacedPod GET /api/v1/namespaces/{namespace}/pods pod列表
patchNamespacedPod PATCH/api/v1/namespaces/{namespace}/pods/{name} 更新pod
readNamespacedPod GET/api/v1/namespaces/{namespace}/pods/{name} 查询指定pod
replaceNamespacedPod PUT/api/v1/namespaces/{namespace}/pods/{name} 替换指定pod内容

优先级

Method HTTP request Description
createPriorityClass POST /apis/scheduling.k8s.io/v1beta1/priorityclasses 创建优先级
deleteCollectionPriorityClass DELETE /apis/scheduling.k8s.io/v1beta1/priorityclasses 删除多个优先级
deletePriorityClass DELETE /apis/scheduling.k8s.io/v1beta1/priorityclasses/{name} 删除优先级
getAPIResources GET /apis/scheduling.k8s.io/v1beta1/ 获取可用资源
listPriorityClass GET /apis/scheduling.k8s.io/v1beta1/priorityclasses 优先级列表
patchPriorityClass PATCH /apis/scheduling.k8s.io/v1beta1/priorityclasses/{name} 修改优先级
readPriorityClass GET /apis/scheduling.k8s.io/v1beta1/priorityclasses/{name} 查询指定优先级
replacePriorityClass PUT /apis/scheduling.k8s.io/v1beta1/priorityclasses/{name} 替换优先级

Services增删改查

使用 CoreV1Api apiInstance = new CoreV1Api(); 操作

Method HTTP request Description
createNamespacedService POST /api/v1/namespaces/{namespace}/services 创建服务
deleteNamespacedService DELETE/api/v1/namespaces/{namespace}/services/{name} 删除服务
listNamespacedService GET /api/v1/namespaces/{namespace}/services 服务列表
patchNamespacedService PATCH/api/v1/namespaces/{namespace}/services/{name} 修改指定服务内容
readNamespacedService GET/api/v1/namespaces/{namespace}/services/{name} 查询指定服务内容
replaceNamespacedService PUT/api/v1/namespaces/{namespace}/services/{name} 替换指定服务内容

操作示例

@Test
    public void CoreV1ApiTest(){

        CoreV1Api apiInstance = new CoreV1Api();
        String pretty = "true"; // String | If 'true', then the output is pretty printed.
        String _continue = "_continue_example"; // String | The continue option should be set when retrieving more results from the server. Since this value is server defined, clients may only use the continue value from a previous query result with identical query parameters (except for the value of continue) and the server may reject a continue value it does not recognize. If the specified continue value is no longer valid whether due to expiration (generally five to fifteen minutes) or a configuration change on the server, the server will respond with a 410 ResourceExpired error together with a continue token. If the client needs a consistent list, it must restart their list without the continue field. Otherwise, the client may send another list request with the token received with the 410 error, the server will respond with a list starting from the next key, but from the latest snapshot, which is inconsistent from the previous list results - objects that are created, modified, or deleted after the first list request will be included in the response, as long as their keys are after the \"next key\".  This field is not supported when watch is true. Clients may start a watch from the last resourceVersion value returned by the server and not miss any modifications.
        String fieldSelector = "fieldSelector_example"; // String | A selector to restrict the list of returned objects by their fields. Defaults to everything.
        String labelSelector = "labelSelector_example"; // String | A selector to restrict the list of returned objects by their labels. Defaults to everything.
        Integer limit = 56; // Integer | limit is a maximum number of responses to return for a list call. If more items exist, the server will set the `continue` field on the list metadata to a value that can be used with the same initial query to retrieve the next set of results. Setting a limit may return fewer than the requested amount of items (up to zero items) in the event all requested objects are filtered out and clients should only use the presence of the continue field to determine whether more results are available. Servers may choose not to support the limit argument and will return all of the available results. If limit is specified and the continue field is empty, clients may assume that no more results are available. This field is not supported if watch is true.  The server guarantees that the objects returned when using continue will be identical to issuing a single list call without a limit - that is, no objects created, modified, or deleted after the first request is issued will be included in any subsequent continued requests. This is sometimes referred to as a consistent snapshot, and ensures that a client that is using limit to receive smaller chunks of a very large result can ensure they see all possible objects. If objects are updated during a chunked list the version of the object that was present at the time the first list result was calculated is returned.
        String resourceVersion = "resourceVersion_example"; // String | When specified with a watch call, shows changes that occur after that particular version of a resource. Defaults to changes from the beginning of history. When specified for list: - if unset, then the result is returned from remote storage based on quorum-read flag; - if it's 0, then we simply return what we currently have in cache, no guarantee; - if set to non zero, then the result is at least as fresh as given rv.
        Integer timeoutSeconds = 56; // Integer | Timeout for the list/watch call. This limits the duration of the call, regardless of any activity or inactivity.
        Boolean watch = true; // Boolean | Watch for changes to the described resources and return them as a stream of add, update, and remove notifications. Specify resourceVersion.
        try {

            // Namespace列表
            V1NamespaceList result = apiInstance.listNamespace(null,pretty,null,null,null,null,null,null,null);

            // Node列表
            // V1NodeList result = apiInstance.listNode(null,pretty,null,null,null,null,null,null,null);

           // Service列表
            // V1ServiceList result = apiInstance.listNamespacedService("kube-system", null, null, null, null, null, null, null, null, null);

            // Service 详情
            // /api/v1/namespaces/kube-system/services/webapi-service
            // V1Service result = apiInstance.readNamespacedService("flyapi-service", "kube-system", null, null, null);

            System.out.println(result);

            // JSON
            Gson gson=new Gson();
            String s = gson.toJson(result);
            System.out.println(s);

        } catch (ApiException e) {
            System.err.println("Exception when calling CoreV1Api#listNode");
            e.printStackTrace();
        }
    }

Deployment增删改查

使用 ExtensionsV1beta1Api apiInstance = new ExtensionsV1beta1Api(); 操作

Method HTTP request Description
createNamespacedDeployment POST /apis/extensions/v1beta1/namespaces/{namespace}/deployments 创建应用
deleteCollectionNamespacedDeployment DELETE/apis/extensions/v1beta1/namespaces/{namespace}/deployments 删除多个应用
deleteNamespacedDeployment DELETE/apis/extensions/v1beta1/namespaces/{namespace}/deployments/{name} 删除应用
listNamespacedDeployment GET /apis/extensions/v1beta1/namespaces/{namespace}/deployments 应用列表
patchNamespacedDeployment PATCH/apis/extensions/v1beta1/namespaces/{namespace}/deployments/{name} 更新应用
readNamespacedDeployment GET/apis/extensions/v1beta1/namespaces/{namespace}/deployments/{name} 查询指定应用
replaceNamespacedDeployment PUT/apis/extensions/v1beta1/namespaces/{namespace}/deployments/{name} 替换指定应用内容

kubernetes-client/java 使用注意(很重要):

  1.  Kubernetes资源内容查询:JSONPath

  2.  kubernetes-client: delete操作异常 Expected a string but was BEGIN_OBJECT at line 1 column 1912

  3.  kubernetes:CPU 和内存单位解释

  4.  kubenetes: patch更新和替换、删除资源内容

  5. kubernetes:字段选择器(field-selector)标签选择器(labels-selector)和筛选 Kubernetes 资源

  6. Kubernetes参数:dryRun理解

  7. kubernetes-client/java:Scale报错400 BadRequest 或 500 cannot unmarshal object

  8. Kubectl :--v 接口调试,以及设置日志输出详细程度

参考链接:

https://k8smeetup.github.io/docs/reference/client-libraries/

kube-apiserver · Kubernetes指南

https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.13/

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/fly910905/article/details/101345091

智能推荐

JavaScript学习笔记_curry函数未定义-程序员宅基地

文章浏览阅读343次。五种原始的变量类型1.Undefined--未定义类型 例:var v;2.String -- ' '或" "3.Boolean4.Number5.Null--空类型 例: var v=null;Number中:NaN -- not a number非数本身是一个数字,但是它和任何数字都不相等,代表非数,它和自己都不相等判断是不是NaN不能用=_curry函数未定义

兑换码编码方案实践_优惠券编码规则-程序员宅基地

文章浏览阅读1.2w次,点赞2次,收藏17次。兑换码编码设计当前各个业务系统,只要涉及到产品销售,就离不开大大小小的运营活动需求,其中最普遍的就是兑换码需求,无论是线下活动或者是线上活动,都能起到良好的宣传效果。兑换码:由一系列字符组成,每一个兑换码对应系统中的一组信息,可以是优惠信息(优惠券),也可以是相关奖品信息。在实际的运营活动中,要求兑换码是唯一的,每一个兑换码对应一个优惠信息,而且需求量往往比较大(实际上的需求只有预期_优惠券编码规则

c语言周林答案,C语言程序设计实训教程教学课件作者周林ch04结构化程序设计课件.ppt...-程序员宅基地

文章浏览阅读45次。C语言程序设计实训教程教学课件作者周林ch04结构化程序设计课件.ppt* * 4.1 选择结构程序设计 4.2 循环结构程序设计 4.3 辅助控制语句 第四章 结构化程序设计 4.1 选择结构程序设计 在现实生活中,需要进行判断和选择的情况是很多的: 如果你在家,我去拜访你 如果考试不及格,要补考 如果遇到红灯,要停车等待 第四章 结构化程序设计 在现实生活中,需要进行判断和选择的情况..._在现实生活中遇到过条件判断的问

幻数使用说明_ioctl-number.txt幻数说明-程序员宅基地

文章浏览阅读999次。幻数使用说明 在驱动程序中实现的ioctl函数体内,实际上是有一个switch{case}结构,每一个case对应一个命令码,做出一些相应的操作。怎么实现这些操作,这是每一个程序员自己的事情。 因为设备都是特定的,这里也没法说。关键在于怎样组织命令码,因为在ioctl中命令码是唯一联系用户程序命令和驱动程序支持的途径 。 命令码的组织是有一些讲究的,因为我们一定要做到命令和设备是一一对应的,利_ioctl-number.txt幻数说明

ORB-SLAM3 + VScode:检测到 #include 错误。请更新 includePath。已为此翻译单元禁用波浪曲线_orb-slam3 include <system.h> 报错-程序员宅基地

文章浏览阅读399次。键盘按下“Shift+Ctrl+p” 输入: C++Configurations,选择JSON界面做如下改动:1.首先把 “/usr/include”,放在最前2.查看C++路径,终端输入gcc -v -E -x c++ - /usr/include/c++/5 /usr/include/x86_64-linux-gnu/c++/5 /usr/include/c++/5/backward /usr/lib/gcc/x86_64-linux-gnu/5/include /usr/local/_orb-slam3 include 报错

「Sqlserver」数据分析师有理由爱Sqlserver之十-Sqlserver自动化篇-程序员宅基地

文章浏览阅读129次。本系列的最后一篇,因未有精力写更多的入门教程,上篇已经抛出书单,有兴趣的朋友可阅读好书来成长,此系列主讲有理由爱Sqlserver的论证性文章,希望读者们看完后,可自行做出判断,Sqlserver是否真的合适自己,目的已达成。渴望自动化及使用场景笔者所最能接触到的群体为Excel、PowerBI用户群体,在Excel中,我们知道可以使用VBA、VSTO来给Excel带来自动化操作..._sqlsever 数据分析

随便推点

智慧校园智慧教育大数据平台(教育大脑)项目建设方案PPT_高校智慧大脑-程序员宅基地

文章浏览阅读294次,点赞6次,收藏4次。教育智脑)建立学校的全连接中台,对学校运营过程中的数据进行处理和标准化管理,挖掘数据的价值。能:一、原先孤立的系统聚合到一个统一的平台,实现单点登录,统一身份认证,方便管理;三、数据共享,盘活了教育大数据资源,通过对外提供数。的方式构建教育的通用服务能力平台,支撑教育核心服务能力的沉淀和共享。物联网将学校的各要素(人、机、料、法、环、测)全面互联,数据实时。智慧校园解决方案,赋能教学、管理和服务升级,智慧教育体系,该数据平台具有以下几大功。教育大数据平台底座:教育智脑。教育大数据平台,以中国联通。_高校智慧大脑

编程5大算法总结--概念加实例_算法概念实例-程序员宅基地

文章浏览阅读9.5k次,点赞2次,收藏27次。分治法,动态规划法,贪心算法这三者之间有类似之处,比如都需要将问题划分为一个个子问题,然后通过解决这些子问题来解决最终问题。但其实这三者之间的区别还是蛮大的。贪心是则可看成是链式结构回溯和分支界限为穷举式的搜索,其思想的差异是深度优先和广度优先一:分治算法一、基本概念在计算机科学中,分治法是一种很重要的算法。字面上的解释是“分而治之”,就是把一个复杂的问题分成两_算法概念实例

随笔—醒悟篇之考研调剂_考研调剂抑郁-程序员宅基地

文章浏览阅读5.6k次。考研篇emmmmm,这是我随笔篇章的第二更,原本计划是在中秋放假期间写好的,但是放假的时候被安排写一下单例模式,做了俩机试题目,还刷了下PAT的东西,emmmmm,最主要的还是因为我浪的很开心,没空出时间来写写东西。  距离我考研结束已经快两年了,距离今年的考研还有90天左右。  趁着这个机会回忆一下青春,这一篇会写的比较有趣,好玩,纯粹是为了记录一下当年考研中发生的有趣的事。  首先介绍..._考研调剂抑郁

SpringMVC_class org.springframework.web.filter.characterenco-程序员宅基地

文章浏览阅读438次。SpringMVC文章目录SpringMVC1、SpringMVC简介1.1 什么是MVC1.2 什么是SpringMVC1.3 SpringMVC的特点2、HelloWorld2.1 开发环境2.2 创建maven工程a>添加web模块b>打包方式:warc>引入依赖2.3 配置web.xml2.4 创建请求控制器2.5 创建SpringMVC的配置文件2.6 测试Helloworld2.7 总结3、@RequestMapping注解3.1 @RequestMapping注解的功能3._class org.springframework.web.filter.characterencodingfilter is not a jakart

gdb: Don‘t know how to run. Try “help target“._don't know how to run. try "help target".-程序员宅基地

文章浏览阅读4.9k次。gdb 远程调试的一个问题:Don't know how to run. Try "help target".它在抱怨不知道怎么跑,目标是什么. 你需要为它指定target remote 或target extended-remote例如:target extended-remote 192.168.1.136:1234指明target 是某IP的某端口完整示例如下:targ..._don't know how to run. try "help target".

c语言程序设计教程 郭浩志,C语言程序设计教程答案杨路明郭浩志-程序员宅基地

文章浏览阅读85次。习题 11、算法描述主要是用两种基本方法:第一是自然语言描述,第二是使用专用工具进行算法描述2、c 语言程序的结构如下:1、c 语言程序由函数组成,每个程序必须具有一个 main 函数作为程序的主控函数。2、“/*“与“*/“之间的内容构成 c 语言程序的注释部分。3、用预处理命令#include 可以包含有关文件的信息。4、大小写字母在 c 语言中是有区别的。5、除 main 函数和标准库函数以..._c语言语法0x1e