Sparky - hacking minikube with mini tool

Alexey Melezhik - Jun 14 - - Dev Community

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 ...

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player