Managing Kubernetes deployments is a crucial task for DevOps engineers and system administrators. This tutorial demonstrates how to create a Bash script that allows users to interactively restart a deployment in a MicroK8s cluster.
We will cover:
- Listing all deployments in the cluster
- Allowing users to select a deployment interactively
- Restarting the selected deployment
- Monitoring the rollout status
Prerequisites
Before running this script, ensure that:
- You have MicroK8s installed and running.
- You have kubectl configured correctly (
microk8s kubectl
). - Your Kubernetes cluster has at least one deployment running.
Bash Script: restart_deployment.sh
Below is the Bash script that performs the deployment restart:
#!/usr/bin/bash
# List all deployments
echo "Fetching deployments..."
DEPLOYMENTS=$(microk8s kubectl get deployments -o jsonpath='{.items[*].metadata.name}')
# Check if there are any deployments
if [ -z "$DEPLOYMENTS" ]; then
echo "No deployments found."
exit 1
fi
# Display deployments and allow user to select one
echo "Select a deployment to restart:"
select DEPLOYMENT_NAME in $DEPLOYMENTS; do
if [ -n "$DEPLOYMENT_NAME" ]; then
echo "You selected $DEPLOYMENT_NAME"
break
else
echo "Invalid selection. Please try again."
fi
done
# Restart the selected deployment
microk8s kubectl rollout restart deployment $DEPLOYMENT_NAME
# Check the status of the rollout
microk8s kubectl rollout status deployment $DEPLOYMENT_NAME
Step-by-Step Breakdown
1. Fetching Deployments
The script first retrieves a list of all available deployments using the following command:
DEPLOYMENTS=$(microk8s kubectl get deployments -o jsonpath='{.items[*].metadata.name}')
- The
-o jsonpath
flag extracts only the deployment names.
2. Checking for Available Deployments
If there are no deployments found, the script exits with a message:
if [ -z "$DEPLOYMENTS" ]; then
echo "No deployments found."
exit 1
fi
3. Interactive Deployment Selection
The script presents a list of deployments and allows the user to select one:
echo "Select a deployment to restart:"
select DEPLOYMENT_NAME in $DEPLOYMENTS; do
if [ -n "$DEPLOYMENT_NAME" ]; then
echo "You selected $DEPLOYMENT_NAME"
break
else
echo "Invalid selection. Please try again."
fi
done
- The
select
command provides a numbered menu for users to choose from. - If the user makes an invalid selection, they are prompted again.
4. Restarting the Selected Deployment
Once a deployment is selected, the script restarts it using:
microk8s kubectl rollout restart deployment $DEPLOYMENT_NAME
This command triggers a rolling restart, ensuring that new pods are created before terminating old ones.
5. Monitoring Rollout Status
To confirm a successful restart, the script waits for the deployment to complete:
microk8s kubectl rollout status deployment $DEPLOYMENT_NAME
This ensures that the update is fully applied before the script exits.
How to Use the Script
1. Save the Script
Create a new file named restart_deployment.sh
and paste the script content into it.
2. Make the Script Executable
Run the following command to make the script executable:
chmod +x restart_deployment.sh
3. Run the Script
Execute the script with:
./restart_deployment.sh
The script will:
- Fetch the list of deployments.
- Present an interactive menu for selection.
- Restart the selected deployment.
- Monitor the rollout status.
Example Output
Fetching deployments...
Select a deployment to restart:
1) my-app
2) backend-service
3) frontend-ui
#? 2
You selected backend-service
deployment.apps/backend-service restarted
Waiting for rollout to finish: 1 out of 3 new replicas updated...
Rollout completed successfully
Conclusion
This Bash script simplifies the process of restarting a deployment in MicroK8s. By automating the selection, restart, and monitoring process, it reduces the risk of errors and improves efficiency for Kubernetes administrators.
Feel free to modify the script to add more functionality, such as:
- Restarting all deployments at once.
- Adding error handling for failed rollouts.
Happy coding! 🚀
Leave a Reply