ArgoCD二:一个完整的CICD流程例子
source link: https://www.bladewan.com/2021/01/27/argo_cd_2/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
ArgoCD二:一个完整的CICD流程例子
此篇文章演示一个完整的GitOps工作流程,使用Gitlab-CI+ArgoCD来实现,其中Gitlab-CI主要用于实现代码编译、镜像构建、修改部署的yaml文件。ArgoCD用于将修改后的部署yaml文件推送到各个集群中。
软件 版本 Gitlab 13.7.0 ArgoCD 1.8.1 Kubernetes 1.17.41、将应用代码和构建Docker镜像的Dockerfile文件放置到Gitlab对应项目中
2、在Gitlab中创建用于专门用于存放部署yaml的项目
3、配置Gitlab-CI用于代码编译镜像构建和业务yaml文件修改
4、配置ArgoCD检测存放部署yaml的项目,有更新后自动部署到对应环境中
Gitlab配置
以一个Go-server项目为例
代码文件
server.go文件
Dockerfile文件
在gitlab中创建proc-yaml的项目,用于集中存放到Kubernetes集群中的业务的yaml文件
在proc-yaml项目中创建go-server项目的文件夹,专门存放go-server项目的yaml文件
deployment.yaml
Gitlab-CI配置
Gitlab-CI配置和基本使用可以参考本博客的Gitlab-CI时用章节
这里体现的主要是和之前的差异性部分
Gitlab-CI环境变量配置
主要添加连接镜像仓库和代码仓库的凭证
创建.gitlab-ci.yml文件,用于配置gitlab-ci
主要包含3个阶段,代码编译阶段、镜像构建阶端、部署yaml文件修改阶段
代码编译阶段:启动一个编译容器,根据代码编译出对应的制品,这里为了能让下个镜像构建阶段直接使用编译好的制品,需要配置artifacts将目录共享给下个阶段。
镜像构建阶段:使用DinD的方式构建Docker镜像,然后镜像tag以我们Pipeline的ID为tag,这里好处在于可以根据对应的Pipeline的迅速定位到对应的镜像版本
部署yaml修改阶段: 通过git命令将对部署yaml项目的repo clone下来然后通过sed进行修改并commit回去,通过此步ArgoCD检测到变化后会拉取新的yaml进行部署
ArgoCD配置
在ArgoCD中配置对接pro-yaml的repo认证
创建App
选择自动同步模式
PATH这配置我们go-server这个项目的路径,就会检测这里面的yaml文件,并自动更新到集群中
最后创建完成
修改server.go的Hello world为Hello Argo
等待git-ci执行
检查proc-yaml项目是否更新
可以看见对应的image的tag修改成了,我们实际上Pipeline的ID
等待片刻后ArgoCD检测到yaml变化,自动部署,此时Kubernetes中应用进行了自动更新
通过GitlabCI与ArgoCD结合确实非常方便的将我们应用进行部署到Kubernetes中,并且后续也非常容易进行回滚,在之前通过Gitlab-CI中集成kubectl镜像,进行部署容易造成kubeconfig文件的泄漏,也不方便做权限的管理和应用状态控制,但结合ArgoCD后一切都美好了起来。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK