Using Kubernetes to Perform Operations on GCP Compute Engines

Using Kubernetes to Perform Operations on GCP Compute Engines

Monitor compute engine service with kubernetes cronjob example

Kubernetes workloads often need batch operations, maintenance, or monitoring on services hosted within Google Cloud Platform (GCP). In this post, I'll show you how to use Kubernetes CronJobs to keep an eye on MySQL services running on compute engines. Plus, I'll explain how to trigger phone call alerts with a simple shell script when something goes wrong.

In this easy-to-follow guide, we'll set up a system to keep your MySQL servers running smoothly and send you instant alerts if any issues pop up. While we’re using a specific technology stack for this tutorial, feel free to tweak the components to suit your needs. Our aim is to show you how Kubernetes can help perform important tasks on compute engines, making sure your services stay reliable and efficient.

Prerequisites

  • Make sure you have git, kubectl, and gcloud installed on your machine.

  • Ensure your GCP user has access to the compute engine and Kubernetes engine.

  • GKE Cluster

  • Compute Engine

Let's get started with each step, one by one!

Clone Repository

git clone https://github.com/Mitesh-Gangaramani/linux-service-phone-call-alert.git

Generate SSH keys and use them for authentication

We need an SSH key to log in to the compute engine, so let's create a key pair. This will give us both a public key and a private key.

ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa_k8s_cronjob

Add the public key to the Compute Engine instance

We need to add the public key id_rsa_k8s_cronjob.pub (which we generated in the previous step) to each compute engine instance. This will allow connections from the Kubernetes CronJob.

gcloud compute instances add-metadata <INSTANCE_NAME> --metadata-from-file ssh-keys=~/.ssh/id_rsa_k8s_cronjob.pub

Add ssh key as kubernetes secret

To securely connect to your MySQL servers, we'll store SSH keys in Kubernetes secrets. This way, your credentials are safely managed and only accessible to the pods that need them. We'll create all resources inside the mitesh-test namespace.

kubectl create secret generic mysql-secret-keys --from-file=~/.ssh/id_rsa_k8s_cronjob -n mitesh-test

Create configmap

Let's add this configmap to store the configuration, and we'll include it in our Kubernetes YAML manifest.

kubectl create configmap mysql-monitoring-configmap --from-file=service-monitor-script.sh -n mitesh-test

Create the cronjob with desired schedule

We will volume mount our configmap and Kubernetes secret that we created earlier, along with the cronjob schedule. You can use any Linux distribution image that has curl installed.

kubectl create -f mysql-monitoring-cron.yaml

We just looked at an example of monitoring a specific Linux service. However, you can use this approach with any stack you prefer.

You can find the source code on GitHub: Linux-service-phone-call-alert.

If you have other methods, please share your thoughts! I would love to hear your feedback and see your comments on this post.