Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
25 changes: 25 additions & 0 deletions java-k8s-lab/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
27 changes: 27 additions & 0 deletions java-k8s-lab/HelloController.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
}
35 changes: 35 additions & 0 deletions java-k8s-lab/\
Original file line number Diff line number Diff line change
@@ -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: {}
33 changes: 33 additions & 0 deletions java-k8s-lab/deployment-with-probes.yaml
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions java-k8s-lab/deployment.yaml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions java-k8s-lab/service-probes.yaml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions java-k8s-lab/service.yaml
Original file line number Diff line number Diff line change
@@ -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)