As a Code Stream administrator or developer, you can use Code Stream and VMware Tanzu Kubernetes Grid Integrated Edition (formerly known as VMware Enterprise PKS) to automate the deployment of your software applications to a Kubernetes cluster. This use case mentions other methods that you can use to automate the release of your application.

In this use case, you will create a pipeline that includes two stages, and will use Jenkins to build and deploy your application.

  • The first stage is for development. It uses Jenkins to pull your code from a branch in your GitHub repository, then build, test, and publish it.
  • The second stage is for deployment. It runs a user operation task that requires approval from key users before the pipeline can deploy your application to your Kubernetes cluster.

When using a Kubernetes API endpoint in the pipeline workspace, Code Stream creates the necessary Kubernetes resources such as ConfigMap, Secret, and Pod to run the continuous integration (CI) task or custom task. Code Stream communicates with the container by using the NodePort.

To share data across pipeline runs, you must provide a persistent volume claim, and Code Stream will mount the persistent volume claim to the container to store the data, and use it for subsequent pipeline runs.

The Code Stream pipeline workspace supports Docker and Kubernetes for continuous integration tasks and custom tasks.

For more information about configuring the workspace, see Configuring the Pipeline Workspace.

The workflow that deploys an application to a Kubernetes cluster uses Jenkins, Code Stream, GitHub, the trigger for Git, and Kubernetes.

The development tools, deployment instances, and pipeline YAML file must be available so that your pipeline can build, test, publish, and deploy your application. The pipeline will deploy your application to development and production instances of Kubernetes clusters.

The workflow helps you navigate the process that releases an application to a Kubernetes cluster.

Other methods that automate the release of your application:

  • Instead of building your application by using Jenkins, you can use the Code Stream native build capability and a Docker build host.
  • Instead of deploying your application to a Kubernetes cluster, you could deploy it to an Amazon Web Services (AWS) cluster.

For more information about using the Code Stream native build capability and a Docker host, see:

Prerequisites

  • Verify that the application code to deploy resides in a working GitHub repository.
  • Verify that you have a working instance of Jenkins.
  • Verify that you have a working email server.
  • In Code Stream, create an email endpoint that connects to your email server.
  • Set up two Kubernetes clusters on Amazon Web Services (AWS), for development and production, where your pipeline will deploy your application.
  • Verify that the GitHub repository contains the YAML code for your pipeline, and alternatively a YAML file that defines the metadata and specifications for your environment.

Procedure

  1. In Code Stream, click Endpoints > New Endpoint, and create a Jenkins endpoint that you will use in your pipeline to pull code from your GitHub repository.
  2. To create Kubernetes endpoints, click New Endpoint.
    1. Create an endpoint for your development Kubernetes cluster.
    2. Create an endpoint for your production Kubernetes cluster.
      The URL for your Kubernetes cluster might or might not include a port number.
      For example:
      https://10.111.222.333:6443
      https://api.kubernetesserver.fa2c1d78-9f00-4e30-8268-4ab81862080d.k8s-user.com
  3. Create a pipeline that deploys a container of your application, such as Wordpress, to your development Kubernetes cluster, and set the input properties for the pipeline.
    1. To allow your pipeline to recognize a code commit in GitHub that will trigger the pipeline, in the pipeline click the Input tab and select Auto inject properties.
    2. Add the property named GIT_COMMIT_ID, and click the star next to it.
      When the pipeline runs, the pipeline execution will display the commit ID that the Git trigger returns.
    In a pipeline that uses Jenkins and Kubernetes, the Input tab displays the Git input properties.
  4. Add notifications to send an Email when the pipeline succeeds or fails.
    1. In the pipeline, click the Notifications tab, and click Add.
    2. To add an email notification when the pipeline finishes running, select Email, and select Completes. Then, select the email server, enter email addresses, and click Save.
    3. To add another email notification for a pipeline failure, select Fails, and click Save.
    When you add an email notification, you select one of the pipeline states, and select the Email server and recipients.
  5. Add a development stage to your pipeline, and add tasks that build, test, and publish your application. Then, validate each task.
    1. To build your application, add a Jenkins task that uses the Jenkins endpoint, and runs a build job from the Jenkins server. Then, for the pipeline to pull your code, enter the Git branch in this form: ${input.GIT_BRANCH_NAME}
    2. To test your application, add a Jenkins task that uses the same Jenkins endpoint, and runs a test job from the Jenkins server. Then, enter the same Git branch.
    3. To publish your application, add a Jenkins task that uses the same Jenkins endpoint, and runs a publish job from the Jenkins server. Then, enter the same Git branch.
    In a pipeline that uses Jenkins and Kubernetes, the Jenkins task that builds the application uses the Jenkins endpoint, Jenkins Job, and the branch name.
  6. Add a deployment stage to your pipeline, then add a task that requires an approval for deployment of your application, and another task that deploys the application to your Kubernetes cluster. Then, validate each task.
    1. To require an approval on the deployment of your application, add a User Operation task, add Email addresses for the users who must approve it, and enter a message. Then, enable Send email.
    2. To deploy your application, add a Kubernetes task. Then, in the Kubernetes task properties, select your development Kubernetes cluster, select the Create action, and select the Local Definition payload source. Then select your local YAML file.
  7. Add a Git webhook that enables Code Stream to use the Git trigger, which triggers your pipeline when developers commit their code.
    In the webook for Git that triggers the Kubernetes pipeline, you must enter the branch and a secret token.
  8. To test your pipeline, go to your GitHub repository, update your application YAML file, and commit the change.
    1. In Code Stream, verify that the commit appears.
    1. Click Triggers > Git > Activity.
    2. Look for the trigger of your pipeline.
    3. Click Dashboards > Pipeline Dashboards.
    4. On your pipeline dashboard, find the GIT_COMMIT_ID in the latest successful change area.
  9. Check your pipeline code and verify that the change appears.

Results

Congratulations! You automated the deployment of your software application to your Kubernetes cluster.

Example: Example pipeline YAML that deploys an application to a Kubernetes cluster

For the type of pipeline used in this example, the YAML resembles the following code:

apiVersion: v1
kind: Namespace
metadata:
  name: ${input.GIT_BRANCH_NAME}
  namespace: ${input.GIT_BRANCH_NAME}
---  
apiVersion: v1
data:
  .dockercfg: eyJzeW1waG9ueS10YW5nby1iZXRhMi5qZnJvZy5pbyI6eyJ1c2VybmFtZSI6InRhbmdvLWJldGEyIiwicGFzc3dvcmQiOiJhRGstcmVOLW1UQi1IejciLCJlbWFpbCI6InRhbmdvLWJldGEyQHZtd2FyZS5jb20iLCJhdXRoIjoiZEdGdVoyOHRZbVYwWVRJNllVUnJMWEpsVGkxdFZFSXRTSG8zIn19
kind: Secret
metadata:
  name: jfrog
  namespace: ${input.GIT_BRANCH_NAME}
type: kubernetes.io/dockercfg
---   
apiVersion: v1
kind: Service
metadata:
  name: codestream
  namespace: ${input.GIT_BRANCH_NAME}
  labels:
    app: codestream
spec:
  ports:
    - port: 80
  selector:
    app: codestream
    tier: frontend
  type: LoadBalancer 
---
apiVersion: extensions/v1
kind: Deployment
metadata:
  name: codestream
  namespace: ${input.GIT_BRANCH_NAME}
  labels:
    app: codestream
spec:
  selector:
    matchLabels:
      app: codestream
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: codestream
        tier: frontend
    spec:
      containers:
      - name: codestream
        image: cas.jfrog.io/codestream:${input.GIT_BRANCH_NAME}-${Dev.PublishApp.output.jobId}
        ports:
        - containerPort: 80
          name: codestream
      imagePullSecrets:
      - name: jfrog
      

What to do next

To deploy your software application to your production Kubernetes cluster, perform the steps again and select your production cluster.

To learn more about integrating Code Stream with Jenkins, see How do I integrate Code Stream with Jenkins.