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

在Azure上为已部署Kubernetes应用集成Jenkins搭建CI/CD咨询

Hey there! Let's break down how to integrate Jenkins into your existing AKS setup for a full CI/CD pipeline—since you already have the voting app up and running, you're halfway there. Here's a tailored, step-by-step guide to get you started:

1. Set Up Jenkins on Azure

You have two solid options here, depending on how much management you want to handle:

  • Azure Managed Jenkins (Recommended): Head to the Azure Portal, search for "Jenkins", and create a managed instance. During setup, you can directly associate your existing AKS cluster—this auto-configures many integrations out of the box.
  • Self-Hosted Jenkins VM: If you prefer full control, spin up an Ubuntu VM in Azure, install Jenkins via sudo apt install jenkins, then open port 8080 in the VM's network security group to access the dashboard.

Whichever route you take, install these critical plugins first (go to Manage Jenkins > Plugins > Available Plugins):

  • Kubernetes Plugin (for AKS integration)
  • Azure Credentials Plugin (to authenticate with Azure services)
  • Docker Plugin (for building/pushing images)
  • Git Plugin (to pull your app code)
2. Configure Jenkins to Connect with ACR and AKS

Add Azure Service Principal Credentials

  1. Go to Manage Jenkins > Manage Credentials > Global credentials > Add Credentials
  2. Select Azure Service Principal as the type
  3. Fill in your tenant ID, client ID, and client secret (you can generate these via Azure CLI or the Portal)
  4. Assign a unique ID (e.g., azure-sp-creds) to reference this credential in pipelines
  1. In the same Credentials menu, add a Username/Password credential
  2. Use your ACR name as the username, and grab the access key from the ACR's Access keys blade in the Portal as the password
  3. Save this with an ID like acr-creds

Connect to Your AKS Cluster

  1. Go to Manage Jenkins > Manage Nodes and Clouds > Configure Clouds > Add a new cloud > Kubernetes
  2. Paste your AKS cluster's API server URL (found in the AKS overview blade in the Portal)
  3. Select the Azure Service Principal credential you created earlier for authentication
  4. Click Test Connection to confirm Jenkins can communicate with AKS
3. Build Your Jenkins Pipeline for the Voting App

Create a new pipeline job (New Item > Pipeline) named something like Voting-App-CI-CD, then configure it with this workflow:

Store a Jenkinsfile in your voting app's Git repo, then set the pipeline to pull from your repo. Here's a sample Jenkinsfile tailored to your setup:

pipeline {
    agent {
        kubernetes {
            yaml '''
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: docker
    image: docker:latest
    command: ["sleep", "999999"]
    volumeMounts:
    - name: docker-sock
      mountPath: /var/run/docker.sock
  volumes:
  - name: docker-sock
    hostPath:
      path: /var/run/docker.sock
'''
        }
    }
    environment {
        ACR_NAME = "your-acr-name"
        ACR_REGISTRY = "${ACR_NAME}.azurecr.io"
        IMAGE_NAME = "vote-app"
        TAG = "build-${BUILD_NUMBER}"
        K8S_NAMESPACE = "default"
    }
    stages {
        stage('Checkout Code') {
            steps {
                git url: 'https://github.com/your-username/voting-app.git', branch: 'main'
            }
        }
        stage('Run Unit Tests') {
            steps {
                // Replace with your app's test command (e.g., pytest for Python)
                sh "echo 'Running unit tests...'"
            }
        }
        stage('Build Docker Image') {
            steps {
                sh "docker build -t ${ACR_REGISTRY}/${IMAGE_NAME}:${TAG} ."
            }
        }
        stage('Push to ACR') {
            steps {
                withCredentials([usernamePassword(credentialsId: 'acr-creds', usernameVariable: 'ACR_USER', passwordVariable: 'ACR_PASS')]) {
                    sh "docker login ${ACR_REGISTRY} -u ${ACR_USER} -p ${ACR_PASS}"
                    sh "docker push ${ACR_REGISTRY}/${IMAGE_NAME}:${TAG}"
                }
            }
        }
        stage('Deploy to AKS') {
            steps {
                withCredentials([azureServicePrincipal(credentialsId: 'azure-sp-creds')]) {
                    sh "az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET --tenant $AZURE_TENANT_ID"
                    sh "az aks get-credentials --resource-group your-resource-group --name your-aks-cluster"
                    // Update the existing deployment with the new image
                    sh "kubectl set image deployment/vote-app vote-app=${ACR_REGISTRY}/${IMAGE_NAME}:${TAG} -n ${K8S_NAMESPACE}"
                }
            }
        }
    }
    post {
        success {
            echo "✅ Deployment succeeded! Your voting app is updated with build ${TAG}."
        }
        failure {
            echo "❌ Deployment failed—check stage logs for details."
        }
    }
}

Make sure to replace all placeholders (like your-acr-name, your-resource-group) with your actual Azure resource values.

Option 2: Inline Pipeline Script

If you don't want to use a Jenkinsfile, paste the same script directly into the Pipeline Script field in the job configuration.

4. Test and Automate the Pipeline
  • Manual Test: Click Build Now on your pipeline job. Check each stage's logs to confirm image building, pushing to ACR, and deployment to AKS works as expected.
  • Automate Triggers: Go to the job's Build Triggers section:
    • Use Poll SCM with a cron schedule like H/15 * * * * to check for code changes every 15 minutes.
    • Or set up a webhook from your Git repo (GitHub/GitLab) to trigger builds automatically when you push code.
5. Optional Enhancements
  • Blue-Green Deployments: Modify the pipeline to deploy new versions to a separate Kubernetes service, then switch traffic over for zero-downtime updates.
  • Pipeline Monitoring: Integrate Jenkins with Azure Monitor to track success rates, deployment times, and alert on failures.
  • Secret Management: Use Kubernetes Secrets or Azure Key Vault to store sensitive values instead of hardcoding them.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:23:15