使用 Terraform 在 Amazon EKS 上的 ArgoCD GitOps:完整实现指南
发布: (2025年12月12日 GMT+8 12:35)
5 min read
原文: Dev.to
Source: Dev.to
GitOps 概览
GitOps 是一种现代的持续部署方法,它使用 Git 作为声明式基础设施和应用程序的唯一真实来源。核心原则包括:
- 声明式配置 – 所有内容都以声明式方式在 Git 中描述。
- 版本控制 – 所有更改都被跟踪并可审计。
- 自动化部署 – Git 中的更改会触发自动部署。
- 持续监控 – 系统持续确保期望状态与实际状态保持一致。
为什么选择 ArgoCD?
ArgoCD 是面向 Kubernetes 的声明式 GitOps 持续交付工具,提供:
- 应用管理 – 对多个应用进行集中管理。
- 多集群支持 – 可部署到多个 Kubernetes 集群。
- 丰富 UI – 直观的网页界面用于监控部署。
- RBAC 集成 – 细粒度的访问控制。
- 回滚能力 – 轻松回滚到先前版本。
架构概览
我们的实现构建了一个稳健、可扩展的架构,包含以下内容:
┌──────────────────────────────────────────────────────────────┐
│ AWS Cloud │
│ ┌──────────────────────────────────────────────────────────┐│
│ │ VPC ││
│ │ ┌─────────────────┐ ┌──────────────────────────────┐ ││
│ │ │ Public Subnets │ │ Private Subnets │ ││
│ │ │ │ │ ┌─────────────────────────┐ │ ││
│ │ │ ┌───────────┐ │ │ │ EKS Cluster │ │ ││
│ │ │ │ NAT │ │ │ │ ┌─────────────────────┐│ │ ││
│ │ │ │ Gateway │ │ │ │ │ NGINX Ingress ││ │ ││
│ │ │ └───────────┘ │ │ │ │ Controller ││ │ ││
│ │ │ │ │ │ └─────────────────────┘│ │ ││
│ │ └─────────────────┘ │ │ ┌─────────────────────┐│ │ ││
│ │ │ │ │ ArgoCD ││ │ ││
│ │ │ │ │ Server ││ │ ││
│ │ │ │ └─────────────────────┘│ │ ││
│ │ │ │ ┌─────────────────────┐│ │ ││
│ │ │ │ │ Application ││ │ ││
│ │ │ │ │ Workloads ││ │ ││
│ │ │ │ └─────────────────────┘│ │ ││
│ │ │ └─────────────────────────┘ │ ││
│ │ └──────────────────────────────┘ ││
│ └──────────────────────────────────────────────────────────┘│
│ │
│ ┌──────────────────────────────────────────────────────────┐│
│ │ Route53 ││
│ │ argocd.chinmayto.com → NGINX Ingress NLB ││
│ │ app.chinmayto.com → NGINX Ingress NLB ││
│ └──────────────────────────────────────────────────────────┘│
└──────────────────────────────────────────────────────────────┘
前置条件
在开始之前,请确保您已具备:
- 已配置好相应权限的 AWS CLI
- 已安装 Terraform(版本 ≥ 1.0)
- 已安装
kubectl - 在 Route53 中注册的域名
- 已安装 Helm(版本 ≥ 3.0)
实施步骤
步骤 1:创建 VPC 和 EKS 集群
我们首先使用 AWS 社区 Terraform 模块创建基础设施。
变量 (infrastructure/variables.tf)
variable "aws_region" {
description = "AWS region"
type = string
default = "us-east-1"
}
variable "cluster_name" {
description = "Name of the EKS cluster"
type = string
default = "CT-EKS-Cluster"
}
variable "cluster_version" {
description = "Kubernetes version for the EKS cluster"
type = string
default = "1.33"
}
variable "vpc_cidr" {
description = "CIDR block for VPC"
type = string
default = "10.0.0.0/16"
}
variable "public_subnet_cidrs" {
description = "CIDR blocks for public subnets"
type = list(string)
default = ["10.0.1.0/24", "10.0.2.0/24"]
}
variable "private_subnet_cidrs" {
description = "CIDR blocks for private subnets"
type = list(string)
default = ["10.0.10.0/24", "10.0.20.0/24"]
}
主 Terraform 配置 (infrastructure/main.tf)
# Data source for availability zones
data "aws_availability_zones" "available" {
state = "available"
}
# VPC Module Configuration
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 5.0"
name = "${var.cluster_name}-VPC"
cidr = var.vpc_cidr
azs = slice(data.aws_availability_zones.available.names, 0, 2)
private_subnets = var.private_subnet_cidrs
public_subnets = var.public_subnet_cidrs
enable_nat_gateway = true
enable_vpn_gateway = false
single_nat_gateway = true
enable_dns_hostnames = true
enable_dns_support = true
public_subnet_tags = {
"kubernetes.io/role/elb" = "1"
}
private_subnet_tags = {
"kubernetes.io/role/internal-elb" = "1"
}
tags = {
Name = "${var.cluster_name}-VPC"
Terraform = "true"
}
}
# EKS Cluster Module Configuration
module "eks" {
source = "terraform-aws-modules/eks/aws"
version = "~> 20.0"
cluster_name = var.cluster_name
cluster_version = var.cluster_version
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnets
cluster_endpoint_public_access = true
enable_cluster_creator_admin_permissions = true
# EKS Managed Node Groups
eks_managed_node_groups = {
EKS_Node_Group = {
min_size = 1
max_size = 3
desired_size = 2
instance_types = ["t3.medium"]
capacity_type = "ON_DEMAND"
subnet_ids = module.vpc.private_subnets
}
}
# EKS Add‑ons
cluster_addons = {
coredns = {
most_recent = true
}
kube-proxy = {
most_recent = true
}
vpc-cni = {
most_recent = true
}
eks-pod-identity-agent = {
most_recent = true
}
}
tags = {
Name = var.cluster_name
Terraform = "true"
}
}
# Null Resource to update the kubeconfig file
resource "null_resource" "update_kubeconfig" {
provisioner "local-exec" {
command = "aws eks --region ${var.aws_region} update-kubeconfig --name ${var.cluster_name}"
}
depends_on = [module.eks]
}