DevOps tutorials
Contact us

Getting started with DevOps with Kubernetes

Summary

  1. Introduction
  2. Prerequisite
  3. Preparation
  4. Gitlab environment
  5. Kubernetes environment
  6. Container registry
  7. CI/CI Pipeline

Introduction

The goal of this tutorial is to explain how to create a CI / CD pipeline in order to deploy an application in Kubernetes running on top of Alibaba Cloud.

The procedure can be summarized in two mains steps:

Prerequisite

The very first step is to create an Alibaba Cloud account and obtain an access key id and secret.

Cloud resources are created with Terraform scripts. If you don’t know this tool, please follow this tutorial and familiarize yourself with the alicloud provider.

Please also make sure you are familiarized with Kubernetes. If you need, you can follow this awesome tutorial to learn the basics. You will also need to setup the command line tool ‘kubectl’.

You should also have Git installed on your computer.

Preparation

Please download the related resources on your computer by cloning this Git repository. Open a terminal and enter the following commands:

# Navigate to a folder where you want to clone this tutorial
cd ~/projects

# Clone this repository
git clone git@github.com:alibabacloud-howto/devops.git

# Navigate to the folder of this tutorial
cd devops/tutorials/getting_started_with_devops_with_kubernetes/

Gitlab environment

This tutorial uses Gitlab to manage Git repositories and to run CI/CD pipelines. The community edition is free, simple to use and have all the features that we need for this demo.

Enter the following commands in your terminal with your own access key and region information:

export ALICLOUD_ACCESS_KEY="your-accesskey-id"
export ALICLOUD_SECRET_KEY="your-accesskey-secret"
export ALICLOUD_REGION="your-region-id"

cd environment/gitlab
terraform init
terraform apply -var 'gitlab_instance_password=YourSecretR00tPassword'

Note: this script is a bit too simple to be used in production, for example a SSL certificate should be configured to allow HTTPS. Here is a more complete tutorial about Gitlab installation.

The output of the script should contain the IP addresses of the newly installed Gitlab instance and a Gitlab runner:

Outputs:

gitlab_runner_public_ip = w.x.y.z
gitlab_public_ip = a.b.c.d

Please open the page “http://a.b.c.d” (from gitlab_public_ip) in your web browser and:

You should be able to see a welcome screen. You can now generate and upload a SSH key:

The next step is to register the Gitlab runner (the ECS instance that runs CI/CD scripts):

Go back to the home page by clicking on the Gitlab icon on the top-left side of the screen and keep this web browser tab opened, we will return to it later.

Kubernetes environment

To keep it simple, this tutorial creates a single-AZ cluster. However, a multi-AZ one is preferred for production. Please read the official documentation for more information.

Open a terminal and enter the following commands with your own access key and region information:

export ALICLOUD_ACCESS_KEY="your-accesskey-id"
export ALICLOUD_SECRET_KEY="your-accesskey-secret"
export ALICLOUD_REGION="your-region-id"

# Navigate to the folder where you have cloned this tutorial
cd ~/projects/devops/tutorials/getting_started_with_devops_with_kubernetes/

cd environment/kubernetes
terraform init
terraform apply -var 'k8s_password=YourSecretR00tPassword'

Note: it takes about 20min to create a Kubernetes cluster.

The output of the script should contain the master node public IP address:

Outputs:

k8s_master_public_ip = e.f.g.h

Execute the following commands in order to configure “kubectl” (the password is the one you set with the variable k8s_password):

mkdir ~/.kube
scp root@e.f.g.h:/etc/kubernetes/kube.conf ~/.kube/config # The IP address is the one from `k8s_master_public_ip`

# Check that it works
kubectl cluster-info

If the configuration went well, the result of the last command should be something like:

Kubernetes master is running at https://161.117.97.242:6443
Heapster is running at https://161.117.97.242:6443/api/v1/namespaces/kube-system/services/heapster/proxy
KubeDNS is running at https://161.117.97.242:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
monitoring-influxdb is running at https://161.117.97.242:6443/api/v1/namespaces/kube-system/services/monitoring-influxdb/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

Container registry

Unfortunately it is not yet possible to create a container registry on Alibaba Cloud via Terraform. Instead, this step must be done manually through the web console:

Keep this page opened in the web browser, we will need to create a repository in the next step.

CI/CD Pipeline

This tutorial uses a very simple Spring Boot app as an example. You can find the source code in the folder app/simple-rest-service.

Docker image repository

The first step is to create a repository where Docker images will be saved. Open you web browser tab from the Container registry section and execute the following instructions:

Gitlab project

Open the web browser tab you created in the Gitlab environment section and create a new project:

The project repository is now ready to host files:

Gitlab automatically recognizes the file “.gitlab-ci.yml” and create a pipeline with 3 steps:

  1. Compile and execute unit tests;
  2. Create the Docker image with JIB and upload it to the Docker image repository;
  3. Deploy the application in Kubernetes.

You can see the pipeline in Gitlab by selecting the item “CI / CD > Pipelines” from the left menu.

After your pipeline has been executed completely, you can check you Kubernetes cluster with the following commands:

Test the application by yourself: