Interacting with Kubernetes from IBM Cloud Function with Kubernetes Client
The most common way to interact with Kubernetes cluster is via kubectl. It is fast, easy to use, and it can be scripted to allow automation for deployment, scheduling of tasks, and so on. However this ease also gave some operational challenge such as maintenance of complex script, lack of logs, and most troublesome is, the expiry of token to access underlying platform and Kubernetes cluster (yet another scripts just to solve that).
Kubernetes Client Library provides a good alternative to automate some of the periodical tasks, by writing small program and execute via serverless technology such as IBM Cloud Function. The following is the example to using Nodejs version of Kubernetes Client library to scale the deployment in IBM Kubernetes Service cluster.
Below you can execute Kubernetes command, you will require to perform authentication to the underlying platform, example IAM from IBM Cloud, then retrieve the KubeConfig. There are 2 main elements to manage Kubernetes cluster, either using API or using kubectl — KubeConfig.yml and PEM (Privacy Enhanced Mail) certificate that is needed by KubeConfig.yml to authenticate and authorize. The flow of interaction is illustrated as below:
Just like my previous post, it is similar approach to package the application into zip file and push to IBM Cloud Function as action.
Step 1: Generate IBM Cloud API key
Login to IBM Cloud Console, navigate to Top Menu > Manage > Access (IAM). At left sub menu select IBM Cloud API Keys, click button Create an IBM Cloud API key to generate a new Cloud API for this job. It is good practice to have different key for different purpose to minimize the security exposure. Copy the key in your clipboard, this will be used later.
Step 2: Get Kubernetes Cluster ID
Navigate to your Kubernetes cluster Overview tab and copy your cluster ID for later use.
Step 3: Application code and packaging
The sample code is available at https://github.com/ongkhaiwei/IKSScale. Use git clone
to download the source code. Execute npm install
to install node dependencies locally. Subsequently zip the file and dependency folder to become a package zip -r action.zip *
Step 4: Push the package to IBM Cloud Function.
Login to IBM Cloud using command line interface. Execute following command to push the action to IBM Cloud Function. The main reason using command line is kubernetes client library is not part of default npm library available in IBM Cloud. If any additional library used in the application, you can package as npm files and upload manually. Also take note that nodejs:10 is required due to async/await.
ibmcloud fn action update packageIKSScale action.zip --kind nodejs:10
Step 5: Define Parameters
Click on packageIKSScale, Parameters and enter the following 5 parameters needed by the application:
{
"icApiToken": "IBM_CLOUD_API_KEY",
"clusterId": "CLUSTER_ID",
"namespace": "NAMESPACE_OF_YOUR_DEPLOYMENT",
"deployment": "DEPLOYMENT_NAME",
"replica": "TARGET_REPLICA_TO_SCALE"
}
Step 6: Test Action
Navigate to code, click Invoke button for 2 times to fully test the application. The second invoke will return you success.
On the other hand, you can either check from kubectl to double confirm the scale worked successfully.
Step 7: Define Trigger
Final step will be define trigger, in this case you can define the scale up and down of deployment every midnight before the backup of persistent volume happened.
Sample code is available at: https://github.com/ongkhaiwei/IKSScale
Happy coding!