如何在Kubernetes中部署Spring Boot版Hello World应用至云端?
Great job getting your Spring Boot app containerized, pushed to Docker Hub, and setting up local Kubernetes manifests! Let's break down exactly what you need to do to get this running on Google Cloud:
Step 1: Set Up Your GCloud Environment
First, make sure you have the Google Cloud CLI installed on your machine. If not, you can download it directly from the GCP console's tools section. Then:
- Authenticate your GCP account to link your local CLI with your cloud project:
gcloud auth login - Set your active GCP project (replace
YOUR_PROJECT_IDwith the actual ID from your GCP console):gcloud config set project YOUR_PROJECT_ID
Step 2: Create a Google Kubernetes Engine (GKE) Cluster
You’re absolutely right—you need a Kubernetes cluster first. GKE handles all the underlying infrastructure management, so you don’t have to set up or maintain nodes manually. Run this command to create a small, test-friendly cluster (adjust the zone and node count based on your location or needs):
gcloud container clusters create hello-world-cluster --zone us-central1-a --num-nodes 1
--zone: Pick a region close to your users for lower latency (e.g.,europe-west1-bif you’re based in Europe)--num-nodes: 1 node is more than enough for a simple Hello World app; you can scale up later if needed
Step 3: Connect kubectl to Your GKE Cluster
After creating the cluster, you need to redirect your local kubectl tool to target the GKE cluster instead of your local Kubernetes instance (like Minikube or Docker Desktop):
gcloud container clusters get-credentials hello-world-cluster --zone us-central1-a
Verify this worked by running kubectl get nodes—you should see your GKE node listed with a Ready status.
Step 4: Deploy Your App to GKE
You already have your deployment.yaml file ready to go! Just apply it to the GKE cluster:
kubectl apply -f deployment.yaml
Step 5: Verify the Deployment & Expose Your App
First, check that your pods are up and running:
kubectl get pods
Wait until you see the STATUS field show Running (this might take a minute while GKE pulls your Docker Hub image). Then check your service:
kubectl get svc
Right now, your service uses ClusterIP—this means it’s only accessible inside the cluster. To make it reachable from the internet, you have two simple options:
Option 1: Convert to LoadBalancer (Quickest for Testing)
Run this command to update your service type to LoadBalancer, which will assign a public IP address:
kubectl patch service hello-world-dep -p '{"spec":{"type":"LoadBalancer"}}'
Wait 2-3 minutes, then run kubectl get svc again. You’ll see an EXTERNAL-IP listed—now you can access your app at http://<EXTERNAL-IP>:8080/hello/
Option 2: Use an Ingress (For Future Scaling)
If you plan to add a custom domain or HTTPS later, you can set up an Ingress resource. But for a simple Hello World test, LoadBalancer is the fastest path.
Step 6: Clean Up (Optional)
When you’re done testing, delete the cluster to avoid unnecessary GCP charges:
gcloud container clusters delete hello-world-cluster --zone us-central1-a
Quick Tips
- Ensure your Docker Hub image is public—if it’s private, you’ll need to create a Kubernetes Secret to let GKE authenticate to Docker Hub and pull the image.
- GKE offers free tier resources (like one node in specific regions) for testing, so you can run this without incurring costs if you stay within those limits.
内容的提问来源于stack exchange,提问作者Oliver Watkins

