Integrate a CI/CD pipeline to a Kubernetes cluster with Slack

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.

Prerequisites

You need the following tools to complete the steps in this tutorial:

Estimated time

It will take you less than one hour to complete this tutorial.

Steps

  1. Fork and clone the GitHub repository
  2. Create a Kubernetes cluster
  3. Create the container registry
  4. Configure a toolchain
  5. Verify the application is running
  6. Activate the Slack API
  7. Integrate your Slack app with your toolchain
  8. Change the toolchain and check updates on Slack

Step 1. Fork and clone the GitHub repository

  1. Open the GitHub repository that contains the sample code for this tutorial.
  2. 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.
  3. 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).
  4. Run the git clone https://github.com/IBM/CICD-Kubernetes-Slack.git command.
  5. Move into the cloned folder and run the npm install command to install the dependencies.
  6. 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 files. This tutorial does not focus on how to write these two files.

Step 2. Create a Kubernetes cluster

  1. Log into your IBM Cloud account and navigate to the Kubernetes Cluster page.
  2. Create a Kubernetes cluster. If you need assistance, refer to the steps in the Getting started with IBM Cloud Kubernetes Service documentation.

    Screen capture of the Create tab within the Kubernetes Cluster page of IBM Cloud

It may take 10 to 15 minutes for your Kubernetes cluster to provision.

Step 3. Create the container registry

  1. Go to the Container Registry page within IBM Cloud.
  2. Click the Create button.
  3. Within the Location drop box, select the region that is closest to you geographically.
  4. Click the Namespaces tab.
  5. Click Create.
  6. In the Create namespace pane, enter a unique name for your registry within the Name field.
  7. 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.

Step 4. Configure a toolchain

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:

  1. Go to the Create a Toolchain dashboard within IBM Cloud.
  2. There are many ready-made toolchains are available on the dashboard. Select Develop a Kubernetes app.
  3. Enter any name you want within the Toolchain Name field.
  4. In the Select Region field, choose the same location as the one you chose in Step 3.
  5. In the Select a source provider field, choose Git Repos and Issue Tracking.
  6. Within the Tool Integrations section, in the Source repository URL field, enter the URL of the forked repository that you created in Step 1.
  7. Click the Delivery Pipeline tab.
  8. In the App name field, enter mypipeline.
  9. In the IBM Cloud API key field, click the New button.
  10. In the Create a new API key with full access window, select OK.
  11. In the Container registry region and Cluster region fields, select the region where you created your services earlier.
  12. Click the Create button.
  13. After the Delivery Pipeline status box indicates that it is configured successfully, click it to open the Delivery Pipeline status page.

    Screen capture of 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.

Step 5. Verify the application is running

  1. Within the Deploy stage, click View logs and history.
  2. Select Deploy to Kubernetes.
  3. Scroll to the end of the logs, until you reach VIEW THE APPLICATION AT: followed by a URL.
  4. Open your browser and run the URL to check whether the application is running.

    Screen capture of the ADMIN LOGIN screen of the application

Now you will verify if the services are running on your Kubernetes cluster.

  1. Go to the Resource list in IBM Cloud.
  2. Expand Cluster and click on the Kubernetes cluster service that you created in Step 2.
  3. Within your cluster service page, select Kubernetes dashboard from the menu bar.
  4. Within the side panel of the Kubernetes dashboard, click Namespaces.
  5. Select default from the Namespaces list.
  6. Check the services running as hello-app.

Step 6. Activate the Slack API

If you do not already have a Slack account, create one before starting the following tasks to activate the Slack API.

  1. Within your browser, go to https://api.slack.com/apps.
  2. Click Create an App.
  3. Within the Create a Slack App window, enter IBM Toolchain in the App Name field.
  4. In the Development Slack Workspace drop down list, select the workspace that you identified or created to fulfill the Prerequisites for this tutorial.
  5. Click Create App. After your app is created, you will be redirected to the settings page for your new app.
  6. Select Incoming Webhooks.
  7. Set Activate Incoming Webhooks to On.
  8. Click Add New Webhook to Workspace.
  9. Pick a channel from the list that you want the app to post notifications to.
  10. Click Allow.
  11. 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.

Step 7. Integrate your Slack app with your toolchain

Now that your Slack app is set up, you can integrate it with your toolchain in IBM Cloud.

  1. Go to the Toolchains dashboard within IBM Cloud.
  2. In the Location list, choose the region where you created your toolchain in Step 4.
  3. Select your toolchain from the list.
  4. On you toolchain overview page, click the Add tool button.
  5. Type Slack in the search box and click the Slack integration option that appears.
  6. On the Configure Slack page, paste the webhook URL that you copied at the end of Step 6 into the Slack webhook field.
  7. Within the Slack channel field, enter the name of the channel that you selected in Step 6 to post notifications to.
  8. 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.)
  9. Click the Create Integration button. Your toolchain overview page will now display your Slack integration within a Culture section of the pipeline.

    Screen capture of updated toolchain overview page

  10. Open your Slack workspace. Inside the configured channel will be a message stating that your Slack service has been bound to your toolchain.

Step 8. Change the toolchain and check updates on Slack

  1. From your toolchain overview page, click the Delivery Pipeline status box to open the Delivery Pipeline status page.
  2. Within the Deploy stage, click the Run Stage icon.
  3. 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.

    Screen capture of Deploy stage updates appearing within the Slack channel

Conclusion

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.