Deployment Options for Kubernetes

Codefresh offers a lot of options when it comes to Kubernetes deployments. These are

  1. Using the Codefresh GUI to deploy on demand. This is the easiest way and was described in the quick start guide
  2. Using the predefined steps of the GUI pipeline. This is also very easy if you are not yet familiar with Codefresh YAML
  3. Using the dedicated deploy step in a pipeline. Explained in detail in this page
  4. Using a freestyle step with your own kubectl commands. This is very flexible, but assumes that you know how to work with kubectl
  5. Using Helm as a package manager. See the Helm quick start guide for more details.

Prerequisites

It is assumed that:

Build and Push your image

The following describe a basic Codefresh pipeline scenario to build and push your image to Dockerhub registry.

YAML

version: '1.0'
steps:
  BuildImage:
    type: build
    image_name: '<your_docker_repo>/<your_image_name>' #specify your future image reference here
    dockerfile: Dockerfile
    tag: '${{CF_BRANCH_TAG_NORMALIZED}}'
    
  PushToDockerRegistry:
    type: push
    candidate: '${{BuildImage}}'
    tag: '${{CF_BRANCH_TAG_NORMALIZED}}'
    registry: 'dockerhub' #the name of the registry you added to Codefresh

Using this YAML example, we’ll add an additional step to deploy the image in Dockerhub to Kubernetes.

Describe your deployment

The follwoing instructions describe how to create a new service in your Kubernetes cluster in order to deploy to it.

Note: If you’re deploying to an existing service in your Kubernetes cluster please skip to the next step

  1. Go to the KubernetesServices page
  1. Click the button “Add Service”
  1. Select the cluster
  1. Select the namespace
  1. Type an arbitrary service name
  1. Specify the number of replicas
  1. Type the name of your pushed image
  1. In the “Internal Ports” field specify the port which your application listens to.
  1. In the “Expose port” field specify the port to be exposed to the Internet and check the checkbox
  1. Click the button “Deploy” to deploy the application.

Wait until the deployment is finished and you will be able to open the deployed application in your browser by clicking on the “endpoint” link.

Screenshot from 2018-02-16 17-09-54.png

Add a Deployment step

So now you have deployed your image manually, which is great. But how to trigger the deployment within your pipeline? For that you will need to add a step of a “Deploy” type to the Codefresh YAML manifest file:

YAML

RunningDeployScript:
    title: Running Deploy Script
    type: deploy
    kind: kubernetes
    cluster: '<cluster_name>' #the name specified when you added the cluster
    namespace: <namespcae_name> #the namespace you wish to deploy into
    service: <service_name> #the service you would like to update the deployment in
    candidate:
      image: '${{BuildImage}}'
      registry: 'dockerhub'

The full Codefresh YAML looks like this:

YAML

version: '1.0'
steps:
  BuildImage:
    type: build
    image_name: '<your_docker_repo>/<your_image_name>'
    dockerfile: Dockerfile
    tag: '${{CF_BRANCH_TAG_NORMALIZED}}'
    
  PushToDockerRegistry:
    type: push
    candidate: '${{BuildImage}}'
    tag: '${{CF_BRANCH_TAG_NORMALIZED}}'
    registry: 'dockerhub' #the name of the registry you added to Codefresh
    
  RunningDeployScript:
    title: Running Deploy Script
    type: deploy
    kind: kubernetes
    cluster: '<cluster_name>' #the name specified when you added the cluster
    namespace: <namespcae_name> #the namespace you wish to deploy into
    service: <service_name> #the service you would like to update the deployment in
    candidate:
      image: '${{BuildImage}}'
      registry: 'dockerhub'

You can now run the whole pipeline that builds your application from source to a docker image, pushes it to a docker registry and deploys it to your Kubernetes cluster.