OpenShift 4 之 GitOps(2)用ArgoCD部署应用

文章目录

  • 用ArgoCD部署应用
    • 向ArgoCD增加Github Repo
    • 通过ArgoCD部署OpenShift应用
  • 用ArgoCD自动更新应用配置
  • 用ArgoCD删除应用配置

用ArgoCD部署应用

向ArgoCD增加Github Repo

首先向ArgoCD增加Github Reop,然后查看ArgoCD的repo列表。

$ argocd repo add https://github.com/liuxiaoyu-git/gitops-on-openshift.git
repository 'https://github.com/liuxiaoyu-git/gitops-on-openshift.git' added
 
$ argocd repo list
REPO                                                      INSECURE  LFS    USER  STATUS      MESSAGE
https://github.com/liuxiaoyu-git/gitops-on-openshift.git  false     false  -     Successful

通过ArgoCD部署OpenShift应用

  1. 查看当前Argo管理的OpenShift或K8s集群。如果ArgoCD和OpenShift运行在一起,那么使用本地地址(https://kubernetes.default.svc)访问。
$ argocd cluster list
SERVER                          NAME  STATUS      MESSAGE
https://kubernetes.default.svc        Successful
  1. 基于Github,在ArgoCD上创建一个应用reverse-words-app,然后查看ArgoCD上的应用列表。其中ArgoCD的create命令参数有:
    –project :ArgoCD应用程序所属的ArgoCD项目
    –name :ArgoCD应用程序名称
    –repo :Git存储库
    –path :Git信息库中存储清单的目录路径
    –dest-server :OpenShift集群
    –dest-namespace :用于将ArgoCD应用程序部署到的OpenShift项目
    –revision :Git存储库的分支
    –sync-policy :与Git存储库同步的策略
$ argocd app create --project default --name reverse-words-app \
>     --repo http://github.com/liuxiaoyu-git/gitops-on-openshift.git \
>     --path simple-app/reversewords_app/ \
>     --dest-server https://kubernetes.default.svc \
>     --dest-namespace reverse-words \
>     --revision master --sync-policy automated
application 'reverse-words-app' created
 
$ argocd app list
NAME               CLUSTER                         NAMESPACE      PROJECT  STATUS     HEALTH       SYNCPOLICY  CONDITIONS  REPO                                                         PATH                          TARGET
reverse-words-app  https://kubernetes.default.svc  reverse-words  default  Synced     Progressing  Auto        <none>      http://github.com/liuxiaoyu-git/gitops-on-openshift.git      simple-app/reversewords_app/  master
  1. 查看ArgoCD中的reverse-words-app应用,注意此时ArgoCD的reverse-words应用状态和Github是“Synced”的同步状态。
$ argocd app get reverse-words-app
Name:               reverse-words-app
Project:            default
Server:             https://kubernetes.default.svc
Namespace:          reverse-words
URL:                https://argocd-server-argocd.apps.cluster-shanghai-c30f.shanghai-c30f.example.opentlc.com/applications/reverse-words-app
Repo:               http://github.com/liuxiaoyu-git/gitops-on-openshift.git
Target:             master
Path:               simple-app/reversewords_app/
Sync Policy:        Automated
Sync Status:        Synced to master (36306dc)
Health Status:      Progressing
 
GROUP  KIND        NAMESPACE      NAME           STATUS   HEALTH       HOOK  MESSAGE
       Namespace   reverse-words  reverse-words  Running  Synced             namespace/reverse-words created
       Service     reverse-words  reverse-words  Synced   Healthy            service/reverse-words created
apps   Deployment  reverse-words  reverse-words  Synced   Healthy        deployment.apps/reverse-words created
       Namespace                  reverse-words  Synced   Unknown
  1. 查看OpenShift的Deployment和Service。
$ oc get deployment -n reverse-words
NAME            READY   UP-TO-DATE   AVAILABLE   AGE
reverse-words   1/1     1            1           43s
 
$ oc get svc -n reverse-words
NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
reverse-words   ClusterIP   172.30.202.52   <none>        8080/TCP   53s
  1. 创建访问应用的Route,然后访问reverse-words应用。
$ oc expose svc reverse-words -n reverse-words
route.route.openshift.io/reverse-words exposed

$ curl -X POST http://$(oc get route reverse-words -n reverse-words -o jsonpath='{.spec.host}') -d '{"word":"PALC"}'
{"reverse_word":"CLAP"}

用ArgoCD自动更新应用配置

  1. 先删除OpenShift中reverse-words的Deployment。
$ oc delete deployment reverse-words -n reverse-words
deployment.extensions "reverse-words" deleted
  1. 查看ArgoCD的应用列表,确认reverse-words-app应用当前HEALTH是“Missing”的状态,并且STATUS是“OutOfSync”状态。
$ argocd app list
NAME               CLUSTER                         NAMESPACE      PROJECT  STATUS     HEALTH   SYNCPOLICY  CONDITIONS  REPO                                                         PATH                          TARGET
reverse-words-app  https://kubernetes.default.svc  reverse-words  default  OutOfSync  Missing  Auto        <none>      http://github.com/liuxiaoyu-git/gitops-on-openshift.git      simple-app/reversewords_app/  master
  1. 查看ArgoCD的reverse-words-app应用。确认Route资源的STATUS是“OutOfSync”,而且在最后显示“1 resources require pruning”,这是由于Route是我们手动生成的,而不是通过ArgoCD的Repo生成的。另外Deployment的HEALTH为“Progressing”状态,这说明ArgoCD已经发现OpenShift的deployment和ArgoCD的Repo不同了,正在进行同步处理。
$ argocd app sync reverse-words-app
TIMESTAMP                  GROUP                     KIND   NAMESPACE                     NAME    STATUS    HEALTH        HOOK  MESSAGE
2020-03-03T16:20:38+00:00                         Service  reverse-words         reverse-words    Synced   Healthy
2020-03-03T16:20:38+00:00   apps               Deployment  reverse-words         reverse-words  OutOfSync  Missing
2020-03-03T16:20:38+00:00  route.openshift.io       Route  reverse-words         reverse-words  OutOfSync  Unknown
2020-03-03T16:20:38+00:00                       Namespace                        reverse-words    Synced   Unknown
 
Name:               reverse-words-app
Project:            default
Server:             https://kubernetes.default.svc
Namespace:          reverse-words
URL:                https://argocd-server-argocd.apps.cluster-shanghai-c30f.shanghai-c30f.example.opentlc.com/applications/reverse-words-app
Repo:               http://github.com/liuxiaoyu-git/gitops-on-openshift.git
Target:             master
Path:               simple-app/reversewords_app/
Sync Policy:        Automated
Sync Status:        OutOfSync from master (36306dc)
Health Status:      Progressing
 
Operation:          Sync
Sync Revision:      36306dc264cea403e33b756de6faf95f605fee5d
Phase:              Succeeded
Start:              2020-03-03 16:20:38 +0000 UTC
Finished:           2020-03-03 16:20:40 +0000 UTC
Duration:           2s
Message:            successfully synced (all tasks run)
 
GROUP               KIND        NAMESPACE      NAME           STATUS     HEALTH       HOOK  MESSAGE
route.openshift.io  Route       reverse-words  reverse-words  OutOfSync  Unknown            ignored (requires pruning)
                    Namespace   reverse-words  reverse-words  Running    Synced             namespace/reverse-words unchanged
                    Service     reverse-words  reverse-words  Synced     Healthy            service/reverse-words unchanged
apps                Deployment  reverse-words  reverse-words  Synced     Progressing        deployment.apps/reverse-words created
                    Namespace                  reverse-words  Synced     Unknown
FATA[0002] 1 resources require pruning
  1. 持续查看reverse-words的Deployment状态变化,最后变为READY状态。
$ oc get deployment -n reverse-words -w
NAME            READY   UP-TO-DATE   AVAILABLE   AGE
reverse-words   0/1     1            0           29s
reverse-words   1/1     1            1           39s
  1. 再次访问reverse-words应用的路由,确认又可以正常访问应用了。
$ curl -X POST http://$(oc get route reverse-words -n reverse-words -o jsonpath='{.spec.host}') -d '{"word":"PALC"}'
{"reverse_word":"CLAP"}

用ArgoCD删除应用配置

  1. 此时进到ArgoCD的控制台,在Application中进入reverse-words-ap,确认是“OutOfSync”状态。其中是名为“reverse-words”的Route是黄色非同步状态,这是由于这个对象是我们在OpenShift中直接创建的,而不是根据Github的配置创建的。
    OpenShift 4 之 GitOps(2)用ArgoCD部署应用_第1张图片
  2. 点击上图中红框中名为“reverse-words”的Route的右侧图标,然后选择Delete,然后确认是Force
    Delete。删除后,ArgoCD的reverse-words-app应用又恢复了“Synced”状态了。
    OpenShift 4 之 GitOps(2)用ArgoCD部署应用_第2张图片
  3. 然后执行以下命令,确认OpenShift的名为“reverse-words”的Route对象已经被删除了。
$ oc get route
No resources found in reverse-words namespace.

你可能感兴趣的:(GitOps,OpenShift,4,ArgoCD)