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
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 adoptopenjdk: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 (including inner classes) into an executable jar
RUN jar cfe app.jar HelloController *.class

# --------- Run Stage ---------
FROM adoptopenjdk:11-jre-hotspot
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());
}
}
}
}
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: aminmoh9/my-java-app:1.0
ports:
- containerPort: 8080
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
selector:
app: java-app
ports:
- protocol: TCP
port: 80 # The service port
targetPort: 8080 # Container's port
nodePort: 30081 # Node port (optional)
Binary file added screenshots/Screenshot 2025-10-30 170738.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/Screenshot 2025-10-30 171314.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/Screenshot 2025-10-30 171504.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/Screenshot 2025-10-30 172150.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/Screenshot 2025-10-30 172206.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/Screenshot 2025-10-30 172224.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/Screenshot 2025-10-30 172249.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/Screenshot 2025-10-30 172454.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/Screenshot 2025-10-30 172552.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.