Observe a running pod on IBM Cloud Code Engine with kubectl commands

In IBM Cloud Code Engine you also can use kubectl commands to get information about your running application in addition to the IBM Cloud Code Engine CLI.

The blog post uses code extractions of bash automation functions from the question-answering GitHub project to show this.

Restriction: The bash automation works only for an application scaled to one instance. This automation is made for initial testing of a running application.

These are the automation steps in bash automation of the observe-deployment-container.sh script:

  1. Log in to IBM Cloud
  2. Connect to an existing IBM Cloud Code Engine project
  3. List basic information from the IBM Cloud Code Engine project, with the kubectl command
  4. Log the information for one pod of the application

Here are the functions:

1. Log in to IBM Cloud

function login_to_ibm_cloud () {
    
    echo ""
    echo "*********************"
    echo "Log in to IBM Cloud"
    echo "*********************"
    echo ""

    ibmcloud login --apikey $IBM_CLOUD_API_KEY
    ibmcloud target -r $IBM_CLOUD_REGION
    ibmcloud target -g $IBM_CLOUD_RESOURCE_GROUP
}

2. Connect to an existing IBM Cloud Code Engine project

These are automated steps:

  1. Connect to IBM Cloud Code Engine project
  2. Get the kubecfg to connect to the related cluster
  3. Get the project namespace
  4. List all running pods
function connect_ce_project() {
  echo "**********************************"
  echo " Using following project: $CODEENGINE_PROJECT_NAME" 
  echo "**********************************"

  # 1. Connect to IBM Cloud Code Engine project
  ibmcloud ce project select -n $CODEENGINE_PROJECT_NAME
  
  # 2. Get the kubecfg to connect to the related cluster
  ibmcloud ce project select -n $CODEENGINE_PROJECT_NAME --kubecfg

  # 3. Get the project namespace
  CODEENGINE_PROJECT_NAMESPACE=$(ibmcloud ce project get --name $CODEENGINE_PROJECT_NAME --output json | grep "namespace" | awk '{print $2;}' | sed 's/"//g' | sed 's/,//g')
  echo "Code Engine project namespace: $CODEENGINE_PROJECT_NAMESPACE"
  
  # 4. List all running pods
  kubectl get pods -n $CODEENGINE_PROJECT_NAMESPACE
  
}

3. List basic information from the IBM Cloud Code Engine project, with the kubectl command

function kube_information(){

    echo "************************************"
    echo " Kubernetes info '$CODEENGINE_APP_NAME': pods, deployments and configmaps details "
    echo "************************************"
    
    kubectl get pods -n $CODEENGINE_PROJECT_NAMESPACE
    kubectl get deployments -n $CODEENGINE_PROJECT_NAMESPACE
    kubectl get configmaps -n $CODEENGINE_PROJECT_NAMESPACE

}

4. Log pod messages

These are automated steps:

  1. Find the pod for the application
  2. Show the logs for this pod
function kube_pod_log(){

    echo "************************************"
    echo " Kubernetes $CODEENGINE_APP_NAME: log"
    echo "************************************"
    
    # 1. Find the pod for the application
    FIND=$CODEENGINE_APP_NAME
    APP_POD=$(kubectl get pod -n $CODEENGINE_PROJECT_NAMESPACE | grep $FIND | awk '{print $1}')
    echo "************************************"
    echo "Show log for the pod: $APP_POD"
    echo "************************************"
    
    # 2. Show the logs for this pod
    kubectl logs -f $APP_POD
}

I hope this was useful to you, and let’s see what’s next?

Greetings,

Thomas

#codeengine, #ce, #bashscripting, #automation, #ibmcloud, #kubernetes, #kubectl

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑

%d bloggers like this: