• Stars
    star
    111
  • Rank 314,510 (Top 7 %)
  • Language
    Lua
  • Created almost 6 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Only to show how we can use canary deployment using only default components of Kubernetes.

Kubernetes Canary Deployment Example

Only to show how we can use canary deployment using only default components of Kubernetes. (It's a demo, if you think a better solution use Istio. I'll do a demo-app using Istio soon)

Using Ansible + AWS + Docker + Kubernetes + Helm + Grafana + Prometheus and one demo-app running OpenResty + Nginx with LUA script to expose metrics to Prometheus.

HOWTO

https://www.youtube.com/watch?v=CTvsdWZrAW0

These playbooks will do:

  • Install 03 Ubuntu instances EC2
  • Install Docker all nodes
  • Install kubeadm, kubectl and kubelet all nodes
  • Create a Kubernetes cluster with 01 master and 02 workers
  • Install helm
  • Install Prometheus
  • Install Grafana
  • Create two images (Version 1.0.0 and 2.0.0) to run OpenResty + Nginx exposing the metrics to Prometheus
  • Deploy in the k8s the app version 1.0.0 with ten replicas
  • Deploy a canary deployment of app version 2.0.0 with 01 replicas (represent 10% of my service)
  • Deploy the new version and remove the old one

Using canary deployment, you can deploy a new version of your app without downtime. For example, you can implement a new version of your app and split the request between both versions. You can redirect your requests where 10% go to the new version, and 90% go to the current version. So, you can check the stability of the latest release in the production environment, and after that, you can determine if you go to the new version in production or not.

Look an example of a dashboard with all requests to the app during deploy of the new version.

Canary Deployment

Prerequisites

You need to install Ansible in your computer and have an account into AWS. If you need to run it in another cloud your need to change the provisioning playbook.

To create the instance is needed that you set two environment variables, AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID, with your AWS account info.

export AWS_ACCESS_KEY_ID="SHDJSJHDJBNTTS"
export AWS_SECRET_ACCESS_KEY="hSs8s8282kkdbJzUdddd/ss/o+ser"

Install PIP:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

Install boto3 and ansible:

pip install ansible
pip install boto3

You need also have a key pair file, for example my-key-pair.pem, that is used to connect into your AWS instances.

Installing

After you create the variables, you need to clone this repo.

git clone <repo>

Access the playbook directory:

cd k8s-canary-deploy-example/provisioning-playbook/

Open the file roles/common/vars/main.yml, and edit any option that you like, for example, the name of your key pair file from AWS.

instance_type: t2.medium #type of instance
security_group: giropops-cluster #name of security group that will be created
image: ami-0d2505740b82f7948 #ami Ubuntu 18.04 TLS official
keypair: my-key-pair.pem #your key pair file
region: us-east-1 # region that will be created the instances
count: 3 #number of instances

Load your key pair "my-key-pair.pem" that you specified in the in the var file using ssh-keyscan.

ssh-add path/of/my-key-pair.pem

Execute the playbook:

ansible-playbook -i hosts main.yml

When the playbook finishes, you can check in hosts file the IP of new instances:

cat hosts
[local]
localhost ansible_connection=local ansible_python_interpreter=python gather_facts=False

[giropops]
IP_EXTERNAL-NODE1
IP_EXTERNAL-NODE2
IP_EXTERNAL-NODE3
IP_INTERNAL-NODE1
IP_INTERNAL-NODE2
IP_INTERNAL-NODE3

Great! You have 03 new instances created in your AWS account. Now, you need to get the IPs and fill the hosts inventory file that exists inside the all other playbooks, like the example below:

cat hosts
[k8s-master]
IP_EXTERNAL-NODE1

[k8s-workers]
IP_EXTERNAL-NODE2
IP_EXTERNAL-NODE3

[k8s-workers:vars]
K8S_MASTER_NODE_IP=IP_INTERNAL-NODE1
K8S_API_SERCURE_PORT=6443

After that, you need to go to the k8s-install-playbook directory:

cd k8s-canary-deploy-example/k8s-install-playbook/

Execute the playbook:

ansible-playbook -i hosts main.yml

Now you have a Kubernetes cluster with 03 nodes and Helm, Prometheus and Grafana installed. You can check the Grafana port that is exposed in the master node.

kubectl get services -n monitoring

To access grafana, use you IP_EXTERNAL-NODE:NODEPORT in your browser.

user: admin
password: admin

Create a new data source to get metrics from Prometheus

Name: Prometheus Server
Type: Prometheus
Url: http://IP_INTERNAL-NODE1:9090
Access: Server

Save and Test!

Now, let's go to create a dashboard.

Type: Graph
Query sum(rate(nginx_http_requests{app="giropops"}[5m])) by (version)
Legend : {{version}}
Draw Modes: Bars
Stack
Percent

Save!

Now, in another terminal execute the command to simulate request the app and let it run to generate requests to us visualize in the Grafana dashboard :

while true; do curl http://54.162.92.118:32222/ ; done

Let's deploy the app version 1.0.0:

cd k8s-canary-deploy-example/deploy-app-v1-playbook/

Execute the playbook:

ansible-playbook -i hosts main.yml

Now, you can see the output of curl that you start to receive a message:

Giropops App - Version 1.0.0

Let's keep the app version 1.0.0 running for 15 minutes, only collect data enough to our Grafana dashboard, like this:

Deploy app version 1.0.0

Now, let's deploy the canary deployment of app version 2.0.0. In this example, we have ten replicas running the version 1.0.0, so, we need to deploy the canary deployment that will represent around 10% of the number of replicas of the app. Now we will have ten replicas running the version 1.0.0 e only one replica running 2.0.0. In other words, we will have 90% of the requests to 1.0.0 and 10% of all requests arriving in the version 2.0.0.

Let's deploy the canary deployment:

cd k8s-canary-deploy-example/canary-deploy-app-v2-playbook/

Execute the playbook:

ansible-playbook -i hosts main.yml

Let's keep the app both versions running for 15 minutes, only to collect more data to our Grafana dashboard.

Canary app version 2.0.0

Ok, everything is right and working with our new version. Now, we want to replace entire the version 1.0.0 by the version 2.0.0.

Let's to deploy the app version 2.0.0:

cd k8s-canary-deploy-example/deploy-app-v2-playbook/

Execute the playbook:

ansible-playbook -i hosts main.yml

Deploy app version 2.0.0

Deploy app version 2.0.0

Great! Now we have the new version running in production and the best part, using canary deployment to prevent downtime of our app.

TODO LIST

  • Create a PV and a PVC to Grafana and Prometheus
  • Create a pipeline using Jenkins/Gitlab
  • Create new dashboards

More Repositories

1

DescomplicandoKubernetes

Shell
3,721
star
2

DescomplicandoDocker

Descomplicando o Docker, o livro.
HTML
3,270
star
3

giropops-monitoring

Full stack tools for monitoring containers and other stuff. ;)
Dockerfile
1,319
star
4

CertifiedContainersExpert

1,170
star
5

MutiraoDevOps

Go
970
star
6

DescomplicandoPrometheus

Repositório do treinamento Descomplicando o Prometheus da LINUXtips
CSS
501
star
7

BondeDoCKA

396
star
8

DescomplicandoHelm

Smarty
269
star
9

cheatsheet

Repo com as cheatsheet da LINUXtips!
227
star
10

descomplicando-ansible-2020

Shell
226
star
11

DescomplicandoArgoCD

CSS
212
star
12

DescomplicandoGitlab

167
star
13

DevOpsExtreme

Repositório com palestras e arquivos relacionados ao ev
HCL
148
star
14

badtuxx

133
star
15

convencendo-seu-chefe

122
star
16

DescomplicandoNomad

HCL
83
star
17

DescomplicandoGit

83
star
18

giropops-senhas-labs

Esse repositório foi criado com as maravilhosas pessoas que estavam no chat da live da LINUXtips na Twitch.
CSS
79
star
19

terraform-101

HCL
75
star
20

LINUXtips

69
star
21

descomplicando-ansible-final

68
star
22

bondedoPI

Stack DevOps / GitOps rodando em 07 Raspberries PI
53
star
23

prometheus_alpine

Prometheus running on Alpine Linux.
Dockerfile
41
star
24

k8s-deploy-nginx-example

36
star
25

ansible-course

31
star
26

create-operators-with-ansible

Makefile
28
star
27

ingress

28
star
28

giropops

27
star
29

giropops-senhas

CSS
26
star
30

domingao

HCL
26
star
31

node-exporter_alpine

Prometheus Node-Exporter running on Alpine Linux.
Dockerfile
24
star
32

ebooks

24
star
33

alertmanager_alpine

Prometheus AlertManager running on Alpine Linux.
Dockerfile
23
star
34

talks

Dockerfile
22
star
35

tekton-examples

20
star
36

katacoda-scenarios

Katacoda Scenarios
Shell
17
star
37

DescomplicandoCrossplane

15
star
38

hpd-wp

Repository created to my students during HPD training. It's an Ansible playbook to install Wordpress with MariaDB, PHP-FPM, and Nginx.
PHP
15
star
39

blackbox-exporter_alpine

Dockerfile
14
star
40

simple-index

very simple index.php
13
star
41

DescomplicandoeBPF

12
star
42

DescomplicandoChaos

11
star
43

DevChef

11
star
44

ebook-External-Secrets-Operator-K8s-Vault

10
star
45

QuebrandoEntrevistas

9
star
46

testing

5
star
47

DescomplicandoOllama

3
star
48

charts-example

Smarty
1
star