You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Kubernetes部署Node.js应用:Express应用部署实操咨询

Troubleshooting & Deploying Your Express App on Kubernetes (EC2 Ubuntu 16.04)

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:first
    

    Note: 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 with docker tag.

  • Create a Kubernetes Deployment Manifest
    You need a deployment YAML to define how your app runs in the cluster. Save this as deployment.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.yaml
    
  • Expose Your App with a Kubernetes Service
    To access your app from outside the cluster (since you’re on EC2), use a LoadBalancer service—it will automatically provision an AWS ELB. Save this as service.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 port
    

    Apply the service:

    kubectl apply -f service.yaml
    

    Wait 3-5 minutes for AWS to assign an external IP, then check it with:

    kubectl get services
    

    You 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).
  • 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.yaml to include the secret under spec:

    spec:
      containers:
      - name: express-app
        image: org/test:first
        ports:
        - containerPort: 8080
      imagePullSecrets:
      - name: regcred
    

    Re-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 on 0.0.0.0 instead of localhost—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 localhost here, rebuild and re-push your image before re-deploying.

内容的提问来源于stack exchange,提问作者Chris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:16:15