端到端 DevOps 项目:构建、部署和监控全栈应用程序

2025-05-26

端到端 DevOps 项目:构建、部署和监控全栈应用程序

目录

  1. 介绍
  2. 项目概述
  3. 先决条件
  4. 步骤 1:AWS 上的基础设施设置
  5. 第 2 步:安装和配置 Jenkins
  6. 步骤 3:使用 Docker 对应用程序进行容器化
  7. 步骤 4:部署到 Kubernetes (Amazon EKS)
  8. 步骤5:使用Prometheus和Grafana实现持续监控
  9. 步骤 6:保护 CI/CD 管道
  10. 步骤 7:使用 Terraform 实现基础设施自动化
  11. 步骤8:实施蓝绿部署
  12. 结论
  13. 进一步阅读和资源

介绍

DevOps 旨在实现流程自动化,改善开发和运营团队之间的协作,并更快速、更可靠地部署软件。本项目将指导您使用行业标准工具创建全面的 CI/CD 流水线。您将使用 Jenkins、Docker、Kubernetes (Amazon EKS)、Prometheus、Grafana、Trivy、SonarQube 和 Terraform 在 AWS 上部署全栈应用程序。本次实践经验将帮助您掌握关键的 DevOps 概念和工具。

项目图

  +------------------------+
  |   Developer Workstation |
  |                        |
  |  - Code Repository     |
  |  - Local Build & Test  |
  +-----------+------------+
              |
              v
  +------------------------+
  |        Jenkins         |
  |                        |
  |  - CI/CD Pipeline      |
  |  - Build & Test        |
  |  - Docker Build        |
  |  - Push Docker Image   |
  +-----------+------------+
              |
              v
  +------------------------+          +----------------------+
  |        Docker Hub      |          |        AWS EKS        |
  |                        |          |                      |
  |  - Docker Image        |          |  - Kubernetes Cluster |
  |                        |          |                      |
  +-----------+------------+          +-----------+----------+
              |                                    |
              v                                    |
  +------------------------+          +----------------------+
  |   Kubernetes Deployment|          |  Prometheus & Grafana|
  |                        |          |                      |
  |  - Deployment          |          |  - Monitoring         |
  |  - Service             |          |  - Dashboards        |
  |                        |          |                      |
  +------------------------+          +----------------------+
              |
              v
  +------------------------+
  |     Amazon RDS         |
  |                        |
  |  - MySQL Database      |
  |                        |
  +------------------------+
Enter fullscreen mode Exit fullscreen mode

项目概述

目标

  • 基础设施设置:配置 AWS 资源,包括 VPC、EC2 实例和 RDS 数据库。
  • CI/CD 管道:使用 Jenkins 自动化构建、测试和部署过程。
  • 容器化:使用 Docker 将应用程序容器化。
  • Kubernetes 部署:在 Amazon EKS 上部署应用程序。
  • 监控:使用 Prometheus 和 Grafana 实现持续监控。
  • 安全性:使用 Trivy 和 SonarQube 保护管道安全。
  • 基础设施即代码:使用 Terraform 实现基础设施管理自动化。
  • 蓝绿部署:实施蓝绿部署策略。

工具和技术

  • AWS: EC2、VPC、RDS、EKS。
  • 詹金斯: CI/CD 自动化。
  • Docker:容器化。
  • Kubernetes:容器编排。
  • Prometheus 和 Grafana:监控和可视化。
  • Trivy 和 SonarQube:安全和代码质量检查。
  • Terraform:基础设施即代码。

先决条件

  • AWS 账户:云资源配置所需。
  • 基本 Linux 知识:用于管理 EC2 实例。
  • Docker 和 Kubernetes 知识:用于容器化和编排。
  • 熟悉 CI/CD:了解基本的 CI/CD 概念。
  • GitHub 帐户:用于版本控制和 Jenkins 集成。

步骤 1:AWS 上的基础设施设置

1.1 设置 VPC 和网络

  1. 创建 VPC:
   aws ec2 create-vpc --cidr-block 10.0.0.0/16
Enter fullscreen mode Exit fullscreen mode
  • 配置子网:

     aws ec2 create-subnet --vpc-id <vpc-id> --cidr-block 10.0.1.0/24 --availability-zone us-east-1a
    
  • 设置 Internet 网关:

     aws ec2 create-internet-gateway
     aws ec2 attach-internet-gateway --vpc-id <vpc-id> --internet-gateway-id <igw-id>
    
  • 创建路由表并关联子网:

     aws ec2 create-route-table --vpc-id <vpc-id>
     aws ec2 create-route --route-table-id <rtb-id> --destination-cidr-block 0.0.0.0/0 --gateway-id <igw-id>
     aws ec2 associate-route-table --subnet-id <subnet-id> --route-table-id <rtb-id>
    
  1. 设置安全组:

    • 创建安全组:
     aws ec2 create-security-group --group-name MySecurityGroup --description "Security group for my app" --vpc-id <vpc-id>
    
  • 允许 SSH、HTTP 和 HTTPS:

     aws ec2 authorize-security-group-ingress --group-id <sg-id> --protocol tcp --port 22 --cidr 0.0.0.0/0
     aws ec2 authorize-security-group-ingress --group-id <sg-id> --protocol tcp --port 80 --cidr 0.0.0.0/0
     aws ec2 authorize-security-group-ingress --group-id <sg-id> --protocol tcp --port 443 --cidr 0.0.0.0/0
    

1.2 配置 EC2 实例

  1. 启动 EC2 实例:

    • 使用 AWS 管理控制台或 CLI:
     aws ec2 run-instances --image-id ami-0abcdef1234567890 --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids <sg-id> --subnet-id <subnet-id>
    
  • 在 EC2 实例上安装 Docker 和 Jenkins:

     sudo yum update -y
     sudo yum install docker -y
     sudo service docker start
     sudo usermod -a -G docker ec2-user
    
     # Jenkins
     sudo yum install java-1.8.0-openjdk -y
     wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
     rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
     sudo yum install jenkins -y
     sudo systemctl start jenkins
     sudo systemctl enable jenkins
    

1.3 设置 RDS 数据库

  1. 配置 RDS 实例:

    • 创建 MySQL 实例:
     aws rds create-db-instance --db-instance-identifier mydbinstance --db-instance-class db.t2.micro --engine mysql --master-username admin --master-user-password password --allocated-storage 20 --vpc-security-group-ids <sg-id>
    
  2. 连接应用程序:

    • 使用 RDS 端点更新应用程序配置:
     jdbc:mysql://<rds-endpoint>:3306/mydatabase
    
  • 通过 MySQL 客户端测试来确保连接


 ```bash
 mysql -h <rds-endpoint> -u admin -p
 ```
Enter fullscreen mode Exit fullscreen mode

第 2 步:安装和配置 Jenkins

2.1 Jenkins安装

  1. 安装 Jenkins:

    • 已在 EC2 配置中介绍。通过 访问 Jenkins <ec2-public-ip>:8080
  2. 解锁詹金斯:

    • 检索初始管理员密码:
     sudo cat /var/lib/jenkins/secrets/initialAdminPassword
    
  • 完成安装向导。

2.2 配置 Jenkins 以集成 GitHub

  1. 安装 GitHub 插件:

    • 导航至Manage Jenkins -> Manage Plugins
    • 搜索“GitHub”并安装。
  2. 生成 GitHub 令牌:

    • 从 GitHub 生成个人访问令牌并将其添加到 Jenkins:
      • Manage Jenkins -> Manage Credentials -> Add Credentials
  3. 创建新作业:

    • 设置一个新的管道作业并将其链接到您的 GitHub 存储库。

2.3 设置 Jenkins 流水线

  1. 定义 Jenkinsfile:

    • 在您的存储库中创建一个Jenkinsfile
     pipeline {
       agent any
       stages {
         stage('Build') {
           steps {
             sh 'mvn clean install'
           }
         }
         stage('Test') {
           steps {
             sh 'mvn test'
           }
         }
         stage('Deploy') {
           steps {
             sh 'docker build -t myapp .'
             sh 'docker push myrepo/myapp'
           }
         }
       }
     }
    
  2. 触发管道:

    • 提交并将 Jenkinsfile 推送到您的存储库。
    • Jenkins 将自动触发构建。

步骤 3:使用 Docker 对应用程序进行容器化

3.1 编写 Dockerfile

  1. 创建 Dockerfile:

    • 在您的应用程序目录中:
     FROM openjdk:8-jdk-alpine
     VOLUME /tmp
     ARG JAR_FILE=target/*.jar
     COPY ${JAR_FILE} app.jar
     ENTRYPOINT ["java","-jar","/app.jar"]
    
  2. 构建 Docker 镜像:

    • 运行以下命令:
     docker build -t myapp:latest .
    

3.2 构建并推送 Docker 镜像

  1. 标记并推送图像:

    • 使用适当的版本标记图像:
     docker tag myapp:latest myrepo/myapp:v1.0.0
    
  • 将镜像推送到 Docker Hub:

     docker push myrepo/myapp:v1.0.0
    

3.3 Docker Compose 用于本地开发

  1. 创建docker-compose.yml文件:

    • 定义您的多容器应用程序:
     version: '3'
     services:
       app:
         image: myrepo/myapp:v1.0.0
         ports:
           - "8080:8080"
       db:
         image: mysql:5.7
         environment:
           MYSQL_ROOT_PASSWORD: password
           MYSQL_DATABASE: mydatabase
         ports:
           - "3306:3306"
    
  2. 运行 Docker Compose:

    • 本地启动应用程序:
     docker-compose up
    

步骤 4:部署到 Kubernetes (Amazon EKS)

4.1 设置EKS集群

  1. 安装kubectleksctl

    • 安装kubectl
     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/
    
  • 安装eksctl

     curl --silent --location "https://github.com/weaveworks/eksctl/releases/download/0.110.0/eksctl_Linux_amd64.tar.gz" | tar xz -C /tmp
     sudo mv /tmp/eksctl /usr/local/bin
    
  1. 创建 EKS 集群:
   eksctl create cluster --name my-cluster --version 1.21 --region us-east-1 --nodegroup-name my-nodes --node-type t3.medium --nodes 3
Enter fullscreen mode Exit fullscreen mode

4.2 创建 Kubernetes 清单

  1. 编写部署清单:

    • 创建一个deployment.yaml
     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: myapp-deployment
     spec:
       replicas: 3
       selector:
         matchLabels:
           app: myapp
       template:
         metadata:
           labels:
             app: myapp
         spec:
           containers:
           - name: myapp
             image: myrepo/myapp:v1.0.0
             ports:
             - containerPort: 8080
    

4.3 在 EKS 上部署应用程序

  1. 应用清单:

    • 将应用程序部署到 EKS:
     kubectl apply -f deployment.yaml
    
  • 监控部署:

     kubectl get pods
    
  1. 公开应用程序:

    • 创建服务来公开应用程序:
     apiVersion: v1
     kind: Service
     metadata:
       name: myapp-service
     spec:
       type: LoadBalancer
       selector:
         app: myapp
       ports:
         - protocol: TCP
           port: 80
           targetPort: 8080
    
  • 申请服务:

     kubectl apply -f service.yaml
    

步骤5:使用Prometheus和Grafana实现持续监控

5.1 安装 Prometheus

  1. 部署 Prometheus:

    • 使用 Helm 安装 Prometheus:
     helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
     helm repo update
     helm install prometheus prometheus-community/prometheus
    
  2. 配置 Prometheus:

    • 编辑values.yaml文件以抓取您的应用程序指标:
     scrape_configs:
       - job_name: 'myapp'
         static_configs:
           - targets: ['myapp-service:8080']
    

5.2 配置Grafana仪表板

  1. 部署 Grafana:

    • 通过 Helm 安装 Grafana:
     helm install grafana grafana/grafana
    
  2. 访问 Grafana:

    • 找回管理员密码:
     kubectl get secret --namespace default grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo
    
  • 转发端口以访问 Grafana:

     kubectl port-forward svc/grafana 3000:80
    
  1. 添加 Prometheus 作为数据源:
    • 登录Grafana并添加Prometheus作为数据源。

5.3 设置警报

  1. 定义警报规则:

    • 在 Prometheus 中针对关键指标创建警报规则:
     groups:
       - name: example
         rules:
         - alert: HighMemoryUsage
           expr: node_memory_Active_bytes > 1e+09
           for: 1m
           labels:
             severity: critical
           annotations:
             summary: "Instance {{ $labels.instance }} high memory usage"
    
  2. 设置 Alertmanager:

    • 配置 Alertmanager 以进行通知:
     receivers:
       - name: 'email'
         email_configs:
           - to: 'your-email@example.com'
    

步骤 6:保护 CI/CD 管道

6.1 使用Trivy扫描漏洞

  1. 安装Trivy:

    • 在 Jenkins 服务器上安装 Trivy:
     sudo apt-get install wget apt-transport-https gnupg lsb-release
     wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo apt-key add -
     echo deb https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main | sudo tee -a /etc/apt/sources.list.d/trivy.list
     sudo apt-get update
     sudo apt-get install trivy
    
  2. 将 Trivy 与 Jenkins 集成:

    • 将 Trivy 添加到 Jenkins 管道:
     stage('Security Scan') {
       steps {
         sh 'trivy image myrepo/myapp:v1.0.0'
       }
     }
    

6.2 集成 SonarQube 以提高代码质量

  1. 安装SonarQube:

    • 在 EC2 实例上安装 SonarQube:
     sudo yum install java-11-openjdk-devel -y
     wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-8.9.6.50800.zip
     unzip
    

sonarqube-*.zip
sudo mv sonarqube-8.9.6.50800 /opt/sonarqube
sudo chown -R sonar: /opt/sonarqube
```

  1. 配置SonarQube:

    • 修改sonar.properties数据库集成的文件:
     sonar.jdbc.username=sonar
     sonar.jdbc.password=sonar
     sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube
    
  2. 将 SonarQube 与 Jenkins 集成:

    • 在 Jenkins 中添加 SonarQube 分析:
     stage('SonarQube Analysis') {
       steps {
         withSonarQubeEnv('My SonarQube Server') {
           sh 'mvn sonar:sonar'
         }
       }
     }
    

结论

本项目指南深入讲解了如何搭建端到端 DevOps 流水线,涵盖 CI/CD、容器化、Kubernetes 部署、监控和安全等功能。遵循本指南,您不仅可以积累实践经验,还能创建一条可用于生产的流水线。请记住,掌握 DevOps 的关键在于持续实践并掌握最新的工具和方法。

最后的想法

您可以根据项目需求自定义步骤并集成更多工具。DevOps 领域广阔,本指南只是您成为熟练 DevOps 工程师的第一步。祝您编码愉快,部署愉快!


👤 作者

横幅

加入我们的电报社区||在 GitHub 上关注我以获取更多 DevOps 内容!

文章来源:https://dev.to/prodevopsguytech/end-to-end-devops-project-building-deploying-and-monitoring-a-full-stack-application-ako
PREV
CI/CD 入门:自动化您的第一个管道的初学者指南(使用 Jenkins)
NEXT
端到端 AWS DevOps 项目:用于 ECS Fargate 和 ECR 及 RDS 的 CI/CD 管道