Continuous Deployment

In this tutorial, you will see how to use cno to manage the deployment of your applications. We will show you how to scale up/down applications, manually deploy new versions (continuous delivery), detect and auto deploy new versions pushed on your docker registry (continuous deployment), use advanced deployment strategies like Blue/Green, Canary and A/B Testing.

Prerequisites

  • CNO cluster (go to installation section to see how to deploy one)
  • Install cnoctl one your computer

Before to begin

  1. Create a project named: lab

                    cnoctl create project lab --cpu 2,1 --memory 4Gi,2Gi --storage 0Gi
                  

    This command allows you to create a project named lab with 2 cores cpu-limit, 1 core cpu-request, 4Gi memory-limit, 2Gi memory-request and 0Gi storage.

    NB. You must wait for quota request validation if you are not super_admin or owner+

  2. Create environment named: cno-cd-lab

                    cnoctl create env cno-cd-lab --cpu 2,1 --memory 4Gi,2Gi --storage 0Gi --project lab --cluster default
                  

    This command allows you to create an environment named cno-cd-lab on the lab project with 2 cores cpu-limit, 1 core cpu-request, 4Gi memory-limit, 2Gi memory-request and 0Gi storage, and deploy the environment on the default cluster

  3. Create and push image to your docker-registry

    1. Clone the version-printer repo

                      git clone https://github.com/beopencloud/version-printer.git
                    
                      cd version-printer
                    
    2. build and push version v1.0.0 to your docker registry

                      git checkout v1.0.0
                    
                      docker build . -t <your username>/version-printer:v1.0.0
                    
                      Docker push <your username>/version-printer:v1.0.0
                    
    3. build and push version v2.0.0 to your docker registry

                      git checkout v2.0.0
                    
                      docker build . -t <your username>/version-printer:v2.0.0
                    
                      Docker push <your username>/version-printer:v2.0.0
                    
  4. Connect to the cno-cd-lab environment

                    cnoctl use env -e cno-cd-lab --project lab
                  

    This command allows you to get a kubeconfig allowing you to access the cno-cd-lab environment. And then you can apply kubectl commands.

  5. Deploy the version-printer application on your environment

                    cnoctl create deploy version-printer --image <your username>/version-printer:v1.0.0
                  
                    cnoctl create service version-printer clusterip --tcp 8080:8080
                  
  6. Port-forward and check if the application responds correctly

    1. Open a new terminal and run the following command

                      kubectl port-forward service/version-printer 8080:8080
                    
    2. On another terminal run the following command to test if application responds correctly

                      curl -w "
      " http://localhost:8080
                    
      version 1.0.0

Overviews:

There are a cd operator on the cno data plane that monitors kubernetes objects such as deployments, StatefulSets, DaemonSets, Services and Ingress present in environments. It's therefore possible to retrieve the list of all workloads (applications) present in an environment. the operator also monitors the docker registry to detect new versions of applications.
cd functionality can be used from cno ui or from cnoctl. For example we can get the list of workloads deployed in the namespace cno-cd-lab with the following command.

                cnoctl get workload --project lab --env cno-cd-lab
              
NAME STRATEGY REPLICAS IMAGE LIVE STAGING
version-printer default 1/1 <your username>/version-printer v1.0.0 -

Scaling

We can scale up/down an application directly from cno without accessing the cluster api-server where the application is deployed.
This is how to do that with cnoctl.

                cnoctl deploy workload version-printer --replicas 2  --project lab --env cno-cd-lab
              

with this command, we increase the number of replicas of the version-printer application to two

                cnoctl get workload --project lab --env cno-cd-lab
              
NAME STRATEGY REPLICAS IMAGE LIVE STAGING
version-printer default 2/2 <your username>/version-printer v1.0.0 -

And we can scale down to 1 by this command

                cnoctl deploy workload version-printer --replicas 1  --project lab --env cno-cd-lab
              

Continuous Delivery

From the cno api, it is easy to manually update the version of an application. which avoids us having to detect the kubernetes cluster on which the application is deployed, to retrieve a kubeconfig allowing us to access it and type kubectl commands to update the app version.
This can be done from the cno user interface or from the command line with cnoctl. Here is how to upgrade the version of the version-printer to v2.0.0 from cnoctl.

                cnoctl deploy workload version-printer --version v2.0.0
              

We can verify if the deployment is effective by this command

                cnoctl get workload --project lab --env cno-cd-lab
              
NAME STRATEGY REPLICAS IMAGE LIVE STAGING
version-printer default 1/1 <your username>/version-printer v2.0.0 -

We can see that the live version of the version-printer app is v2.0.0

                curl -w "
" http://localhost:8080
              
version 2.0.0

Auto deploy

CNO's CD operator has the ability to monitor your docker registry and detect any new versions of your applications. It is possible to activate the auto-deployment functionality so that each time a new version of your application is pushed on your docker registry, it will be automatically deployed. We can also apply filters on the name of the versions to be deployed.
Run the following command to activate the auto deploy functionality for the version-printer app.

                cnoctl deploy workload version-printer --auto-deploy=true --project lab --env cno-cd-lab
              

For that, version-printer will point to the last created version of the nginx image and each time a new version, it will automatically be deployed.

                cnoctl get workload --project lab --env cno-cd-lab
              
NAME STRATEGY REPLICAS IMAGE AUTO-DEPLOY LIVE STAGING
version-printer default 1/1 beopenit/version-printer true v2.0.0 -

Let’s build and push version v3.0.0 to your docker registry

                git checkout v3.0.0
              
                docker build . -t <your username>/version-printer:v3.0.0
              
                Docker push <your username>/version-printer:v3.0.0
              

After this, v3.0.0 will be automatically deployed

NAME STRATEGY REPLICAS IMAGE AUTO-DEPLOY LIVE STAGING
version-printer default 1/1 beopenit/version-printer true v3.0.0 -

The we can test with the following command

                curl -w "
" http://localhost:8080
              
version 3.0.0

Run the following command to disable the auto-deploy.

                cnoctl deploy workload version-printer --auto-deploy=true --project lab --env cno-cd-lab
              

We can check if it’s ok.

                cnoctl get workload --project lab --env cno-cd-lab
              
NAME STRATEGY REPLICAS IMAGE AUTO-DEPLOY LIVE STAGING
version-printer default 1/1 beopenit/version-printer false v3.0.0 -

Now the deployment will be manual. Run the following command to deploy version v2.0.0

                cnoctl deploy workload version-printer --version v2.0.0
              

Blue/Green

It is also possible to do Blue/Green deployment with CNO CD. You can deploy a new version on your production environment without exposing it to the general public. So the staging version would be production ready and you can choose the ideal moment to easily switch the flow to the new version or cancel if you have not trust.
Run this command to use the blue/green strategy.

                cnoctl deploy workload version-printer --strategy blue/green
              

There will be two versions of the application running. One containing the live version and the other, the staging version.

                cnoctl deploy get deployment
              
NAME READY UP-TO-DATE AVAILABLE AGE
version-printer 1/1 1 1 23h
version-printer-green-workload 1/1 1 1 83s

Now we have the same version on the live and the staging version.

                cnoctl get workload version-printer -e cno-cd-lab -p lab
              
NAME STRATEGY REPLICAS IMAGE AUTO-DEPLOY LIVE STAGING
version-printer blue/green 1/1 beopenit/version-printer false v2.0.0 v2.0.0

We can deploy version v3.0.0 with this command

                cnoctl deploy workload version-printer --version v3.0.0
              

Now the staging deployment will contain version v3.0.0 and the live version still with v2.0.0

                cnoctl get workload -e cno-cd-lab -p lab
              
NAME STRATEGY REPLICAS IMAGE AUTO-DEPLOY LIVE STAGING
version-printer blue/green 1/1 beopenit/version-printer false v2.0.0 v3.0.0

We can check if the flow is redirected to the live version with this command.

                curl -w "
" http://localhost:8080
              
version 2.0.0

Run the following command to make the staging version live.

                cnoctl deploy workload version-printer --go-to-live -e cno-cd-lab -p lab
              

From now, live and intermediate deployment will be switched. Live deployment contains version v3.0.0 and staging 2.0.0.

                cnoctl get workload -e cno-cd-lab -p lab
              
NAME STRATEGY REPLICAS IMAGE AUTO-DEPLOY LIVE STAGING
version-printer blue/green 1/1 beopenit/version-printer false v3.0.0 v2.0.0

The flow is redirected to version v3.0.0.

                curl -w "
" http://localhost:8080
              
version 3.0.0