《OpenShift 4.x HOL教程汇总》
说明:本文已经在OpenShift 4.8环境中验证

《OpenShift 4 - DevSecOps Workshop 系列视频 》

文章目录

  • 用 Task 向 Gitee 推送部署应用的 YAML
    • 准备环境
      • 将 Quay 的 Repository 公开
      • 创建GitOps使用的Git Repository
    • 添加 create-yaml-file 任务
    • 创建 update-gitops-repo 任务
    • 在Pipeline中调用任务
  • 用 OpenShift GitOps 部署应用
    • 安装 OpenShift GitOps 环境
    • 配置ArgoCD客户端环境
    • 向目标项目部署应用
  • 参考:使用证书访问Gitee
    • 生成用SSH访问访问Gitee的证书
    • 将公钥添加到Gitee
    • 通过证书对Gitee的Repo进行操作

本节将向“ tasks-stage-pipeline”新增加一个Task,将部署应用需要的YAML文件推送到Gitee的Repository上。然后我们再利用OpenShift GitOps自动将YAML资源自动部署到2个目标项目里,在部署应用过程中会使用到已经在《 OpenShift 4 - DevSecOps Workshop (13) - 将镜像推送到Quay,并进行漏洞扫描》步骤中推送到Quay上的应用镜像。

用 Task 向 Gitee 推送部署应用的 YAML

准备环境

将 Quay 的 Repository 公开

  1. 为了测试方便,在Quay控制台中设置 “tekton-tasks” 的Repository属性,点击“Make Public”将它设为可公开访问。注意:我们在正式运行管道的时候还会将 “tekton-tasks” 的Repository再设回私有状态。

创建GitOps使用的Git Repository

  1. 在Gitea中创建一个名为“gitops-tekton-tasks”的公开仓库。

添加 create-yaml-file 任务

  1. 在"USER_ID-cicd项目中根据以下YAML创建 create-yaml-file 任务。该任务会生成部署指定Quay上Image的YAML文件,即“tekton-tasks.yaml”,保存在共享Workspace中。说明:缺省生成的部署YAML中有个别没用的配置会影响ArgoCD的同步状态,因此需要删掉。
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:name: create-yaml-file
spec:params:- name: USERNAMEtype: string- name: IMAGE_NAMEtype: string- name: IMAGE_TAGtype: string- name: QUAY_URLtype: stringsteps:- image: 'quay.io/openshift/origin-cli:latest'name: create-yaml-fileresources: {}script: >#!/bin/shset -e -o pipefailecho "Creating YAML file"oc new-app --image="$(params.QUAY_URL)/$(params.USERNAME)/$(params.IMAGE_NAME):$(params.IMAGE_TAG)" --dry-run -oyaml > $(workspaces.gitops-repo.path)/tekton-tasks.yaml sed -i '/- image: /c\        - image: quay.apps.cluster-394c.394c.sandbox1709.opentlc.com\/user1\/tekton-tasks@sha256:fc3f0f3c81e847ae9b4dcaf2c5ac664c4c91de47cdca851788cd1c16859732d0' $(workspaces.gitops-repo.path)/tekton-tasks.yamlsed -i '/status:/d' $(workspaces.gitops-repo.path)/tekton-tasks.yamlsed -i '/generation: null/d' $(workspaces.gitops-repo.path)/tekton-tasks.yamlsed -i '/referencePolicy:/d' $(workspaces.gitops-repo.path)/tekton-tasks.yamlsed -i '/        type: ""/d' $(workspaces.gitops-repo.path)/tekton-tasks.yamlsed -i '/dockerImageRepository:/d' $(workspaces.gitops-repo.path)/tekton-tasks.yamlsed -i '/loadBalancer:/d' $(workspaces.gitops-repo.path)/tekton-tasks.yamlworkspaces:- description: Location for storing gitops filesname: gitops-repo

创建 update-gitops-repo 任务

  1. 在"USER_ID-cicd项目中根据以下YAML创建 update-gitops-repo 任务。该任务将存在共享Workspace的“tekton-tasks.yaml”文件更新至Gitee的Repo中(为了方面测试,每次向Repo添加一个随机文件,正式环境可删除)
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:name: update-gitops-repo
spec:params:- name: GIT_URLtype: string- name: USERNAMEtype: string- name: PASSWORDtype: string- name: REPO_NAMEtype: stringsteps:- image: 'gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/git-init:latest'name: update-gitops-reporesources: {}script: >#!/bin/shset -e -o pipefailgit config --global user.email "you@example.com"git config --global user.name "$(params.USERNAME)"git clone https://$(params.GIT_URL)/$(params.USERNAME)/$(params.REPO_NAME)cd $(params.REPO_NAME)if [ ! -d app ]; then mkdir app; fi\cp $(workspaces.gitops-repo.path)/tekton-tasks.yaml /workspace/$(params.REPO_NAME)/appgit rm -f *.testing && echo testing > $RANDOM.testing    # Remove this line in productgit add *git commit -m "update tekton-tasks.yaml"git remote set-url origin https://$(params.USERNAME):$(params.PASSWORD)@$(params.GIT_URL)/$(params.USERNAME)/$(params.REPO_NAME)git push -u origin masterworkspaces:- description: Location for storing gitops filesname: gitops-repo

在Pipeline中调用任务

  1. 在"USER_ID-cicd项目中根据以下YAML创建update-gitops-pipeline管道。
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:name: update-gitops-pipeline
spec:params:- name: GIT_URLtype: string- name: GIT_USERNAMEtype: string- name: GIT_PASSWORDtype: string- name: GIT_REPO_NAMEtype: string- name: QUAY_URLtype: string- name: QUAY_USERNAMEtype: string- name: IMAGE_TAGtype: stringtasks:- name: create-yaml-fileparams:- name: IMAGE_NAMEvalue: tekton-tasks- name: IMAGE_TAGvalue: $(params.IMAGE_TAG)- name: QUAY_URLvalue: $(params.QUAY_URL)- name: USERNAMEvalue: $(params.QUAY_USERNAME)taskRef:kind: Taskname: create-yaml-fileworkspaces:- name: gitops-repoworkspace: local-maven-repo- name: update-gitops-repoparams:- name: GIT_URLvalue: $(params.GIT_URL)- name: REPO_NAMEvalue: $(params.GIT_REPO_NAME)- name: USERNAMEvalue: $(params.GIT_USERNAME)- name: PASSWORDvalue: $(params.GIT_PASSWORD)runAfter:- create-yaml-filetaskRef:kind: Taskname: update-gitops-repoworkspaces:- name: gitops-repoworkspace: local-maven-repoworkspaces:- name: local-maven-repo
  1. 执行命令测试update-gitops-pipeline管道,然后在Gitee中的gitops-tekton-tasks中可以看到测试的testing文件。注意:测试中“IMAGE_TAG”参数设为“quay1”,因此需要确认在Quay中有该tag的镜像。
$ GIT_URL=$(oc get route gitea-server -n devsecops -ojsonpath={.spec.host})
$ QUAY_URL=$(oc get route quayecosystem-quay -n devsecops -ojsonpath={.spec.host})
$ tkn pipeline start update-gitops-pipeline -n ${USER_ID}-cicd --showlog -L \-p GIT_URL=${GIT_URL} \-p GIT_USERNAME=${USER_ID} \-p GIT_PASSWORD=openshift \-p GIT_REPO_NAME=gitops-tekton-tasks \-p QUAY_URL=${QUAY_URL} \-p QUAY_USERNAME=${USER_ID} \-p IMAGE_TAG=quay1 \--workspace name=local-maven-repo,claimName=maven-repo-pvc
PipelineRun started: update-gitops-pipeline-3zhu98-cfc7n
Waiting for logs to be available...
[create-yaml-file : create-yaml-file] Creating YAML file[update-gitops-repo : update-gitops-repo] Cloning into 'gitops-tekton-tasks'...
[update-gitops-repo : update-gitops-repo] fatal: pathspec '*.testing' did not match any files
[update-gitops-repo : update-gitops-repo] [master 6a3c339] update tekton-tasks.yaml
[update-gitops-repo : update-gitops-repo]  1 file changed, 91 insertions(+)
[update-gitops-repo : update-gitops-repo]  create mode 100644 app/tekton-tasks.yaml
[update-gitops-repo : update-gitops-repo] remote: . Processing 1 references
[update-gitops-repo : update-gitops-repo] remote: Processed 1 references in total
[update-gitops-repo : update-gitops-repo] To https://gitea-server-devsecops.apps.cluster-394c.394c.sandbox1709.opentlc.com/user1/gitops-tekton-tasks
[update-gitops-repo : update-gitops-repo]    4c9f10d..6a3c339  master -> master
[update-gitops-repo : update-gitops-repo] Branch 'master' set up to track remote branch 'master' from 'origin'.

用 OpenShift GitOps 部署应用

安装 OpenShift GitOps 环境

请参照《GitOps(1)通过OpenShift GitOps Operator安装ArgoCD》安装 OpenShift GitOps 环境和客户端环境。
注意:此过程使用OpenShift ClusterAdmin用户操作一次即可。

配置ArgoCD客户端环境

  1. 先用OpenShift的集群管理员用户执行以下命令,让用户能操作openshift-gitops项目中的ArgoCD。
$ oc adm policy add-role-to-user edit ${USER} -n openshift-gitops
  1. 用OpenShift的一般用户登录ArgoCD客户端。注意:ArgoCD的用户和OpenShift的用户是两套用户,但可以用RHSSO统一认证。
$ ARGOCD_VER=$(curl --silent "https://api.github.com/repos/argoproj/argo-cd/releases/latest" | grep '"tag_name"' | sed -E 's/.*"([^"]+)".*/\1/')
$ ARGOCD_URL=$(oc get route openshift-gitops-server -n openshift-gitops -o jsonpath='{.spec.host}')
$ ARGOCD_PASSWD=$(oc get secret openshift-gitops-cluster -n openshift-gitops -ojsonpath='{.data.admin\.password}' | base64 -d)
$ sudo curl -L https://github.com/argoproj/argo-cd/releases/download/${ARGOCD_VER}/argocd-linux-amd64 -o /usr/local/bin/argocd
$ sudo chmod +x /usr/local/bin/argocd
$ argocd login --username admin --password ${ARGOCD_PASSWD} --insecure ${ARGOCD_URL}

向目标项目部署应用

  1. 用OpenShift的一般用户创建2个测试Gitops用的项目。
$ oc new-project ${USER_ID}-prod-1
$ oc new-project ${USER_ID}-prod-2
  1. 用OpenShift的集群管理员为这两个项目添加标签“argocd.argoproj.io/managed-by=openshift-gitops”。
$ oc label namespace ${USER_ID}-prod-1 argocd.argoproj.io/managed-by=openshift-gitops
$ oc label namespace ${USER_ID}-prod-2 argocd.argoproj.io/managed-by=openshift-gitops
  1. 用OpenShift的一般用户执行命令,根据Git的配置向OpenShift的测试项目同步两个应用。
$ argocd app create --name ${USER_ID}-tekton-task-1 --project default \--repo https://${GIT_URL}/${USER_ID}/gitops-tekton-tasks.git \--path app \--revision HEAD \--dest-server https://kubernetes.default.svc \--dest-namespace ${USER_ID}-prod-1 \--sync-policy automated
$ argocd app create --name ${USER_ID}-tekton-task-2 --project default \--repo https://${GIT_URL}/${USER_ID}/gitops-tekton-tasks.git \--path app \--revision HEAD \--dest-server https://kubernetes.default.svc \--dest-namespace ${USER_ID}-prod-2 \--sync-policy automated
  1. 在ArgoCD控制台查看应用同步状态即可。
  2. 执行命令也可查看ArgoCD的应用状态
$ argocd app list
NAME                 CLUSTER                         NAMESPACE         PROJECT  STATUS  HEALTH   SYNCPOLICY  CONDITIONS  REPO                                                                                                         PATH                          TARGET
user1-tekton-task-1  https://kubernetes.default.svc  user1-prod-1      default  Synced  Healthy  Auto        <none>      https://gitea-server-devsecops.apps.cluster-394c.394c.sandbox1709.opentlc.com/user1/gitops-tekton-tasks.git  app                           HEAD
user1-tekton-task-2  https://kubernetes.default.svc  user1-prod-2      default  Synced  Healthy  Auto        <none>      https://gitea-server-devsecops.apps.cluster-394c.394c.sandbox1709.opentlc.com/user1/gitops-tekton-tasks.git  app                           HEAD

参考:使用证书访问Gitee

生成用SSH访问访问Gitee的证书

  1. 执行命令生成一对秘钥,运行命令后全部回车即可。成功后会在~/.ssh/目录中生成一对秘钥文件:gitee-key和gitee-key.pub。
$ ssh-keygen -t rsa -C "your_email@youremail.com" -f ~/.ssh/gitee-key
  1. 查看生成的公钥字符串。
$ cat ~/.ssh/gitee-key.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCYbE/qUrT5q/ip0NcEJSAWzxbxgTiC5hYtKqpxGV49oT6XM5q48vw2XwBXj/SitLj3kSKr+52aA9apy637RaJz9hclazEKu+PH/UDRCCOeBS4R54sDQJYrQplE3c98q/3k2F3TxU1QAmpawkKbaZZMGyq0MiFvfFyF4DjXAzPb9X3libXkdDo7n4paqdkfqX2ukPtFWAsNp+OUTHnpLL0SExvL06DOPAT4shgmJMxZVP2DPnkRJhkGig3lAy9c0txh2PmGynMfBqXYoZizVhzuUJbYigkqjJCz+OdUuml9me8r04V5PDOQ1wC/8Tra0fYQtfVODUPzxKtrE6PmnLsSbDPHFsJ3kBbqsEO0Nc2pUal8ilw3hehkn6pGejxQaEjY1vWgm4bqze/ghM6JjwwjihshlYCbtFGGIjwI1zKLNAj6UBpmv1PeTPBpqJWlKGyKgZeXcW76O0oki+u08l34jYuHrMZi2551hhqVNZF1Yoqnk93ZoXWd48bNmcpZ7G0= your_email@youremail.com

将公钥添加到Gitee

  1. 登录Gitee后进入用户的“设置”菜单,然后通过左侧菜单再进入“安全设置”的“SSH公钥”。
  2. 将上一步的公钥字符创粘贴到“公钥”区域,最后点击“确定”按钮。

通过证书对Gitee的Repo进行操作

git clone https://gitee.com/dawnskyliu/gitops-helm-argocd
cd gitops-helm-argocd
oc new-app --name=mywar jboss-webserver31-tomcat8-openshift:1.4~https://github.com/liuxiaoyu-git/tomcat-war --dry-run=true -oyaml > mywar.yaml
git add *
git commit -m "add mywar.yaml"
git remote add origin https://gitee.com/dawnskyliu/gitops-helm-argocd.git
ssh -T git@gitee.com -i ~/.ssh/gitee-key
git push -u origin master

OpenShift 4 - DevSecOps Workshop (15) - 利用OpenShift GitOps向多个目标部署应用相关推荐

  1. OpenShift 4 - DevSecOps Workshop (10) - 向Stage环境部署应用镜像

    <OpenShift 4.x HOL教程汇总> 说明:本文已经在OpenShift 4.8环境中验证 <OpenShift 4 - DevSecOps Workshop 系列视频 & ...

  2. OpenShift 4 - DevSecOps Workshop (3) - 从PipelineResource、Task到一个简单的Pipeline

    <OpenShift 4.x HOL教程汇总> 说明:本文已经在OpenShift 4.8环境中验证 <OpenShift 4 - DevSecOps Workshop 系列视频 & ...

  3. [OpenShift 4 - DevSecOps Workshop (16) - 使用 VSCode 编辑运行 Tekton Pipeline 资源

    <OpenShift 4.x HOL教程汇总> 说明:本文已经在OpenShift 4.8环境中验证 <OpenShift 4 - DevSecOps Workshop 系列视频 & ...

  4. OpenShift 4 - DevSecOps Workshop (14) - 镜像合规扫描

    <OpenShift 4.x HOL教程汇总> 说明:本文已经在OpenShift 4.8环境中验证 <OpenShift 4 - DevSecOps Workshop 系列视频 & ...

  5. OpenShift 4 - DevSecOps Workshop (13) - 将镜像推送到Quay,并进行漏洞扫描

    <OpenShift 4.x HOL教程汇总> 说明:本文已经在OpenShift 4.8环境中验证 <OpenShift 4 - DevSecOps Workshop 系列视频 & ...

  6. OpenShift 4 - DevSecOps Workshop (12) - 用CodeReady Workspace编辑提交应用代码

    <OpenShift 4.x HOL教程汇总> 说明:本文已经在OpenShift 4.8环境中验证 <OpenShift 4 - DevSecOps Workshop 系列视频 & ...

  7. OpenShift 4 - DevSecOps Workshop (11) - 通过Trigger启动Pipeline运行

    <OpenShift 4.x HOL教程汇总> 说明:本文已经在OpenShift 4.8环境中验证 <OpenShift 4 - DevSecOps Workshop 系列视频 & ...

  8. OpenShift 4 - DevSecOps Workshop (9) - 向Dev环境部署应用镜像

    <OpenShift 4.x HOL教程汇总> 说明:本文已经在OpenShift 4.8环境中验证 <OpenShift 4 - DevSecOps Workshop 系列视频 & ...

  9. OpenShift 4 - DevSecOps Workshop (7) - 为Pipeline增加向Nexus制品库推送任务

    <OpenShift 4.x HOL教程汇总> 说明:本文已经在OpenShift 4.8环境中验证 <OpenShift 4 - DevSecOps Workshop 系列视频 & ...

最新文章

  1. GPU 图像并行处理
  2. C++使用Merge Sort排序计数反转的实现算法(附完整源码)
  3. 扫描枪连接zebra打印机打印条码标签无需电脑
  4. js.domReady
  5. 使用正确的垃圾收集器将Java内存使用量降至最低
  6. linux系统shell知识点,Linux 系统中shell知识点说明和常用的帮助命令简单介绍 | IT工程师的生活足迹...
  7. 辉哥给rockchip修复了一个内存溢出问题
  8. 信息学奥赛一本通 1844:【06NOIP提高组】金明的预算方案 | 洛谷 P1064 [NOIP2006 提高组] 金明的预算方案
  9. flash FMS的一些最优参数设置
  10. flash与javacript通信(1)
  11. JavaBean 与 EJB 的区别
  12. 《零基础入门深度学习》解读
  13. Mac下安装emacs+cscopse+auto-complete
  14. Java生成随机数的4种方式
  15. win10内置计算机和天气闪退,win10系统中天气闪退怎么办?Win10天气应用闪退问题解决方法...
  16. 需要管理员权限解决办法
  17. meltdown linux检测,如何检查你的Linux PC是否受Meltdown和Spectre安全缺陷影响
  18. 2023跨境出海指南:韩国网红营销白皮书
  19. Arcgis经纬线标注设置(英文、刻度显示)
  20. android面试!一线互联网移动架构师设计思想解读开源框架!复习指南

热门文章

  1. python语言pos_Python自然语言处理(二)--NLTK调用Stanford_NLP_Tools完成NLP任务
  2. 运行gulp_Gulp的基本使用
  3. 米的换算单位和公式_小学三年级数学常用公式和单位换算,孩子复习宝典!
  4. echo输出换行_Bash shell教程[5] echo命令
  5. linux fortran 内存不足,内存不够不用怕! 虚拟内存不足的十种解决办法
  6. python难度大吗_python需要学多久?自学两年也很难达到企业标准
  7. Mac中安装NetBeans方法
  8. 遍历QListWidget的item
  9. slab 着色如何最大限度地利用 Cache Lines 或 Cache Rows?
  10. 跨平台异步IO库 libuv 源代码接口详解