Logo

dev-resources.site

for different kinds of informations.

Sparky - hacking minikube with mini tool

Published at
6/14/2024
Categories
k8s
raku
kubernetes
Author
melezhik
Categories
3 categories in total
k8s
open
raku
open
kubernetes
open
Author
8 person written this
melezhik
open
Sparky - hacking minikube with mini tool

TL; DR: How to deploy docker to minikube when one does not need anything fancy, but pure Raku.


So, you have your own pet K8s cluster deployed as minikube and you want to play with it. You have few microservices to build and you don't want to bother with kubernetes low level commands at all.

On the other hand, you setup is complex enough to express it in a bunch of yaml files or kubectl commands. Here is an elegant way to handle this in pure Raku, and it's called Sparky ...


Show me the design

      |-------------------------------------|
      | Sparky -> kubectl -> MiniKube       | 
      |                     /\  /\  /\      |
      |                     pod pod pod     |
      |-------------------------------------| 
Enter fullscreen mode Exit fullscreen mode

So the infrastructure part is simple - on the same host we install minikube and Sparky that underlying uses kubectl to deploy containers into k8s cluster.

Show me the code

As usually Sparky job is a pure Raku code, but this time some plugins will be of use as well ...

Sparky is integrated with - Sparrowhub - https://sparrowhub.io - repository of Sparrow plugins - useful building blocks for any sort of automation.

Let's use a couple of them - k8s-deployment and k8s-pod-check to deploy and check Kubernetes pods. From Sparky point of view those are just Raku functions, with some input parameters.

task-run "dpl create", "k8s-deployment", %(
  :deployment_name<nginx>,
  :app_name<nginx>,
  :image<nginx:1.14.2>,
  :3replicas,
);


# give it some time to allow all pods to start ...
sleep(5);

task-run "nginx pod check", "k8s-pod-check", %(
  :name<nginx>,
  :namespace<default>,
  :die-on-check-fail,
  :3num,
);
Enter fullscreen mode Exit fullscreen mode

For the tutorial purpose we are going to deploy nginx server with 3 replicas, by using k8s-deployment plugin.

Let's give it a try.

First run

And the very first deploy ... fails:

... some output ...

11:03:34 :: deployment.apps/nginx created
11:03:34 :: [repository] - installing k8s-pod-check, version 0.000012
11:03:34 :: [repository] - install Data::Dump to /home/astra/.sparrowdo/minikube/sparrow6/plugins/k8s-pod-check/raku-lib
All candidates are currently installed
No reason to proceed. Use --force-install to continue anyway
[task run: task.pl6 - nginx pod check]
[task stdout]
11:03:41 :: ${:die-on-check-fail(Bool::True), :name("nginx"), :namespace("default"), :num(3)}
11:03:41 :: ===========================
11:03:41 :: NAME                           READY   STATUS             RESTARTS         AGE
11:03:41 :: nginx-77d8468669-5gxbf         0/1     ErrImagePull       0                5s
11:03:41 :: nginx-77d8468669-c5vbl         0/1     ErrImagePull       0                5s
11:03:41 :: nginx-77d8468669-lhc54         0/1     ErrImagePull       0                5s
11:03:41 :: ===========================
11:03:41 :: nginx-77d8468669-5gxbf POD_NOT_OK
11:03:41 :: nginx-77d8468669-c5vbl POD_NOT_OK
11:03:41 :: nginx-77d8468669-lhc54 POD_NOT_OK
[task check]
stdout match <^^ 'nginx' \S+ \s+ POD_OK $$> False
---
Enter fullscreen mode Exit fullscreen mode

Although Kubernetes deployment has been successfully created, further k8s-pod-check failed to verify that all pods are running.

Use of die-on-check-fail option made the job stops strait away after this point.

The reason is in ErrImagePull - nginx docker image is not accessible from within a minikube, which a known minkube DNS issue, which is easy to fix.

It's fixed!

All we need to do is to upload nginx docker image manually, so that minukube will pick it up from a file cache:

minikube image load nginx:1.14.2
Enter fullscreen mode Exit fullscreen mode

Now, when have restarted the failed job we get this:

... some output ...

11:05:42 :: deployment.apps/nginx unchanged
[task run: task.pl6 - nginx pod check]
[task stdout]
11:05:47 :: ${:die-on-check-fail(Bool::False), :name("nginx"), :namespace("default"), :num(3)}
11:05:47 :: ===========================
11:05:47 :: NAME                           READY   STATUS             RESTARTS         AGE
11:05:47 :: nginx-77d8468669-5gxbf         1/1     Running            0                2m10s
11:05:47 :: nginx-77d8468669-c5vbl         1/1     Running            0                2m10s
11:05:47 :: nginx-77d8468669-lhc54         1/1     Running            0                2m10s
11:05:47 :: ===========================
11:05:47 :: nginx-77d8468669-5gxbf POD_OK
11:05:47 :: nginx-77d8468669-c5vbl POD_OK
11:05:47 :: nginx-77d8468669-lhc54 POD_OK
[task check]
stdout match <^^ 'nginx' \S+ \s+ POD_OK $$> True
<3 pods are running> True
---
Enter fullscreen mode Exit fullscreen mode

The last deployment has not changed (with is denoted by "deployment.apps/nginx unchanged" line), as we did not change anything, however minikube now is able to pick the recently uploaded docker image and all pods now are running.

Congratulation with the very first successfully deployment to Kubernetes via Sparky!

Clean up

In the end let's remove our test pods, by using k8s-deployment plugin:

task-run "dpl delete", "k8s-deployment", %(
  :deployment_name<nginx>,
  :action<delete>,
);
Enter fullscreen mode Exit fullscreen mode

Further thoughts

This simple scenario is going to give us some ideas on how to deploy to Kubernetes in imperative way using pure Raku, I, personally like this approach better, as having a bunch of helm charts and yaml files seems overkill when one need just to deploy some none production code, however, as always YMMV, thanks for reading ...

raku Article's
30 articles in total
Favicon
SSH port forwarding from within code
Favicon
SSH port forwarding from within Raku code
Favicon
Solving the Weekly Challenge 302 Task 1: Ones and Zeroes in Python
Favicon
Solving the Weekly Challenge 302 Task 2: Step by Step in Python
Favicon
My Python Language Solution to Task 2: Nested Array from The Weekly Challenge 300
Favicon
My Python Language Solution to Task 1: Beautiful Arrangement from The Weekly Challenge 300
Favicon
My Python Language Solution to Task 1 from The Weekly Challenge 299
Favicon
Sparky - composable user interfaces for internal services
Favicon
Sparky - hacking minikube with mini tool
Favicon
Sparky - simple and efficient alternative to Ansible
Favicon
Confirming The LPW 2024 Venue & Date
Favicon
Announcing The London Perl & Raku Workshop 2024
Favicon
Stability
Favicon
Practicing Raku Grammars On Exercism
Favicon
Languages wanted!
Favicon
Perl and Raku Dev Room @FOSDEM 24
Favicon
Introducing Humming-Bird v3
Favicon
Publishing Raku modules
Favicon
Sorting numbers in Raku with the help of ChatGPT
Favicon
UTF-8 series wrap up
Favicon
UTF-8 Byte Order Mark
Favicon
Fun with UTF-8: Homoglyphs
Favicon
UTF-8 regular expressions
Favicon
Fun with UTF-8: variables and operators
Favicon
UTF-8 sorting and collation
Favicon
UTF-8 grapheme clusters
Favicon
UTF-8 (de)composition
Favicon
UTF-8 code point properties
Favicon
Fun with UTF-8: browsing code points namespace
Favicon
UTF-8 Glyphs and Graphemes

Featured ones: