Kubernetes deployment tutorial
This tutorial shows you how to deploy a publicly-available NGINX Docker image and manifest into your Kubernetes cluster using a rolling update strategy in Harness.
Objectives
You'll learn how to:
- Install and launch a Harness Kubernetes Delegate in your target cluster.
- Set up a Kubernetes Pipeline.
- Run the new Kubernetes Pipeline and deploy a Docker image to your target cluster.
Before You Begin
Make sure you have the following set up before you begin this quickstart:
- GitHub account: this quickstart uses a publicly available NGINX manifest and Docker image. DockerHub allows anonymous connections, but GitHub requires that you log into your account to access their repos.
Set up your Kubernetes Cluster
You'll need a target Kubernetes cluster for the Harness Delegate and deployment. Ensure your cluster meets the following requirements:
- Number of nodes: 2.
- vCPUs, Memory, Disk Size: 4vCPUs, 16GB memory, 100GB disk. In GKE, the e2-standard-4 machine type is enough for this quickstart.
- Networking: outbound HTTPS for the Harness connection to app.harness.io, github.com, and hub.docker.com. Allow TCP port 22 for SSH.
- A Kubernetes service account with permission to create entities in the target namespace is required. The set of permissions should include
list
,get
,create
, anddelete
permissions. In general, the cluster-admin permission or namespace admin permission is enough.
For more information, see User-Facing Roles from Kubernetes.
Step 1: Create the Deploy Stage
Pipelines are collections of stages. For this tutorial, we'll create a new Pipeline and add a single stage.
Create a Project for your new CD Pipeline: if you don't already have a Harness Project, create a Project for your new CD Pipeline. Ensure that you add the Continuous Delivery module to the Project. See Create Organizations and Projects.
In your Harness Project, click Deployments, and then click Create a Pipeline.
Enter the name CD Quickstart and click Start.
Your Pipeline appears.
Click Add Stage and select Deploy.
Enter the name Deploy Service, make sure Service is selected, and then click Set Up Stage.
The new stage settings appear.
In About the Service, click New Service.
noteLet's take a moment and review Harness Services and Service Definitions (which are explained below). Harness Services represent your microservices/apps logically. You can add the same Service to as many stages as you need. Service Definitions represent your artifacts, manifests, and variables physically. They are the actual files and variable values.
By separating Services and Service Definitions, you can propagate the same Service across stages while changing the artifacts, manifests, and variables with each stage.
Give the Service the name nginx and click Save.
Once you have created a Service, it is persistent and can be used throughout the stages of this or any other Pipeline in the Project.
Step 2: Add the Manifest
Next, we can add a Kubernetes manifest for NGINX. We'll use the publicly-available manifest available from Kubernetes.
In Service Definition, in Deployment Type, click Kubernetes.
In Manifests, click Add Manifest.
noteWhat about Artifacts? In this quickstart we are using a publicly-available NGINX Docker image from DockerHub, and the location of the image is hardcoded in the public manifest from Kubernetes. The Artifacts section is only used when the public artifact is not hardcoded in the manifest or the repo is private. In those cases, you add the image in Artifacts with a Connector for the repo and then reference the image in your values.yaml (
image: <+artifact.image>
). See Add Container Images as Artifacts for Kubernetes Deployments.Select K8s Manifest, and click Continue.
In Select K8sManifest Store, click GitHub, and then click New GitHub Connector.
The Git Connector settings appear. Enter the following settings.
Name: enter a name for the Connector.
URL Type: select Repository.
Connection Type: select HTTP.
Git Repository URL: enter
https://github.com/kubernetes/website
.Username and Token: Enter the username and a Github Personal Access Token for your Github account. You'll have to create a Harness secret for the password.
- In Personal Access Token, click Create or Select a Secret.
- Click New Secret Text.
- In Secret Name, enter a name for the secret like github-pat.
- In Secret Value, paste in a GitHub Personal access token.When you're logged into GitHub, these are typically listed at https://github.com/settings/tokens. For steps on setting up a GitHub PAT, see Creating a personal access token from GitHub.Ensure you PAT has the repo scope selected:
Click Continue.
In Connect to the provider, select Connect through a Harness Delegate, and then click Continue. We don't use the Connect through Harness Platform option here simply because you'll need a Delegate later for the connection to your target Kubernetes cluster. Typically, the Connect through Harness Platform option is a very quick way to make connections without having to use Delegates.
In Delegates Setup, click Install new Delegate. The Delegate wizard appears.
Click Kubernetes, and then click Continue.
Enter a name for the Delegate, like quickstart, click the Small size.
Click Continue.
Click Download YAML file. The YAML file for the Kubernetes Delegate will download to your computer.
Open a terminal and navigate to where the Delegate file is located. You will connect to your cluster using the terminal so you can simply run the YAML file on the cluster.
In the same terminal, log into your Kubernetes cluster. In most platforms, you select the cluster, click Connect, and copy the access command. Next, install the Harness Delegate using the harness-delegate.yaml file you just downloaded.
In the terminal connected to your cluster, run this command:
kubectl apply -f harness-delegate.yaml
You can find this command in the Delegate wizard:
The successful output is something like this:
% kubectl apply -f harness-delegate.yaml
namespace/harness-delegate unchanged
clusterrolebinding.rbac.authorization.k8s.io/harness-delegate-cluster-admin unchanged
secret/k8s-quickstart-proxy unchanged
statefulset.apps/k8s-quickstart-sngxpn created
service/delegate-service unchangedIn Harness, click Verify. It will take a few minutes to verify the Delegate. Once it is verified, close the wizard.
Back in Set Up Delegates, you can select the new Delegate. In the list of Delegates, you can see your new Delegate and its tags.
Select the Connect using Delegates with the following Tags option.
Enter the tag of the new Delegate and click Save and Continue. When you are done, the Connector is tested.
Click Continue.
In Manifest Details, enter the following settings, test the connection, and click Submit. We are going to provide connection and path information for a manifest located at
https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/application/nginx-app.yaml
.- Manifest Identifier: enter nginx.
- Git Fetch Type**: select Latest from Branch**.
- Branch: enter main.
- File/Folder path:
content/en/examples/application/nginx-app.yaml
. This is the path from the repo root.
The manifest is now listed.
Click Next at the bottom of the Service tab.
Now that the artifact and manifest are defined, you can define the target cluster for your deployment.
Step 3: Define Your Target Cluster
The target cluster is your own Kubernetes cluster, hosted in your cloud environment. This is where we will deploy the Docker image using the manifest you selected.
Harness connects to all of the common cloud platforms and provides a platform-agnostic Kubernetes cluster connection that can connect to Kubernetes anywhere.
In Infrastructure Details, in Specify your environment, click New Environment. Just like with a Service, you can create a new Environment or selecting an existing one. We'll create a new one.
In New Environment, enter a name, select Pre-Production, and click Save. The new Environment appears.
In Infrastructure Definition, click Kubernetes.
noteLet's take a moment and review Harness Environments and Infrastructure Definitions. Harness Environments represent your deployment targets logically (QA, Prod, etc). You can add the same Environment to as many stages as you need. Infrastructure Definitions represent your target infrastructure physically. They are the actual clusters, hosts, etc.
By separating Environments and Infrastructure Definitions, you can use the same Environment in multiple stages while changing the target infrastructure settings with each stage.
An Infrastructure Definition is where you specify the target for your deployment. In this case, your Kubernetes cluster and namespace.
In Cluster Details, in Connector, click Select a connector.
Click New Connector.
The Kubernetes Cluster Connector appears.
The Kubernetes Cluster Connector is covered in detail here, but let's quickly walk through it.
Let's look at the steps:
In Kubernetes Cluster Connector, in Name, enter Kubernetes Quickstart, and click Continue.
In Details, select Use the credentials of a specific Harness Delegate. We will select the Delegate next.
Click Continue.
Select the Kubernetes Delegate you added earlier using its Tags, and then click Save and Continue. Harness verifies the Connector.
Click Finish.
Select the new Connector and then click Apply Selected.
Back in Cluster Details, in Namespace, enter the target namespace for the deployment. For example, default. You can use any namespace in your target cluster.
In Advanced, in Release name, enter
quickstart
. For a release name, you can use a built-in or custom expression or any text you like. Harness requires a release name for tracking. It is applied to the pods as a label. The release name must be unique across the cluster.When you are done, the Cluster Details will look something like this:
The target infrastructure is complete. Now we can add our stage steps.
Click Next to move onto Execution.
Step 4: Add a Rollout Deployment Step
Now you can select the deployment strategy for this stage of the Pipeline.
In Execution Strategies, select Rolling, and then click Use Strategy.
The Rollout Deployment step is added.
This is a standard Kubernetes rolling update. By default, Harness uses a
25% max unavailable, 25% max surge
strategy.
That's it. Now the Pipeline stage is complete and you can deploy.
Step 5: Deploy and Review
Click Save > Save Pipeline and then Run. Now you can select the specific artifact to deploy.
In Primary Artifact, select stable. This is the same as using
docker pull nginx:stable
.Click Run Pipeline. Harness will verify the Pipeline and then run it. You can see the status of the deployment, and pause or abort it.
Toggle Console View to watch the deployment with more detailed logging.Click the Rollout Deployment step and expand Wait for Steady State.
You can see
deployment "my-nginx" successfully rolled out
.
Congratulations! The deployment was successful.
In your Project's Deployments, you can see the deployment listed:
If you run into any errors, it is typically because the cluster does meet the requirements from Before You Begin or the cluster's network setting does not allow the Delegate to connect to Docker Hub.In this tutorial, you learned how to:
- Install and launch a Harness Kubernetes Delegate in your target cluster.
- Connect Harness to your Kubernetes cluster and an artifact server.
- Add your manifests to Harness.
- Create an Infrastructure Definition that targets your cluster and namespace.
- Add a Kubernetes rolling update.
- Deploy your Kubernetes Pipeline to your target cluster.
Next, try Harness Continuous Integration to build a codebase, upload it to a repo, and run unit and integrations tests: CI Pipeline Quickstart.
Clean Up
For steps on deleting the Delgate, go to Delete a delegate.
Next Steps
See CD tutorials for other deployment features.