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集羣。
相關文章
相關標籤/搜索