## From "Hello World" to Kubernetes: An End-to-End Guide
- Mark Kendall
- 2 days ago
- 3 min read
## From "Hello World" to Kubernetes: An End-to-End Guide
This article will guide you through the process of taking a simple "Hello World" Spring Boot application, containerizing it with Docker, automating the build and push process using GitHub Actions (including the crucial step of using secrets), and finally, making it ready for deployment on Kubernetes.
### 1. Crafting the "Hello World" Spring Boot Application
```java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/")
public String hello() {
return "Hello, World!";
}
}
```
Ensure you have the `spring-boot-starter-web` dependency in your project's build file.
### 2. Dockerizing for Containerization
```dockerfile
FROM openjdk:17-jdk-alpine
WORKDIR /app
COPY target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app/app.jar"]
```
Build and run locally:
```bash
docker build -t your-dockerhub-username/hello-world-app:latest .
docker run -d -p 8080:8080 your-dockerhub-username/hello-world-app:latest
```
### 3. Automating with GitHub Actions (Including Secrets Setup)
Now, let's set up the automation using GitHub Actions, focusing on how to add the necessary secrets.
#### 3.1. Setting up Secrets in GitHub (Step-by-Step UI)
Here's how to add your Docker Hub credentials as secrets in your GitHub repository:
1. Navigate to your Repository: Go to the main page of your GitHub repository.
2. Click on "Settings": You'll find this tab towards the right of the menu bar (next to "Insights").
3. Go to "Secrets and variables" then "Actions": In the left sidebar, scroll down until you see "Secrets and variables". Click on it, and then click on "Actions".
4. Click "New repository secret": On the "Actions secrets" page, you'll see a button labeled "New repository secret". Click on it.
5. Add the `DOCKERHUB_USERNAME` secret:
* In the "Name" field, type: `DOCKERHUB_USERNAME`
* In the "Value" field, enter your Docker Hub username.
* Click the "Add secret" button.
6. Add the `DOCKERHUB_TOKEN` secret:
* Click the "New repository secret" button again.
* In the "Name" field, type: `DOCKERHUB_TOKEN`
* In the "Value" field, enter your Docker Hub personal access token (generate one from your Docker Hub settings).
* Click the "Add secret" button.
You should now see `DOCKERHUB_USERNAME` and `DOCKERHUB_TOKEN` listed under your repository secrets.
#### 3.2. Creating the GitHub Actions Workflow
Create `.github/workflows/ci-cd.yml`:
```yaml
name: CI/CD Pipeline
on:
push:
branches:
- main
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
- name: Build with Maven
run: ./mvnw clean package -DskipTests
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push Docker image
run: |
docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/hello-world-app:${{ github.sha }} .
docker push ${{ secrets.DOCKERHUB_USERNAME }}/hello-world-app:${{ github.sha }}
docker tag ${{ secrets.DOCKERHUB_USERNAME }}/hello-world-app:${{ github.sha }} ${{ secrets.DOCKERHUB_USERNAME }}/hello-world-app:latest
docker push ${{ secrets.DOCKERHUB_USERNAME }}/hello-world-app:latest
```
### 4. Ready for Kubernetes: Deployment Manifest
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world-app
spec:
replicas: 2
selector:
matchLabels:
app: hello-world-app
template:
metadata:
labels:
app: hello-world-app
spec:
containers:
- name: hello-world-container
image: your-dockerhub-username/hello-world-app:latest
ports:
- containerPort: 8080
```
### 5. Ready for Kubernetes: Service Manifest
```yaml
apiVersion: v1
kind: Service
metadata:
name: hello-world-service
spec:
selector:
app: hello-world-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
```
### Deploying to Kubernetes
```bash
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
```
```bash
kubectl get deployments
kubectl get services
```
---
Comments