俗话说:“慷慨之人必昌盛;滋润他人者,必得滋润。” 如果您觉得这篇文章对您有有所帮助,请点赞,关注,转发!
在不断发展的云计算世界中,应用程序的无缝部署和管理至关重要。Azure DevOps 应运而生——这是 Microsoft 提供的一套强大的开发工具,提供用于构建、测试和部署应用程序的端到端 DevOps 功能。今天,我们将探索 Azure DevOps 如何简化将 Helm 应用程序配置到 Azure Kubernetes 服务 (AKS) 的过程。我们将逐步指导您完成此过程,并提供完整的代码示例,确保您能够充分利用这些工具的强大功能。
Azure DevOps 包含一系列服务,包括 Azure Repos、Azure Pipelines、Azure Boards、Azure Artifacts 和 Azure Test Plans。在我们的旅程中,我们将重点介绍 Azure Pipelines,它支持持续集成和持续部署 (CI/CD)。这些管道对于自动化应用程序部署、确保一致性和减少手动错误至关重要。
构建代理是执行构建和发布管道中定义的任务的工作器。这些任务包括编译代码、运行测试、创建部署包等等。管道运行时,它会从代理池中请求代理来执行这些任务。
Azure DevOps 中有两种类型的代理:
代理池将构建代理分组。在 Azure DevOps 中创建流水线时,您会将其分配给一个代理池,该代理池将管理任务在可用代理之间的分配。Azure DevOps 中的每个项目都可以使用一个或多个代理池,从而提供一种灵活且可扩展的方式来管理您的构建环境。
在深入研究 Azure DevOps 之前,我们需要设置一个 AKS 集群。
# Log in to your Azure account
az login
# Create a resource group
az group create --name myResourceGroup --location eastasia
# Create an AKS cluster
az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 1 --enable-addons monitoring --generate-ssh-keys
为您的应用程序创建 Helm Chart。在本例中,我们将使用 NGINX 部署。
# Create a new Helm chart
helm create my-nginx-chart
# Navigate to the chart directory
cd my-nginx-chart
# Update the values.yaml file for your deployment needs
以下是 NGINX 部署的基本 values.yaml 配置:
replicaCount: 2
image:
repository: nginx
pullPolicy: IfNotPresent
tag: "1.17.1"
service:
type: LoadBalancer
port: 80
ingress:
enabled: false
resources: {}
创建 Azure DevOps 项目:导航到 Azure DevOps 并创建一个新项目。
设置 Azure Repos:将您的 Helm Chart 推送到 Azure Repos。
# Initialize a new Git repository
git init
# Add your files and commit
git add .
git commit -m "Initial commit of Helm chart"
# Add your Azure Repo as a remote and push
git remote add origin https://dev.azure.com/your-organization/your-project/_git/your-repo
git push -u origin master
步骤 1:导航至“组织设置”
步骤 2:访问“代理池”部分
步骤 3:创建新的代理池
步骤 4:将自托管代理添加到池中(可选)
要允许 Azure DevOps 与您的 Azure 资源交互,请创建服务连接。
在您的代码库中创建一个管道文件 (azure-pipelines.yml) 来定义 CI/CD 流程。
# azure-pipelines.yml
trigger:
- master
pool:
name: YourAgentPoolName //指定步骤4创建的代理池
variables:
azureSubscription: 'your-service-connection-name'
resourceGroup: 'myResourceGroup'
aksCluster: 'myAKSCluster'
namespace: 'default'
chartPath: 'my-nginx-chart'
releaseName: 'nginx-release'
stages:
- stage: Build
jobs:
- job: Build
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.x'
- script: |
echo "##vso[task.setvariable variable=HelmVersion]$(helm version --short --client | cut -d'.' -f1,2)"
displayName: 'Set Helm version variable'
- task: HelmInstaller@0
inputs:
helmVersionToInstall: $(HelmVersion)
- stage: Deploy
dependsOn: Build
jobs:
- deployment: Deploy
environment: 'Azure AKS'
strategy:
runOnce:
deploy:
steps:
- task: AzureCLI@2
inputs:
azureSubscription: $(azureSubscription)
scriptType: bash
scriptLocation: inlineScript
inlineScript: |
az aks get-credentials --resource-group $(resourceGroup) --name $(aksCluster)
helm upgrade --install $(releaseName) $(chartPath) --namespace $(namespace)
addSpnToEnvironment: true
提交并将 azure-pipelines.yml 文件推送到您的代码库。这将触发流水线运行。
# Add and commit the pipeline file
git add azure-pipelines.yml
git commit -m "Add Azure DevOps pipeline for Helm deployment"
git push
导航到 Azure DevOps 中的“流水线”部分,监控构建和部署过程。完成后,在 AKS 集群中验证部署。
kubectl get all -n default
您应该看到 NGINX Pod 正在运行,并且 LoadBalancer 服务正在公开应用程序。
通过利用 Azure DevOps 流水线,我们已将 Helm 应用程序自动部署到 Azure AKS。这种方法不仅节省时间,还能确保一致性并降低人为错误的风险。Azure DevOps 的强大之处在于它能够与各种工具和服务无缝集成,从而提供紧密结合且高效的 CI/CD 流水线。