Java连接K8s_6-java操作k8s

java 操作k8s 这里使用的是 fabric8

1:添加maven ,引入依赖

io.fabric8

kubernetes-client

3.1.12

/**

* 生成kubernetes client实体

*

* @return

*/

2:public KubernetesClient getClient() {

List list = clusterService.get();

String clusterIp = "http://" + clusterIp;

Config config = new ConfigBuilder().withMasterUrl(masterURL).build();

return new DefaultKubernetesClient(config);

}

3:然后可以通过getClient()点出来好多东西

Java连接K8s_6-java操作k8s_第1张图片

也可以查看api信息。获取不同的操作

4:根据fabric8提供的这些操就可以创建删除对应的组件了

来个例子吧 创建Deployment

// Deployment

public Deployment createDeployment(String namespace, Deployment deployment) {

return getClient().extensions().deployments().inNamespace(namespace).create(deployment);

}

关键就是组装Deployment 数据

巧妙的是fabric8 提供了构建方法如下

createDeployment(namespace, new DeploymentBuilder().withKind("Deployment")

.withApiVersion("extensions/v1beta1")

.withNewMetadata()

.withName(ku8Deployment.getName())

.withNamespace(namespace)

.withLabels(label)

.endMetadata()

.withNewSpec()

.withReplicas(replica)

.withSelector(selector)

.withNewTemplate()

.withNewMetadata()

.withLabels(podSelector)

.endMetadata()

.withNewSpec()

.withHostNetwork(ku8Deployment.isHostNetwork())

.withVolumes(f8Volumes)

.withContainers(f8Containers)

.endSpec()

.endTemplate()

.endSpec().build());

是不是一头雾水呀,这数据怎么来搞呀,谁的后面跟谁呀。别急 听我慢慢说

其实格式很简单 登录k8s 集群,查看一下 deployment

[root@localhost ~]# kg deployment

NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE

api-getway 1 1 1 1 1d

kube-dns 1 1 1 1 1d

traefik-ingress-controller 1 1 1 1 1d

然后干什么呢,随便找一个deployment 这里只是个我集群上的deployment例子 执行如下

[root@localhost ~]# kg deploymentapi-getway -o yaml

apiVersion: extensions/v1beta1

kind: Deployment

metadata:

annotations:

deployment.kubernetes.io/revision: "1"

creationTimestamp: 2018-05-28T15:29:02Z

generation: 1

labels:

k8s-app: api-getway

task: monitoring

name: api-getway

namespace: kube-system

resourceVersion: "94687"

selfLink: /apis/extensions/v1beta1/namespaces/kube-system/deployments/api-getway

uid: d990741b-628b-11e8-86b9-000c2938aca0

spec:

replicas: 1

selector:

matchLabels:

k8s-app: api-getway

task: monitoring

strategy:

rollingUpdate:

maxSurge: 1

maxUnavailable: 1

type: RollingUpdate

template:

metadata:

creationTimestamp: null

labels:

k8s-app: api-getway

task: monitoring

spec:

containers:

- image: muhaifeng/api-getway

imagePullPolicy: Always

name: api-getway

resources: {}

terminationMessagePath: /dev/termination-log

terminationMessagePolicy: File

dnsPolicy: ClusterFirst

restartPolicy: Always

schedulerName: default-scheduler

securityContext: {}

terminationGracePeriodSeconds: 30

status:

availableReplicas: 1

conditions:

- lastTransitionTime: 2018-05-28T15:29:02Z

lastUpdateTime: 2018-05-28T15:29:02Z

message: Deployment has minimum availability.

reason: MinimumReplicasAvailable

status: "True"

type: Available

observedGeneration: 1

readyReplicas: 1

replicas: 1

updatedReplicas: 1

好了格式出来了。不要怂就是干,

new DeploymentBuilder().withKind("Deployment")

.withApiVersion("extensions/v1beta1")

.withNewMetadata()

.withName(ku8Deployment.getName())

.withNamespace(namespace)

.withLabels(label)

.endMetadata()

.withNewSpec()

.withReplicas(replica)

.withSelector(selector)

.withNewTemplate()

.withNewMetadata()

.withLabels(podSelector)

.endMetadata()

.withNewSpec()

.withHostNetwork(ku8Deployment.isHostNetwork())

.withVolumes(f8Volumes)

.withContainers(f8Containers)

.endSpec()

.endTemplate()

.endSpec().build());

这个格式就是上面集群中那个格式,照着上面的那个集群中查到的格式往下拼。 就可以了。

拼完之后。跑一下程序,在集群中查看一下就出来了。

同样的,根据fabric8 提供的接口,基本都能实现 操作k8s集群。

你可能感兴趣的:(Java连接K8s)