#KubeWeek Day6 -Kubernetes Cluster Maintenance

I am continue learning variety of DevOps technologies, including AWS, Linux, Python, Shell Scripting, Docker, Terraform, Jenkins, Git/GitHub, and Computer Networking. I have a strong ability to troubleshoot and resolve issues, and are consistently motivated to expand the knowledge and skills through exploration of new technologies.
UPGRADE KUBERNETES CLUSTER
Below are the steps we need to perform for upgrading the Kubernetes cluster.
Note: This document is only from the exam point of view and for upgrading production-based clusters, we need to consider more points.
Get detailed information about the upgrade process from the below-mentioned website.
UPGRADE CONTROLPLANE NODE
# Update the repositiry
sudo apt update
# Check for the lastest kubernetes release versions
sudo apt-cache madison kubeadm
UPGRADE KUBEADM
# Unholds to upgrade kubeadm package
sudo apt-mark unhold kubeadm
# Install the new version of kubeadm, here the version is 1.20.4-00
sudo apt update && sudo apt install -y kubeadm=1.20.4-00
# Hold the package to prevent accidential upgrade
sudo apt-mark hold kubeadm
# Fetches the control plane component versions to which we can update to.
sudo kubeadm upgrade plan
# Upgrade kubeadm
sudo kubeadm upgrade apply v1.20.4
UPGRADE KUBECTL AND KUBELET
# Drain the control plane node
kubectl drain manager --ignore-daemonsets
# Unholds to upgrade for kubelet and kubectl
sudo apt-mark unhold kubelet kubectl
# Install the new version of kubelet and kubectl, here the version is 1.20.4-00
sudo apt-get update && sudo apt-get install -y kubelet=1.20.4-00 kubectl=1.20.4-00
# Hold the package to prevent accidential upgrade
sudo apt-mark hold kubelet kubectl
# Will reloads the systemd manager configuration
sudo systemctl daemon-reload
# Will restart the kubelet service
sudo systemctl restart kubelet
# Check the status of the service
sudo systemctl status kubelet
# Uncordon the cntrol plane node.
kubectl uncordon manager
UPGRADE WORKER NODES
Note: worker nodes should not be upgraded simultaneously.
UPGRADE KUBEADM
On Worker node
# Update the repositiry
sudo apt update
# Unholds to upgrade kubeadm
sudo apt-mark unhold kubeadm
# Install the new version of kubeadm, here the version is 1.20.4-00
sudo apt update && sudo apt install -y kubeadm=1.20.4-00
# Hold the package to prevent upgrade
sudo apt-mark hold kubeadm
# Upgrade the local configuration
sudo kubeadm upgrade node
UPGRADE KUBECTL AND KUBELET
On the control plane node
# Drain the worker1 node
kubectl drain worker1 --ignore-daemonsets --delete-emptydir-data
On Worker node
# Unholds to upgrade kubelet and kubectl
sudo apt-mark unhold kubelet kubectl
# Install the new version of kubelet and kubectl, here the version is 1.20.4-00
sudo apt-get update && sudo apt-get install -y kubelet=1.20.4-00 kubectl=1.20.4-00
# Hold the package to prevent upgrade
sudo apt-mark hold kubelet kubectl
# Will reload the systemd manager configuration
sudo systemctl daemon-reload
# Will restart the kubelet service
sudo systemctl restart kubelet
On the control plane node
# Uncordon the worker node to bring it online and run the below command on control plane node.
kubectl uncordon worker1
Perform the above steps on all the worker nodes to upgrade kubeadm, kubelet and kubectl.
On the control plane node
# Verify the cluster to list the updated nodes.
kubectl get nodes
Backing up and Restoring data
Backup a specific resource:
kubectl get <resource> <resource_name> -o yaml > backup.yaml
For example, to backup a deployment named my-deployment in the default namespace, you would run:
kubectl get deployment my-deployment -n default -o yaml > backup.yaml
Backup all resources in a namespace
kubectl get all -n <namespace> -o yaml > backup.yaml
For example, to backup all resources in the default namespace, you would run:
kubectl get all -n default -o yaml > backup.yaml
Restore a specific resource from a backup:
kubectl apply -f backup.yaml
Restore all resources from a backup:
kubectl apply -f backup.yaml --prune
The --prune flag will remove any resources that are not in the backup.
Backup and restore persistent volumes:
To backup a persistent volume, you can use your storage provider's tools or utilities such as velero. For example, to backup a persistent volume named my-pv, you would run:
velero backup create my-backup --include-namespaces <namespace> --selector key=value
To restore the persistent volume, you would run:
velero restore create --from-backup my-backup
Scaling the Cluster
Scale a deployment:
kubectl scale deployment <deployment_name> --replicas=<number_of_replicas>
For example, to scale a deployment named my-deployment to 3 replicas, you would run:
kubectl scale deployment my-deployment --replicas=3
Scale a statefulset:
kubectl scale statefulset <statefulset_name> --replicas=<number_of_replicas>
For example, to scale a statefulset named my-statefulset to 3 replicas, you would run:
kubectl scale statefulset my-statefulset --replicas=3
Scale a deployment using autoscaling:
kubectl autoscale deployment <deployment_name> --min=<minimum_number_of_replicas> --max=<maximum_number_of_replicas> --cpu-percent=<cpu_utilization_percentage>
For example, to create an autoscaling deployment named my-deployment with a minimum of 2 replicas, a maximum of 5 replicas, and a target CPU utilization of 80%, you would run:
kubectl autoscale deployment my-deployment --min=2 --max=5 --cpu-percent=80
Check the current scaling status of a deployment:
kubectl get deployment <deployment_name> -o=jsonpath='{.spec.replicas}'
For example, to check the current scaling status of a deployment named my-deployment, you would run:
kubectl get deployment my-deployment -o=jsonpath='{.spec.replicas}'
Thank you for reading this Blog. Hope you learned something new today! If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future.




