整体 aws 架构和CICD 的整体工作流程

整体 aws 架构和CICD 的整体工作流程

需要以下知识

Terraform: https://developer.hashicorp.com/terraform/intro)

Jenkins: https://www.jenkins.io/

AWS: https://docs.aws.amazon.com/

Helm chart: https://helm.sh/

应用程序托管在由 Terraform 代码配置的 AWS EKS 上,CICD 由 Jenkins 驱动。标准的自动工作流程是

开发提交代码到 bitbucket repo 分支。该分支将触发 Jenkins 管道开始工作。或者我们可以手动触发jenkins 上的构建

- Jenkins pipeline将

- 检查工作节点中的代码

- 为 api/web 生成构建

- 将图像推送到 AWS ECR

- 将数据库更改应用于 rds

- 运行 helm install 以更新/创建 eks biz 服务

- 代码更新将发布到在线环境中

AWS cloudwatch 上有应用程序日志组来显示 biz pod 日志,container insights也显示 eks 机器指标

如何为 aws 部署运行/维护 terraform

The terraform scirpt was setup in Jenkins pipeline, we can use jenkins to run the script. but for terraform maintainer, below are the steps to run it locally.

- Install the AWS CLI

- First we need to install the AWS CLI. Below is an example for macOS and please refer to [Getting Started EKS](https://docs.aws.amazon.com/zh_cn/eks/latest/userguide/getting-started-console.html) for other operating systems.

``` pip3 install awscli --upgrade --user ```

- Check the installation with aws --version to confirm your CLI is installed successfully

- Configure AWS CLI credentials.

``` $ aws configure ```

``` AWS Access Key ID [None]: [put your access key here] ```

``` AWS Secret Access Key [None]: [put your secret key here] ```

``` Default region name [None]: [ put your region-code here, put ca-central-1 in this case] ```

``` Default output format [None]: json ```

- Create kubeconfig file with the AWS CLI. Then once the eks instance is created, we can use kubectl to reveal it.

``` aws eks --region ca-central-1 update-kubeconfig --name [cluster_name] ```

Replace above cluster_name with app-nonprod or other cluster name you want to connect

- Run Terraform

- Install terraform CLI following the reference https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli

- Open terminate and goto /ca-nonprod/aws

- Before run below script, we need to check the /development/terraform.tfvars file, it defines most of the environment variables used to setup the eks, for example the AWS key, secret, region etc, make sure those values are your expected.

- Run below script in /development folder to init the terraform

``` terraform init ```

- Run below script to plan the script, it will list all the changes between code and actual AWS existing resources, be sure to review the changes and make sure these are your expectation before run below cmd

``` terraform plan -out plan ```

- Run below script to apply the plan, it will apply the chagnes to AWS

``` terraform apply plan ```

- Run destroy to clean up the AWS resources, it will remove everything and can't be undo. We should never run this cmd after the eks is live

``` terraform destroy ```

以下是整个 Jenkins 流水线工作流程

![](jenkins-workflow.drawio.png)

<center> [图2]</center>

上图 [图 2] 显示了 jenkins 管道工作流程的详细信息

正如 [图 1] 所示,Jenkins master 设置在它自己的 VPC 中。vpc/ec2 由 /jenkins/aws 文件夹中的 terraform 提供。正在运行的 jenkins master 应用程序由 /home/ec2-user/jenkins-docker 下的 master ec2 中的 docker-compose.yml 管理。SSH pem 文件位于代码 /jenkins/aws/jenkins-key.pem 中。

这个 jenkins 应用程序的 lbs/domain 配置是手动的,所以继续前进,建议手动管理这个 jenkins 服务器

请注意,jenkins master ec2 有一个 100GB 的 ebs 作为 ec2 中的 /jenkinsdata 文件夹附加,并且 ebs 设置为在 ec2 终止时未删除。我们将 jenins 配置数据文件夹从 docker 挂载到 ec2 /jenkinsdata 文件夹。这将避免在我们意外终止 ec2 时丢失数据。

Jenkins 有一个插件 [EC2 Fleet](https://plugins.jenkins.io/ec2-fleet/) 来管理 agent worker。详细配置在 manage Jenkins->Manage Nodes and Clouds-> Configure Clouds 下。

目前的做法是我们有一个 master 留在一个单独的 VPC 中,而 agent box 在 eks 所在的 VPC 中创建。例如,在 ca-central-1 nonprod VPC 中创建的“agent-ca-nonprod”和在 ca-central-1 prod VPC 中创建的“agent-ca-prod”。在这种情况下,当我们需要在非生产环境中运行 CICD 工作时,我们总是使用 `agent-ca-nonprod` 作为代理标签。当我们想要进行生产部署时,我们在管道脚本中使用“agent-ca-prod”。

在 Jenkins 系统中,需要在 manage Jenkins->Manage Nodes and Clouds->Configure Clouds 中创建 agent 配置。请注意,当我们运行 terraform 以创建新集群时,会配置实时代理自动缩放组。检查 /stack/jenkins-agent。这意味着当我们想要设置一组新的 VPC/EKS 时,也会创建 jenkins-agent 自动缩放配置。自动缩放配置将等待 Jenkins 节点配置集成。

- 在 jenkins 门户中配置 jenkins 代理的步骤

- 转到 jenkins 网站并导航到管理 Jenkins->管理节点和云->配置云。

- 如下图所示。填写名称、aws 凭据、区域。对于 EC2 Feel,选择自动缩放组 [jenkins-auto-scale-group-nonprod],在这里您应该找到我们运行 terraform apply 后创建的预期自动缩放组。然后点击右下角的测试连接。

- 如下图所示。填写启动器信息。和标签。请注意,标签名称在管道脚本中被引用为代理部分,因此让我们更清楚地了解代理所在的 VPC。例如,aws-agent-ca-nonprod

- 对于其余参数,请根据需要填写,但最佳做法是我们可以将生产 VPC 的最小集群大小设置为 0,因为我们只需要它用于生产发布,我们不需要让代理一直在线。

- 现在我们有一个非生产 vpc jenkins agent worker 设置。我们可以为 Prod VPC agent worker 做类似的事情。

以后可以改进

- 将 karpenter 视为自动缩放工具

- 考虑利用 VPC 对等更好地跨 VPC 连接

- 用 EFS 替换 jenkins master ec2 上的 EBS 卷以降低丢失构建内容的风险

- 在 EKS 中定义更多具有特定权限的集群角色,以便我们可以对不同的 IAM 用户级别进行更深入的控制

这是公司最近完成的一个项目CICD aws 迁移项目,简单列了一下,有些与公司相关的就剔除去了,大概提供个思路吧

猜你喜欢

转载自blog.csdn.net/qq_36279445/article/details/129545553