aws appconfig 理解和使用appconfig对应用程序进行动态配置

参考资料

appconfig的基本概念

appconfig主要的功能是创建、管理以及快速部署应用程序配置。可以集成的服务包括ec2,lambda,ecs,eks。iot等。

来自chatgpt的解释

AWS AppConfig is a service provided by Amazon Web Services (AWS) that allows you to manage and deploy application configurations across different environments. It helps you deploy configurations and feature flags for your applications in a controlled manner, with the ability to monitor and roll back changes easily.

使用appconfig的优势如下

  • 配置源包括appconfig 托管配置,ssm 参数,github和codecommit仓库等
  • 使用自带的json校验,或者通过lambda函数定义配置校验规则
  • 控制配置的部署速度、部署时间和bake时间。
  • 监控配置过程,失败回滚

appconfig的关键概念

  • application,组织和管理配置数据的单位
  • envionment,环境是appconfig的逻辑部署组,application可以有多个环境
  • configuration profile,使得appconfig能够访问配置数据(可以附加校验器),可以设置的配置数据包括
    • yaml和json数据
    • s3对象
    • codepipeline管道
    • secret manager
    • parameter store
    • ssm documents

应用程序检查和获取配置数据的流程

https://docs.aws.amazon.com/appconfig/latest/userguide/what-is-appconfig.html#learn-more-appconfig-how-it-works

appconfig的api在2021-11-18进行了迁移,GetConfiguration的api调用被GetLatestConfiguration取代

在这里插入图片描述

appconfig入门使用

应用和环境

创建application,应用程序是一个管理和部署配置文件的单元

  • 应用程序可以关联插件,包括eventbridge,sqs和sns
aws appconfig create-application --name my-app

创建environment,环境是application的逻辑部署组

  • 环境可以配置监控,需要设置cw alarm和对应的iam角色
  • 环境可以关联插件,包括eventbridge,sqs和sns
aws appconfig create-environment \
  --application-id ml6665u \
  --name my-testenv \
  --description "my test appconfig env"
  #--monitors "AlarmArn=ARN_of_the_Amazon_CloudWatch_alarm,AlarmArnRole=ARN_of_the_IAM role_for_AWS AppConfig_to_monitor_AlarmArn" \
  #--tags User_defined_key_value_pair_metadata_of_the_environment

配置文件

创建配置文件(configuration profile),包含具体的配置数据,需要指定以下信息

https://docs.aws.amazon.com/zh_cn/appconfig/latest/userguide/appconfig-creating-configuration-and-profile.html

  • 配置数据源
  • 访问配置数据的iam角色
  • 数据校验程序(json/yaml的静态校验,或lambda函数校验)

在这里插入图片描述

关于lambda函数校验

  • appconfig调用lambda函数校验的event示例如下

    {
        "ApplicationId": "The application Id of the configuration profile being validated", 
        "ConfigurationProfileId": "The configuration profile Id of the configuration profile being validated",
        "ConfigurationVersion": "The configuration version of the configuration profile being validated",
        "Content": "Base64EncodedByteString", 
        "Uri": "The uri of the configuration"    
    }
    
  • appconfig在调用StartDeploymentValidateConfigurationActivity时验证lambda,需要为appconfig.amazonaws.com授权调用该lambda函数

配置文件可以创建两种类型

  • faeature flag,Create, manage and safely deploy a single or a group of feature flags. Feature Flags help you release features quickly and safely.
  • freeform configuration,Create your own configurations and store them within Amazon AppConfig or reference existing data in S3, Parameter Store, or Secrets Manager.
feature flag

创建功能标志(feature flag),主要用于启用或禁用应用程序中的功能。注意

  • 类型为,AWS.AppConfig.FeatureFlags
  • 位置uri为,hosted
aws appconfig create-configuration-profile \
  --application-id ml6665u \
  --name my-config-profile \
  --location-uri hosted \
  --type AWS.AppConfig.FeatureFlags

简单创建一个功能标志

在这里插入图片描述

freeform configuration

对于freeform的配置文件,可以选择更多的数据源

在这里插入图片描述

部署策略

创建部署策略,即部署速度、部署时间和bake时间等

https://docs.aws.amazon.com/zh_cn/appconfig/latest/userguide/appconfig-creating-deployment-strategy.html

  • 部署类型,支持线性指数部署类型
  • 步骤百分比,在部署的每个步骤中作为目标的调用
  • 部署时间,appconfig部署到主机的花费时间
  • Bake time,部署100%后,在完成之前对cw告警监控的时间,如果告警则回滚

类似codedeploy的部署策略,有三个预定义策略

  • AppConfig.AllAtOnce
  • AppConfig.Linear50PercentEvery30Seconds
  • AppConfig.Canary10Percent20Minutes

部署配置

实际就是将环境和配置文件绑定并发布

在这里插入图片描述

检索配置

将配置文件发布到环境中,就能够通过对应的接口检索到配置数据了

应用程序检索配置文件的逻辑参考上文的描述

  • 获取客户端会话

    $ aws appconfigdata start-configuration-session \
         --application-identifier ml6665u \
         --environment-identifier ez41c69 \
         --configuration-profile-identifier my-config-profile
    {
        "InitialConfigurationToken": "AYADeKP07JoESGyhSikVkg/JFc8AXwABABVhd3MtY3J5cHRvLXB1YmxpYy1rZXkAREFraGZmN3JKU2FHa0x4RnZXQ1hiV2xieU1KSzRhelEvR0FUOU5TaG9oUUkwNEtDbUx3TmZ0azZSL1ZKQTZyckxCZz09AAEAB2F3cy1rbXMAT2Fybjphd3MtY246a21zOmNuLW5vcnRoLTE6NDYzNDg3MTE3ODUzOmtleS9kZjQ0NjJjYi04ZTRkLTQ0ZjUtODQ0Ni1lMmY5NzgwMDdmZGEA9gEBA/83mxxxTS7TErRNGSXrJSRHsPbaRH1jcwJ+13883JraBviC0heW1uaOUwm3icxYUcCMHtohdSrBx990aU/etwA4vQVIBsmFQkXyL4ZC6KPis7Gx/Ssi89aH25FKZr2b3gxrg=="
    }
    
  • 请求配置,并得到了NextPollConfigurationToken

    $ export token=AYADeKP07JoxA4vQVIBsmFQkXyL4ZC6KPis7Gx/Ssi89aH25FKZr2b3gxrg==
    $ aws appconfigdata get-latest-configuration \
        --configuration-token $token mydata.json
    {
        "NextPollConfigurationToken": "AYADeJNIG08gqrex7XXHbUValqIAXwxtY/a0auK9QWr+EAjEA/a2Lp8/rMhdaMGgcrwI4JyBAGM1C9525wkkjJbFrmHSpZw6T+dxkwalgOa3gi4Xp",
        "NextPollIntervalInSeconds": "60",
        "ContentType": "application/json"
    }
    $ cat mydata.json
    {
    "action":
        {
            "action":"eat",
            "age":26,
            "enabled":true,
            "gender":"male",
            "married":true,
            "name":"zhangsan"
        }
    }
    
  • 后续调用必须提供NextPollConfigurationToken

将appconfig和eks集成

将appconfig和eks集成非常简单

  • appconfig作为一个sidecar从appconfig服务轮询并获取配置文件
  • application从appconfig-agent检索配置数据

创建以下deployment

kind: Pod
apiVersion: v1
metadata:
  name: test-appconfig
  labels:
    app: test-appconfig
spec:
  #serviceAccountName: eks-appconfig
  containers:
  - name: amazonlinux
    image: xxxxxxx.dkr.ecr.cn-north-1.amazonaws.com.cn/amazonlinux:latest
    command: ["/bin/sh","-c","sleep 3600"]
  - name: appconfig-agent
    image: public.ecr.aws/aws-appconfig/aws-appconfig-agent:2.x
    ports:
    - name: http
      containerPort: 2772
      protocol: TCP
    env:
    - name: SERVICE_REGION
      value: cn-north-1

默认情况下,appconfig代理在端口 2772 上运行

在amazonlinux中访问appconfig

$ curl "http://localhost:2772/applications/my-app/environments/my-config-profile/configurations/my-config-profile"
curl: (3) URL using bad/illegal format or missing URL
curl: (97) Could not resolve host: curl
{"Message":"User: arn:aws-cn:sts::xxxxxxx:assumed-role/eksctl-test124-nodegroup-test124-NodeInstanceRole-XTF9X5WL2NMR/i-0a661ae8d299361
96 is not authorized to perform: appconfig:StartConfigurationSession on resource: arn:aws-cn:appconfig:cn-north-1:xxxxxxx:application/m
l6665u/environment/my-config-profile/configuration/thtqvv5 because no identity-based policy allows the appconfig:StartConfigurationSession a
ction"}

创建策略如下

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "myappconfig",
            "Effect": "Allow",
            "Action": "appconfig:*",
            "Resource": "*"
        }
    ]
}

我们需要给appconfig-agent配置appconfig的权限

eksctl create iamserviceaccount \
   --cluster test124 \
  --name eks-appconfig \
  --namespace default \
  --attach-policy-arn arn:aws-cn:iam::xxxxxxx:policy/MyAppconfigFullAccess \
  --override-existing-serviceaccounts \
  --region cn-north-1 \
  --approve

之后取消pod中的sa注释,重新创建pod

再次访问成功拿到配置数据

bash-5.2# curl "http://localhost:2772/applications/my-app/environments/my-config-profile/configurations/my-config-profile"
{"Message":"Environment not found","ReferencedBy":{"EnvironmentIdentifier":"my-config-profile"},"ResourceType":"Environment"}

此外,appconfig代理的环境变量配置

https://docs.aws.amazon.com/zh_cn/appconfig/latest/userguide/appconfig-integration-containers-agent.html#appconfig-integration-containers-agent-configuring

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值