AKS 如何有效地创建 pod 配置设置和机密

AKS how to create pod configuration settings and secrets efficiently

我有 10 个 Azure Web 应用,我们希望将它们从 Azure 应用服务迁移到 Azure Kubernetes 服务 (AKS)。所有应用程序均使用 net core 3.1 或 5.0 编写。我们目前正在使用 Azure DevOps 管道将应用程序部署到 Azure App Service。在部署期间,我们使用 FileTransform@2 任务为每个应用程序转换 appsettings.json 文件。一些应用程序在 yaml 文件中定义了 50-100 种不同的配置设置,以正确配置 运行 应用程序。例如,在我们的网络应用程序 Azure DevOps 管道中可以找到这样的语句:

  - task: FileTransform@2
    displayName: Replace tokens in web zip file
    inputs:
      folderPath: '$(System.ArtifactsDirectory)$(webZipFile)'
      fileType: json
      xmlTransformationRules: ''
      enableXmlTransform: false
      jsonTargetFiles: '**/appsettings.json'

其中 webZipFile 变量是应用程序构建的 zip 文件的名称。使用这种部署方法的好处是一个 yaml 文件可以很容易地在多个应用程序中重复使用,唯一的变化是传递到文件中的变量或参数。

然后我们所要做的就是在 yaml 中定义我们想要转换的变量,转换工作正常并且符合预期。

我们已经在 AKS 中部署了一个应用程序。为了在应用程序中设置配置,我们必须执行以下步骤(我们也在使用 devops):

apiVersion: v1
kind: Secret
metadata:
  name: webappsettings
  namespace: webapp  
type: Opaque
data:
  settingsconnections: __settingsconnectionsbase64__

n: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  generation: 1
  labels:
    app: webapp
  name: webapp
  namespace: webapp
spec:
  progressDeadlineSeconds: 600
  replicas: 2
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: webapp
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - image: organisation.azurecr.io/webapp:#{tagBuildId}#
        imagePullPolicy: IfNotPresent
        name: webapp
        resources:
          requests:
            memory: "100Mi"
            cpu: "200m"
          limits:
            memory: "200Mi"
            cpu: "500m"
        env:
        - name: Settings__Connections
          valueFrom:
            secretKeyRef:
              name: webappsettings
              key: settingsconnections

上述过程非常耗费人力和复杂,尤其是当我们考虑到我们需要迁移多个应用程序时,每个应用程序都有许多需要配置的应用程序设置。

问题是,是否有更有效的方法在部署时使用 Azure DevOps(或市场上的任务)设置配置和机密?理想情况下,它将能够及时将所有配置添加到 Azure DevOps 管道 yaml 文件中,并且一些智能任务会在容器启动时以某种方式配置容器。

有很多选择,一种方法是否比另一种更好或更正确是有争议的。

一种方法是使用 Azure App Configuration 和 Azure KeyVault(或等效服务),让您的应用程序在运行时检索配置值。无需在配置文件或环境变量中管理数百个应用程序设置,您的配置文件仅指向服务,您的应用程序可以在运行时自动检索正确的值。

您还可以创建 configmaps containing file data,然后将 configmap 作为一个卷安装,这样一个“文件”就存在于 pod 的磁盘上。

归根结底,您将不得不做 一些 的工作并使方法标准化。我要说的一件事是,您应该 使用 Azure Pipeline 变量来驱动应用程序配置——应用程序配置应该与您的持续交付提供商解耦 .您应该能够部署应用程序的工作副本 而无需 运行 管道。这使得将来更容易交换管道提供程序,并支持在本地快速开发、测试和调试应用程序。