Deployment Options for Kubernetes
Learn how to deploy to Kubernetes with the declarative deploy step
Codefresh offers a lot of options when it comes to Kubernetes deployments:
- Using the Codefresh GUI to deploy on demand. This is the easiest way and was described in the quick start guide.
- Using the dedicated deploy step in a pipeline. Explained in detail in the present page.
- Using the cf-deploy-kubernetes step in a pipeline. This can also perform simple templating on Kubernetes manifests.
- Using a freestyle step with Kustomize. Described in details in this page.
- Using a freestyle step with your own
kubectl
commands. This is very flexible, but assumes that you know how to work withkubectl
. Described in details in this page. - Using Helm as a package manager. See the Helm quick start guide for more details.
Prerequisites
It is assumed that
- You have already added your K8s cluster into Codefresh
- You are familiar with Codefresh YAML and basic pipeline steps and know how to describe it
- You know how to integrate your docker registry with Codefresh
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 following 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
- Go to the
Kubernetes
→Services page
. - Click the button “Add Service”.
- Select the cluster.
- Select the namespace.
- Type an arbitrary service name.
- Specify the number of replicas.
- Type the name of your pushed image.
- In the “Internal Ports” field specify the port which your application listens to.
- In the “Expose port” field specify the port to be exposed to the Internet and check the checkbox.
- 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.
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.