Kuber-the-netes
Docker is a high level container, which was brought about by compactibility issues. Imagine employing a new developer and getting them to set up their environment to be similar to the production or test environment. This requires a lot of back and forths due to OS compactibility or system/application dependencies etc.
Therefore there is no guarantee that the application in development would run the same way in different environment.since developers might prefer different operating systems.
With docker, we can kiss all these issues goodbye because all dvelopers can just get started with a simple docker run comand since docker packages application in development together with its dependencies in a container so these containers can run independent of the base os. Think of it like running a virtual machine inside a host machine without affecting the host machine in any way, like having a lot of virtual machines running on a hypervisor. but unlike hypervisors, docker is not meant to virtualize operating systems but to containerise applications,run and ship them
K8S Architechture
Kubernetes, is an open-source system for automating deployment, scaling, and managing containerized applications.
K8s consist of 5000 worker nodes per cluster currently.
To better understand how it works
, think of it as A master node linked to a cluster which contains several worker nodes and within this worker nodes are pods with several or multiple dependent containers.
Important components of K8s Master
Scheduler
etcd - storing the current state of the cluster
Control Manager
Api server
Important components of K8s Worker
Kubelet
Kube-proxy - maintains entire network config
Pod - deploy multiple dependent containers
Containers - provides runtime environment
*Component
-container runtime - Docker (others could be rocket or crio)
-etcd
Building a cluster from scratch
We can set up K8s using Minikube (set up a single instance in an all in one setup) and Kubeadm (multi node setup)
Set up Requirements
/Prerequisite
-Hypervisor( virtual box or HyperV- windows & kvm for Linux)
-kubectl (command line utility)
-Minicube executable
Kubeadm
When Installing Kubeadm we have a master Node and worker Nodes, next is to setup a container runtime(maybe docker), install Kubeadm, initialize the master node setup
create a POD or cluster Network across board , join the other nodes and voila.
Let’s get started
First you need to confirm that Kubernetes is installed on your local machine by running
kubectl version- This tells you the version of k8s
Next is to check the available context
k config get-contexts
K config current-context
Switch between Clusters
kubectl config set current-context <my-context>
k config (gives you all the option regarding clusters)
Kubectl run hello-minikube
Kubectl cluster-info
Kubectl get nodes
Access shell from within a pod :
kubectl exec --stdin --tty {pod} -- /bin/bash
**# Pod
Sample pod definition file.yaml
————————————————————————————**
apiVersion: v1
kind: Pod
metadata:
name: webapp-color
labels:
name: webapp-color
spec:
containers:
image: kodekloud/webapp-color
name: webcontainer
ports:
- containerPort: 80
env:
name: APP_COLOR
value: green**
—————————————————————————————
KUBECTL Commands:
Pods**
Kubectl get pods
kubectl describe pod
kubectl run nginx --image=nginx
Kubectl get pods -o wide (for more details including IP of all the pods)
kubectl delete pod nginx
kubectl edit pod redis
kubectl cluster-info dump (Get more info on the cluster)
kubectl get po <pod-name> -o yaml > pod.yaml - This command copied the pod def into pod, yaml file
kubectl get po nginx -o jsonpath='{.spec.containers[].image}{"\n"}' -n namespace - Check the image of the pod
# Namespace:
kubectl get namespace - this provides a list of all the namespaces in the cluster, including the 4 default ones. (default, kube-node-lease, kube-public, kube-system)
kubectl get pods --namespace=xxxxxx - Add a specific namespace
kubectl config set-context --current --namespace=<insert-namespace-name-here> - Permanently set up a namespace for subsequent kubectl commands
kubectl config view --minify | grep namespace:
kubectl get all -n <namespace>
# Apply
kubectl apply -f <filename>.yaml
# Others
cat /etc/resolv.conf - View the DNS server for the pod
# Roll Back k8s
kubectl rollout status deployment/test-deploy (check roll out status of a deployment)
kubectl rollout undo deployment/test-deploy (roll back a deployment)
kubectl rollout history deployment/test-deploy (rollback to a certain revision history with its tag)
kubectl rollout undo deployment/test-deploy --to-revision=3
EKS
Setup a new context/cluster
aws eks update-kubeconfig --name <eks_cluster_name>
Config Map
A ConfigMap allows you to decouple environment-specific configuration from your container images, so that your applications are easily portable. Config Maps hold a collection of env variables for the K8s cluster and this is mostly used when you have too many definition files with a lot of env. It could be declarative of imperative. Below Is a SAMPLE CONFIG MAP, Notice that instead of spec, we have data.
Sample configmap.yaml
————————————————
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
APP_COLOR:blue
APP_MODE:prod
————————————————
Imperatively, you can run “kubectl create configmap webapp-config-map --from-literal=APP_COLOR=darkblue”
Commands:
1. K create -f configmap.yaml - To create a new config map
K describe configmaps
K get configmaps
To inject an env variable to a pod def file via config map
———————————————
env:
name: APP
valueFrom:
configMapKeyRef:
name: app-config
key: APP_COLOR
——————————————————
Secrets
It is best to use external secrets using the k8s operator https://external-secrets.io/v0.6.0/overview/
Performance Metrics
K top node
k top pod