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

Jenkins集成Kubernetes部署Replica Set/Deployment及服务的技术疑问

How to Create Custom Kubernetes Deployments in Your Jenkins CI/CD Pipeline

Hey there! Let's break down how you can create and manage your own Kubernetes Deployments in your Jenkins CI/CD workflow, especially since you're coming from writing Deployment definitions directly in shell scripts. Here's a practical, step-by-step approach:

1. Ditch Shell Scripts for Kubernetes YAML Files

Instead of constructing Deployment manifests via shell commands, use dedicated Kubernetes YAML files—they’re far more readable, maintainable, and easy to version-control alongside your code in SVN.

Here’s a sample deployment.yaml you can customize:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
  labels:
    app: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: your-dockerhub-username/my-app-image:{{BUILD_TAG}}
        ports:
        - containerPort: 8080
        resources:
          requests:
            cpu: "100m"
            memory: "128Mi"
          limits:
            cpu: "200m"
            memory: "256Mi"

Store this file in your SVN repo (e.g., in a k8s/ directory) so it’s tracked with your application code.

2. Integrate Deployment into Your Jenkinsfile

Update your Jenkinsfile to handle substituting the Docker image tag (from your CI build) and applying the YAML to your Kubernetes cluster.

First, ensure your Jenkins agent has access to your Kubernetes cluster:

  • If Jenkins runs inside your K8s cluster: Use a ServiceAccount with limited deployment permissions.
  • If Jenkins runs outside: Store your kubeconfig file as a Jenkins secret credential.

Here’s how the deployment stage might look in your Jenkinsfile:

stage('Deploy to Kubernetes Cluster') {
  steps {
    script {
      // Replace the placeholder tag with your actual build tag from Docker Hub
      sh "sed -i 's/{{BUILD_TAG}}/${BUILD_NUMBER}/' k8s/deployment.yaml"
      
      // Apply the Deployment to your cluster (use your namespace if needed)
      sh "kubectl apply -f k8s/deployment.yaml --namespace=my-app-namespace"
      
      // Optional: Verify the rollout completes successfully
      sh "kubectl rollout status deployment/my-app-deployment --namespace=my-app-namespace"
    }
  }
}

3. Upgrade to Helm for Complex Deployments

If your app requires environment-specific configurations, multiple dependent resources (like Services, Ingresses), or easy rollbacks, consider using Helm (Kubernetes package manager):

  1. Create a Helm chart for your app (run helm create my-app-chart to start)
  2. Customize the chart’s values.yaml to parameterize image tags, replicas, and resources
  3. Update your Jenkinsfile to use Helm commands:
    stage('Deploy with Helm') {
      steps {
        sh "helm upgrade --install my-app ./my-app-chart \
          --set image.tag=${BUILD_NUMBER} \
          --namespace=my-app-namespace \
          --create-namespace"
      }
    }
    

4. Key Best Practices

  • Secure Credentials: Never hardcode kubeconfig or Docker Hub credentials in your Jenkinsfile/SVN repo. Use Jenkins’ built-in Credentials Manager to store secrets and reference them in your pipeline.
  • Limit Permissions: The Kubernetes account used by Jenkins should have the minimum necessary permissions (e.g., only create/update for Deployments, not full cluster access).
  • Test Deployments: Add a stage to run smoke tests against your deployed app before marking the pipeline as successful.

内容的提问来源于stack exchange,提问作者Mr.DevEng

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:23:24