Docker
Kubernetes Basic

Kubernetes Basics

website : play-with-k8s & katacoda

Kubernetes architecture:

  1. Master Node:
    • API Server: Control plane for managing and interacting with the cluster.
    • etcd: Distributed key-value store for cluster data.
    • Controller Manager: Manages controllers for maintaining cluster state.
    • Scheduler: Schedules containers onto nodes based on policies.
  2. Worker Nodes:
    • Kubelet: Manages containers and communicates with the master node.
    • Container Runtime: Software that runs and manages containers.
    • kube-proxy: Handles networking and load balancing.
  3. Networking:
    • Pod Networking: Assigns IP addresses to pods for communication.
    • Service Networking: Provides a stable virtual IP address for services.
  4. Add-Ons:
    • DNS: Provides service discovery for the cluster.
    • Dashboard: Web-based GUI for cluster management.
    • Ingress Controller: Manages incoming traffic to services.

Kubernetes Container Abstractions

  1. Pod: A pod is the smallest unit of deployment in Kubernetes. It represents a single instance of a running process in the cluster. A pod can contain one or more containers that are tightly coupled and share resources, such as network and storage. Pods are usually created and managed by higher-level abstractions like deployments or stateful sets.
  2. Deployment: Manages the lifecycle of pods and ensures a desired number of replicas are running. Deployments support rolling updates, rollbacks, and scaling applications up or down without downtime.
  3. Service: Provides a stable network endpoint for accessing pods. Services enable load balancing, service discovery, and external or internal access to applications. They decouple pods' dynamic nature from clients.
  4. Namespace: A virtual cluster within Kubernetes, namespaces logically partition resources. They provide isolation, resource allocation, and access control, allowing multiple teams or projects to share the same cluster securely.

5 :- Controller: For creating/ updating pods and other objects: Many type of controller . Deployment, ReplicaSet, StatefulSet, DaemonSet , Job , CronJob ,etc

  1. ReplicaSet: Ensures a specified number of pod replicas are running. Replica sets are commonly used by deployments to maintain the desired state and handle scaling and self-healing of applications.
  2. StatefulSet: Manages stateful applications requiring stable network identities and persistent storage. Stateful sets guarantee ordering and uniqueness of pod instances, making them suitable for databases and other stateful workloads.

These Kubernetes abstractions simplify container management, enabling scalable and resilient deployments while providing flexibility, stability, and separation between components.


note:

container runtimes don’t know pods

first pod go control plan then kubelet after that in runtime runs container

deployment (api → etcd → c-m → sched)→ replicaSet → Pods

Commands

# my-apache is the name of deployment
kubectl create deployment my-apache --image httpd
# show all resources in your k8s cluster
kubectl get all
# increase replicas for my-apache deployment
kubectl scale deployment my-apache --replicas 2
# to delete pod and deployment
kubectl delete pod {name of a pod}
kubectl delete deploy {name of a deployment}

to inspect k8s objects

#detail of a pod
kubectl describe pod/my-apache-86c695c9b6-229vs
#get container logs
kubectl logs deploy/my-apache
#watch a command
kubectl get pods -w # now do changes will reflect the live changes
 

Exposing Containers

  • If we want to connect to pod(s), we need a service.
  • A service is a stable address for pod(s)
  • kubectl expose creates a service for existing pods
  • CoreDNS allows us to resolve services by name

There are different types of services

  • ClusterIP (default)

    • Single, internal virtual IP allocated
    • Only reachable from within cluster (nodes and pods)
    • Pods can reach service on apps port number
    • ClusterIP is only good for cluster
  • NodePort

    • High port on each node (that assign to each service
    • Anyone can connect (if they can reach node)
    • Port is open on very node’s IP
  • LoadBalancer (mostly used in cloud)

    • controls a LB endpoints external to the cluster through in kubectl
    • Bunch of automation
    • Not built-in only available when ifra provider gives you a LB(AWS ELB,etc)
    • Creates NodePort+ ClusterIP services, tells LB to send to NodePort
  • ExternalName

    • Adds CNAME DNS record to CoreDNS only
    • Not used for Pods, but for giving pods a DNS name to use for outside Kubernetes

Create Service

creating a NodePort Service

Let’s expose a NodePort so we can acces it via host IP (localhost)

kubectl expose deployment/httpenv —port 8888 —name httpenv-np —type NodePort

Note : NodePort service also creates a ClusterIP

Add a LoadBalancer Service

If you’re on decker desktop, it provides a build-in LoadBalancer that publish port to localhost

kubectl expose deployment/httpenv --port 8888 --name httpenv-lb --type LoadBalancer

if you’re on kubeadm, minikube, or microk8s

  • No built-in LBalancer (LB)