Kubernetes, often shortened as K8s, is a free tool that helps manage how software
applications run in the cloud. Originally created by Google, it's now looked after
by a group called the Cloud Native Computing Foundation.
Think of Kubernetes like a conductor for an orchestra of software containers. These
containers are like tiny, self-contained packages of software that can run just
about anywhere. Kubernetes makes sure they're all playing the right notes together,
whether there's just a few or hundreds of them.
Some things Kubernetes does:
Organizes Containers: It groups these containers together in sensible ways so
they're easier to keep track of.
Scales Up and Down: If there's more demand for a certain part of your
application, Kubernetes can make sure there are more containers running to handle
it. And when the demand drops, it can shrink things back down.
Handles Updates: When you want to update your software, Kubernetes can do it in
a way that doesn't interrupt anything. And if something goes wrong, it can quickly
switch back to the previous version.
Fixes Problems Automatically: If a container crashes or something goes wrong
with the computer it's running on, Kubernetes can quickly replace it somewhere
else.
Keeps Track of Services: It knows where all your different parts of your
application are and makes sure they can find and talk to each other.
Kubernetes has become really popular because it makes managing complex applications
much simpler. It's like having a smart assistant for your cloud-based software.
Below mentioned the steps of installaion of minikube
Steps:
1. sudo apt-get update
2. sudo apt-get install docker.io
3. sudo apt-get update && sudo apt-get install -y apt-transport-https
4. curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add
-
5. echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee
/etc/apt/sources.list.d/kubernetes.list
6. sudo apt-get update
7. sudo apt-get install -y kubectl
8. sudo curl -LO "https://dl.k8s.io/release/$(curl -L -s
https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
9. sudo chmod +x kubectl
10. sudo mv kubectl /usr/local/bin/
11.minikube start --driver=none
12. kubectl version --client
13.kubectl cluster-info
Now, you can proceed with setting up Minikube:
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-
amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
sudo apt-get update
sudo apt-get install -y conntrack
curl -LO https://github.com/kubernetes-sigs/cri-tools/releases/download/v1.22.0/
crictl-v1.22.0-linux-amd64.tar.gz
sudo tar zxvf crictl-v1.22.0-linux-amd64.tar.gz -C /usr/local/bin
# Download cri-dockerd
curl -LO https://github.com/Mirantis/cri-dockerd/releases/download/v1.0.0/cri-
dockerd-v1.0.0-linux-amd64.tar.gz
# Extract the cri-dockerd binary
sudo tar zxvf cri-dockerd-v1.0.0-linux-amd64.tar.gz -C /usr/local/bin
# Download cri-dockerd
sudo curl -L https://github.com/Mirantis/cri-dockerd/releases/download/v1.0.0/cri-
dockerd-v1.0.0-linux-amd64.tar.gz -o cri-dockerd-v1.0.0-linux-amd64.tar.gz
# Extract the cri-dockerd binary
sudo tar zxvf cri-dockerd-v1.0.0-linux-amd64.tar.gz -C /usr/local/bin
minikube start --driver=docker --force
sudo sysctl fs.protected_regular=0
minikube start --driver=docker
minikube start --driver=docker --force
kubectl version --client
Now lets create a test deployment using minikube
Deployment YAML (test-app-deployment.yaml):
nano test-app-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-app
spec:
replicas: 1
selector:
matchLabels:
app: test-app
template:
metadata:
labels:
app: test-app
spec:
containers:
- name: test-app
image: nginx:latest
ports:
- containerPort: 80
Service YAML (test-app-service.yaml):
nano test-app-service.yaml
apiVersion: v1
kind: Service
metadata:
name: test-app
spec:
selector:
app: test-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: NodePort
Apply the deployment and service:
kubectl apply -f test-app-deployment.yaml
kubectl apply -f test-app-service.yaml
Once deployed, you can access your application using Minikube's IP and NodePort.
minikube service test-app --url
For example, if the URL is http://192.168.49.2:30980, you can access your
application by visiting http://192.168.49.2:30980 in your browser.
lets Discuss some commands
here are some basic Kubernetes commands to help you manage your cluster:
Pods:
List all pods: kubectl get pods
Get detailed information about a specific pod: kubectl describe pod <pod_name>
Delete a pod: kubectl delete pod <pod_name>
Deployments:
List all deployments: kubectl get deployments
Get detailed information about a specific deployment: kubectl describe deployment
<deployment_name>
Scale a deployment: kubectl scale deployment <deployment_name> --
replicas=<number_of_replicas>
Delete a deployment: kubectl delete deployment <deployment_name>
Services:
List all services: kubectl get services
Get detailed information about a specific service: kubectl describe service
<service_name>
Delete a service: kubectl delete service <service_name>
Namespaces:
Create a new namespace: kubectl create namespace <namespace_name>
Switch to a different namespace: kubectl config set-context --current --
namespace=<namespace_name>
List all namespaces: kubectl get namespaces
Get detailed information about a specific namespace: kubectl describe namespace
<namespace_name>
Other useful commands:
Get the Kubernetes cluster information: kubectl cluster-info
Get the version of kubectl: kubectl version
Access the Kubernetes dashboard: minikube dashboard
ConfigMaps:
List all ConfigMaps: kubectl get configmaps
Get detailed information about a specific ConfigMap: kubectl describe configmap
<configmap_name>
Create a ConfigMap from a file: kubectl create configmap <configmap_name> --from-
file=<path_to_file>
Delete a ConfigMap: kubectl delete configmap <configmap_name>
Secrets:
List all Secrets: kubectl get secrets
Get detailed information about a specific Secret: kubectl describe secret
<secret_name>
Create a Secret from literal values: kubectl create secret generic <secret_name> --
from-literal=key1=value1 --from-literal=key2=value2
Delete a Secret: kubectl delete secret <secret_name>
PersistentVolumes (PV) and PersistentVolumeClaims (PVC):
List all PersistentVolumes: kubectl get pv
Get detailed information about a specific PersistentVolume: kubectl describe pv
<pv_name>
List all PersistentVolumeClaims: kubectl get pvc
Get detailed information about a specific PersistentVolumeClaim: kubectl describe
pvc <pvc_name>
Pod Logs:
Get logs from a pod: kubectl logs <pod_name>
Stream logs from a pod: kubectl logs -f <pod_name>
Exec into Pod:
Start a shell session inside a pod: kubectl exec -it <pod_name> -- /bin/bash
Label and Annotations:
Label a resource: kubectl label <resource_type> <resource_name>
<label_key>=<label_value>
Annotate a resource: kubectl annotate <resource_type> <resource_name>
<annotation_key>=<annotation_value>
Node:
List all nodes: kubectl get nodes
Get detailed information about a specific node: kubectl describe node <node_name>
Helm (Kubernetes Package Manager):
Install a Helm chart: helm install <release_name> <chart_name>
Upgrade a Helm release: helm upgrade <release_name> <chart_name>
Rollback a Helm release: helm rollback <release_name> <revision_number>
Custom Resource Definitions (CRDs):
List all CRDs: kubectl get crds
Get detailed information about a specific CRD: kubectl describe crd <crd_name>
Rolling Updates and Rollbacks:
Update a Deployment: kubectl set image deployment/<deployment_name>
<container_name>=<new_image_version>
Check the status of a rolling update: kubectl rollout status
deployment/<deployment_name>
Rollback a deployment to a previous revision: kubectl rollout undo
deployment/<deployment_name>
Pod Affinity and Anti-affinity:
Add pod affinity to a deployment: kubectl patch deployment <deployment_name> -p
'{"spec":{"affinity":{"podAffinity":
{"requiredDuringSchedulingIgnoredDuringExecution":[{"labelSelector":
{"matchExpressions":[{"key":"<label_key>","operator":"In","values":
["<label_value>"]}]},"topologyKey":"<topology_key>"}]}}}}'
Add pod anti-affinity to a deployment: kubectl patch deployment <deployment_name> -
p '{"spec":{"affinity":{"podAntiAffinity":
{"requiredDuringSchedulingIgnoredDuringExecution":[{"labelSelector":
{"matchExpressions":[{"key":"<label_key>","operator":"In","values":
["<label_value>"]}]},"topologyKey":"<topology_key>"}]}}}}'
Network Policies:
List all network policies: kubectl get networkpolicies
Get detailed information about a specific network policy: kubectl describe
networkpolicy <network_policy_name>
Allow traffic to a specific pod:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-pod
spec:
podSelector:
matchLabels:
app: <app_name>
ingress:
- {}
egress:
- {}
policyTypes:
- Ingress
- Egress
Horizontal Pod Autoscaler (HPA):
Create an HPA for a deployment: kubectl autoscale deployment <deployment_name> --
cpu-percent=50 --min=1 --max=10
Get information about the HPA: kubectl get hpa
Resource Usage:
Check resource utilization of pods: kubectl top pods
Check resource utilization of nodes: kubectl top nodes
Custom Metrics:
Install Prometheus Adapter:
helm repo add stable https://charts.helm.sh/stable
helm repo update
helm install prometheus-adapter stable/prometheus-adapter --namespace kube-system
Configure HPA to use custom metrics:
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: <hpa_name>
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: <deployment_name>
minReplicas: 1
maxReplicas: 10
metrics:
- type: Pods
pods:
metric:
name: <metric_name>
target:
type: AverageValue
averageValue: "50"