前言
ArgoCD是一个基于Kubernetes的GitOps持续交付工具,应用的部署和更新都可以在Git仓库上同步实现,并自带一个可视化界面。本文介绍如何使用Git+Helm+Argocd方式来实现在k8s中部署和更新应用服务;
安装Argocd
- 准备一个k8s集群,然后从官网获取yaml部署清单执行部署即可;
# 创建命名空间
kubectl create namespace argocd # 部署argocd
wget https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
kubectl apply -n argocd -f install.yaml# 查看状态
kubectl get pod -n argocd -w# 调整下svc为nodeport默认,访问ui页面用的
kubectl -n argocd edit svc argocd-server
- 访问页面,输入地址后会自动跳转https;
# 获取ui页面的登陆密码,管理员为 admin
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d
默认管理密码是随机的,可以使用如下方式修改;
# 在线获取bcrpyt加密后的密码值: https://www.bejson.com/encrypt/bcrpyt_encode/
准备一个密码,自行到上面的地址加密即可# 将加密后的密文替换为下面的 admin.password 的值
kubectl -n argocd patch secret argocd-secret \-p '{"stringData": {"admin.password": "$2a$10$dVCUtDIFah893qSLMMIReeyNa8vHx1112/kLYTbglAQMpbzBR5dbK","admin.passwordMtime": "'$(date +%FT%T%Z)'"
}}'
验证1:(官方给出的样例)
Argo CD 提供了一个官网样例,我们就创建一下这个项目吧
样例github地址: https://github.com/argoproj/argocd-example-apps
web页面创建APP
- Application Name: app的名称,填写的是样例项目的名称
- Project: 是一种资源,用于组织和管理不同的 Kubernetes 应用(Application,目前先写default
- SYNC POLICY: 同步策略,有手动和自动,样例项目,我们先选择手动
- AUTO-CREATE NAMESPACE: 自动创建应用部署的k8s的命名空间
- SOURCE: Git 仓库,就是样例项目的github仓库地址
- Revision: 分支名
- Path: 资源文件所在的相对路径,Argo CD目前支持多种 Kubernetes 清单,这里需要选择使用那种资源配置模式就选择哪一个路径下的资源清单
- Cluster URL: Kubernetes API Server 的访问地址,由于 Argo CD 和下发应用的 Kubernetes 集群是同 一个,因此可以直接使用 http://kubernetes.default.svc 来访问
- Namespace: 应用部署在k8s中的命名空间
- 点击页面上面的create按钮
- 手动同步
- web页面展示
验证2:(自己手戳)
argocd支持多种配置管理/模板工具,如 Kustomize,Helm,Ksonnet,Jsonnet,plain-YAML,这里我使用的是helm来做,先简单制作一个helm chart 来定义应用的部署清单;
# 准备一个主目录存放资源文件
mkdir gitops && cd gitops/ && git init
mkdir -p {helm,argocd} && cd helm/# 添加一个helm chart
# tree -L 2
└── app1├── Chart.yaml├── templates│ ├── deployment.yaml│ └── service.yaml└── values.yaml
Chart.yaml
apiVersion: v2
appVersion: "1.0"
name: app1
description: app1 for kubernetes
type: application
version: 0.1.0
values.yaml
global:replicas: 2
image:repository: harbor.example.cn/public/nginx#repository: nginxtag: stable-alpine
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: appnamespace: {{ .Release.Namespace }}
spec:replicas: {{ .Values.global.replicas }}selector:matchLabels:demo: app1template:metadata:labels:demo: app1spec:containers:- name: nginx# 这里引入变量,层级调用,.Values表声明作用image: {{ .Values.image.repository }}:{{ .Values.image.tag }}ports:- containerPort: 80name: http
service.yaml
apiVersion: v1
kind: Service
metadata:name: app1-svcnamespace: {{ .Release.Namespace }}labels:demo: app1
spec:type: ClusterIPselector:demo: app1ports:- port: 80name: httptargetPort: httpprotocol: TCP
定义argocd清单
- argocd有自己的声明式写法,这里定义一个用来调用和管理应用部署的资源清单;
# 进入argocd的存放目录,在下面创建4个目录来区分不同的集群环境
cd gitops/argocd && mkdir {dev,test,pre,prod}# 定义清单文件
cd argocd /
tree -L 2
.
├── dev
│ └── Application.yaml
├── pre
│ └── Application.yaml
├── prod
│ └── Application.yaml
└── test└── Application.yaml
Application.yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:name: demo-argocdnamespace: argocd
spec:project: default # 所属项目,默认即可source:repoURL: https://git.example.com/gitops/gitops1.git # helm所在的git仓库地址(后面需要推送的git仓库)# git仓库的分支版本targetRevision: HEAD# helm文件在git仓库的相对路径path: helm/app1# 这里定义传递给helm执行的参数# 类型values.yamlhelm:parameters:- name: image.repositoryvalue: harbor.example.cn/public/nginx- name: image.tagvalue: stable-alpine- name: global.replicasvalue: "2"destination:# 部署到当前k8s集群的地址server: https://kubernetes.default.svc# 部署目标命名空间namespace: demosyncPolicy:automated: prune: trueselfHeal: trueallowEmpty: falsesyncOptions:- Validate=false- CreateNamespace=true- PrunePropagationPolicy=foreground- PruneLast=trueretry:limit: 5backoff:factor: 2maxDuration: 1m
- 推送Git仓库
准备好资源文件后,需要创建一个git仓库来存放,将写好的资源文件推送到git仓库上;
# 整体目录文件如下
gitops # tree -L 3
.
├── argocd
│ ├── dev
│ │ └── Application.yaml
│ ├── pre
│ │ └── Application.yaml
│ ├── prod
│ │ └── Application.yaml
│ └── test
│ └── Application.yaml
└── helm└── app1├── Chart.yaml├── templates└── values.yaml# 推送git仓库
git init
git add ..
git ...
集成argocd
- 登陆argocd页面,添加一个git仓库地址,这里的大概流程是通过同步git上的argocd资源清单来引用helm的部署文件来实现部署和更新;
- 填写仓库地址信息;
- 接着创建一个应用,定义git仓库配置;
- 此时资源是没有开启自动同步的,需要先手动同步一下;
- 查看应用的部署状态和Git同步状态;
- 成功部署后,将 demo-production 调整为自动同步;
版本更新
应用已经通过argocd部署到集群中,此时在argocd中存在有两个应用;
- demo-production :与dev/Application.yaml 文件同步,用来更新应用的版本和配置的;
- demo-argocd :定义服务的部署,读取的是git仓库下的helm资源清单;
当我们需要更新demo这个服务的版本时,修改 dev/Application.yaml 这个文件的参数,重新提交到git仓库即可;
# vim argocd/dev/Application.yaml
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:name: demo-argocdnamespace: argocd
spec:project: defaultsource:repoURL: https://git.example.com/gitops/gitops1.gittargetRevision: HEADpath: helm/app1helm:parameters:- name: image.repositoryvalue: harbor.example.cn/public/nginx- name: image.tagvalue: v1.0.0 # 修改镜像的版本- name: global.replicasvalue: "1" # 修改下副本数destination:server: https://kubernetes.default.svcnamespace: demosyncPolicy:automated: prune: trueselfHeal: trueallowEmpty: falsesyncOptions:- Validate=false- CreateNamespace=true- PrunePropagationPolicy=foreground- PruneLast=trueretry:limit: 5backoff:factor: 2maxDuration: 1m
等待一会后,argocd会自动检测git上的文件版本,然后将最新修改的配置参数同步到k8s集群中;