diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..7fd6a54 Binary files /dev/null and b/.DS_Store differ diff --git a/java-k8s-lab/Dockerfile b/java-k8s-lab/Dockerfile new file mode 100644 index 0000000..0583754 --- /dev/null +++ b/java-k8s-lab/Dockerfile @@ -0,0 +1,25 @@ +# --------- Build Stage --------- +FROM openjdk:11 as build +WORKDIR /app + +# Copy the Java source file into the container +COPY HelloController.java . + +# Compile the Java source file +RUN javac HelloController.java + +# Package all compiled classes into an executable jar +RUN jar cfe app.jar HelloController *.class + +# --------- Run Stage --------- +FROM openjdk:11-jre +WORKDIR /app + +# Copy the executable jar from the build stage +COPY --from=build /app/app.jar . + +# Expose the port on which the app listens +EXPOSE 8080 + +# Run the jar file +ENTRYPOINT ["java", "-jar", "app.jar"] \ No newline at end of file diff --git a/java-k8s-lab/HelloController.java b/java-k8s-lab/HelloController.java new file mode 100644 index 0000000..e684120 --- /dev/null +++ b/java-k8s-lab/HelloController.java @@ -0,0 +1,27 @@ +import com.sun.net.httpserver.HttpServer; +import com.sun.net.httpserver.HttpHandler; +import com.sun.net.httpserver.HttpExchange; +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; + +public class HelloController { + public static void main(String[] args) throws IOException { + HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0); + server.createContext("/", new MyHandler()); + server.setExecutor(null); + server.start(); + System.out.println("Server started on port 8080..."); + } + + static class MyHandler implements HttpHandler { + @Override + public void handle(HttpExchange t) throws IOException { + String response = "Hello, Kubernetes!"; + t.sendResponseHeaders(200, response.length()); + try (OutputStream os = t.getResponseBody()) { + os.write(response.getBytes()); + } + } + } +} \ No newline at end of file diff --git "a/java-k8s-lab/\\" "b/java-k8s-lab/\\" new file mode 100644 index 0000000..2f4a991 --- /dev/null +++ "b/java-k8s-lab/\\" @@ -0,0 +1,35 @@ +:a# Please edit the object below. Lines beginning with a '#' will be ignored, +# and an empty file will abort the edit. If an error occurs while saving this file will be +# reopened with the relevant failures. +# +apiVersion: v1 +kind: Service +metadata: + annotations: + kubectl.kubernetes.io/last-applied-configuration: | + {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"java-app-service","namespace":"default"},"spec":{"ports":[{"nodePort":30080,"port":80,"protocol":"TCP","targetPort":8080}],"selector":{"app":"java-app"},"type":"NodePort"}} + creationTimestamp: "2025-10-30T15:44:34Z" + name: java-app-service + namespace: default + resourceVersion: "29843" + uid: 88426e7d-7dc0-4403-98c9-ea4f622e4800 +spec: + clusterIP: 10.98.17.126 + clusterIPs: + - 10.98.17.126 + externalTrafficPolicy: Cluster + internalTrafficPolicy: Cluster + ipFamilies: + - IPv4 + ipFamilyPolicy: SingleStack + ports: + - nodePort: 30080 + port: 80 + protocol: TCP + targetPort: 8080 + selector: + app: java-app-probes + sessionAffinity: None + type: NodePort +status: + loadBalancer: {} diff --git a/java-k8s-lab/deployment-with-probes.yaml b/java-k8s-lab/deployment-with-probes.yaml new file mode 100644 index 0000000..1c6174c --- /dev/null +++ b/java-k8s-lab/deployment-with-probes.yaml @@ -0,0 +1,33 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: java-app-with-probes +spec: + replicas: 3 + selector: + matchLabels: + app: java-app-probes + template: + metadata: + labels: + app: java-app-probes + spec: + containers: + - name: java-app-container + image: gfiamon/my-java-app:1.0 # ← Use your actual username + ports: + - containerPort: 8080 + livenessProbe: + httpGet: + path: / + port: 8080 + initialDelaySeconds: 15 # Wait 15s after container starts + periodSeconds: 10 # Check every 10 seconds + timeoutSeconds: 5 # Wait max 5s for response + failureThreshold: 3 # 3 failures = unhealthy + readinessProbe: + httpGet: + path: / + port: 8080 + initialDelaySeconds: 5 + periodSeconds: 5 diff --git a/java-k8s-lab/deployment.yaml b/java-k8s-lab/deployment.yaml new file mode 100644 index 0000000..0363025 --- /dev/null +++ b/java-k8s-lab/deployment.yaml @@ -0,0 +1,19 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: java-app-deployment +spec: + replicas: 1 + selector: + matchLabels: + app: java-app + template: + metadata: + labels: + app: java-app + spec: + containers: + - name: java-app-container + image: gfiamon/my-java-app:1.0 # ← Replace with your username + ports: + - containerPort: 8080 diff --git a/java-k8s-lab/service-probes.yaml b/java-k8s-lab/service-probes.yaml new file mode 100644 index 0000000..2313f84 --- /dev/null +++ b/java-k8s-lab/service-probes.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Service +metadata: + name: java-app-service-probes +spec: + type: NodePort + selector: + app: java-app-probes # Matches our probe deployment + ports: + - protocol: TCP + port: 80 + targetPort: 8080 + nodePort: 30081 # Different port to avoid conflict diff --git a/java-k8s-lab/service.yaml b/java-k8s-lab/service.yaml new file mode 100644 index 0000000..5238770 --- /dev/null +++ b/java-k8s-lab/service.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Service +metadata: + name: java-app-service +spec: + type: NodePort # Makes it accessible from outside + selector: + app: java-app # Finds pods with this label + ports: + - protocol: TCP + port: 80 # Service port (internal) + targetPort: 8080 # Pod port (where your Java app runs) + nodePort: 30080 # External port (for your browser) \ No newline at end of file