基于Kubernetes部署Node.js应用:Express应用部署实操咨询
Hey there, let’s work through getting your Express app deployed on your Kubernetes cluster step by step—since you’ve already got the image built and tagged, we can focus on the critical deployment pieces. Here are the key checks and fixes for common hurdles:
First: Push Your Tagged Docker Image to the Registry
Kubernetes can’t pull your image if it’s only stored locally. Run this command to push your tagged image to your container registry:docker push org/test:firstNote: Double-check your tag name—you mentioned
org/test:firs...which looks truncated. Make sure the tag in this command matches exactly what you created withdocker tag.Create a Kubernetes Deployment Manifest
You need a deployment YAML to define how your app runs in the cluster. Save this asdeployment.yaml:apiVersion: apps/v1 kind: Deployment metadata: name: express-app-deployment labels: app: express-app spec: replicas: 2 # Start with 2 pods for basic redundancy selector: matchLabels: app: express-app template: metadata: labels: app: express-app spec: containers: - name: express-app image: org/test:first # Match your pushed image tag exactly ports: - containerPort: 8080 # Match your Express app's listening port resources: requests: cpu: "100m" memory: "128Mi" limits: cpu: "200m" memory: "256Mi"Apply it to your cluster with:
kubectl apply -f deployment.yamlExpose Your App with a Kubernetes Service
To access your app from outside the cluster (since you’re on EC2), use aLoadBalancerservice—it will automatically provision an AWS ELB. Save this asservice.yaml:apiVersion: v1 kind: Service metadata: name: express-app-service spec: type: LoadBalancer selector: app: express-app # Matches the label in your deployment ports: - protocol: TCP port: 80 # External access port targetPort: 8080 # Maps to your container's internal portApply the service:
kubectl apply -f service.yamlWait 3-5 minutes for AWS to assign an external IP, then check it with:
kubectl get servicesYou can then access your app at
http://<external-ip>.Validate Cluster and Pod Health
If your pods aren’t starting, run these debug commands:- Check if all cluster nodes are ready:
kubectl get nodes - Check pod statuses:
kubectl get pods - If you see
ImagePullBackOff, it means Kubernetes can’t pull your image. Double-check the image tag, or if it’s a private registry, add image pull secrets (see next point).
- Check if all cluster nodes are ready:
Handle Private Registry Image Pulls
If your Docker image is in a private repository, create a Kubernetes secret to store your Docker credentials:kubectl create secret docker-registry regcred \ --docker-server=docker.io \ --docker-username=ccastelli \ --docker-password=<your-docker-password> \ --docker-email=<your-email>Then update your
deployment.yamlto include the secret underspec:spec: containers: - name: express-app image: org/test:first ports: - containerPort: 8080 imagePullSecrets: - name: regcredRe-apply the deployment with
kubectl apply -f deployment.yaml.Verify Your Express App’s Listening Address
A super common gotcha: make sure your Express app listens on0.0.0.0instead oflocalhost—otherwise it won’t be accessible outside the container. Your code should look like this:app.listen(8080, '0.0.0.0', () => { console.log('Server running on port 8080'); });If you had
localhosthere, rebuild and re-push your image before re-deploying.
内容的提问来源于stack exchange,提问作者Chris

