DEV Community

Cover image for #040 Kubernetes - Deployment Lab 2
Omar
Omar

Posted on

#040 Kubernetes - Deployment Lab 2

Introduction

intro
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

kubectl delete deployment myapp-deployment
kubectl delete rs myapp-replicaset
Enter fullscreen mode Exit fullscreen mode

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 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


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

first let's look what we have in the folder of today lab
files
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

let's create our deployment now.

create

kubectl create -f app_040_v1.yml --record
Enter fullscreen mode Exit fullscreen mode

you know the process from the older part , he going to create 6 pods and...
take a break while the pods are creating
create

kubectl get pods
Enter fullscreen mode Exit fullscreen mode

all my pods are ready , so we are good to move on
run

kubectl port-forward deployments/myapp-deployment 8080
Enter fullscreen mode Exit fullscreen mode

this trick will start our app , 8080 is the port we specify in the source code.
browser
Now we can see in browser we have our v1 running.
apply

kubectl apply -f app_040_v2.yml
Enter fullscreen mode Exit fullscreen mode

this is how we update the version from v1 to v2
describe

kubectl describe deployments myapp-deployment
Enter fullscreen mode Exit fullscreen mode

here we can see we have the new version 2.0
run

kubectl port-forward deployments/myapp-deployment 8080
Enter fullscreen mode Exit fullscreen mode

now let's go to browser and refresh our page and see what happens.
v2-live
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

kubectl rollout history deployment/myapp-deployment
Enter fullscreen mode Exit fullscreen mode

I need the revision 1 , so I will go back to it using :
back-v1

kubectl rollout undo deployment/myapp-deployment --to-revision 1
Enter fullscreen mode Exit fullscreen mode

now let's check what version it is

kubectl describe deployments myapp-deployment
Enter fullscreen mode Exit fullscreen mode

v1.0
here we can see now the version is back to 1

kubectl port-forward deployments/myapp-deployment 8080
Enter fullscreen mode Exit fullscreen mode

now let's go to browser and refresh our page and see what happens.
browser
It's the old version again!

Top comments (0)