k8s中更优雅的持续发布ArgoCD
Argocd是一种更优雅的持续发布K8S服务的一种产品。
argocd是cncf毕业项目,前途不可估量。
Argocd介绍
前情介绍:
gitops哲学理念
GitOps可谓一个操作模型,提供了一组最佳实践,对于容器集群和应用的统一部署,管理和监控。所有的变更都应该落地到git仓库,所有的运行状态与git仓库配置一致。
比如你的yaml配置在git repo里面,但是k8s里面运行的实际情况,如果有不一致如何发现?
多个devops、SRE如何协同工作?
所以Gitops的核心思想:
“将应用系统的声明性基础架构和应用程序存放在Git的版本控制库中”
gitops的优点:
安全的云原生CI/CD管道模型
更快的平均部署时间和平均恢复时间
稳定且可重现的回滚(例如,根据Git恢复/回滚/ fork)
与监控和可视化工具相结合,对已经部署的应用进行全方位的监控
Argocd组成
1.argocd-server argocd的api服务,无状态。
2.argocd-application应用服务控制器,通过和k8s api以及argocd-repo的声明进行服务状态对比,无状态。
3.argocd-repo服务,负责管理gitops的仓库、认证凭证等等,无状态。
4.argo-dex-server和redis是argocd的基础设施数据库。
核心概念
Application:清单定义的一组Kubernetes资源。这是一个自定义资源定义(CRD)。
Application source type:使用哪个工具来构建应用程序
Target state:应用程序的期望状态,由Git存储库中的文件表示。
Live state:该应用程序的实时状态。部署了哪些Pod等。
Sync status:实时状态是否与目标状态匹配。部署的应用程序是否与Git所说的相同?
Sync:使应用程序移至其目标状态的过程。例如。通过将更改应用于Kubernetes集群。
Refresh:将Git中的最新代码与实时状态进行比较。找出有什么不同。
Health:应用程序的运行状况是否正常运行?它可以满足请求吗?
Tool:从文件目录创建清单的工具。例如。Kustomize或Ksonnet。请参阅应用程序源类型。
英文版本:
Application A group of Kubernetes resources as defined by a manifest.
This is a Custom Resource Definition (CRD).
通俗理解:就是需要部署的服务
Application source type Which Tool is used to build the application.
Target state The desired state of an application, as represented by files in a Git repository.
通俗理解:应用部署服务的源类型,因为现在部署k8s的手段有点多,就是部署源文件可能来自helm、yaml、json、Kustomize等各种变态工具。
Live state The live state of that application. What pods etc are deployed.
通俗理解:实时状态,服务在k8s集群中的实时状态,到底是运行还是拉取,还是崩溃状态。
Sync status Whether or not the live state matches the target state. Is the deployed application the same as Git says it should be?
Sync The process of making an application move to its target state. E.g. by applying changes to a Kubernetes cluster.
通俗理解:同步状态,到底是同步还是不同步在状态,可能仓库比较新,而运行状态比较老。或者哪个沙雕手工直接更改了k8s的部署,而没有更新git状态,也会导致不一致。
Sync operation status Whether or not a sync succeeded.
通俗理解:同步,就是说你实际运行与git仓库里面的编排不一致,就需要同步操作。
Refresh Compare the latest code in Git with the live state. Figure out what is different.
通俗理解:刷新运行态与仓库实际定义是否一致,手动挡刷新。
Health The health of the application, is it running correctly? Can it serve requests?
通俗理解:运行服务是否正常,是不是按预期运行。
Tool A tool to create manifests from a directory of files. E.g. Kustomize or Ksonnet. See Application Source Type.
通俗理解:编排工具选择。
Configuration management tool See Tool.
Configuration management plugin A custom tool.
服务部署
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
服务情况
通过argocd管理UI进行发布服务
kubectl port-forward svc/argocd-server 1800:80 -n argocd
就可以通过http://127.0.0.1:1800进行访问argocd-ui
用户名admin密码是argocd-server的pod名称
如何获取argocd-server的pod名称就不需要我说了吧:-)
手工部署好后的一个服务状态
查看服务部署详情
后面的就只需持续发布更新我们的gitops repo就可以实现Gitops了!!!
如何在CICD中无缝集成呢,先抛一段代码
#Makefile
build:
$你的编译流程
docker-build: build
docker build -t $img:$(GIT_VERSION) .
push: docker-build
docker push $img:$(GIT_VERSION)
gitops:
kustomize edit set $img:$(GIT_VERSION)
git pull && git commit -am "注释" && git push
#push完成后,如果你在argo上设置的是自动更新,就会自动发布,如果设置的手功更新就手工操作