Kubernetes

What is Kubernetes?

Kubernetes is a open-sourced platform used to deploy containerized application. If a container crashed, kubernetes can restart or replace it automatically. It is automates deployment, monitoring and scaling.

What are the features of Kubernetes?

These are the features of Kubernetes:

  • Self-healing
  • Resource optimization
  • Scaling and load balancing
  • Rollouts and Rollbacks
  • Automated scheduling
  • Containerized infrastructure

What is Kubernetes pod?

Kubernetes pod is the smallest object. Kubernetes do not run container directly, it run pod.

What is Ingress?

It is used to manage external service within a cluster. Traffic routing rules are defined in Ingress resources.

What is a Cluster?

A Cluster is a collection of nodes which works in a single unit. All of these nodes work together to manage containerized application in a single unit.

How to setup Kubernetes Cluster in Ubuntu?

Step 1:

Setup host name at node

$ sudo hostnamectl set-hostname master-node

Step 2:

update and defined host-to-IP mapping

$ sudo nano /etc/hosts
$ 192.168.43.95 master-node

Step 3:

disable swap and load kernel modules

$ sudo swapoff -a
$ sudo swapoff -$ sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
$ sudo modprobe overlay
$ sudo modprobe br_netfilter
$ sudo modprobe br_netfilter

create configuration file and IP forwarding

$ echo -e "overlay\nbr_netfilter" | sudo tee /etc/modules-load.d/k8s.conf
$ echo -e "net.bridge.bridge-nf-call-ip6tables = 1\nnet.bridge.bridge-nf-call-iptables = 1\nnet.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.d/kubernetes.conf
$ echo -e "net.bridge.bridge-nf-call-ip6tables = 1\nnet.bridge.bridge-nf-call-iptables = 1\nnet.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.d/kubernetes.conf
$ sudo sysctl --system

Step 4:

install and configure containered

install depndency

$ sudo apt update 
$ sudo apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates

Add GPG key and repository

$ sudo mkdir -p /etc/apt/keyrings $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo tee /etc/apt/keyrings/docker.asc > /dev/null $ echo "deb [signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
$ sudo apt update && sudo apt install -y containerd.io
$ sudo mkdir -p /etc/containerd $ containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1 $ sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml $ sudo systemctl restart containerd
$ sudo curl -fsSLo /etc/apt/keyrings/kubernetes-apt-keyring.asc https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key $ echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.asc] https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list

Step 5:

install Kubernetes components and initialized kubernetes cluster

$ sudo apt update 
$ sudo apt install -y kubelet kubeadm kubectl

How to install minikube at Ubuntu ?

curl -LO https://github.com/kubernetes/minikube/releases/latest/download/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube && rm minikube-linux-amd64

Start your cluster

minikube start

How to install kubectl at Ubuntu ?

Download the latest release

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"

Make kubectl binary executable

chmod +x ./kubectl

Move the binary into your PATH

sudo mv ./kubectl /usr/local/bin/kubectl

install kubectl-convert

sudo install -o root -g root -m 0755 kubectl-convert /usr/local/bin/kubectl-convert

check the version

kubectl version

make the shorthand alias for kubectl = k

alias k=kubectl complete -o default -F __start_kubectl k

How to get basic information and addresses about cluster’s control plane ?

kubectl cluster-info

get more detail information about each component:

kubectl cluster-info dump

get status information about nodes:

k get nodes

get health status of a node

k describe node minikube

How to unscheduled new pod but make existing pod running ?

k cordon minikube

How to reverse previous step?

k uncordon minikube

How to lists all virtual clusters inside my cluster?

k get namespace

How to create namespaces?

k create namespace prod
k create namespace dev
k create namespace test

How to check status of all these namespaces?

kubectl get namespaces

How to delete a namespace?

k delete namespace prod

How to get all information about a “namespace”?

k describe namespace prod

How to find resources from default namespace?

kubectl get pods

How to get resources from “dev” namespace?

k get pods -n dev

How to create node?

kubectl create deployment hello-node --image=k8s.gcr.io/echoserver:1.4
kubectl create deployment hello-node --image=k8s.gcr.io/echoserver:1.4 -n dev

How to get status of all pods in all namespaces?

kubectl get pods --all-namespaces

How to get a list of activity?

kubectl get events -n dev

How to get a list of all events?

kubectl get events --all-namespaces

How to create service and expose a node to the port?

kubectl expose deployment hello-node --type=LoadBalancer --port=8080 -n dev

How to check status of the service?

k get services

How to check kubernetes cluster is working correctly and check to access?

minikube service hello-node -n dev

Minikube commands

Installation: https://minikube.sigs.k8s.io/docs/start/
minikube config set driver docker
minikube start // stop
minikube status
minikube dashboard --url
minikube service <applicaiton-service-name>

Kubectl Insallation/Configuration

Installation: https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/
curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
cat ~/.kube/config // kubectl config view
alias k='kubectl'
cat ~/.kube/config // kubectl config view alias k='kubectl'

Kubectl commands

kubectl get namespace
kubectl get deployment
kubectl get replicasetP
kubectl get configmap
kubectl get nodes
kubectl describe nodes
kubectl get events
minikube service mywebapp

Cluster Management

kubectl cluster-info
kubectl get nodes
kubectl describe node minikube
kubectl cordon minikube
kubectl drain minikube --ignore-daemonsets=true --force
kubectl uncordon minikube

Namespaces

kubectl get namespace
kubectl create namespace dev
kubectl create namespace test
kubectl delete namespace test
k create -f namespaces/namespace-prod.yaml
k describe namespace prod
# OPTIONAL kubectl config set-context --current --namespace=<NAMESPACE NAME>

Your Hello World Kubernetes Project

kubectl get get pods
kubectl get pods -n dev
kubectl create deployment hello-node --image=k8s.gcr.io/echoserver:1.4
kubectl create deployment hello-node --image=k8s.gcr.io/echoserver:1.4 -n dev
kubectl get deployments --all-namespaces
kubectl get events -n dev
kubectl expose deployment hello-node --type=LoadBalancer --port=8080
kubectl get services
minikube service hello-node
//On cloud providers that support load balancers, an external IP address would be provisioned to access the Service. On minikube, the LoadBalancer type makes the Service accessible through the minikube service command.

Deployment

kubectl apply -f solution/v1.yaml

Service LoadBalancers

kubectl apply -f solution/v2.yaml

Config Maps and Scaling

kubectl apply -f solution/v3.yaml

Resource Limits

kubectl apply -f solution/v4.yaml

Troubleshooting, Logs, Rollouts, Draining Nodes

k describe deployment mydeployment

Logs

k logs -f -l app=mywebapp

Rollouts

kubectl rollout k rollout restart deployment mydeployment kubectl drain minikube --ignore-daemonsets=true --force