You've successfully subscribed to Florin Loghiade
Great! Next, complete checkout for full access to Florin Loghiade
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.
Playing around with Kubernetes

Playing around with Kubernetes

in

Today, the first of June, is International Children's Day in Romania, so that is a better way to celebrate than playing around with different things. When I do training or talk with the people I mentor, I tell them that Kubernetes is so powerful and flexible that you can do anything you want, and the limit is your imagination. 

Looking around Github for things that might make my life easier, I found this gem of a repo: it instantiates retro games inside a Kubernetes cluster.

paolomainardi/additronk8s-retro games-Kubernetes-controller: A retro games Kubernetes controller built-in Javascript (github.com)

As a gamer, I had to try and see it for myself. It took me 5 minutes to have Mario running in my Kubernetes cluster. It's so stupid, I love it!

Here's the yaml for MARIO :)

apiVersion: retro.sparkfabrik.com/v1
kind: Game
metadata:
  name: mario
  namespace: games
spec:
  name: "Mario"
  zipUrl: "https://archive.org/download/marioDOS/MARIO.zip"
  dir: "."
  exe: "MARIO.EXE"

But beyond the nostalgic charm, this project served as a reminder of Kubernetes' immense potential. With the proper knowledge and a willingness to experiment, the possibilities are truly endless. This wasn't just about reliving childhood memories but about pushing boundaries and uncovering innovative solutions. Imagine the possibilities – deploying complex AI models within a Kubernetes environment, orchestrating microservices for a high-traffic website, or creating a distributed rendering pipeline for a groundbreaking animation project. The applications are as diverse as our collective imagination.

Now, let's transition from the whimsical to the practical. While retro games may be an amusing diversion, Kubernetes is a powerhouse for real-world automation. I extensively utilize PowerShell scripts and functions within my Kubernetes environment, and they integrate seamlessly; after playing around with them, I cannot imagine doing things differently. In the past, I had VMs with Task Schedulers; I moved on to Azure Automation Accounts and then Azure Functions. The main differentiator? I made the time to figure the system out, and from there, I was hooked. 

PowerShell scripts can seamlessly integrate into Kubernetes pods, automating each process stage. These scripts can be triggered by events within the cluster, ensuring a smooth and efficient flow. Similarly, routine system administration tasks can be automated through PowerShell scripts within Kubernetes, freeing valuable time and resources for more strategic endeavours.

You know what's the fun part? I always need help finding articles on getting this machine started. In Kubernetes, cronjobs are easy, and running Powershell jobs is not a problem.

Here's an example of a CronJob in Kubernetes that schedules a PowerShell script to run every hour:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: hourly-powershell-script
spec:
  schedule: "0 * * * *" # Runs every hour 
  concurrencyPolicy: Forbid 
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: powershell-script
            image: mcr.microsoft.com/powershell:latest
            command: ["pwsh"]
            args: ["-ExecutionPolicy", "Bypass", "-File", "/path/to/script.ps1"]
          restartPolicy: OnFailure

Explanation:

  • apiVersion: Defines the API version for CronJobs (batch/v1).
  • kind: Specifies the kind of object (CronJob).
  • metadata:
    • name: Unique name for your CronJob (here, hourly-powershell-script).
  • spec: Configuration for the CronJob.
    • schedule: Cron expression defining when the job runs (replace with your desired schedule).
      • "0 * * * *" runs every hour (minute 0 of every hour).
    • concurrencyPolicy: Prevents multiple instances of the job running simultaneously (Forbid).
    • jobTemplate: Template for the Job that gets created by the CronJob.
      • spec: Job specification.
        • template: Pod template for the Job.
          • spec: Pod specification.
            • containers: Defines the container running the script.
              • name: Name of the container (powershell-script).
              • image: Docker image containing PowerShell (mcr.microsoft.com/powershell:latest [invalid URL removed]).
              • command: Base command to run PowerShell (pwsh).
              • args: Arguments for the command:
                • "-ExecutionPolicy Bypass": Allows running scripts from untrusted sources (adjust based on your script's security).
                • "-File": Path to your script within the container (/path/to/script.ps1).
            • restartPolicy: Restarts the container if it fails (OnFailure).

This example demonstrates scheduling a basic PowerShell script execution in Kubernetes using a CronJob. You can adapt this approach to various automation tasks that can run anywhere.

So, we started with a bit of fun. We played Mario, and we learned something. At least I did :)

That being said, have a good one!