部署应用
上面我们创建好了应用,但还没有部署,所以 namespace、pod、deployment、svc 都没有
使用 CLI 同步
应用创建完成后,我们可以通过如下所示命令查看其状态
# 查看应用 app01 的状态 [root@master1 k8s-yaml]# argocd app get app01 Name: app01 # 应用名称 Project: default Server: https://kubernetes.default.svc # 部署的服务 Namespace: demo1 # 部署的 ns URL: https://10.6.215.30:32382/applications/app01 Repo: https://gitee.com/zouzou_busy/devops_test.git # 资源仓库 Target: Path: app # 仓库里的资源路径 SyncWindow: Sync Allowed Sync Policy: <none> Sync Status: OutOfSync from (d7306f4) # 同步状态 Health Status: Missing # 健康状态 GROUP KIND NAMESPACE NAME STATUS HEALTH HOOK MESSAGE Service demo1 myapp OutOfSync Missing apps Deployment demo1 myapp OutOfSync Missing
因为 app01 是我们通过命名行创建的,ns 写的是 demo1,没有设置自动创建。如果你集群上没有这个命名空间,需要先手动创建
# 创建 ns [root@master1 k8s-yaml]# kubectl create ns demo1 namespace/demo1 created
应用程序状态为初始 OutOfSync
状态,因为应用程序尚未部署,并且尚未创建任何 Kubernetes 资源。要同步(部署)应用程序,可以执行如下所示命令
# 同步应用 app01 [root@master1 k8s-yaml]# argocd app sync app01 TIMESTAMP GROUP KIND NAMESPACE NAME STATUS HEALTH HOOK MESSAGE 2022-08-18T21:40:18+08:00 Service demo1 myapp OutOfSync Missing 2022-08-18T21:40:18+08:00 apps Deployment demo1 myapp OutOfSync Missing 2022-08-18T21:40:18+08:00 Service demo1 myapp Synced Healthy 2022-08-18T21:40:19+08:00 apps Deployment demo1 myapp OutOfSync Missing deployment.apps/myapp created 2022-08-18T21:40:19+08:00 Service demo1 myapp Synced Healthy service/myapp created 2022-08-18T21:40:19+08:00 apps Deployment demo1 myapp Synced Progressing deployment.apps/myapp created Name: app01 Project: default Server: https://kubernetes.default.svc Namespace: demo1 URL: https://10.6.215.30:32382/applications/app01 Repo: https://gitee.com/zouzou_busy/devops_test.git Target: Path: app SyncWindow: Sync Allowed Sync Policy: <none> Sync Status: Synced to (d7306f4) Health Status: Progressing Operation: Sync Sync Revision: d7306f4c729378979e1972a2cf857a9847ba0b8e Phase: Succeeded Start: 2022-08-18 21:40:18 +0800 CST Finished: 2022-08-18 21:40:19 +0800 CST Duration: 1s Message: successfully synced (all tasks run) GROUP KIND NAMESPACE NAME STATUS HEALTH HOOK MESSAGE Service demo1 myapp Synced Healthy service/myapp created apps Deployment demo1 myapp Synced Progressing deployment.apps/myapp created
此命令从 Git 仓库中检索资源清单并执行 kubectl apply
部署应用,执行上面命令后 guestbook 应用便会运行在集群中了,现在我们就可以查看其资源组件、日志、事件和评估其健康状态了。
在查看 app01 的状态
# 查看应用 app01 的状态 [root@master1 k8s-yaml]# argocd app get app01 Name: app01 Project: default Server: https://kubernetes.default.svc Namespace: demo1 URL: https://10.6.215.30:32382/applications/app01 Repo: https://gitee.com/zouzou_busy/devops_test.git Target: Path: app SyncWindow: Sync Allowed Sync Policy: <none> Sync Status: Synced to (d7306f4) Health Status: Progressing # 状态为 Progressing(进行中)了 GROUP KIND NAMESPACE NAME STATUS HEALTH HOOK MESSAGE Service demo1 myapp Synced Healthy service/myapp created # 状态是 Healthy apps Deployment demo1 myapp Synced Progressing deployment.apps/myapp created # 状态也是 Progressing
等一会在去查看状态
[root@master1 k8s-yaml]# argocd app get app01 Name: app01 Project: default Server: https://kubernetes.default.svc Namespace: demo1 URL: https://10.6.215.30:32382/applications/app01 Repo: https://gitee.com/zouzou_busy/devops_test.git Target: Path: app SyncWindow: Sync Allowed Sync Policy: <none> Sync Status: Synced to (d7306f4) Health Status: Healthy # 状态为 Healthy(健康)的了 GROUP KIND NAMESPACE NAME STATUS HEALTH HOOK MESSAGE Service demo1 myapp Synced Healthy service/myapp created # 状态也是 Healthy apps Deployment demo1 myapp Synced Healthy deployment.apps/myapp created # 状态也是 Healthy
然后查看 pod、deploy、svc
# pod、svc、deploy 都正常运行了 [root@master1 k8s-yaml]# kubectl get pod,svc,deploy -n demo1 NAME READY STATUS RESTARTS AGE pod/myapp-85757559dc-2j46l 1/1 Running 0 8m12s pod/myapp-85757559dc-hz57n 1/1 Running 0 8m12s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/myapp NodePort 10.111.135.216 <none> 8008:32060/TCP 8m12s NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/myapp 2/2 2 2 8m12s
访问 ip:32060,可以看到下面页面则表示部署成功
通过 UI 页面同步
可以看到上面我们使用命令行同步的已经在 ui 上显示正常了,状态也是 Healthy 的
在页面上同步我们只需要点击 SYNC
,会弹出一个抽屉,可以进行一些更改,在点击 SYNCHRONIZE
即可
同步完成后可以看到我们的资源状态:
也可以查看日志、event 等信息
查看 pod、deploy、svc。都运行正常
# 查看 pod,svc,deploy [root@master1 k8s-yaml]# kubectl get pod,svc,deploy -n demo2 NAME READY STATUS RESTARTS AGE pod/myapp-85757559dc-b5j9p 1/1 Running 0 4m25s pod/myapp-85757559dc-kxzbt 1/1 Running 0 4m25s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/myapp NodePort 10.100.144.60 <none> 8008:31264/TCP 4m26s NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/myapp 2/2 2 2 4m26s
访问 ip:31264 看到如下页面,则部署成功
更新应用
上面我们已经部署好了两个应用 app01 和 app02,现在来更改一下 myapp-deployment.yaml 文件,将镜像改为 v2
apiVersion: apps/v1 kind: Deployment metadata: name: myapp spec: replicas: 2 selector: matchLabels: app: myapp template: metadata: labels: app: myapp spec: containers: - image: zouzou0214/myapp:v2 # 改为 v2 版 name: myapp ports: - containerPort: 8008
等待几分钟,查看应用状态(也可以点击下面的刷新按钮),可以看到 app02 的状态变成了 OutOfSync。这是因为 argocd 检测到了仓库里有变动。app01 是我手动点击了 SYNC
我们点击 app02 的 SYNC
进行同步,等待一会查看状态
查看 pod、svc、deploy,都正常运行
# 查看 pod,svc,deploy [root@master1 k8s-yaml]# kubectl get pod,svc,deploy -n demo2 NAME READY STATUS RESTARTS AGE pod/myapp-77c6b7455d-rbnvk 1/1 Running 0 5m32s pod/myapp-77c6b7455d-slh68 1/1 Running 0 2m50s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/myapp NodePort 10.100.144.60 <none> 8008:31264/TCP 19m NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/myapp 2/2 2 2 19m
如果看到如下页面则部署成功
查看详情
查看 rs,可以看到,是有两个 rs 的,一个副本数为 0,一个副本数为 2
# 查看 rs [root@master1 k8s-yaml]# kubectl get rs -n demo2 NAME DESIRED CURRENT READY AGE myapp-77c6b7455d 2 2 2 14m myapp-85757559dc 0 0 0 28m
回滚
上面我们的 app02 已经有两个版本了,现在最新的是 v2 版本,我们也可以可以回滚到第一个版本
点击 HISTORY AND ROLLBACK
找到要回滚的版本,点击 Rollback
在回滚的时候需要禁用 AUTO-SYNC 自动同步,点击 OK 确认即可。
等待一会可以看到此时已经回滚成功,此时 Pod 是 v1 版本的,并且由于此时线上的版本并不是 Git 仓库中最新的版本,因此此时同步状态是 OutOfSync
查看 rs
# 回滚之前的 [root@master1 k8s-yaml]# kubectl get rs -n demo2 NAME DESIRED CURRENT READY AGE myapp-77c6b7455d 2 2 2 14m myapp-85757559dc 0 0 0 28m # 回滚之后的,可以看到之前的 rs 的副本数从 0 变为了 2 [root@master1 k8s-yaml]# kubectl get rs -n demo2 NAME DESIRED CURRENT READY AGE myapp-77c6b7455d 0 0 0 20m myapp-85757559dc 2 2 2 34m
查看 pod,svc,deployment
# 查看 pod,svc,deploy [root@master1 k8s-yaml]# kubectl get pod,svc,deploy -n demo2 NAME READY STATUS RESTARTS AGE pod/myapp-85757559dc-4xv27 1/1 Running 0 2m53s pod/myapp-85757559dc-5659l 1/1 Running 0 2m57s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/myapp NodePort 10.100.144.60 <none> 8008:31264/TCP 36m NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/myapp 2/2 2 2 36m
访问 31264 端口