Kubernetes

What is Docker ?


What is Kubernetes ?

Why Kubernetes ?

Kubernetes Architecture

K8S Architecture

Namespaces

Types of workloads

Pods

Pods are the smallest deployable units of computing that you can create and manage in Kubernetes.

A Pod (as in a pod of whales or pea pod) is a group of one or more containers, with shared storage and network resources, and a specification for how to run the containers.

apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
  -name: nginx
image: nginx:1.14.2
ports:
    -containerPort: 80

Deployment

Statefulset

Daemonset

Job or Cron Job

Other important API components

K8s Services

Now that we understand the basic purpose of a Kubernetes Service, let's take a closer look at how different types of Services work and what they're used for.

Creating a service

Applying this manifest creates a new Service named "my-service", which targets TCP port 9376 on any Pod with the app.kubernetes.io/name: MyApp label.

Types of Services in K8s

ClusterIP


Now you know what a Kubernetes ClusterIP Service is and how it works. Next, let's dive into NodePort Service

NodePort

Now that you have a good understanding of the NodePort Service, it’s time to examine the LoadBalancer Service.

LoadBalancer

Note that the LoadBalancer Service this time builds on top of the NodePort Service, with an added benefit: It adds load balancing functionality to distribute traffic between nodes. This reduces the negative effects of any one node failing or becoming overloaded with requests.

The traffic coming from external clients goes through a path like this: External client -> Loadbalancer -> Worker node IP -> NodePort -> ClusterIP Service -> Pod

ClusterIP vs NodePort vs LoadBalancer: Key Differences & Use Cases

Key difference

ClusterIPNodePortLoadBalancer
CommunicationPod-to-PodExternal client-to-Pod (No load balancing between nodes)External client-to-Pod (Load balancing between nodes)
Cloud platform required?NoNoYes

Use cases

ClusterIPNodePortLoadBalancer
Use caseTo allow Pod-to-Pod communication within the same clusterTo expose app(s) inside the cluster to external clients (outside the cluster). Client requests go to the same node they connected to.To expose app(s) inside the cluster to external clients (outside the cluster). Client requests are load balanced across multiple nodes.

The choice of which Kubernetes Service type to use depends on the specific requirements of your application and the environment where it is running. Having said that, here is a brief overview of when to use which Service type:

ClusterIP:

NodePort:

LoadBalancer:

But, with a LoadBalancer, if one node fails, the LoadBalancer doesn't rely on a single node (it sends traffic to all). So only a few requests hitting the problematic node will fail, not all.

How to Use and Start Kubernetes ?

Prerequisites

Steps / Workflow

Replication

user@devops:~/devops_training/node/src/EchoAPI$ minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured

user@devops:~/devops_training/node/src/EchoAPI$ docker build -t echoapi:0.0.1 .
[+] Building 1.0s (12/12) FINISHED                                                                                                             
 => [internal] load build definition from Dockerfile                                                                                      0.0s
 => => transferring dockerfile: 196B                                                                                                      0.0s
 => [internal] load .dockerignore                                                                                                         0.0s
 => => transferring context: 2B                                                                                                           0.0s
 => [internal] load metadata for docker.io/library/node:12.16.3-slim                                                                      0.8s
 => [internal] load metadata for docker.io/library/node:12.16.3                                                                           0.8s
 => [builder 1/4] FROM docker.io/library/node:12.16.3@sha256:b51dc2876a5d1e184190d76a2a1f11da034d16acd95ab2e0c2191b8f1ab65d4c             0.0s
 => [stage-1 1/2] FROM docker.io/library/node:12.16.3-slim@sha256:03d1fd98a9b4fc95133eee2d47d2b77cf89d51312eb1a8eeec36757568c1ec9e        0.0s
 => [internal] load build context                                                                                                         0.0s
 => => transferring context: 90B                                                                                                          0.0s
 => CACHED [builder 2/4] WORKDIR /app                                                                                                     0.0s
 => CACHED [builder 3/4] COPY . /app                                                                                                      0.0s
 => CACHED [builder 4/4] RUN npm install                                                                                                  0.0s
 => CACHED [stage-1 2/2] COPY --from=builder /app .                                                                                       0.0s
 => exporting to image                                                                                                                    0.0s
 => => exporting layers                                                                                                                   0.0s
 => => writing image sha256:42e52fc6702187c8fcf55ba86ac3dc61366c4737a4335355c7abc308c7914297                                              0.0s
 => => naming to docker.io/library/echoapi:0.0.1                                                                                          0.0s
user@devops:~/devops_training/node/src/EchoAPI$ minikube image ls
k8s.gcr.io/pause:3.5
k8s.gcr.io/kube-scheduler:v1.22.0
k8s.gcr.io/kube-proxy:v1.22.0
k8s.gcr.io/kube-controller-manager:v1.22.0
k8s.gcr.io/kube-apiserver:v1.22.0
k8s.gcr.io/etcd:3.5.0-0
k8s.gcr.io/coredns/coredns:v1.8.4
gcr.io/k8s-minikube/storage-provisioner:v5
docker.io/library/echoapi:0.0.1
user@devops:~/devops_training/node/src/EchoAPI$ minikube image load echoapi:0.0.1
user@devops:~/devops_training/node/src/EchoAPI$ kubectl apply -f ../../services/echo.yaml 
deployment.apps/echoapi-deployment unchanged
service/echoservice unchanged
user@devops:~/devops_training/node/src/EchoAPI$ minikube ip
192.168.49.2
user@devops:~/devops_training/node/src/EchoAPI$ kubectl get pods
NAME                                  READY   STATUS    RESTARTS        AGE
echoapi-deployment-5d77cc94bf-5k8xd   1/1     Running   1 (9m20s ago)   4d23h
echoapi-deployment-5d77cc94bf-9tg4f   1/1     Running   1 (9m20s ago)   4d23h
echoapi-deployment-5d77cc94bf-w9br5   1/1     Running   1 (9m20s ago)   4d23h
user@devops:~/devops_training/node/src/EchoAPI$ kubectl get services
NAME          TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
echoservice   NodePort    10.101.27.233   <none>        9595:31122/TCP   4d23h
kubernetes    ClusterIP   10.96.0.1       <none>        443/TCP          5d
user@devops:~/devops_training/node/src/EchoAPI$ curl http://192.168.49.2:31122/echo/hi/after/2000
hi
user@devops:~/devops_training/node/src/EchoAPI$ kubectl get rs
NAME                            DESIRED   CURRENT   READY   AGE
echoapi-deployment-5d77cc94bf   3         3         3       4d23h
user@devops:~/devops_training/node/src/EchoAPI$ kubectl describe rs/echoapi-deployment-5d77cc94bf 
Name:           echoapi-deployment-5d77cc94bf
Namespace:      default
Selector:       app=echo,pod-template-hash=5d77cc94bf
Labels:         app=echo
                pod-template-hash=5d77cc94bf
Annotations:    deployment.kubernetes.io/desired-replicas: 3
                deployment.kubernetes.io/max-replicas: 4
                deployment.kubernetes.io/revision: 1
Controlled By:  Deployment/echoapi-deployment
Replicas:       3 current / 3 desired
Pods Status:    3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=echo
           pod-template-hash=5d77cc94bf
  Containers:
   echo:
    Image:      echoapi:0.0.1
    Port:       9595/TCP
    Host Port:  0/TCP
    Limits:
      cpu:     100m
      memory:  256Mi
    Requests:
      cpu:        100m
      memory:     256Mi
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:           <none>
user@devops:~/devops_training/node/src/EchoAPI$

🤔 Things to keep in mind

Suggested Reading