diff --git a/spring-cloud/pom.xml b/spring-cloud/pom.xml index c093b87be3..4d320f8fda 100644 --- a/spring-cloud/pom.xml +++ b/spring-cloud/pom.xml @@ -22,6 +22,7 @@ spring-cloud-consul spring-cloud-zuul-eureka-integration spring-cloud-contract + spring-cloud-kubernetes pom diff --git a/spring-cloud/spring-cloud-kubernetes/demo-backend/.gitignore b/spring-cloud/spring-cloud-kubernetes/demo-backend/.gitignore new file mode 100644 index 0000000000..2af7cefb0a --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes/demo-backend/.gitignore @@ -0,0 +1,24 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes/demo-backend/Dockerfile b/spring-cloud/spring-cloud-kubernetes/demo-backend/Dockerfile new file mode 100644 index 0000000000..a0a9d57662 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes/demo-backend/Dockerfile @@ -0,0 +1,5 @@ +FROM openjdk:8-jdk-alpine +VOLUME /tmp +COPY target/demo-backend-1.0-SNAPSHOT.jar app.jar +ENV JAVA_OPTS="" +ENTRYPOINT exec java -jar /app.jar --debug \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes/demo-backend/pom.xml b/spring-cloud/spring-cloud-kubernetes/demo-backend/pom.xml new file mode 100644 index 0000000000..b951671612 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes/demo-backend/pom.xml @@ -0,0 +1,43 @@ + + + 4.0.0 + + + com.baeldung.spring.cloud + spring-cloud-kubernetes + 1.0-SNAPSHOT + + demo-backend + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/spring-cloud/spring-cloud-kubernetes/demo-backend/src/main/java/com/baeldung/spring/cloud/kubernetes/backend/KubernetesBackendApplication.java b/spring-cloud/spring-cloud-kubernetes/demo-backend/src/main/java/com/baeldung/spring/cloud/kubernetes/backend/KubernetesBackendApplication.java new file mode 100644 index 0000000000..1605d59f28 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes/demo-backend/src/main/java/com/baeldung/spring/cloud/kubernetes/backend/KubernetesBackendApplication.java @@ -0,0 +1,24 @@ +package com.baeldung.spring.cloud.kubernetes.backend; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +@SpringBootApplication +@RestController +public class KubernetesBackendApplication { + + public static void main(String[] args) { + SpringApplication.run(KubernetesBackendApplication.class, args); + } + + @GetMapping("") + public String helloWorld() throws UnknownHostException { + + return "Hello from " + InetAddress.getLocalHost().getHostName(); + } +} diff --git a/spring-cloud/spring-cloud-kubernetes/demo-backend/src/main/resources/application.properties b/spring-cloud/spring-cloud-kubernetes/demo-backend/src/main/resources/application.properties new file mode 100644 index 0000000000..a3ac65cee5 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes/demo-backend/src/main/resources/application.properties @@ -0,0 +1 @@ +server.port=8080 \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes/demo-backend/src/test/java/com/baeldung/spring/cloud/kubernetes/backend/KubernetesBackendApplicationTests.java b/spring-cloud/spring-cloud-kubernetes/demo-backend/src/test/java/com/baeldung/spring/cloud/kubernetes/backend/KubernetesBackendApplicationTests.java new file mode 100644 index 0000000000..5ccc49eaa7 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes/demo-backend/src/test/java/com/baeldung/spring/cloud/kubernetes/backend/KubernetesBackendApplicationTests.java @@ -0,0 +1,16 @@ +package com.baeldung.spring.cloud.kubernetes.backend; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class KubernetesBackendApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/spring-cloud/spring-cloud-kubernetes/demo-frontend/.gitignore b/spring-cloud/spring-cloud-kubernetes/demo-frontend/.gitignore new file mode 100644 index 0000000000..2af7cefb0a --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes/demo-frontend/.gitignore @@ -0,0 +1,24 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes/demo-frontend/Dockerfile b/spring-cloud/spring-cloud-kubernetes/demo-frontend/Dockerfile new file mode 100644 index 0000000000..69adb03a17 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes/demo-frontend/Dockerfile @@ -0,0 +1,5 @@ +FROM openjdk:8-jdk-alpine +VOLUME /tmp +COPY target/demo-frontend-1.0-SNAPSHOT.jar app.jar +ENV JAVA_OPTS="" +ENTRYPOINT exec java -jar /app.jar \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes/demo-frontend/pom.xml b/spring-cloud/spring-cloud-kubernetes/demo-frontend/pom.xml new file mode 100644 index 0000000000..2231556149 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes/demo-frontend/pom.xml @@ -0,0 +1,43 @@ + + + 4.0.0 + + + com.baeldung.spring.cloud + spring-cloud-kubernetes + 1.0-SNAPSHOT + + demo-frontend + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/spring-cloud/spring-cloud-kubernetes/demo-frontend/src/main/java/com/baeldung/spring/cloud/kubernetes/frontend/KubernetesFrontendApplication.java b/spring-cloud/spring-cloud-kubernetes/demo-frontend/src/main/java/com/baeldung/spring/cloud/kubernetes/frontend/KubernetesFrontendApplication.java new file mode 100644 index 0000000000..635a79e8bb --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes/demo-frontend/src/main/java/com/baeldung/spring/cloud/kubernetes/frontend/KubernetesFrontendApplication.java @@ -0,0 +1,30 @@ +package com.baeldung.spring.cloud.kubernetes.frontend; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +import java.net.UnknownHostException; + +@RestController +@SpringBootApplication +public class KubernetesFrontendApplication { + + public static void main(String[] args) { + SpringApplication.run(KubernetesFrontendApplication.class, args); + } + + @GetMapping + public String helloWorld() throws UnknownHostException { + + RestTemplate restTemplate = new RestTemplate(); + String resourceUrl + = "http://demo-backend:8080"; + ResponseEntity response + = restTemplate.getForEntity(resourceUrl, String.class); + return "Message from backend is: " + response.getBody(); + } +} diff --git a/spring-cloud/spring-cloud-kubernetes/demo-frontend/src/main/resources/application.properties b/spring-cloud/spring-cloud-kubernetes/demo-frontend/src/main/resources/application.properties new file mode 100644 index 0000000000..bafddced85 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes/demo-frontend/src/main/resources/application.properties @@ -0,0 +1 @@ +server.port=8081 \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes/demo-frontend/src/test/java/com/baeldung/spring/cloud/kubernetes/frontend/KubernetesFrontendApplicationTests.java b/spring-cloud/spring-cloud-kubernetes/demo-frontend/src/test/java/com/baeldung/spring/cloud/kubernetes/frontend/KubernetesFrontendApplicationTests.java new file mode 100644 index 0000000000..0e34eb45f8 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes/demo-frontend/src/test/java/com/baeldung/spring/cloud/kubernetes/frontend/KubernetesFrontendApplicationTests.java @@ -0,0 +1,16 @@ +package com.baeldung.spring.cloud.kubernetes.frontend; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class KubernetesFrontendApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/spring-cloud/spring-cloud-kubernetes/object-configurations/backend-deployment.yaml b/spring-cloud/spring-cloud-kubernetes/object-configurations/backend-deployment.yaml new file mode 100644 index 0000000000..a7e26a6ec5 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes/object-configurations/backend-deployment.yaml @@ -0,0 +1,36 @@ +kind: Service +apiVersion: v1 +metadata: + name: demo-backend +spec: + selector: + app: demo-backend + tier: backend + ports: + - protocol: TCP + port: 8080 + type: ClusterIP +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: demo-backend +spec: + selector: + matchLabels: + app: demo-backend + tier: backend + replicas: 3 + template: + metadata: + labels: + app: demo-backend + tier: backend + spec: + containers: + - name: demo-backend + image: demo-backend:latest + imagePullPolicy: Never + ports: + - containerPort: 8080 + diff --git a/spring-cloud/spring-cloud-kubernetes/object-configurations/frontend-deployment.yaml b/spring-cloud/spring-cloud-kubernetes/object-configurations/frontend-deployment.yaml new file mode 100644 index 0000000000..bf44dce21c --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes/object-configurations/frontend-deployment.yaml @@ -0,0 +1,34 @@ +kind: Service +apiVersion: v1 +metadata: + name: demo-frontend +spec: + selector: + app: demo-frontend + ports: + - protocol: TCP + port: 8081 + nodePort: 30001 + type: NodePort +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: demo-frontend +spec: + selector: + matchLabels: + app: demo-frontend + replicas: 3 + template: + metadata: + labels: + app: demo-frontend + spec: + containers: + - name: demo-frontend + image: demo-frontend:latest + imagePullPolicy: Never + ports: + - containerPort: 8081 + diff --git a/spring-cloud/spring-cloud-kubernetes/pom.xml b/spring-cloud/spring-cloud-kubernetes/pom.xml new file mode 100644 index 0000000000..0e34de5a14 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes/pom.xml @@ -0,0 +1,26 @@ + + + + com.baeldung.spring.cloud + spring-cloud-kubernetes + 1.0-SNAPSHOT + pom + + + demo-frontend + demo-backend + + + + parent-boot-5 + com.baeldung + 0.0.1-SNAPSHOT + ../../parent-boot-5 + + 4.0.0 + + + + \ No newline at end of file