-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathPodDemo.java
54 lines (46 loc) · 1.75 KB
/
PodDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package io.fabric8;
import java.util.logging.Level;
import java.util.logging.Logger;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
/*
* Spins up simple pods using PodBuilder() and yaml files on cluster
*/
public class PodDemo {
private static final Logger logger = Logger.getLogger(PodDemo.class
.getName());
public static void main(String[] args) {
String namespace = "default";
try {
final KubernetesClient client = new KubernetesClientBuilder().build();
Pod aPod = new PodBuilder().withNewMetadata().withName("demo-pod1")
.endMetadata().withNewSpec().addNewContainer()
.withName("nginx").withImage("nginx:1.7.9").addNewPort()
.withContainerPort(80).endPort().endContainer().endSpec()
.build();
/*
* Creating a simple pod from Builder.
*/
logger.log(Level.INFO, "Creating pod demo-pod1");
client.pods().inNamespace(namespace).resource(aPod).create();
logger.log(Level.INFO, "Successfully created pod");
/*
* Creating pod from yaml file
*/
Pod pod2 = client.pods().inNamespace(namespace).load(PodDemo.class.getResourceAsStream("/test-redis.yml")).item();
client.pods().inNamespace(namespace).resource(pod2).create();
logger.log(Level.INFO, "created " + pod2.getMetadata().getName());
client.close();
} catch (KubernetesClientException aException) {
logger.log(Level.SEVERE,
"Problem encountered with Kubernetes client!!");
aException.printStackTrace();
} catch (Exception e) {
logger.log(Level.SEVERE,
"Exception encountered : " + e.getMessage());
}
}
}