Integrate a CI/CD pipeline to a Kubernetes cluster with Slack
Use a toolchain to maintain apps running on the IBM Cloud Kubernetes Service and send notifications to a Slack channel
As enterprises move workloads to the cloud, transparency and visibility across development and operations (DevOps) teams are really important. The real power of the Slack messaging platform is to help teams collaborate and coordinate their work no matter where they are located, in the field office, at home, or anywhere around the globe. This tutorial shows you how to set up a toolchain of IBM Cloud services that uses a continuous integration and continuous delivery (CI/CD) stack to maintain and deploy applications running on a Kubernetes cluster. Then, it shows how you can enhance the collaboration experience within your DevOps team by integrating the toolchain with the Slack platform so your Slack channel receives notifications about deployment activities.
You need the following tools to complete the steps in this tutorial:
- IBM Cloud account.
- Access to the IBM Cloud Kubernetes Service to create a Kubernetes cluster. If needed, you can create one free cluster for 30 days to get familiar with Kubernetes capabilities.
- Slack workspace.
- Visual Studio Code or another integrated development environment (IDE) for local development.
- A GitHub account and some knowledge of Git commands.
- Fork and clone the GitHub repository
- Create a Kubernetes cluster
- Create the container registry
- Configure a toolchain
- Verify the application is running
- Activate the Slack API
- Integrate your Slack app with your toolchain
- Change the toolchain and check updates on Slack
- Open the GitHub repository that contains the sample code for this tutorial.
- Click Fork from the menu bar to copy the repository into your GitHub account. You will use the URL of your forked repository in Step 4.
- Open your terminal and change your directory by using the
cd downloads
command (or any other directory in which you want to clone the project). - Run the
git clone https://github.com/mahsankhaan/CICD-in-kubernetes.git
command. - Move into the cloned folder and run the
npm install
command to install the dependencies. - Run the
node app.js
command to check that the application runs successfully on you local device.
Note: The project contains the Dockerfile and deployment files that you use in the next few steps, so please go through the file. This tutorial does not focus on how to write these two files.
- Log into your IBM Cloud account and navigate to the Kubernetes Cluster page.
- Create a Kubernetes cluster. If you need assistance, refer to the steps in the Getting started with IBM Cloud Kubernetes Service documentation.
It may take 10 to 15 minutes for your Kubernetes cluster to provision.
- Go to the Container Registry page within IBM Cloud.
- Click the Create button.
- Within the Location drop box, select the region that is closest to you geographically.
- Click the Namespaces tab.
- Click Create.
- In the Create namespace pane, enter a unique name for your registry within the Name field.
- Click Create.
Note: Although you could skip this step and directly integrate the container registry within Step 4, it could cause an error if your registry name is not unique.
Toolchains provide an integrated set of tools to build, deploy, and manage your apps. By creating a toolchain on IBM Cloud, you can develop and deploy an application securely into a Kubernetes cluster managed by the IBM Cloud Kubernetes Service. The toolchain includes Vulnerability Advisor to provide a secure container.
After you create the toolchain in this step and push the changes to your repository, the delivery pipeline will automatically build and deploy the code. Learn more about creating toolchains on IBM Cloud.
Perform the following tasks:
- Go to the Create a Toolchain dashboard within IBM Cloud.
- There are many ready-made toolchains are available on the dashboard. Select Develop a Kubernetes app.
- Enter any name you want within the Toolchain Name field.
- In the Select Region field, choose the same location as the one you chose in Step 3.
- In the Select a source provider field, choose Git Repos and Issue Tracking.
- Within the Tool Integrations section, in the Source repository URL field, enter the URL of the forked repository that you created in Step 1.
- Click the Delivery Pipeline tab.
- In the App name field, enter
mypipeline
. - In the IBM Cloud API key field, click the New button.
- In the Create a new API key with full access window, select OK.
- In the Container registry region and Cluster region fields, select the region where you created your services earlier.
- Click the Create button.
- After the Delivery Pipeline status box indicates that it is configured successfully, click it to open the Delivery Pipeline status page.
The screen capture of the Delivery Pipeline page shows the three pipeline stages:
- Build: If a
manifest.yml
file exists in the root folder, it is used to determine which buildpack to use. - Containerize: This stage checks for the Dockerfile in your root folder, creates a container registry after the image is successfully built, and deploys the image in the registry. This stage also checks for any vulnerabilities in the image, and if there are any, then images with high warnings will not deploy.
- Deploy: This stage checks for cluster readiness and namespace existence, configures the cluster namespace, updates the
deployment.yml
manifest file, and grants access to the private image registry.
Note: The screen capture image displays a warning because we did not activate the SSL certificate.
- Within the Deploy stage, click View logs and history.
- Select Deploy to Kubernetes.
- Scroll to the end of the logs, until you reach
VIEW THE APPLICATION AT:
followed by a URL. - Open your browser and run the URL to check whether the application is running.
Now you will verify if the services are running on your Kubernetes cluster.
- Go to the Resource list in IBM Cloud.
- Expand Cluster and click on the Kubernetes cluster service that you created in Step 2.
- Within your cluster service page, select Kubernetes dashboard from the menu bar.
- Within the side panel of the Kubernetes dashboard, click Namespaces.
- Select default from the Namespaces list.
- Check the services running as
hello-app
.
If you do not already have a Slack account, create one before starting the following tasks to activate the Slack API.
- Within your browser, go to https://api.slack.com/apps.
- Click Create an App.
- Within the Create a Slack App window, enter
IBM Toolchain
in the App Name field. - In the Development Slack Workspace drop down list, select the workspace that you identified or created to fulfill the Prerequisites for this tutorial.
- Click Create App. After your app is created, you will be redirected to the settings page for your new app.
- Select Incoming Webhooks.
- Set Activate Incoming Webhooks to On.
- Click Add New Webhook to Workspace.
- Pick a channel from the list that you want the app to post notifications to.
- Click Allow.
- Back on the Incoming Webhooks page, there is a new entry within the Webhook URL section for the channel you selected. Click the Copy button to save the URL for an upcoming task.
Now that your Slack app is set up, you can integrate it with your toolchain in IBM Cloud.
- Go to the Toolchains dashboard within IBM Cloud.
- In the Location list, choose the region where you created your toolchain in Step 4.
- Select your toolchain from the list.
- On you toolchain overview page, click the Add tool button.
- Type
Slack
in the search box and click the Slack integration option that appears. - On the Configure Slack page, paste the webhook URL that you copied at the end of Step 6 into the Slack webhook field.
- Within the Slack channel field, enter the name of the channel that you selected in Step 6 to post notifications to.
- In the Slack team name field, enter the name of your Slack team. (To find your team name, open your Slack workspace, expand your workspace name located in the side panel, and copy the word or phrase that appears before
.slack.com
.) - Click the Create Integration button. Your toolchain overview page will now display your Slack integration within a Culture section of the pipeline.
- Open your Slack workspace. Inside the configured channel will be a message stating that your Slack service has been bound to your toolchain.
- From your toolchain overview page, click the Delivery Pipeline status box to open the Delivery Pipeline status page.
- Within the Deploy stage, click the Run Stage icon.
- The stage will start running and verification messages will appear in your Slack workspace channel. In a real world scenario, everyone on your team would have better visualization of the project changes through these types of automated deployment messages.
In this tutorial, you learned how to work with toolchains to manage applications running on a Kubernetes cluster with automated stages that replace the manual interaction of development and operations teams. In addition, this tutorial demonstrated how the Slack platform can enhance DevOps collaboration by integrating it with an IBM Cloud toolchain to verify executions performed by a specific role. Teams could use DevOps toolchains for multi-staging strategies as part of their best practices. For example, before deploying a complete workload to a production Kubernetes cluster, it could be deployed in the test environment stage and not affect the production environment if there are any failures.