6-java操作k8s

java 操作k8s 这里使用的是 fabric8
1:添加maven ,引入依赖
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>3.1.12</version>
</dependency>
 
 
/**
* 生成kubernetes client实体
*
* @return
*/
2:public KubernetesClient getClient() {
List<Ku8Cluster> list = clusterService.get();
String clusterIp = "http://" + clusterIp;
Config config = new ConfigBuilder().withMasterUrl(masterURL).build();
return new DefaultKubernetesClient(config);
}
 
3:然后可以通过 getClient()点出来好多东西
也可以查看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 deployment api-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集群。
 
 

转载于:https://www.cnblogs.com/tdf-/p/9330028.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值