{"id":1155,"date":"2021-07-27T00:38:27","date_gmt":"2021-07-27T00:38:27","guid":{"rendered":"http:\/\/tiemensfamily.com\/timoncs\/?p=1155"},"modified":"2021-08-01T03:29:59","modified_gmt":"2021-08-01T03:29:59","slug":"kubernetes-and-buildah-and-microk8s","status":"publish","type":"post","link":"https:\/\/tiemensfamily.com\/timoncs\/2021\/07\/27\/kubernetes-and-buildah-and-microk8s\/","title":{"rendered":"Kubernetes and Buildah and microk8s"},"content":{"rendered":"\n<p>Kubernetes is awesome.  But it is very annoying to be halfway through a guide just to have that guide &#8220;drop into&#8221; docker.  Kubernetes _can_ use docker, but it doesn&#8217;t _need_ docker.  In particular, this is true of <a href=\"https:\/\/microk8s.io\/\" data-type=\"URL\" data-id=\"https:\/\/microk8s.io\/\">microk8s<\/a>.  If you have your kubernetes cluster because of microk8s, you won&#8217;t have docker installed.<\/p>\n\n\n\n<p>As an alternative to &#8220;docker build&#8221;, you can use <a href=\"https:\/\/buildah.io\/\" data-type=\"URL\" data-id=\"https:\/\/buildah.io\/\">buildah<\/a> &#8211; &#8220;a tool that builds Open Container Initiative (OCI) container images&#8221;.  All that needs done is (1) translate those &#8220;docker xxx&#8221; commands to buildah, and (2) navigate the registries so that your buildah-built image ends up somewhere that your kubectl can use it.<\/p>\n\n\n\n<b style=\"text-align: right\">Dockerfile &#8211; original<\/b>\n<pre class=\"wp-block-code\"><code style=\"font-size: 11px\">FROM openjdk:11-jre-slim\nWORKDIR \/app\nCOPY target\/java-hello-world-0.0.1.jar java-hello-world.jar\nENTRYPOINT [\"java\", \"-jar\", \"\/app\/java-hello-world.jar\"]\nEXPOSE 8080<\/code><\/pre>\n\n\n\n<p>(Gads &#8211; a little side note here: WordPress by default allows .doc file types, but denies .txt file types &#8220;for security reasons&#8221;.  It brings into doubt WordPress&#8217;s concept of &#8216;security&#8217;. )<\/p>\n\n\n\n<p>Note: there are two machines here:  &#8220;bld$&#8221; is the prompt of the buildah machine.  &#8220;k8s$&#8221; is the prompt of the microk8s kubernetes machine.<\/p>\n\n\n\n<p>0. Diversion &#8211; create the target\/java-hello-world-0.0.1.jar file by installing java, cloning source from github.  You can skip this step if you already have a java .jar file.<\/p>\n\n\n\n<b style=\"text-align: right\">Diversion: install java, create java-hello-world-0.0.1.jar<\/b>\n<pre class=\"wp-block-code\"><code style=\"font-size: 11px\">bld$ sudo apt install openjdk-11-jdk-headless\nbld$ git clone https:\/\/github.com\/bmuschko\/ckad-study-guide.git\nbld$ cd ckad-study-guide\/ch02\/containerized-java-app\nbld$ .\/mvnw  package spring-boot:repackage\n...snip...\nbld$ ls -hl target\/java-hello-world-0.0.1.jar\n-rw-rw-r-- 17M Jun  8 23:53 target\/java-hello-world-0.0.1.jar\n<\/code><\/pre>\n\n\n\n<p>1. Install buildah, fix the Dockerfile, and create the container image.<\/p>\n\n\n\n<b style=\"text-align: right\">Command: install buildah and java<\/b>\n<pre class=\"wp-block-code\"><code style=\"font-size: 11px\">bld$ sudo apt-get -y install buildah<\/code><\/pre>\n\n\n\n<b style=\"text-align: right\">Dockerfile &#8211; fixed for buildah<\/b>\n<pre class=\"wp-block-code\"><code style=\"font-size: 11px\">FROM docker.io\/openjdk:11-jre-slim\nWORKDIR \/app\nCOPY target\/java-hello-world-0.0.1.jar java-hello-world.jar\nENTRYPOINT [\"java\", \"-jar\", \"\/app\/java-hello-world.jar\"]\nEXPOSE 8080<\/code><\/pre>\n\n\n\n<b style=\"text-align: right\">Command: build with buildah<\/b>\n<pre class=\"wp-block-code\"><code style=\"font-size: 11px\">bld$ buildah bud -t java-hello-world:1.0.0 . \n\n-- NOTE: \"buildah images\" is the correct command to list images\nbld$ buildah images\nREPOSITORY                    TAG           IMAGE ID       CREATED          SIZE\nlocalhost\/java-hello-world    1.0.0         a24b49859451   7 weeks ago      241 MB\n\n-- Note: \"buildah list\" == \"buildah containers\" == not the correct command\n--       at this step in the process.  kept here only for historical purposes.\nbld$ buildah list\nCONTAINER ID  BUILDER  IMAGE ID     IMAGE NAME                       CONTAINER NAME\n39d1427e4dad     *     a24b49859451 localhost\/java-hello-world:1.0.0 java-hello-world-working-container<\/code><\/pre>\n\n\n\n<p>At this time, you have a OCI container image.<\/p>\n\n\n\n<ul><li>(Diversion &#8211; create a registry using &#8220;buildah run&#8221;.  Push the container image.)<\/li><\/ul>\n\n\n\n<b style=\"text-align: right\">Diversion: running a registry with buildah run<\/b>\n<pre class=\"wp-block-code\"><code style=\"font-size: 11px\"># NOTE: these steps are not required.  The goal is to get it to your microk8s registry, not to create another registry as done here:\nbld$ registry=$(buildah from registry)\nbld$ buildah run $registry\nbld$ buildah push --tls-verify=false java-hello-world:1.0.0 docker:\/\/localhost:5000\/java-hello-world\/java-hello-world:1.0.0\n <\/code><\/pre>\n\n\n\n<p>2. Start microk8s registry, collect IP information<\/p>\n\n\n\n<b style=\"text-align: right\">Command: Start the microk8s registry<\/b>\n<pre  class=\"wp-block-code\"><code style=\"font-size: 11px\">k8s$ microk8s enable registry\nThe registry will be created with the default size of 20Gi.\nYou can use the \"size\" argument while enabling the registry, eg microk8s.enable registry:size=30Gi\nAddon storage is already enabled.\nApplying registry manifest\nnamespace\/container-registry created\npersistentvolumeclaim\/registry-claim created\ndeployment.apps\/registry created\nservice\/registry created\nconfigmap\/local-registry-hosting configured\nThe registry is enabled\n\n\n\n$ kubectl get po -A | grep registry\ncontainer-registry   registry-9b57d9df8-hp79v                     1\/1     Running   0          3d19h\nk8s$ kubectl get po --namespace=container-registry registry-9b57d9df8-hp79v                  -o wide\nNAME                       READY   STATUS    RESTARTS   AGE     IP             NODE                NOMINATED NODE   READINESS GATES\nregistry-9b57d9df8-hp79v   1\/1     Running   0          3d19h   10.1.119.169   ub2004microk8stwo   <none>           <none>\nk8s$ echo 10.1.119.169 is not \"externally available\"\nk8s$ kubectl get no -o wide | grep ub2004microk8stwo\nub2004microk8stwo     Ready    <none>   211d   v1.19.10-34+8f8eec7c3f1428   10.0.1.80     <none>        Ubuntu 20.04 LTS   5.4.0-72-generic   containerd:\/\/1.3.7\nk8s$ REGISTRY=10.0.1.80:32000\n <\/code><\/pre>\n\n\n\n<b style=\"text-align: right\">Command: Push from buildah machine to microk8s registry<\/b>\n<pre class=\"wp-block-code\"><code style=\"font-size: 11px\">bld$ REGISTRY=10.0.1.80:32000\nbld$ buildah push --tls-verify=false java-hello-world:1.0.0 ${REGISTRY}\/java-hello-world:1.0.0\n <\/code><\/pre>\n\n\n\n<b style=\"text-align: right\">Command: Confirm the image is in the registry<\/b>\n<pre class=\"wp-block-code\"><code style=\"font-size: 11px\">k8s$ REGISTRY=10.0.1.80:32000\nk8s$ curl http:\/\/$REGISTRY\/v2\/_catalog\n{\"repositories\":[\"java-hello-world\"]}\nk8s$ curl http:\/\/$REGISTRY\/v2\/java-hello-world\/tags\/list\n{\"name\":\"java-hello-world\",\"tags\":[\"1.0.0\"]}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<b style=\"text-align: right\">deployment.yaml &#8211; using minik8s registry as image source<\/b>\n<pre class=\"wp-block-code\"><code style=\"font-size: 11px\">apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: exported-java-deployment\n  labels:\n    app: exportedjava\nspec:\n  selector:\n    matchLabels:\n      app: exportedjava\n  template:\n    metadata:\n      labels:\n        app: exportedjava\n    spec:\n      containers:\n      - name: java-hello-world\n        image: localhost:32000\/java-hello-world:1.0.0      \n        imagePullPolicy: Never\n        ports:\n        - containerPort: 8080\n<\/code><\/pre>\n\n\n\n<b style=\"text-align: right\">Command: Apply the deployment<\/b>\n<pre class=\"wp-block-code\"><code style=\"font-size: 11px\">k8s$ kubectl apply -f deployment.yaml <\/code><\/pre>\n\n\n\n<b style=\"text-align: right\">Command: Test deployment<\/b>\n<pre  class=\"wp-block-code\"><code style=\"font-size: 11px\">k8s$ kubectl get po -o wide\nNAME                                        READY   STATUS    RESTARTS   AGE    IP             NODE                NOMINATED NODE   READINESS GATES\nexported-java-deployment-54cbc44997-ttgxc   1\/1     Running   0          103s   10.1.119.149   ub2004microk8stwo   <none>           <none>\nk8s$ curl http:\/\/10.1.119.149:8080\nHello World!<\/code><\/pre>\n\n\n\n<p>(Miscellaneous commands &#8211; query running containers for image names and sizes)<\/p>\n\n\n\n<b style=\"text-align: right\">Command: Show running images &#8211; names and sizes<\/b>\n<pre class=\"wp-block-code\"><code style=\"font-size: 11px\">k8s$ kubectl get nodes -o json | jq '.items[].status.images[] | .names[1], .sizeBytes'\n\"docker.io\/bitnami\/mongodb:4.4.2-debian-10-r27\"\n180259295\n\"docker.io\/kubernetesui\/dashboard:v2.0.0\"\n66209190\n\"docker.io\/coredns\/coredns:1.6.6\"\n12932169\n\"k8s.gcr.io\/pause:3.1\"\n317164\n\"localhost:32000\/java-hello-world:1.0.0\"\n97008161\n<\/code><\/pre>\n\n\n\n<b style=\"text-align: right\">Command: Alternative way to copy\/import image<\/b>\n<pre class=\"wp-block-code\"><code style=\"font-size: 11px\">\n-- FAILS, wrong way to \"push\"\/export the image\nbld$ buildah push 1bb7f7cb0d2c dir:\/tmp\/1bb7f7cb0d2c\nGetting image source signatures\nCopying blob 40093787e10f done  \nCopying blob e1929b65aa97 done  \nCopying blob 1b739069a094 done  \nCopying blob 2655e3fdba88 done  \nCopying blob d32026d4252e done  \nCopying blob c76dc6af9411 done  \nCopying blob 89efdf5cc8ae done  \nCopying blob d9b44548153b done  \nCopying blob 8f2b297d408f done  \nCopying blob 89f7fb50be94 done  \nCopying config 1bb7f7cb0d done  \nWriting manifest to image destination\nStoring signatures\n$bld tar cvf 1bb7f7cb0d2c.tar .\/1bb7f7cb0d2c\/\n$bld scp 1bb7f7cb0d2c.tar k8s:~\n\nk8s$ mkcrok8s ctr image import 1bb7f7cb0d2c.tar\nctr: unrecognized image format\n--   Fail.\n\n-- Correct way to 'buildah push\", but incorrect way to create .tar file, FAILS:\nbld$ buildah push 1bb7f7cb0d2c oci:\/tmp\/oci-path:localhost\/mysimpleapi:1.0.0\nbld$ cd \/tmp\nbld$ tar cvf oci-path.tar  .\/oci-path\nk8s$ microk8s ctr image import oci-path.tar\nctr: unrecognized image format\n\n-- Correct way to create the .tar file:  WORKS:\nbld$ cd \/tmp\nbld$ cd oci-path\nbld$ tar cvf ..\/mysimpleapi.tar .\nbld$ scp\nk8s$ microk8s ctr image import mysimpleapi.tar\nunpacking localhost\/mysimpleapi:1.0.0 (sha256:xxxxxx)...done\nk8s$ microk8s ctr image list -q | grep mysimpleapi\nlocalhost\/mysimpleapi:1.0.0\n\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Kubernetes is awesome. But it is very annoying to be halfway through a guide just to have that guide &#8220;drop into&#8221; docker. Kubernetes _can_ use docker, but it doesn&#8217;t _need_ docker. In particular, this is true of microk8s. If you &hellip; <a href=\"https:\/\/tiemensfamily.com\/timoncs\/2021\/07\/27\/kubernetes-and-buildah-and-microk8s\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/posts\/1155"}],"collection":[{"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/comments?post=1155"}],"version-history":[{"count":24,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/posts\/1155\/revisions"}],"predecessor-version":[{"id":1180,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/posts\/1155\/revisions\/1180"}],"wp:attachment":[{"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/media?parent=1155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/categories?post=1155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/tags?post=1155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}