《OpenShift 4.x HOL教程汇总》
说明:本文已经 在OpenShift 4.15
文章目录
安装 ArgoCD
用 OpenShift GitOps Operator 安装 ArgoCD
- 用集群管理员登录OpenShift控制台,然后进入管理员视图。
- 在OperatorHub中找到“OpenShift GitOps”,然后接受缺省配置安装该Operator。
- 当安装完OpenShift GitOps Operator后会自动创建ArgoCD运行环境。可以在控制台的“开发者”视图中查看openshift-gitops安装的资源。
- 执行命令获取ArgoCD的admin用户的缺省密码。
$ PASSWD=$(oc get secret openshift-gitops-cluster -n openshift-gitops -ojsonpath='{.data.admin\.password}' | base64 -d)
也可在OpenShift 控制台中进入openshift-gitops项目中名为openshift-gitops-cluster的Secret,也可以看到admin.password中显示的密码。
- 在安装完GitOps Operator后会在OpenShift Console顶端的菜单中增加ArgoCD的链接,点击即可进入ArgoCD控制台。
- 用admin和密码登录ArgoCD控制台。
安装 ArgoCD 客户端
- 安装ArgoCD客户端
$ ARGO_VER=$(curl --silent "https://api.github.com/repos/argoproj/argo-cd/releases/latest" | grep '"tag_name"' | sed -E 's/.*"([^"]+)".*/\1/')
$ sudo curl -L https://github.com/argoproj/argo-cd/releases/download/${ARGO_VER}/argocd-linux-amd64 -o /usr/local/bin/argocd
$ sudo chmod +x /usr/local/bin/argocd
- 登录ArgoCD
$ ARGOCD_URL=$(oc get route openshift-gitops-server -n openshift-gitops -o jsonpath='{.spec.host}')
$ argocd login --username admin --password ${PASSWD} --insecure ${ARGOCD_URL}
- 查看当前ArgoCD集群
$ argocd cluster list
SERVER NAME VERSION STATUS MESSAGE
https://kubernetes.default.svc in-cluster Unknown Cluster has no application and not being monitored.
创建新用户
- 修改 openshift-gitops 项目中名为 “openshift-gitops” 的 ArgoCD 对象。在 “spec” 区域增加 “extraConfig” 配置。
spec:
extraConfig:
accounts.user1: apiKey, login
- 确认在 openshift-gitops 项中名为 argocd-cm 的 ConfigMap 对象中已经有 accounts.user1 数据。
- 运行命令查看当前用户。
$ argocd account list
NAME ENABLED CAPABILITIES
admin true login
user1 true apiKey, login
- 执行命令,修改 “user1” 的密码。
$ argocd account update-password --account user1 --new-password mypassword --current-password ${PASSWD}
- 可以用新创建的用户登录 ArgoCD 控制台。
用 ArgoCD 部署应用
创建目标项目
- 创建spring-petclinic项目并打标签,表明是该项目是由“openshift-gitops”管理。
$ oc new-project spring-petclinic
$ oc label namespace spring-petclinic argocd.argoproj.io/managed-by=openshift-gitops
注意:如果 namespace 没有上述特定 label,则后面 GitOps 同步时会提示权限错误。
创建ArgoCD应用
- 在ArgoCD控制台上点击“+ NEW APP”按钮。
- 在右滑页面提供以下配置,然后点击CREATE按钮。
Application Name: spring-petclinic
Project: default
Sync Policy: Automatic
Self-heal: checked
Repository URL: https://github.com/siamaksade/openshift-gitops-getting-started
Revision: HEAD
Path: app
Cluster URL: https://kubernetes.default.svc
Namespace: spring-petclinic
- 在ArgoCD送创建完应用会自动在OpenShift创建项目。
$ oc get project spring-petclinic
NAME DISPLAY NAME STATUS
spring-petclinic Active
- 此时在openshift-gitops项目中的OpenShift GitOps Operator中也可以查看到通过ArgoCD部署的名为spring-petclinic的Application。
- 在ArgoCD控制台中“spring-petclinic”应用的状态显示为“Synced”。
- 点击进入上图。
- 最后可以在spring-petclinic项目中通过路由访问部署的应用。
参考
https://github.com/siamaksade/openshift-gitops-getting-started
https://github.com/RedHatWorkshops/argocd-getting-started
https://github.com/wael2000/openshift-gitops-getting-started
https://github.com/argoproj/argo-cd/blob/master/assets/builtin-policy.csv
https://medium.com/geekculture/create-a-new-user-in-argocd-using-the-cli-and-configmap-8cbb27cf5904#:~:text=Create%20a%20New%20User%20in%20ArgoCD%20using%20the,Local%20User.%20…%207%20Disabled%20Admin%20Account.%20
https://itnext.io/argocd-users-access-and-rbac-ddf9f8b51bad
https://faun.pub/create-argo-cd-local-users-9e830db3763f