Jenkins集成Kubernetes部署Replica Set/Deployment及服务的技术疑问
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
kubeconfigfile 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):
- Create a Helm chart for your app (run
helm create my-app-chartto start) - Customize the chart’s
values.yamlto parameterize image tags, replicas, and resources - 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
kubeconfigor 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/updatefor 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

