#040 Kubernetes - Deployment Lab 2
Tags: kubernetes, devops, tutorial
Published: 2020-07-25 21:38:50

# Introduction
![intro](https://media1.tenor.com/images/f15057a41413c960db1f328ca5a732e3/tenor.gif?itemid=6203225)
this is part 40 from the journey it's a long journey(360 day) so go please check previous parts , and if you need to walk in the journey with me please make sure to follow because I may post more than once in 1 Day but surely I will post daily at least one 😍.

And I will cover lot of tools as we move on.
***

# Prepare images for this Lab

first let's delete every thing we done so far
![delete](https://dev-to-uploads.s3.amazonaws.com/i/zq21pn106mvhnrpr1tve.png)
```bash
kubectl delete deployment myapp-deployment
kubectl delete rs myapp-replicaset
```
let's recreate a new container from scratch will be hosted on my dockerhub. It's same as old one but now we need to do some edits to the image. [here](https://dev.to/elkhatibomar/012-upload-to-docker-hub-3640) how we can build and push to dockerhub.
I am going to build them as tags 1.0 and 2.0
1.0 is same as old one , 2.0 will have some edits on image.
So we can see how to switch between two versions.
it's not my API so it doesn't have an docker-compose file.
We can add one but meh let's just build it without docker-compose because it's doesn't have a lot to do with it.

if you already have the DevOps folder just pull it and you will get the new files.
if not go and clone the [repo](https://github.com/OmarElKhatibCS/DevOpsJourney/)
***

# Lab 2
Now after we have our image with tag 1.0 and 2.0 we can start.
You don't need to build them , because I already do they are on my docker hub as public just follow the steps , Kubernetes will download them for you when you run the yml file.
![mad](https://media1.tenor.com/images/4800838e793768298c7d6132734abca4/tenor.gif?itemid=11957205)

first let's look what we have in the folder of today lab
![files](https://dev-to-uploads.s3.amazonaws.com/i/ezl8akweamq4qbgaxrax.png)
v1 and v2 contain the source code of the simple-api , v2 have different message than v1 that will shown.
app_040_v1.yml  app_040_v2.yml are the Kubernetes configs the only difference between 1 and 2 it's the version in image here a look at v1
![v1](https://dev-to-uploads.s3.amazonaws.com/i/kc7iq6nlnynrgqil4l53.png)

let's create our deployment now.

![create](https://dev-to-uploads.s3.amazonaws.com/i/aexwhx29r22rd2cs6huv.png)

```bash
kubectl create -f app_040_v1.yml --record
```
you know the process from the older part , he going to create 6 pods and...
take a break while the pods are creating
![create](https://dev-to-uploads.s3.amazonaws.com/i/2za1zppwtsnurdvg9iyg.png)
```bash
kubectl get pods
```
all my pods are ready , so we are good to move on
![run](https://dev-to-uploads.s3.amazonaws.com/i/b951pv4lzoboc500fawu.png)
```bash
kubectl port-forward deployments/myapp-deployment 8080
```
this trick will start our app , 8080 is the port we specify in the source code.
![browser](https://dev-to-uploads.s3.amazonaws.com/i/4ycb5bzzogjfhi0q8819.png)
Now we can see in browser we have our v1 running.
![apply](https://dev-to-uploads.s3.amazonaws.com/i/ti07uzbinujgko878ic8.png)
```bash
kubectl apply -f app_040_v2.yml
```
this is how we update the version from v1 to v2
![describe](https://dev-to-uploads.s3.amazonaws.com/i/ozibugqyz3yws3a2ldjw.png)
```bash
kubectl describe deployments myapp-deployment
```
here we can see we have the new version 2.0
![run](https://dev-to-uploads.s3.amazonaws.com/i/b951pv4lzoboc500fawu.png)
```bash
kubectl port-forward deployments/myapp-deployment 8080
```
now let's go to browser and refresh our page and see what happens.
![v2-live](https://dev-to-uploads.s3.amazonaws.com/i/qy4q0vbuwusqjquhdbkb.png)
voila we have now the v2 live!
***

# little talk about DevOps
I edit the app in this lab , the moment between the commit and the customer see the updates is the DevOps Engineer. In advanced parts when we push our code all those stuff will be automated , it's called Contentious Integration and Contentious Delivery as shortcut CI/CD.
In our case the task will be after commit he will send the version to the docker hub then Kubernetes will handle the new image.
The automation here will be described in a script written by you (the DevOps).
Basically this is the core of the DevOps.
***

# Back to lab
let's say now we don't like this version we need to roll back to v1.
let's look at the history and decide what we need to rollback
![his](https://dev-to-uploads.s3.amazonaws.com/i/1hmgwqy80vhx0il53de8.png)
```bash
kubectl rollout history deployment/myapp-deployment
```
I need the revision 1 , so I will go back to it using : 
![back-v1](https://dev-to-uploads.s3.amazonaws.com/i/qhxz2lz4xyfdb1gl34sl.png)
```bash
kubectl rollout undo deployment/myapp-deployment --to-revision 1
```
now let's check what version it is
```bash
kubectl describe deployments myapp-deployment
```
![v1.0](https://dev-to-uploads.s3.amazonaws.com/i/pbqac3emie51gf2gg7tw.png)
here we can see now the version is back to 1
```bash
kubectl port-forward deployments/myapp-deployment 8080
```
now let's go to browser and refresh our page and see what happens.
![browser](https://dev-to-uploads.s3.amazonaws.com/i/4ycb5bzzogjfhi0q8819.png)
It's the old version again!