In this tutorial, we'll walk you through the steps of automating the scaling of worker nodes in a Kubernetes cluster using IBM Cloud Code Engine. We will use Podman instead of Docker for container image creation and deployment. We'll also utilize IBM Cloud Container Registry to store the images and create automated jobs in Code Engine to scale up and down the nodes.
Prerequisites
- IBM Cloud Account: Ensure you have an active IBM Cloud account with access to the Kubernetes service and Code Engine.
- Podman: Make sure Podman is installed on your local machine (as a substitute for Docker).
- IBM Cloud CLI: Install the IBM Cloud CLI and configure it with your credentials.
Step 1: Write Dockerfile and Shell Script
We will first create a Dockerfile
to define the container image and a scale_nodes.sh
shell script to automate the scaling of the Kubernetes nodes. The script will allow us to dynamically scale the worker nodes based on the desired node count
Dockerfile
This Dockerfile sets up the environment needed to run the scaling script in a container:
# Use an official lightweight base image
FROM alpine:latest
# Install required packages: bash, curl, jq, and IBM Cloud CLI
RUN apk add --no-cache bash curl jq
# Install the IBM Cloud CLI
RUN curl -fsSL https://clis.cloud.ibm.com/install/linux | sh
# Install the Kubernetes Service plugin for IBM Cloud
RUN ibmcloud plugin install container-service
# Copy the scale script into the container
COPY scale_nodes.sh /scale_nodes.sh
# Make the script executable
RUN chmod +x /scale_nodes.sh
# Set the entrypoint to run the script
ENTRYPOINT ["/scale_nodes.sh"]
Shell Script (scale_nodes.sh)
This shell script logs in to IBM Cloud using an API key, sets the Kubernetes cluster configuration, and scales the nodes based on the desired count.
#!/bin/bash
# Cluster details
CLUSTER_NAME="your-cluster-name"
NODE_POOL_NAME="default"
REGION="us-south"
# IBM Cloud API key (use environment variable to keep it secure)
IBM_CLOUD_API_KEY="your-ibm-cloud-api-key"
# Desired node count (passed as an argument)
DESIRED_NODE_COUNT=$1
# Login to IBM Cloud
ibmcloud login --apikey $IBM_CLOUD_API_KEY -r $REGION
# Set the Kubernetes context for the cluster
ibmcloud ks cluster config --cluster $CLUSTER_NAME
# Check if node pool exists
if [ -z "$NODE_POOL_NAME" ]; then
echo "Node pool name is not provided."
exit 1
fi
# Update the number of nodes in the pool
ibmcloud ks worker-pool resize --cluster $CLUSTER_NAME --worker-pool $NODE_POOL_NAME --size-per-zone $DESIRED_NODE_COUNT
echo "Scaled node pool $NODE_POOL_NAME to $DESIRED_NODE_COUNT nodes in cluster $CLUSTER_NAME."
Step 2: Create Container Image Using Podman
we'll use Podman for building the container image locally. Here’s how to do it:
-
Build the Image: Ensure you are in the directory containing the Dockerfile
and scale_nodes.sh
. Run the following Podman command:
podman build -t scale-nodes:latest .
This command will build the container image using Podman.
2. Verify the Image: After the build is complete, verify the image has been created:
podman images
Step 3: Push the Image to IBM Cloud Container Registry
Now that the container image is built, you need to push it to IBM Cloud Container Registry.
1.Install the Container Registry Plug-in
Before you can push your image to IBM Cloud Container Registry, ensure that the Container Registry plug-in is installed in your IBM Cloud CLI:
ibmcloud plugin install container-registry -r 'IBM Cloud'
2. Log in to IBM Cloud: Use the IBM Cloud CLI to log in to your IBM Cloud account:
ibmcloud login
3 Set the Correct IBM Cloud Container Registry Region
Ensure that you're targeting the correct IBM Cloud Container Registry region for your project. To set the region, run the following command:
ibmcloud cr region-set global
4. Create a Namespace in IBM Cloud Container Registry
Next, you need to create a namespace in your IBM Cloud Container Registry. A namespace is used to organize your images in the registry.
Choose a name for your namespace (e.g., my_namespace) and create it:
ibmcloud cr namespace-add <my_namespace>
5. Tag the Image
Once your IBM Cloud Container Registry environment is set up, you can now tag your locally built image with the appropriate registry path:
podman tag scale-nodes:latest us.icr.io/<my_namespace>/scale-nodes:latest
6. Push the Image
Finally, push the tagged image to your IBM Cloud Container Registry:
podman push us.icr.io/<my_namespace>/scale-nodes:latest
This command will upload your image to the specified namespace in IBM Cloud Container Registry.
Step 4: Set Up Code Engine Project and Jobs
Next, you need to create a Code Engine project and two jobs—one for scaling up the nodes and one for scaling down.
Create the Code Engine Project
- Log in to the IBM Cloud Console.
- Navigate to Code Engine and create a new project
Create Jobs for Scaling Up and Scaling Down
- In the Code Engine project, create a new job for scaling up the nodes. Name it
scale-up-job
.
- In the job configuration, specify the container image you pushed to IBM Cloud Container Registry.
- Set the entrypoint to run the
scale_nodes.sh
script with the argument for the desired node count (e.g., 3
for scaling up to 3 nodes).
- Create another job for scaling down, named
scale-down-job
, and pass 1
as the argument to scale down the nodes.