Kubernetes Cluster Maintenance

Kubernetes Cluster Maintenance

29th April Saturday

☸ Upgrading the Cluster

☘️ Check the current version of your cluster:

kubectl version

☘️ Check the current version of the Kubernetes control plane:

kubectl version --short

☘️ List the available versions of Kubernetes:

kubectl get nodes

☘️ Upgrade the control plane to a specific version:

sudo kubeadm upgrade plan
sudo kubeadm upgrade apply <version>

☘️ Upgrade the nodes to a specific version:

sudo apt-get update && sudo apt-get upgrade -y
sudo reboot

☘️ Verify that the upgrade was successful:

kubectl get nodes
kubectl get pods --all-namespace

☸ 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}'

Thanks for your attention!!

~Ritul Gupta