├── .gitignore
├── pom.xml
├── readme.adoc
└── src
└── main
├── java
└── org
│ └── example
│ └── webapp
│ ├── Database.java
│ └── Main.java
└── webapp
├── WEB-INF
└── beans.xml
└── index.jsp
/.gitignore:
--------------------------------------------------------------------------------
1 | nb-configuration.xml
2 | /target/
3 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 | org.sample
5 | kubernetes-maven-sample
6 | 1.0-SNAPSHOT
7 | jar
8 | kubernetes-maven-sample
9 |
10 | 1.8
11 | arungupta/${project.artifactId}
12 |
13 |
14 |
15 | com.couchbase.client
16 | java-client
17 | 2.4.0
18 |
19 |
20 |
21 | ${project.name}
22 |
23 |
24 | org.apache.maven.plugins
25 | maven-shade-plugin
26 | 2.3
27 |
28 | false
29 |
30 |
31 |
32 | package
33 |
34 | shade
35 |
36 |
37 |
38 |
39 |
40 | io.fabric8
41 | fabric8-maven-plugin
42 | 3.2.14
43 |
44 |
45 |
46 | resource
47 | build
48 |
49 |
50 |
51 |
52 |
53 | org.codehaus.mojo
54 | exec-maven-plugin
55 | 1.5.0
56 |
57 |
58 |
59 | java
60 |
61 |
62 |
63 |
64 | org.example.webapp.Main
65 |
66 |
67 |
68 |
69 |
70 |
--------------------------------------------------------------------------------
/readme.adoc:
--------------------------------------------------------------------------------
1 | = Kubernetes Maven Sample
2 |
3 | This example will show how to deploy a Java application to a Kubernetes cluster using Maven. It requires to run a http://developer.couchbase.com/server[Couchbase server] that can be easily started on https://github.com/couchbase-guides/couchbase-amazon-cli[AWS] or using https://github.com/couchbase-guides/couchbase-docker[Docker].
4 |
5 | == Main Targets
6 |
7 | . `package`: Builds the WAR file
8 | . `install`: Builds the WAR and Docker image using it
9 | . `exec:java`: Runs the application with a Couchbase server running on `localhost`
10 | . `fabric8:apply`: Deploys the application to Kubernetes, require `install` target to be invoked first
11 | .. By default, the Kubernetes cluster is `localhost`. Set KUBERNETES_MASTER environment variable to point to Kubernetes master.
12 |
13 | == Additional Targets
14 |
15 | . `fabric8:build`: Builds the Docker image and generated Kubernetes resources
16 | . `fabric8:resource`: Generates the Kubernetes configuration file in `target/classes/META-INF/fabric8` directory
17 | . `fabric8:cluster-start`: Bootstrap a Kubernetes cluster using minikube (download, install and run)
18 |
19 | More content on deploying your Java applications with Kubernetes at https://github.com/arun-gupta/kubernetes-java-sample/.
20 |
21 |
--------------------------------------------------------------------------------
/src/main/java/org/example/webapp/Database.java:
--------------------------------------------------------------------------------
1 | package org.example.webapp;
2 |
3 | import com.couchbase.client.java.Bucket;
4 | import com.couchbase.client.java.CouchbaseCluster;
5 | import java.util.concurrent.TimeUnit;
6 |
7 | /**
8 | * @author arungupta
9 | */
10 | public class Database {
11 |
12 | static CouchbaseCluster cluster;
13 | static Bucket bucket;
14 |
15 | public static final CouchbaseCluster getCluster() {
16 | if (null == cluster) {
17 | System.out.println(System.getenv());
18 | String host = System.getenv("DB_URI");
19 | if (null == host) {
20 | host = "localhost";
21 | System.out.println("Invalid host, setting to " + host);
22 | }
23 | System.out.println("Using host: " + host);
24 | cluster = CouchbaseCluster.create(host);
25 | }
26 | return cluster;
27 | }
28 |
29 | public static Bucket getBucket(String bucketName) {
30 | if (null == bucket) {
31 | bucket = getCluster().openBucket(bucketName, 30, TimeUnit.SECONDS);
32 | }
33 | return bucket;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/org/example/webapp/Main.java:
--------------------------------------------------------------------------------
1 | package org.example.webapp;
2 |
3 | import com.couchbase.client.java.Bucket;
4 | import com.couchbase.client.java.document.JsonDocument;
5 | import com.couchbase.client.java.document.json.JsonObject;
6 |
7 | /**
8 | * @arungupta
9 | */
10 | public class Main {
11 |
12 | public static void main(String[] args) {
13 | Bucket bucket = Database.getBucket("books");
14 |
15 | JsonObject jsonObject = JsonObject.create();
16 | jsonObject.put("isbn", "978-1-4919-1889-0");
17 | jsonObject.put("name", "Minecraft Modding with Forge");
18 | jsonObject.put("cost", "29.99");
19 | JsonDocument document = JsonDocument.create("minecraft", jsonObject);
20 | bucket.upsert(document);
21 |
22 | JsonDocument doc = bucket.get("minecraft");
23 | System.out.println(doc.content());
24 |
25 | System.out.println("Hello World!");
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/webapp/WEB-INF/beans.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
--------------------------------------------------------------------------------
/src/main/webapp/index.jsp:
--------------------------------------------------------------------------------
1 | <%@page contentType="text/html" pageEncoding="UTF-8"%>
2 |
3 |
4 |
5 |
6 | Hello Java EE 7 Continuous Delivery!
7 |
8 |
9 | Hello Java EE 7 Continuous Delivery!
10 |
11 | GET all the persons.
12 |
13 |
14 |
--------------------------------------------------------------------------------