上海最大的网站建设,推广营销策划,wordpress多大vps,东阳做网站本文介绍了一项新工具#xff0c;可以基于Gitops手动或者自动实现Kubernetes集群应用测试#xff0c;确保集群的健康状态与Git仓库定义的一致。原文: GitOps-Powered Kubernetes Testing Machine: ArgoCD Testkube 简介#xff1a;GitOps 云原生测试面临的挑战 现代云原生应… 本文介绍了一项新工具可以基于Gitops手动或者自动实现Kubernetes集群应用测试确保集群的健康状态与Git仓库定义的一致。原文: GitOps-Powered Kubernetes Testing Machine: ArgoCD Testkube 简介GitOps 云原生测试面临的挑战 现代云原生应用开发的主要趋势之一是采用 GitOps即用 Git 管理 Kubernetes 集群状态GitHub 和 GitLab 等现代化 Git 平台在工作流、审计、安全、工具等方面提供了各种功能。ArgoCD 或 Flux 等工具可用于保持 Kubernetes 集群与 Git 仓库同步的繁重工作一旦发现 Git 与集群存在差异就会立即部署以确保仓库是运行时环境的真实来源。 你是否同意现在也是时候将测试和相关活动纳入这一范例了吗没错Kubeshop正在努力为你提供首个GitOps友好的云原生测试协调/执行框架--Testkube以确保质量保证工作与这一全新的应用程序配置和集群配置管理方法保持一致。结合上述 GitOps 方法Testkube 将在集群状态中包含测试工件和应用程序配置并使 git 成为这些测试工件的真实来源。 GitOps 方法的优势 由于测试包含在集群状态中因此可以随时验证应用程序组件/服务是否按要求运行。 由于测试是在集群内部执行的因此没有必要纯粹为了测试而从外部暴露被测服务。 集群中的测试始终与用于编写测试的外部工具同步。 测试执行并非严格与 CI 绑定也可手动触发以进行临时验证或通过内部触发器Kubernetes 事件触发 可以利用 Postman 或 Cypress甚至用于端到端测试或其他执行器插件的现有自动化测试用例。 从概念上讲这可以说明如下 GitOps 教程 话不多说让我们来看看实际操作。下面是一个逐步演练的过程以便在本地 Minikube 集群中自动部署应用程序和以及 Postman 集合并进行测试。 我们从设置 GitOps 驱动的测试环境开始 GitOps 测试的前提条件 首先遵循文档[1]安装 minikube。 然后按照 ArgoCD 安装指南[2]安装 ArgoCD。 注对于其中第 3 步访问 Argo CD API 服务器请选择端口转发方法因为这是用 Minikube 集群连接 Argo CD API 服务器的最简单方法。 按照文档[3]安装Testkube确保在集群中安装 CLI 客户端和组件。 设置Hello Kubernetes应用程序和测试 在集群中安装Hello Kubernetes!应用 我们将为一个简单的Hello Kubernetes应用程序创建 YAML 文件然后根据该文件创建集成测试。 apiVersion: v1kind: Servicemetadata: name: hello-kubernetes-servicespec: ports: - name: http port: 80 targetPort: 8080 selector: app: hello-kubernetes - - 然后用以下方法部署 Hello Kubernetes kubectl apply -f hello-kubernetes.yaml 运行以下程序来测试应用程序是否已正确安装 minikube service hello-kubernetes-service 建立包含 Postman 程序集的 Git 仓库 我们将使用 Postman 创建并导出到 Postman 集合文件[4]中的测试。 可以将其上传到与应用程序相同的 Git 仓库但实际上该仓库可以是托管应用程序的同一仓库也可以是管理所有测试工件的单独仓库。 创建 hello-kubernetes.json并将其推送到仓库中 { info: { _postman_id: 02c90123-318f-4680-8bc2-640adabb45e8, name: New Collection, schema: https://schema.getpostman.com/json/collection/v2.1.0/collection.json }, item: [ { name: hello-world test, event: [ { listen: test, script: { exec: [ pm.test(\Body matches string\, () {, pm.expect(pm.response.text()).to.contain(\Hello Kubernetes\), }), , pm.test(\Body matches string\, () {, pm.expect(pm.response.status).to.equal(\OK\), }) ], type: text/javascript } } ], request: { method: GET, header: [], url: { raw: http://hello-kubernetes-service.default, protocol: http, host: [ hello-kubernetes-service, default ] } }, response: [] } ]} 可以在 Github[5] 上看到该仓库的完整示例。 配置 ArgoCD 与 Testkube 协同工作 配置 ArgoCD 以使用 Testkube 插件 要让 ArgoCD 使用 Testkube需要将 Testkube 添加为插件[6]。为此请将插件配置文件嵌套到 plugin.yaml 下的 ConfigMap 清单中。 apiVersion: v1kind: ConfigMapmetadata: name: argocd-cm-plugin namespace: argocddata: plugin.yaml: | apiVersion: argoproj.io/v1alpha1 kind: ConfigManagementPlugin metadata: name: testkube spec: version: v1.0 generate: command: [bash, -c] args: - | testkube generate tests-crds . 然后执行以下命令应用 kubectl apply -f argocd-plugins.yaml 我们通过 testkube 命令生成 tests-crds 创建自定义资源清单然后 ArgoCD 会将其添加到集群中。 要安装插件请为 argocd-repo-server 部署打上补丁使其作为边车运行插件容器。 apiVersion: apps/v1kind: Deploymentmetadata: name: argocd-repo-serverspec: template: spec: containers: - name: testkube command: [/var/run/argocd/argocd-cmp-server] image: kubeshop/testkube-argocd:latest securityContext: runAsNonRoot: true runAsUser: 999 volumeMounts: - mountPath: /var/run/argocd name: var-files - mountPath: /home/argocd/cmp-server/plugins name: plugins - mountPath: /home/argocd/cmp-server/config/plugin.yaml subPath: plugin.yaml name: argocd-cm-plugin - mountPath: /tmp name: cmp-tmp volumes: - configMap: name: argocd-cm-plugin name: argocd-cm-plugin - emptyDir: {} name: cmp-tmp 使用以下命令打上补丁 kubectl patch deployments.apps -n argocd-repo-server — patch-file deployment.yaml 创建包含 ArgoCD 应用程序的文件 apiVersion: argoproj.io/v1alpha1kind: Applicationmetadata: name: testkube-tests namespace: argocdspec: project: default source: repoURL: https://github.com/USERNAME/testkube-argocd.git targetRevision: HEAD path: postman-collections plugin: name: testkube-v1.0 destination: server: https://kubernetes.default.svc namespace: testkube 请注意我们定义了path: postman-collections这是包含前面步骤中 Postman 集合的测试文件夹。在 Testkube 中可以使用多个测试执行器(例如 curl)因此为每个执行器定义一个文件夹非常方便。我们还将 .destination.namespace 定义为 testkube也就是在集群中部署测试的地方。 现在用以下指令创建应用 kubectl apply -f testkube-application.yaml 在 ArgoCD 的仪表板上我们将看到新创建的应用。点击进入同步测试。 点击同步(Sync)即可看到已创建的测试。 瞧ArgoCD 创建并管理着测试集在包含测试的 Github 资源库中创建并更新每一个新测试 运行 ArgoCD 测试 通过 CLI 运行临时测试 现在一切准备就绪我们用 Testkube 的 CLI 来执行一些临时测试。 用以下命令列出集群中的测试 testkube get tests 应该能看到已部署的测试工件: 要运行这些测试请执行以下命令 testkube run test hello-kubernetes 测试将在后台开始执行可以执行下图中的命令来检查测试的执行结果 testkube get execution EXECUTION_ID 应该会看到测试已成功运行就像下图一样。 此外还可以在漂亮的仪表板中查看测试结果。只需使用以下命令打开 Testkube 面板 testkube dashboard 如下图所示可以在执行Executions选项卡中看到执行结果。 GitOps 收获 一旦完全实现基于 GitOps 测试 Kubernetes 应用就能提供一个强大的替代方案。而在传统方法中调度器与当前的 CI/CD 工具绑定与 Kubernetes 应用生命周期并不密切相关。 你好我是俞凡在Motorola做过研发现在在Mavenir做技术工作对通信、网络、后端架构、云原生、DevOps、CICD、区块链、AI等技术始终保持着浓厚的兴趣平时喜欢阅读、思考相信持续学习、终身成长欢迎一起交流学习。为了方便大家以后能第一时间看到文章请朋友们关注公众号DeepNoMind并设个星标吧如果能一键三连(转发、点赞、在看)则能给我带来更多的支持和动力激励我持续写下去和大家共同成长进步 参考资料 [1] Minikube Document: https://minikube.sigs.k8s.io/docs/start [2] ArgoCD Document: https://argo-cd.readthedocs.io/en/stable/getting_started [3] Testkube Installing: https://kubeshop.github.io/testkube/installing [4] Postman Collections: https://www.postman.com/collection [5] Testkube ArgoCD tests: https://github.com/aabedraba/testkube-argocd-tests [6] ArgoCD Config Management Plugins: https://argo-cd.readthedocs.io/en/stable/user-guide/config-management-plugins 本文由 mdnice 多平台发布