在此專案中,我們將建立一個自動化管道,用於建立 Node.js 應用程式並將其部署到 Amazon ECS。該專案展示了使用 GitLab 作為版本控制、使用 Docker 進行容器化以及使用 ECS、ECR 和 CodePipeline 等 AWS 服務進行編排和部署。
在本指南結束時,您將全面了解 AWS 中的 CI/CD 工作流程,這對於現代 DevOps 實踐至關重要。
我們將自動執行以下任務:
建立 Node.js 應用程式。
使用 Docker 將應用程式容器化。
將 Docker 映像推送到 Amazon ECR。
使用 Fargate 將容器部署到 Amazon ECS。
使用 GitLab CI/CD 進行持續整合和部署。
使用 AWS CloudWatch 和 SNS 新增監控和通知。
AWS 服務:
Amazon ECS(彈性容器服務)
Amazon ECR(彈性容器註冊表)
AWS 程式碼管道
AWS 安全中心
亞馬遜事件橋
亞馬遜社群網路服務
AWS 雲端觀察
其他工具:
GitLab :原始碼管理和 CI/CD 管道。
Docker :應用程式容器化。
Node.js :範例 Web 應用程式框架。
此專案的高層架構如下:
開發人員將程式碼推送到 GitLab。
GitLab CI/CD 管道建立 Docker 映像並將其推送到 Amazon ECR。
此映像已部署至 Amazon ECS (Fargate)。
監控和日誌記錄是使用 AWS CloudWatch 完成的。
使用 Amazon SNS 發送通知。
在開始之前確保您已進行以下設定:
AWS 帳戶:具有 ECS、ECR 和 CodePipeline 的管理員存取權。
GitLab 帳戶:使用為 Node.js 應用程式建立的儲存庫。
已安裝 AWS CLI :用於從命令列與 AWS 服務互動。
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /
aws --version
sudo apt update
sudo apt install docker.io
docker --version
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/
由於您使用的是現有的 GitLab 儲存庫,請按照以下步驟複製並在您的專案中使用它:
在 GitLab 中開啟儲存庫: Nanuchi Node.js App 。
按一下「分叉」以在您的 GitLab 帳戶下建立副本。
git clone https://gitlab.com/<your-username>/node-app.git
cd node-app
- `server.js`
- `package.json`
用於容器化的Dockerfile 。
.gitlab-ci.yml
用於 CI/CD 管道(我們稍後會修改它)。
如果您想對儲存庫進行變更(例如,更新程式碼、新增更多檔案),請將更新推回:
git add .
git commit -m "Updated application for CI/CD project"
git push origin main
此步驟基本上保持不變,但現在它與您正在部署的 Node.js 應用程式保持一致。
建立一個私有 ECR 儲存庫來儲存應用程式的 Docker 映像:
aws ecr create-repository --repository-name node-app
使用 ECR 註冊表對本機 Docker 用戶端進行身份驗證:
aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <account_id>.dkr.ecr.<region>.amazonaws.com
建立用於執行應用程式容器的 ECS 叢集:
aws ecs create-cluster --cluster-name node-app-cluster
請依照前面概述的步驟操作:
建立IAM 任務執行角色。
設定VPC 、子網路和安全群組。
在安全群組中開放3000
連接埠用於應用程式流量。
使用克隆的 GitLab 應用程式建置並推送 Docker 映像。
導航到儲存庫的根目錄並建立映像:
docker build -t node-app .
為您的 ECR 儲存庫標記影像:
docker tag node-app:latest <account_id>.dkr.ecr.<region>.amazonaws.com/node-app:latest
將映像推送到您的 Amazon ECR 儲存庫:
docker push <account_id>.dkr.ecr.<region>.amazonaws.com/node-app:latest
確認鏡像推送成功:
aws ecr list-images --repository-name node-app
Amazon ECS(彈性容器服務)是一項可讓您執行容器的託管服務。我們正在使用Fargate ,這是一種無伺服器選項,無需手動管理 EC2 執行個體。以下是為我們的專案設定 ECS 的詳細演練:
叢集是執行任務或服務所需的資源的邏輯分組。
aws ecs create-cluster --cluster-name node-app-cluster
此命令會建立一個名為node-app-cluster
新叢集。
aws ecs list-clusters
確保node-app-cluster
被列為叢集之一。
任務定義指定用於執行應用程式的容器設定(例如,記憶體、CPU、連接埠)。將其視為容器化應用程式的藍圖。
task-def.json
檔案: {
"family": "node-app-task",
"executionRoleArn": "arn:aws:iam::account_id:role/ecsTaskExecutionRole",
"networkMode": "awsvpc",
"containerDefinitions": [
{
"name": "node-app-container",
"image": "<account_id>.dkr.ecr.<region>.amazonaws.com/node-app:latest",
"memory": 512,
"cpu": 256,
"essential": true,
"portMappings": [
{
"containerPort": 3000,
"hostPort": 3000,
"protocol": "tcp"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/node-app",
"awslogs-region": "<region>",
"awslogs-stream-prefix": "ecs"
}
}
}
],
"requiresCompatibilities": ["FARGATE"],
"cpu": "256",
"memory": "512"
}
將<account_id>
和<region>
替換為您的 AWS 帳戶 ID 和區域。
確保executionRoleArn
指向有效的ECS任務執行角色。
aws ecs register-task-definition --cli-input-json file://task-def.json
這會將藍圖註冊到 ECS。
ECS 服務可確保所需數量的任務正在執行,並為任務啟用負載平衡。
aws ecs create-service \
--cluster node-app-cluster \
--service-name node-app-service \
--task-definition node-app-task \
--desired-count 1 \
--launch-type FARGATE \
--network-configuration "awsvpcConfiguration={subnets=[subnet-xxx],securityGroups=[sg-xxx],assignPublicIp=ENABLED}" \
--region <region>
將subnet-xxx
和sg-xxx
替換為您的VPC的公有子網路和安全群組的ID。
desired-count
是要執行的任務數。
aws ecs describe-services --cluster node-app-cluster --services node-app-service
確保服務處於活動狀態並正在執行。
aws ecs list-tasks --cluster node-app-cluster
使用任務 ID 描述任務並尋找公用 IP 位址:
aws ecs describe-tasks --cluster node-app-cluster --tasks <task_id>
http://<public_ip>:3000
GitLab CI/CD 自動化建置和部署流程,確保應用程式始終是最新的。請依照以下步驟設定管道:
.gitlab-ci.yml
該文件定義管道的階段、作業和命令。
.gitlab-ci.yml
檔案加入專案的根目錄: stages:
- build
- deploy
build:
image: docker:latest
services:
- docker:dind
script:
- docker build -t node-app .
- docker tag node-app <account_id>.dkr.ecr.<region>.amazonaws.com/node-app:latest
- aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <account_id>.dkr.ecr.<region>.amazonaws.com
- docker push <account_id>.dkr.ecr.<region>.amazonaws.com/node-app:latest
deploy:
image: amazon/aws-cli:latest
script:
- aws ecs update-service --cluster node-app-cluster --service node-app-service --force-new-deployment --region <region>
- Builds a Docker image from your `Dockerfile`.
- Tags the image with the ECR repository URL.
- Pushes the image to Amazon ECR.
- Updates the ECS service to use the latest image in Amazon ECR.
前往 GitLab 儲存庫中的設定 → CI/CD → 變數並新增下列環境變數:
AWS_ACCESS_KEY_ID
:您的 AWS 存取金鑰。
AWS_SECRET_ACCESS_KEY
:您的 AWS 金鑰。
AWS_REGION
:您的 AWS 區域。
CloudWatch 支援對您的應用程式和基礎架構進行監控和日誌記錄。
aws logs create-log-group --log-group-name /ecs/node-app
aws logs create-log-stream --log-group-name /ecs/node-app --log-stream-name app-logs
在任務定義 ( task-def.json
) 中,確保logConfiguration
部分如下:
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/node-app",
"awslogs-region": "<region>",
"awslogs-stream-prefix": "ecs"
}
}
您可以在 CloudWatch 中設定警報來監控 CPU 使用率、記憶體和應用程式錯誤等指標。
aws cloudwatch put-metric-alarm \
--alarm-name HighCPUUsage \
--metric-name CPUUtilization \
--namespace AWS/ECS \
--statistic Average \
--period 300 \
--threshold 80 \
--comparison-operator GreaterThanThreshold \
--evaluation-periods 1 \
--alarm-actions <sns_topic_arn>
建立 SNS 主題以發送通知:
aws sns create-topic --name ecs-alerts
aws sns subscribe --topic-arn <sns_topic_arn> --protocol email --notification-endpoint <your_email>
現在,您將收到有關 CPU 使用率過高或其他警報的電子郵件通知。
加入我們的Telegram 社群||在 GitHub 上關注我以獲取更多 DevOps 內容!