Merge pull request #1721 from Felixnoo/update-multi-pro-pipeline

Update Doc: Deploy Apps in a Multi-cluster Project Using a Jenkinsfile
This commit is contained in:
Patrick-LuoYu 2021-06-29 15:28:11 +08:00 committed by GitHub
commit caca0ec460
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 67 additions and 53 deletions

View File

@ -1,6 +1,6 @@
---
title: "Deploy Apps in a Multi-cluster Project Using a Jenkinsfile"
keywords: 'Kubernetes, KubeSphere, docker, devops, jenkins, multi-cluster'
keywords: 'Kubernetes, KubeSphere, Docker, DevOps, Jenkins, Multi-cluster'
description: 'Learn how to deploy apps in a multi-cluster project using a Jenkinsfile-based pipeline.'
linkTitle: "Deploy Apps in a Multi-cluster Project Using a Jenkinsfile"
weight: 11420
@ -8,10 +8,11 @@ weight: 11420
## Prerequisites
- You need to [enable the multi-cluster feature](../../../../docs/multicluster-management/).
- You need to [enable the multi-cluster feature](../../../../docs/multicluster-management/) and create a workspace with your multiple clusters.
- You need to have a [Docker Hub](https://hub.docker.com/) account.
- You need to [enable the KubeSphere DevOps System](../../../../docs/pluggable-components/devops/) on your host cluster.
- You need to create a workspace with multiple clusters, a DevOps project on your **host** cluster, a multi-cluster project (in this tutorial, this multi-cluster project is created on the host cluster and one member cluster), and an account (`project-regular`). This account needs to be invited to the DevOps project and the multi-cluster project with the role `operator`. For more information, see [Create Workspaces, Projects, Accounts and Roles](../../../quick-start/create-workspace-and-project/), [Multi-cluster Management](../../../multicluster-management/) and [Multi-cluster Projects](../../../project-administration/project-and-multicluster-project/#multi-cluster-projects).
- You need to [enable the KubeSphere DevOps System](../../../../docs/pluggable-components/devops/) on your Host Cluster.
- You need to use an account (for example, `project-admin`) with the role of `workspace-self-provisioner` to create a multi-cluster project and a DevOps project on the Host Cluster. This tutorial creates a multi-cluster project on the Host Cluster and one Member Cluster.
- You need to invite an account (for example, `project-regular`) to the DevOps project and grant it the role of `operator`. For more information, see [Create Workspaces, Projects, Accounts and Roles](../../../quick-start/create-workspace-and-project/), [Multi-cluster Management](../../../multicluster-management/) and [Multi-cluster Projects](../../../project-administration/project-and-multicluster-project/#multi-cluster-projects).
## Create a Docker Hub Access Token
@ -33,11 +34,11 @@ weight: 11420
## Create Credentials
You need to create credentials in KubeSphere for the access token created so that the pipeline can interact with Docker Hub for imaging pushing. Besides, you also need to create kubeconfig credentials for the access to the Kubernetes cluster.
You need to create credentials in KubeSphere for the access token created so that the pipeline can interact with Docker Hub for pushing images. Besides, you also need to create kubeconfig credentials for the access to the Kubernetes cluster.
1. Log in to the web console of KubeSphere as `project-regular`. Go to your DevOps project and click **Create** in **Credentials**.
![create-dockerhub-id](/images/docs/devops-user-guide/examples/compile-and-deploy-a-go-multi-cluster-project/create-dockerhub-id.jpg)
![create-dockerhub-id](/images/docs/devops-user-guide/examples/compile-and-deploy-a-go-multi-cluster-project/create-dockerhub-id.png)
2. In the dialog that appears, set a **Credential ID**, which will be used later in the Jenkinsfile, and select **Account Credentials** for **Type**. Enter your Docker Hub account name for **Username** and the access token just created for **Token/Password**. When you finish, click **OK**.
@ -49,35 +50,41 @@ You need to create credentials in KubeSphere for the access token created so tha
{{</ notice >}}
3. Click **Create** again and select **kubeconfig** for **Type**. Note that KubeSphere automatically populates the **Content** field, which is the kubeconfig of the current user account. Set a **Credential ID** and click **OK**.
3. Log out of the KubeSphere web console and log back in as `project-admin`. Go to your DevOps project and click **Create** in **Credentials**. Select **kubeconfig** for **Type**. Note that KubeSphere automatically populates the **Content** field, which is the kubeconfig of the current account. Set a **Credential ID** and click **OK**.
![create-kubeconfig](/images/docs/devops-user-guide/examples/compile-and-deploy-a-go-multi-cluster-project/create-kubeconfig.jpg)
{{< notice note >}}
In future releases, you will be able to invite the account `project-regular` to your multi-cluster project and grant it the necessary role to create the kubeconfig credentials.
{{</ notice >}}
## Create a Pipeline
With the above credentials ready, you can create a pipeline using an example Jenkinsfile as below.
With the above credentials ready, you can use the account `project-regular` to create a pipeline with an example Jenkinsfile as below.
1. To create a pipeline, click **Create** on the **Pipelines** page.
![create-pipeline](/images/docs/devops-user-guide/examples/compile-and-deploy-a-go-project/create-pipeline.jpg)
![create-pipeline](/images/docs/devops-user-guide/examples/compile-and-deploy-a-go-project/create-pipeline.png)
2. Set a name in the pop-up window and click **Next** directly.
![set-pipeline-name](/images/docs/devops-user-guide/examples/compile-and-deploy-a-go-project/set-pipeline-name.jpg)
![set-pipeline-name](/images/docs/devops-user-guide/examples/compile-and-deploy-a-go-project/set-pipeline-name.png)
3. In this tutorial, you can use default values for all the fields. In **Advanced Settings**, click **Create** directly.
![create-pipeline-2](/images/docs/devops-user-guide/examples/compile-and-deploy-a-go-project/create-pipeline-2.jpg)
![create-pipeline-2](/images/docs/devops-user-guide/examples/compile-and-deploy-a-go-project/create-pipeline-2.png)
## Edit the Jenkinsfile
1. In the pipeline list, click this pipeline to go to its detail page. Click **Edit Jenkinsfile** to define a Jenkinsfile and your pipeline runs based on it.
![edit-jenkinsfile](/images/docs/devops-user-guide/examples/compile-and-deploy-a-go-project/edit-jenkinsfile.jpg)
![edit-jenkinsfile](/images/docs/devops-user-guide/examples/compile-and-deploy-a-go-project/edit-jenkinsfile.png)
2. Copy and paste all the content below to the pop-up window as an example Jenkinsfile for your pipeline. You must replace the value of `DOCKERHUB_USERNAME`, `DOCKERHUB_CREDENTIAL`, `KUBECONFIG_CREDENTIAL_ID`, `MULTI_CLUSTER_PROJECT_NAME`, and `MEMBER_CLUSTER_NAME` with yours. When you finish, click **OK**.
```
```groovy
pipeline {
agent {
node {
@ -88,19 +95,19 @@ With the above credentials ready, you can create a pipeline using an example Jen
environment {
REGISTRY = 'docker.io'
// username of dockerhub
DOCKERHUB_USERNAME = 'yuswift'
// Docker Hub username
DOCKERHUB_USERNAME = 'Your Docker Hub username'
APP_NAME = 'devops-go-sample'
// dockerhubid is the dockerhub credential id you created on ks console
DOCKERHUB_CREDENTIAL = credentials('dockerhubid')
// the kubeconfig credential id you created on ks console
KUBECONFIG_CREDENTIAL_ID = 'multi-cluster'
// dockerhub-go is the Docker Hub credentials ID you created on the KubeSphere console
DOCKERHUB_CREDENTIAL = credentials('dockerhub-go')
// the kubeconfig credentials ID you created on the KubeSphere console
KUBECONFIG_CREDENTIAL_ID = 'dockerhub-go-kubeconfig'
// mutli-cluster project name under your own workspace
MULTI_CLUSTER_PROJECT_NAME = 'devops-with-go'
// the member cluster name you want to deploy app on
// in this tutorial, you are assumed to deploy app on host and only one member cluster
// for more member clusters, please edit manifest/multi-cluster-deploy.yaml
MEMBER_CLUSTER_NAME = 'c9'
MULTI_CLUSTER_PROJECT_NAME = 'demo-multi-cluster'
// the name of the Member Cluster where you want to deploy your app
// in this tutorial, the apps are deployed on Host Cluster and only one Member Cluster
// for more Member Clusters, please edit manifest/multi-cluster-deploy.yaml
MEMBER_CLUSTER_NAME = 'Your Member Cluster name'
}
stages {
@ -129,7 +136,7 @@ With the above credentials ready, you can create a pipeline using an example Jen
script {
withCredentials([
kubeconfigFile(
credentialsId: 'multi-cluster',
credentialsId: 'dockerhub-go-kubeconfig',
variable: 'KUBECONFIG')
]) {
sh 'envsubst < devops-go-sample/manifest/multi-cluster-deploy.yaml | kubectl apply -f -'

View File

@ -1,6 +1,6 @@
---
title: "使用 Jenkinsfile 在多集群项目中部署应用"
keywords: 'Kubernetes, KubeSphere, docker, devops, jenkins, 多集群'
keywords: 'Kubernetes, KubeSphere, Docker, DevOps, Jenkins, 多集群'
description: '学习如何使用基于 Jenkinsfile 的流水线在多集群项目中部署应用。'
linkTitle: "使用 Jenkinsfile 在多集群项目中部署应用"
weight: 11420
@ -8,10 +8,11 @@ weight: 11420
## 准备工作
- 您需要[启用多集群功能](../../../../docs/multicluster-management/)。
- 您需要[启用多集群功能](../../../../docs/multicluster-management/)并创建一个多集群企业空间
- 您需要有一个 [Docker Hub](https://hub.docker.com/) 帐户。
- 您需要[启用 KubeSphere DevOps 系统](../../../../docs/pluggable-components/devops/)。
- 您需要创建一个多集群企业空间,在 **Host** 集群上创建一个 DevOps 工程,创建一个多集群项目(本教程中,该多集群项目创建于 Host 集群和一个 Member 集群上),并创建一个帐户 (`project-regular`),需要邀请该帐户至 DevOps 工程和多集群项目中,并赋予 `operator` 角色。有关更多信息,请参见[创建企业空间、项目、帐户和角色](../../../quick-start/create-workspace-and-project/)、[多集群管理](../../../multicluster-management/)和[多集群项目](../../../project-administration/project-and-multicluster-project/#多集群项目)。
- 您需要在 Host 集群上[启用 KubeSphere DevOps 系统](../../../../docs/pluggable-components/devops/)。
- 您需要使用具有 `workspace-self-provisioner` 角色的帐户(例如 `project-admin`)创建一个多集群项目,并在 Host 集群上创建一个 DevOps 工程。本教程中的多集群项目创建于 Host 集群和一个 Member 集群上。
- 您需要邀请一个帐户(例如 `project-regular`)至 DevOps 工程中,赋予 `operator` 角色。有关更多信息,请参见[创建企业空间、项目、帐户和角色](../../../quick-start/create-workspace-and-project/)、[多集群管理](../../../multicluster-management/)和[多集群项目](../../../project-administration/project-and-multicluster-project/#多集群项目)。
## 创建 Docker Hub 访问令牌 (Token)
@ -35,13 +36,13 @@ weight: 11420
您需要在 KubeSphere 中为已创建的访问令牌创建凭证,以便流水线能够向 Docker Hub 推送镜像。此外,您还需要创建 kubeconfig 凭证,用于访问 Kubernetes 集群。
1. 以 `project-regular` 身份登录 KubeSphere Web 控制台,转到您的 DevOps 工程,在**凭证**页面点击**创建**。
1. 以 `project-regular` 身份登录 KubeSphere Web 控制台,前往您的 DevOps 工程,在**凭证**页面点击**创建**。
![创建 dockerhub ID](/images/docs/zh-cn/devops-user-guide/examples/deploy-apps-in-multicluster-project-using-jenkinsfile/create-dockerhub-id.PNG)
![创建 dockerhub ID](/images/docs/zh-cn/devops-user-guide/examples/deploy-apps-in-multicluster-project-using-jenkinsfile/create-dockerhub-id-1.png)
2. 在弹出对话框中,设置**凭证 ID**,稍后会用于 Jenkinsfile 中,**类型**选择**帐户凭证**。**用户名**输入您的 Docker Hub 帐户名称,**token / 密码**中输入刚刚创建的访问令牌。操作完成后,点击**确定**。
![创建 Docker 凭证](/images/docs/zh-cn/devops-user-guide/examples/deploy-apps-in-multicluster-project-using-jenkinsfile/credential-docker-create.PNG)
![创建 Docker 凭证](/images/docs/zh-cn/devops-user-guide/examples/deploy-apps-in-multicluster-project-using-jenkinsfile/credential-docker.png)
{{< notice tip >}}
@ -49,35 +50,41 @@ weight: 11420
{{</ notice >}}
3. 再次点击**创建****类型**选择 **kubeconfig**KubeSphere 会自动填充 **Content** 字段,即当前用户帐户的 kubeconfig。设置**凭证 ID**,然后点击**确定**。
3. 登出 KubeSphere Web 控制台,再以 `project-admin` 身份登录。前往您的 DevOps 工程,在**凭证**页面点击**创建**。**类型**选择 **kubeconfig**KubeSphere 会自动填充 **Content** 字段,即当前帐户的 kubeconfig。设置**凭证 ID**,然后点击**确定**。
![创建 kubeconfig](/images/docs/zh-cn/devops-user-guide/examples/deploy-apps-in-multicluster-project-using-jenkinsfile/create-kubeconfig.PNG)
{{< notice note >}}
在未来版本中,您可以邀请 `project-regular` 帐户至您的多集群项目中,并赋予必要角色,以使用此帐户创建 kubeconfig 凭证。
{{</ notice >}}
## 创建流水线
创建完上述凭证后,您可以按照以下步骤使用示例 Jenkinsfile 创建流水线。
创建完上述凭证后,您可以使用 `project-regular` 帐户按照以下步骤使用示例 Jenkinsfile 创建流水线。
1. 要创建流水线,请在**流水线**页面点击**创建**。
![创建流水线](/images/docs/zh-cn/devops-user-guide/examples/deploy-apps-in-multicluster-project-using-jenkinsfile/create-pipeline.PNG)
![创建流水线](/images/docs/zh-cn/devops-user-guide/examples/deploy-apps-in-multicluster-project-using-jenkinsfile/create_pipeline.png)
2. 在弹出窗口中设置名称,然后点击**下一步**。
![设置流水线名称](/images/docs/zh-cn/devops-user-guide/examples/deploy-apps-in-multicluster-project-using-jenkinsfile/set-pipeline-name.PNG)
![设置流水线名称](/images/docs/zh-cn/devops-user-guide/examples/deploy-apps-in-multicluster-project-using-jenkinsfile/set-pipeline_name.png)
3. 在本教程中,您可以为所有字段使用默认值。在**高级设置**页面,直接点击**创建**。
![创建流水线-2](/images/docs/zh-cn/devops-user-guide/examples/deploy-apps-in-multicluster-project-using-jenkinsfile/create-pipeline-2.PNG)
![创建流水线-2](/images/docs/zh-cn/devops-user-guide/examples/deploy-apps-in-multicluster-project-using-jenkinsfile/create-pipeline_2.png)
## 编辑 Jenkinsfile
1. 在流水线列表中,点击该流水线进入其详情页面。点击**编辑 Jenkinsfile** 定义一个 Jenkinsfile流水线会基于它来运行。
![编辑 jenkinsfile](/images/docs/zh-cn/devops-user-guide/examples/deploy-apps-in-multicluster-project-using-jenkinsfile/edit-jenkinsfile.PNG)
![编辑 jenkinsfile](/images/docs/zh-cn/devops-user-guide/examples/deploy-apps-in-multicluster-project-using-jenkinsfile/edit_jenkinsfile.png)
2. 将以下所有内容复制并粘贴到弹出窗口中,用作流水线的示例 Jenkinsfile。您必须将 `DOCKERHUB_USERNAME`、`DOCKERHUB_CREDENTIAL`、`KUBECONFIG_CREDENTIAL_ID`、`MULTI_CLUSTER_PROJECT_NAME` 和 `MEMBER_CLUSTER_NAME` 的值替换成您自己的值。操作完成后,点击**确定**。
```
```groovy
pipeline {
agent {
node {
@ -88,19 +95,19 @@ weight: 11420
environment {
REGISTRY = 'docker.io'
// username of dockerhub
DOCKERHUB_USERNAME = 'yuswift'
// Docker Hub 用户名
DOCKERHUB_USERNAME = 'Your Docker Hub username'
APP_NAME = 'devops-go-sample'
// dockerhubid is the dockerhub credential id you created on ks console
DOCKERHUB_CREDENTIAL = credentials('dockerhubid')
// the kubeconfig credential id you created on ks console
KUBECONFIG_CREDENTIAL_ID = 'multi-cluster'
// mutli-cluster project name under your own workspace
MULTI_CLUSTER_PROJECT_NAME = 'devops-with-go'
// the member cluster name you want to deploy app on
// in this tutorial, you are assumed to deploy app on host and only one member cluster
// for more member clusters, please edit manifest/multi-cluster-deploy.yaml
MEMBER_CLUSTER_NAME = 'c9'
// dockerhub-go 即您在 KubeSphere 控制台上创建的 Docker Hub 凭证 ID
DOCKERHUB_CREDENTIAL = credentials('dockerhub-go')
// 您在 KubeSphere 控制台上创建的 kubeconfig 凭证 ID
KUBECONFIG_CREDENTIAL_ID = dockerhub-go-kubeconfig
// 您企业空间中的多集群项目名称
MULTI_CLUSTER_PROJECT_NAME = 'demo-multi-cluster'
// 您用来部署应用的 Member 集群名称
// 本教程中,应用部署在 Host 集群和一个 Member 集群上
// 若需要部署在多个 Member 集群上, 请编辑 manifest/multi-cluster-deploy.yaml
MEMBER_CLUSTER_NAME = 'Your Member Cluster name'
}
stages {
@ -129,7 +136,7 @@ weight: 11420
script {
withCredentials([
kubeconfigFile(
credentialsId: 'multi-cluster',
credentialsId: 'dockerhub-go-kubeconfig',
variable: 'KUBECONFIG')
]) {
sh 'envsubst < devops-go-sample/manifest/multi-cluster-deploy.yaml | kubectl apply -f -'
@ -152,4 +159,4 @@ weight: 11420
保存 Jenkinsfile 后,点击**运行**。如果一切顺利,您会在您的多集群项目中看到部署 (Deployment) 工作负载。
![Deployment](/images/docs/zh-cn/devops-user-guide/examples/deploy-apps-in-multicluster-project-using-jenkinsfile/multi-cluster-ok.PNG)
![Deployment](/images/docs/zh-cn/devops-user-guide/examples/deploy-apps-in-multicluster-project-using-jenkinsfile/multi-cluster_ok.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 403 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 193 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 561 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 45 KiB

After

Width:  |  Height:  |  Size: 554 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 132 KiB