Learning: Kubernetes – Service, Scalability, Rolling Updates

Service

There multiple challenges with pods. Suppose we have two pods with one front-end and one back-end. Now we have couple of questions –

  • How does the front-end app expose to the outside world?
  • How the front-end app talks to the back-end app?
  • When a pod dies a new pod gets created and get assigned with a new IP Address. How to resolve Pod IP changes, when pod die?

So, the services are the way of grouping of pods in a cluster. We can have as many as services in cluster. There are mainly three type of services in k8s –

  1. ClusterIP – It actually deals with the pod IP change problem. It’s a static IP address that can be attached with each pod. So even the pod dies the service stays in place and don’t change. Exposes the Service on an internal IP in the cluster. Here the service only reachable within the cluster.
  2. NodePort – Makes a Service accessible from outside the cluster.
  3. Load Balancer – Creates an external load balancer in the current cloud (if supported) and assigns a fixed, external IP to the Service.
# Get the running services
$ kubectl get services

# Expose service to outside of the world
$ kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080

# Delete a specific service
$ kubectl delete service -l app=kubernetes-bootcamp

Scalability

When we want to scale our app then we create multiple replica of the pods on the nodes and to balance the request on each pods we use load balancer service.

Rolling Update

K8s allows us to do rolling updates and let’s see how it does –

  • First it creates the new pod with updated config.
  • Then it replaces the new pods with old ones one by one and change the pod IP addresses.

It allows app to update in zero down time.

# Update image of the application
$ kubectl set image

# Get Rollout update status
$ kubectl rollout status <service_name>

# Get the service info
$ kubectl describe <service_name>

# Roll Back to the deployment to your last working version
$ kubectl rollout undo <deployment_name>
Advertisement

Don’t Be Afraid Of Being A Beginner

In this blog I am going to share my journey about from where I started and how I started and as well as where I am now.

Starting College Life

Well I got into Diploma in year 2017 in Electronics And Telecommunication stream. As like other students I just took engineering and didn’t had any knowledge of which branch I should take and where I want to see me in future. The 1st semester just crossed with just academic studies related to curriculum.

How It Began

In the 2nd semester one day I came to college and got to know that there is a cyber security seminar that happened and as usual I was pretty late so I missed the seminar but one of my friend came in the class room and was discussing about what he listened in the seminar. I asked him about what happened there and he told that the person came was taking about web and particularly python which is a very easy to learn language and has very good demand in the job market. Also he gave me the person’s phone number. So, I was pretty curious about the python for the whole day. I went to home and pinged the person “Hi, I want to learn python and how I can start?”. He replied “You can start with python itself.”.

So I opened the YouTube and searched the Python tutorial and got this playlist. And I started learning it. While going through the playlist I started doing research about the python about what I can do and what are the areas I can work after learning the basics. So I got to know about a lot of new terms including machine learning, web development, back end development. Then I made a small calculator with the help of a blog from the internet and I couldn’t understand the codes at that time because I was pretty new so I copied a lot of code. Also I got to know about frameworks like Django and flask and I was not sure which one should I pick. Then I finally finished the playlist and picked Django and started learning it from YouTube. And at that point of time I also got to know about Git & GitHub. I opened my GitHub account and starts pushing whatever I was learning from the YouTube and blogs.

How I started Open Source Contribution

Then I got to know about Open Source and contribution to where I can get experience in writing industry standard codes. So again I start searching in the web and I got few answers about “How to contribute to open source?” and I got one answer from Sayan from Quora. I gathered a lot of information from this post and was searching through all the code base of these project but somehow nothing was working for me. So I pinged Sayan on Instagram. He then guided me to pick one project from Mozilla which is pontoon then I joined their IRC channel and asked the maintainer if he can pick me one issue so that I can work on. He gave me one issue and eventually after discussing with maintainer I solved the issue and opened my first PR. It was quite hard for me first time figuring out where to start and what to do but the feelings after solving a issue gives you a whole lot of joy and confidence. Then in the pontoon project I solved a quite few other issues.

Doing DGPLUG Summer Training

After that I got to know about a summer training where Sayan and few other folks from different open source project conduct in order to bring people into open source. So I joined the #dgplug channel in the freenode. I joined the summer training the year 2019 and it was awesome in one word because I got to know a lot of insights of various technologies like git and ssh and how asymmetric and symmetric encryption works etc. Also they encouraged me to start writing blogs. The main thing was I got to know a lot of new people from the community like Kuntal, Pradvan, Priyanka, Pravar and lot more.

Attending Pycon India

While going to college I was talking to Sayan and he told me to attend the Pycon India and I came home and talked to my parents and asked if I can go. They said yes and I went there in Chennai and It was an unique experience in itself. I wrote a blog post on pycon India experience.

2020 Pandemic Start 😦

And it was my one of the worst year of my life completely sitting in home for 1 year and also I regret not doing/learning anything new in this year. I just skipped everything this year from studies to programming.

2021 BTech started

Yes I am in the middle of pandemic batch. I started my btech and from here a lot have changed in my life I shifted my stream from ETCE in Diploma to CSE in Btech. The 3rd(Lateral) was very short due to pandemic we just got 1.5 months to complete all the subjects and then semester exam. After that one day I was talking to Sayan on his company got acquired by Microsoft he told me about the essence of learning the core subjects of Computer Science beside doing coding all the time. And it was a life saving conversation to me because I was about to skip the subjects like Automata and Compiler. I will tell be What I have studied so far –

  • Computer Organization & Architecture – This is one of the most important subject in the curriculum. I studied it mostly by watching various YouTube channels like GateSmashers.
  • Discrete Math – From the beginning I am not that good in math and I also didn’t paid much attention in this subject to be honest.
  • Automata – Yep! this subject I have studied from various YouTube channels and I used to solve question regarding NFA, DFA, Context Free Grammar, Turing Machine etc.
  • Compiler – I started this subject with lot of motivation but it gradually decreased as I was having problem a lot in understanding the subject from the Dragon Book. I still have plans to improve this subject in future and make my own compiler.
  • Operating System – This is the subject I love the most in my entire curriculum. First I completed the Galvin’s book on OS. Then I watched several YouTube videos of different topics in the OS. The most interesting part in the OS was semaphores.
  • Object Oriented Programming – This is again one of the most important subject if you want to write complex code and design large scale systems. I knew the basic OOPs concepts already so I started with this book. There I got to know a lot about design patterns like Singleton Design pattern, Factory design pattern etc.
  • DBMS – It is again one of my most favorite subject. I knew the basics of DBMS as I used to develop applications using Django. But I was lacking in the inner concepts of DBMS like how it handles concurrency and how it do recovery etc. And also I got to know a lot about complex Join queries and how these works. I watched playlist by Knowledge gate where Sanchit Jain sir have explained each and every topic very clearly.
  • Computer Network – This is the subject I am currently studying and the most amazing this whatever I study in this subject I use day to day in my life. I started learning Networking using Ravindrababu Ravula channel and after finishing it I started the Knowledge Gate playlist. And also side by side me Sayan, Nabarun and few other folks sir regularly and study the CN from the book CompTIA Network+ Certification All-in-One Exam Guide, Eighth Edition (Exam N10-008), 8th Edition. Also we are planning to start doing practical using whatever we studied in CN.

Learning Golang and Contributing to Flatcar Linux and Kubernetes

When Sayan’s company got acquired by the Microsoft he told that his entire product is open source and it is related to Operating Systems. I was pretty exited about it because I always wanted to contribute to some OS projects. I joined their Matrix channel and introduced myself. After getting into the project I talked to the maintainers and picked one issue. But the main thing was I had to learn about Golang and I started learning it. After learning the basics of Golang I started tinkering with the issue and worked on something that interested me very much in the OS was semaphores. After several discussions with the maintainer I solved the issue and here is the PR also I gave a talk in their monthly community call.

After that I kept contributing to the OS and fixed several issues. The OS has an immutable file structure so the organization has to deliver the required software for the community and is responsible for updating and adding new applications. I added and upgraded quite a few applications as well from the Gentoo ebuild repository.

Later in the Flatcar Linux the most interesting and challenging thing that I worked on is that I integrated the Fleetlock Protocol in their locksmith project. The idea was that locksmith was highly bound to etcd. So the users who want to have a cluster reboot coordination needs to use etcd. The idea is to implement a FleetLock client into the locksmith. So the Fleetlock will stay in the middle and whichever application supports the Fleetlock client will be able to interact with the Locksmith and will be able to reboot a set of clusters. I also gave a talk about this too in their community call.

Still I was lacking a lot of knowledge in Golang but thanks to Nabarun he helped me understanding a lot of design patterns while designing a Go application and helped me understand about the interface and made a key-value storage. I also wrote a blog about the interfaces. Currently I am contributing to CAPG project and looking forward to contribute more in this and other Kubernetes project in future. Currently I am learning Kubernetes and System Design and giving a lot of interview these days for the internships.

I am still improving myself everyday by learning various concepts in the CS and interconnecting each other and applying in the projects whenever possible. But If I look back and see myself, I can clearly see my improvement from the past years. As Steve Jobs once said “You can’t connect the dots looking forward; you can only connect them looking backward“.

Learning: Kubernetes – Pods and ReplicaSet Simplified

Pods

A Pod is the smallest execution unit of a Kubernetes application. Each Pod represents a part of a workload that is running on your cluster.

We usually have one pod per application. Inside that pod we could have multiple container.

  • A Pod is a Kubernetes abstraction that represents a group of one or more application containers and some shared resources.
    • It has shared volumes.
    • Cluster IP(Every pod has unique IP even in same Node)
    • Info about how to run container.
  • We don’t deal with containers instead we work with pods.
  • If a container dies inside a pod it will be automatically restarted.
  • Each pod is tied to one node until termination.
  • Pods that are running inside k8s are only visible from other pods and services inside the k8s cluster.
  • We have to expose the app to outside the k8s.

Multiple Container Pods – The pods are always designed to support multiple correlated containers. The containers in a pod is automatically scheduled in same VM or physical machine in the cluster.

The containers can communicate to each other and share resources.

Pods Networking –

  • Each pod is assigned with a unique IP address.
  • Each container in pods share the network share the same IP address with port.
  • The containers inside a pod can communicate to each other with localhost.
  • The containers inside a pod can also communicate using Inter Process Communication.

Life Cycle of a Pod

  • A pod is said to be ephemeral.
  • A pod is never rescheduled to a different node Instead the pod is replaced by a new one.
  • If a node fails the pods assigned to it also fail.

Generally a pod has 5 phases –

  1. Pending – Pod has been accepted by the cluster but one or more container haven’t been setup.
  2. Running – A pod has been bound to a node and containers have started.
  3. Succeeded – All containers in the pod have been terminated successfully.
  4. Failed – At least one container have been terminated in failure.
  5. Unknown – For some reason the state of pod could not be obtained.
# Create a deployment
$ kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1

# Get deployment info
$ kubectl get deployments

# Get the list of pods running
$ kubectl get pods

# See which containers are running inside a pod
$ kubectl describe pods

# Run a command inside a container
$ kubectl exec $POD_NAME -- env

# Open bash inside a container
$ kubectl exec -it $POD_NAME -- bash

ReplicaSet

We don’t create the pods directly. The reason is suppose we need 4 pods in our deployment always and if we create the pods directly and the one pod goes down then we have to create the pods manually.

That’s why we use ReplicaSet. It is a management system that ensure that I have the desired set of pods in the k8s cluster. And the controller check the current state with the desired state and see if the current pod count match the ReplicaSet count or not. If not it creates or deletes pods.

# Get the replica set
$ kubectl get rs

# Scale up the app and change replicaset
$ kubectl scale deployments/kubernetes-bootcamp --replicas=4

# Scale down the app
$ kubectl scale deployments/kubernetes-bootcamp --replicas=2

# To see a pod in managed by ReplicaSet
$ kubectl get pods <pod_name> -o yaml

# Delete the ReplicaSet
$ kubectl delete rs <replica_name>

# Delete the replica set but keep the pods
$ kubectl delete rs <replica_name> --cascade=false