GitOps 与 ArgoCD

195次阅读

共计 5586 个字符,预计需要花费 14 分钟才能阅读完成。

什么是 GitOps?

GitOps 是一个概念,将软件的端到端描述放置到 Git 中,然后尝试着让集群状态和 Git 仓库持续同步,其中有两个概念需要说明下。

GitOps 是以 Git 为基础,使用CI/CD来更新运行在云原生环境的应用,它秉承了DevOps的核心理念 – 构建它并交付它(you built it you ship it)

  • 当开发人员将开发完成的代码推送到git仓库会触发CI制作镜像并推送到镜像仓库
  • CI处理完成后,可以手动或者自动修改应用配置,再将其推送到git仓库
  • GitOps会同时对比目标状态和当前状态,如果两者不一致会触发CD将新的配置部署到集群中

其中,目标状态是 Git 中的状态,现有状态是集群的里的应用状态。

不用GitOps可以么?

当然可以,我们可以使用kubectl、helm等工具直接发布配置,但这会存在一个很严重的安全问题,那就是密钥共享。

为了让CI系统能够自动的部署应用,我们需要将集群的访问密钥共享给它,这会带来潜在的安全问题。

为什么需要 GitOps?

GitOps 可以在很多方面都产生价值,下面我们来看看其中的一些关键的价值。

应用交付速度

持续的 GitOps 可以通过以下几个方面来提高产品交付速度。

  1. 可以比较最终的 YAML 和集群状态的能力,这也可以作为批准发布的决策指南。
  2. 借助 Prometheus 的应用程序指标,通过自动化的蓝绿部署,非常容易进行部署。
  3. 根据策略自动更新容器镜像,例如,Istio sidecar 次要版本的发布是向后兼容的,可以自动更新。
  4. GitOps 将以运维和开发为中心,提高效率。
  5. 应用团队可以接管一些运维工作,而运维团队则可以更加专注于平台建设。
  6. GitOps 仓库可以绕过完整的持续部署流程进行紧急发布。

端到端的自动化

在 GitOps 中,所有和应用开发、应用运维和集群运维相关的声明都通过 git 嵌入到 YAML 文件中,实现了端到端的自动化。

安全、审计和合规性

零手动更改到集群中应用的策略将大大增加集群的安全性,由于集群中的所有配置都在 git 中,我们将拥有一个完整的审计日志,记录集群中发生的事情。

集群可观测性

有了完整的审计日志,我们就可以很容易获得集群中发生的变化,来帮助调试一些问题。

关注点分离和迁移

GitOps 将应用开发者、应用运维和集群运维之间的关注点进行分离,这些团队中的依赖关系以声明式的方式注入到 git 中,这将大大缓解我们对底层 K8S 集群、治理策略等工具的迁移。

什么是Argo CD?

Argo CD遵循GitOps模式,使用Git存储库存储所需应用程序的配置。

Kubernetes清单可以通过以下几种方式指定:

  • kustomize应用程序
  • helm图表
  • ksonnet应用程序
  • jsonnet文件
  • 基于YAML/json配置
  • 配置管理插件配置的任何自定义配置管理工具

Argo CD实现为kubernetes控制器,它持续监视运行中的应用程序,并将当前的活动状态与期望的目标状态进行比较(如Git repo中指定的那样)。如果已部署的应用程序的活动状态偏离了目标状态,则认为是OutOfSync。Argo CD报告和可视化这些差异,同时提供了方法,可以自动或手动将活动状态同步回所需的目标状态。在Git repo中对所需目标状态所做的任何修改都可以自动应用并反映到指定的目标环境中。

它的优势总结如下:

  • 应用定义、配置和环境信息是声明式的,并且可以进行版本控制;
  • 应用部署和生命周期管理是全自动化的,是可审计的,清晰易懂;
  • Argo CD是一个独立的部署工具,支持对多个环境、多个Kubernetes集群上的应用进行统一部署和管理

本文就详细介绍了 Argo 的相关内容。

Argo CD安装

[~]$ kubectl create namespace argocd
namespace/argocd created
[~]$ kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
➜  ~ kubectl get all -n argocd
NAME                                     READY   STATUS    RESTARTS   AGE
pod/argocd-application-controller-0      1/1     Running   0          15h
pod/argocd-dex-server-746b887b45-fzvs8   1/1     Running   0          15h
pod/argocd-redis-86c97c8c64-fhkqs        1/1     Running   0          15h
pod/argocd-repo-server-fff8b99f9-2wlnd   1/1     Running   0          15h
pod/argocd-server-8f9d5ff4c-j8dt2        1/1     Running   0          15h

NAME                            TYPE           CLUSTER-IP       EXTERNAL-IP      PORT(S)                      AGE
service/argocd-dex-server       ClusterIP      172.20.255.190   <none>           5556/TCP,5557/TCP,5558/TCP   15h
service/argocd-metrics          ClusterIP      172.20.253.60    <none>           8082/TCP                     15h
service/argocd-redis            ClusterIP      172.20.255.45    <none>           6379/TCP                     15h
service/argocd-repo-server      ClusterIP      172.20.255.175   <none>           8081/TCP,8084/TCP            15h
service/argocd-server           LoadBalancer   172.20.253.94    150.158.228.37   80:30932/TCP,443:32105/TCP   15h
service/argocd-server-metrics   ClusterIP      172.20.255.233   <none>           8083/TCP                     15h

NAME                                 READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/argocd-dex-server    1/1     1            1           15h
deployment.apps/argocd-redis         1/1     1            1           15h
deployment.apps/argocd-repo-server   1/1     1            1           15h
deployment.apps/argocd-server        1/1     1            1           15h

NAME                                           DESIRED   CURRENT   READY   AGE
replicaset.apps/argocd-dex-server-746b887b45   1         1         1       15h
replicaset.apps/argocd-redis-86c97c8c64        1         1         1       15h
replicaset.apps/argocd-repo-server-fff8b99f9   1         1         1       15h
replicaset.apps/argocd-server-8f9d5ff4c        1         1         1       15h

NAME                                             READY   AGE
statefulset.apps/argocd-application-controller   1/1     15h

通过kubectl edit -n argocd svc argocd-server将 service 的 type 类型改为 LoadBalancer(公有云部署)

GitOps 与 ArgoCD

登录账号为admin,密码通过以下命令获取

kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d

GitOps 与 ArgoCD

至此,ArgoCD 部署完毕。

Argo CD 客户端安装

Linux

[root@VM-1-7-tlinux ~]# curl -sSL -o /usr/local/bin/argocd https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
[root@VM-1-7-tlinux ~]# chmod +x /usr/local/bin/argocd
[root@VM-1-7-tlinux ~]# argocd
argocd controls a Argo CD server

Usage:
  argocd [flags]
  argocd [command]

MacOS

➜  ~ brew install argocd
➜  ~ argocd version
argocd: v2.2.5+8f981cc.dirty
  BuildDate: 2022-02-05T05:39:12Z
  GitCommit: 8f981ccfcf942a9eb00bc466649f8499ba0455f5
  GitTreeState: dirty
  GoVersion: go1.17.6
  Compiler: gc
  Platform: darwin/amd64
argocd-server: v2.2.5+8f981cc
  BuildDate: 2022-02-05T01:33:25Z
  GitCommit: 8f981ccfcf942a9eb00bc466649f8499ba0455f5
  GitTreeState: clean
  GoVersion: go1.16.11
  Compiler: gc
  Platform: linux/amd64
  Ksonnet Version: v0.13.1
  Kustomize Version: v4.2.0 2021-06-30T22:49:26Z
  Helm Version: v3.7.1+g1d11fcb
  Kubectl Version: v0.22.2
  Jsonnet Version: v0.17.0

Argo CD使用

CLI 登录

[root@VM-1-7-tlinux ~]# argocd login 150.158.228.37
WARNING: server certificate had error: x509: cannot validate certificate for 150.158.228.37 because it doesn't contain any IP SANs. Proceed insecurely (y/n)? y
Username: admin 
Password: 
'admin:login' logged in successfully
Context '150.158.228.37' updated

集群操作

1 新增集群

新增集群只能通过cli命令

argocd cluster add --kubeconfig <新集群kubeconfig文件> --name <新集群名>

集群1:

argocd cluster add --kubeconfig ./argo-cd-1 --name cluster-1 cls-xxxx-353-context-default

集群2:

argocd cluster add --kubeconfig ./argo-cd-2 --name cluster-2 cls-xxxx-353-context-default
WARNING: This will create a service account `argocd-manager` on the cluster referenced by context `cls-xxxx-353-context-default` with full cluster level admin privileges. Do you want to continue [y/N]? y
INFO[0001] ServiceAccount "argocd-manager" already exists in namespace "kube-system" 
INFO[0001] ClusterRole "argocd-manager-role" updated    
INFO[0001] ClusterRoleBinding "argocd-manager-role-binding" updated 
Cluster 'https://cls-xxxx.ccs.tencent-cloud.com' added

2 列出集群

[root@VM-1-7-tlinux ~]# argocd cluster list
SERVER                                      NAME        VERSION  STATUS      MESSAGE                                              PROJECT
https://cls-9g7phzaz.ccs.tencent-cloud.com  cluster-2   1.18+    Successful                                                       
https://cls-jrn4ycu5.ccs.tencent-cloud.com  cluster-1   1.18+    Successful                                                       

3 删除集群

[root@VM-1-7-tlinux ~]# argocd cluster rm https://cls-xxxx.ccs.tencent-cloud.com
Cluster 'https://cls-xxxx.ccs.tencent-cloud.com' removed

添加应用

一个app表示一组跟踪的资源文件,如deployment + service 的经典组合

GitOps 与 ArgoCD

SYNC POLICY: 自动 or 手动同步。
PRUNE LAST: 最后才做删除操作,根据需求使用。
REPLACE: 放弃 kubectl apply,使用 kubectl replacekubectl create

Repository URL: 资源文件地址,支持 kustomize、helm、ksonnet、jsonnet files、yaml files 等格式
Revision: 跟踪的分支或 tag 名
Path: 资源文件相对根目录的路径
Cluster URL: 部署的集群
namespace: 集群命名空间
创建完成,点击 sync 同步

GitOps 与 ArgoCD

GitOps 与 ArgoCD

GitOps 与 ArgoCD

此时,Argo 会同步集群中资源和 Git 中的定义是否同步

修改 Git

GitOps 与 ArgoCD

GitOps 与 ArgoCD

OutOfSync: 应用程序活动正常,但是与Git中同步状态异常,即未更新

可以通过APP中的 DIFF 查看对比差异

GitOps 与 ArgoCD

再次 SYNC 后即可

GitOps 与 ArgoCD

正文完
 
mervinwang
版权声明:本站原创文章,由 mervinwang 2022-06-15发表,共计5586字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
文章搜索