Kubernetes — Part 1

Architecture overview, connecting kubectl from terminal, commands to keep handy, Centralized logging and monioring GKE,

Nikitha Gullapalli
4 min readJul 5, 2023

GitHub: https://github.com/nikitha2/Currency-Exchange-Conversion-Microservices-Backend.git

Linkedin: https://www.linkedin.com/in/nikitha-gullapalli/

Why do we need Kubernetes? How is it different from Docker?

We know from here, docker is a light weight application to deploy our applications. Then why do we need Kubernetes?

Well, consider a scenario where you have a lot of traffic in the weekend and weekdays its back to normal. We need to scale some micro-services to support the traffic and scale down when it is back to normal. Kubernetes automatically handles this sceanrio for us. Using Docker, we need to increase/decrease the instances manually.

Additionally, Kubernetes provides gateway, load balancing and distributed tracking, service discovery, self healing and zero downtime deployment. This is less boiler plate code and less code maintenance.

Below are the different components of kubernetes. Lets look at what each one of these components contribute to make the kubernetes experience better.

Kubernetes architecture

There are several worker nodes and one manager node (for complex system can have more than 1). All these worker nodes are manages by the manager node.

Control plane Node (Master node): Manager node to manage all worker nodes

  • Distributed database (EDCD) - Store the required state/configurations of the micro services.
  • kube-apiserver - Any command we run with kubectl or in google console will be submitted to the kube-apiserver.
  • Scheduler — schedules the pods on the appropriate nodes (worker nodes)
  • kubelet : The Kubernetes agent that makes sure the workloads are running as expected, and registers new nodes to the apiserver. The kubelet runs on every node.

Worker node: Node where the actual work happens after master node schedules the work to it.

  • Node Agent -monitors whats happening in the worker node and communicates it back tot he master node.
  • Networking components (kube proxy) - helps in exposing servies around nodes and pods.
  • Container runtime — E.g: Docker. Used to run containers inside the pods
System layers with Kubernetes

A Kubernetes cluster is a set of nodes that run containerized applications. Containerizing applications packages an app with its dependences and some necessary services. They are more lightweight and flexible than virtual machines.

Note: Each worker node can have multiple pods and multiple containers

In this article, lets look at,

  • Run kubectl commands from terminal window
  • Commonly used command in Kubernetes.

Run kubectl commands from terminal

1.gcloud command line interface

  • Install gcloud CLI here
  • You can now select project and run your gcloud command from here
  • Some userful commands to keep handy
// to login to your gcloud user from terminal
gcloud auth login
//If already logged in and want to change project. Use below command
gcloud config set project <PROJECT_ID>

2.Run kubectl command line interface

  • Check if you already have kubectl installed
kubectl version
  • If not already installed. Can install from here.
kubectl version

Now you can run all your kubectl commands from your terminal :D

Simply connect your cluster and if you run kubectl version will give you both client and server versions of the kubectl.

Kubectl Commands to keep handy

  • kubectl get command to get information
#Gets all events
# "-o wide" at the end will add more informations like ip address
kubectl get events -o wide

#return events in sorted order by time
kubectl get events --sort-by=metadata.creationTimestamp

#Gets all pods
kubectl get pods

#Gets all replicaset
kubectl get replicaset

#Gets all deployment
kubectl get deployment

#Gets all service
kubectl get service

#Gets all component statuses
kubectl get componentstatuses

#Run a url in terminal. --watch will watch for result every 2 seconds
curl <url>

#Send the curl every 2 seconds
watch curl <url>
  • kubectl explain command to understand a topic
kubectl explain pods
  • kubectl describe to get more information
# Will give more information about the pod with is <podId>
kubectl describe pod <podId>
  • Delete
kubectl delete pod <podId>
  • Scale the replica set. Add 3 instances of the pod in the node. Will auto scale to maximum 10 instances.
kubectl scale deployment <deployment name> --replicas=3 --max=10
  • Create Deployment
kubectl create deployment <deploymentName> --image=<dockerId>/<dockerImageId>:<tag>
E.g: kubectl create deployment microservices101-deployment --image=<dockerID>/mmv3-currency-exchange-service:0.0.1-SNAPSHOT
/** Now if I want to update my deployment to a newer version. 
Can do it with the below command */
kubectl set image deployment <deploymentName> <containerId>=<dockerId>/<dockerImageId>:<tag>
E.g: kubectl set image deployment microservices101-deployment <containerName>=<dockerId>/mmv3-currency-exchange-service:0.0.1-SNAPSHOT
// kubectl get events -> Will show you events on how resources were managed
// kubectl get pods -> WIll give information about pods
// kubectl get rs-> gives info about replica set, containerId
  • Expose deployment
kubectl expose deployment microservices101-deployment --type=LoadBalancer --port=8080

Now lets look at step by step on how to deploy containers on kubernetes.

References:

--

--