Argo CD是Kubernetes的一款声明式、GitOps连续交付工具。
为什么使用Argo CD
应用程序定义、配置和环境应该是声明式的,并受版本控制。应用程序部署和生命周期管理应该是自动化的、可审计的,并且易于理解。
流程图:
安装:
非HA部署
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
HA部署
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/ha/install.yaml
如果需要调整时区:
将yaml文件所有deployments增加环境变量
env:
- name: TZ
value: "Asia/Shanghai"
更改Argo SVC nodeport
kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "NodePort"}}'
获取初始密码:
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d; echo
添加APP:
首先在gitlab创建仓库,提交yaml文件到仓库
Argo在添加app前需要添加仓库信息
SYNC POLICY
可以选择手动或者自动同步,自动同步会每三四分钟检查下仓库里的文件与当前状态做对比,有文件更新触发应用更新
安装命令行工具:
命令行登陆:
argocd login "argo svc 地址"
argocd app list
argocd cluster list
创建APP
argocd app create guestbook --repo https://github.com/argoproj/argocd-example-apps.git --path guestbook --dest-namespace default --dest-server https://kubernetes.default.svc
同步
argocd app sync guestbook
删除
argocd app delete guestbook
添加多集群:
kubectl config get-contexts
argocd cluster add default --kubeconfig admin_config --name testcluter
argocd cluster list
同步应用后会显示正常
配置同步状态消息通知,装app状态发送到企业微信,需要先添加微信机器人:
kubectl edit cm argocd-notifications-cm -n argocd
在configmap 添加以下内容,重启argocd-notifications-controller pod
data:
context: |
argocdUrl: https://argo地址
service.webhook.wechat: |
url: https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxxxxxxxx
headers:
- name: Content-Type
value: application/json
subscriptions: |
- recipients: [wechat]
triggers: [on-sync-running, on-deployed, on-sync-failed, on-sync-succeeded]
template.app-sync-change: |
webhook:
wechat:
method: POST
body: |
{
"msgtype": "markdown",
"markdown": {
"content": "### ArgoCD同步状态\n> - app名称: {{.app.metadata.name}}\n> - app同步状态: {{ .app.status.operationState.phase}}\n> - 时间:{{.app.status.operationState.startedAt}}\n>
- URL: [点击跳转ArgoCD]({{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true) \n"
}
}
trigger.on-deployed: |
- description: Application is synced and healthy. Triggered once per commit.
oncePer: app.status.sync.revision
send: [app-sync-change] # template names
# trigger condition
when: app.status.operationState.phase in ['Succeeded'] and app.status.health.status == 'Healthy'
trigger.on-health-degraded: |
- description: Application has degraded
send: [app-sync-change]
when: app.status.health.status == 'Degraded'
trigger.on-sync-failed: |
- description: Application syncing has failed
send: [app-sync-change] # template names
when: app.status.operationState.phase in ['Error', 'Failed']
trigger.on-sync-running: |
- description: Application is being synced
send: [app-sync-change] # template names
when: app.status.operationState.phase in ['Running']
trigger.on-sync-status-unknown: |
- description: Application status is 'Unknown'
send: [app-sync-change] # template names
when: app.status.sync.status == 'Unknown'
trigger.on-sync-succeeded: |
- description: Application syncing has succeeded
send: [app-sync-change] # template names
when: app.status.operationState.phase in ['Succeeded']
新增project:
同步HPA:
memory 须在cpu前面,不然argocd页面看到HPA同步状态是异常的