当前位置: 首页 > news >正文

【云原生与DevOps】05-GitOps工作流:ArgoCD+GitHub Actions完整方案

专栏:云原生 & DevOps
难度:进阶
标签:GitOpsArgoCDGitHub ActionsK8s自动化部署


前言

GitOps 的核心思想:Git 是唯一事实来源,所有变更都通过 Git PR 触发。本文实现一套完整的 GitOps 流水线。


一、架构设计

开发者 push 代码 ↓ GitHub Actions(CI) - 构建镜像 - 推送到 Registry - 更新 GitOps 仓库中的镜像 tag ↓ ArgoCD(CD) - 监控 GitOps 仓库变化 - 自动同步到 K8s 集群

二、安装 ArgoCD

kubectl create namespace argocd kubectl apply-nargocd-f\https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml# 等待Pod就绪kubectlwait--for=condition=available\deployment/argocd-server-nargocd--timeout=300s# 获取初始密码kubectl-nargocd get secret argocd-initial-admin-secret\-ojsonpath="{.data.password}"|base64-d# 暴露UI(开发用)kubectl port-forward svc/argocd-server-nargocd8080:443

三、创建 ArgoCD Application

# argocd-app.yamlapiVersion:argoproj.io/v1alpha1kind:Applicationmetadata:name:myapp-productionnamespace:argocdspec:project:defaultsource:repoURL:https://github.com/yourorg/gitops-configs.gittargetRevision:mainpath:apps/myapp/overlays/productiondestination:server:https://kubernetes.default.svcnamespace:productionsyncPolicy:automated:prune:true# 自动删除Git中不存在的资源selfHeal:true# 检测到集群状态与Git不符时自动修复syncOptions:-CreateNamespace=true

四、GitHub Actions 工作流

# .github/workflows/ci-cd.ymlname:CI/CD Pipelineon:push:branches:[main]pull_request:branches:[main]env:REGISTRY:ghcr.ioIMAGE_NAME:${{github.repository}}GITOPS_REPO:yourorg/gitops-configsjobs:build-and-push:runs-on:ubuntu-latestoutputs:image-tag:${{steps.meta.outputs.version}}steps:-uses:actions/checkout@v4-name:Docker metaid:metauses:docker/metadata-action@v5with:images:${{env.REGISTRY}}/${{env.IMAGE_NAME}}tags:|type=sha,prefix=,suffix=,format=short-name:Build and pushuses:docker/build-push-action@v5with:push:${{github.event_name!='pull_request'}}tags:${{steps.meta.outputs.tags}}update-gitops:needs:build-and-pushif:github.ref == 'refs/heads/main'runs-on:ubuntu-lateststeps:-uses:actions/checkout@v4with:repository:${{env.GITOPS_REPO}}token:${{secrets.GITOPS_TOKEN}}-name:Update image tagrun:|cd apps/myapp/overlays/production sed -i "s|newTag:.*|newTag: ${{ needs.build-and-push.outputs.image-tag }}|" kustomization.yaml-name:Commit and pushrun:|git config user.email "ci@example.com" git config user.name "CI Bot" git add . git commit -m "ci: update myapp to ${{ needs.build-and-push.outputs.image-tag }}" git push

五、回滚操作

# 通过ArgoCD UI或命令行回滚argocd apphistorymyapp-production argocd app rollback myapp-production<REVISION># 或者通过Git回滚(推荐,有记录)gitrevert HEADgitpush

结语:GitOps 最大的价值是审计性——所有变更都有 Git 记录,随时可以追溯谁在什么时候改了什么。这对于合规要求高的场景非常重要。

http://www.jsqmd.com/news/1099470/

相关文章:

  • 批量图片翻译视频字幕智能抠图一站式解决跨境电商难题
  • MoE模型参数量与激活机制的技术辨析
  • 免费开源:用pk3DS打造你的专属宝可梦3DS游戏世界
  • 双向链表,反转链表
  • 2026年家电原生智能品牌实用推荐
  • 61.吃透 PLC 状态机编程!带超时停机 + 料仓满报警 + 传感器防抖完整实战
  • PHP本地开发环境一键部署与API集成实践指南
  • MySQL索引深潜:从B+树到查询优化器的艺术
  • Spring Boot 3.0.5 + Vue 3 实战:手把手教你搞定WebSocket消息推送(含完整前后端代码)
  • 浏览器中的专业SVG编辑器:如何用SVG-Edit解决矢量图形编辑难题
  • 基于stm32单片机的智能空气净化器设计家居成品PM2.5甲醛检测定制3(设计源文件+万字报告+讲解)(支持资料、图片参考_相关定制)_文章底部可以扫码
  • 基于STM32单片机智能窗帘设计 智能晾衣架控制 定时开关光照 雨滴3(设计源文件+万字报告+讲解)(支持资料、图片参考_相关定制)_文章底部可以扫码
  • 2026年值得关注的AI外呼厂商盘点:从云厂商到垂直方案,怎么选更合适?
  • 不止传照片——140+应用已适配鸿蒙7碰一碰分享
  • Java中实现html转pdf
  • 鸿蒙NEXT应用开发实战:SM3国密算法在数据安全与完整性校验中的应用
  • 单片机IWIP SNTP实验
  • 3分钟学会Untrunc:快速拯救损坏视频文件的终极指南
  • 3-IPV6域名解析
  • Web作业(八)
  • 好用的亚洲汽美抛光赛事供应商
  • 实战掌握Adobe软件激活:全面解析GenP 3.0破解工具高效配置
  • 后端性能瓶颈排查实战:从慢接口到系统优化的完整落地思路
  • 66.TIA V17 实测无 BUG!带 20ms 软件滤波、边沿检测、急停联锁 PLC 工程
  • STM32单片机家用智能热水器水温水位检测加热恒温控制无线app设计2(设计源文件+万字报告+讲解)(支持资料、图片参考_降重降ai)
  • 让AI读懂你的大脑:BrainAgent用LLM驱动多Agent实现脑信号全流程自动分析
  • 《Debezium + Kafka Connect 实战:从零搭建 MySQL CDC 数据管道,踩坑全记录》
  • 2026效率榜!好用的降AIGC网站全盘点,过审成功率直接拉满
  • HCIA-Datacom 课程学习心得
  • 金属浮栅提升NAND性能