CLOVER🍀

That was when it all began.

MicroK8sのregistryアドオンの組み込みレジストリー(プライベートレジストリー)を試す

これは、なにをしたくて書いたもの?

Kubernetesでいろいろ試そうと思うとプライベートレジストリーが欲しくなったりするものですが、割と面倒な気がします。

MicroK8sだとregistryアドオンで多少簡単に導入できそうだったので試してみることにしました。

How to use the built-in registry

MicroK8sのregistryアドオン

registryアドオンを使ったレジストリーの使い方はこちら。

How to use the built-in registry

アドオンを使って導入するプライベートレジストリーは、組み込みレジストリーと呼んでいるみたいです。

流れとしては以下になります。

  • MicroK8sのregistryアドオンを有効にする
  • Dockerデーモンに対してinsecure-registriesの設定をする

レジストリーの用意が簡単なのがよいところでしょうか。試してみましょう。

ちなみに、組み込みではないプライベートレジストリーについてのドキュメントはこちらで、

How to work with a private registry

アドオンの一覧はこちらです。

MicroK8s Addons

環境

今回の環境はこちら。

$ microk8s version
MicroK8s v1.31.2 revision 7394


$ microk8s kubectl version
Client Version: v1.31.2
Kustomize Version: v5.4.2
Server Version: v1.31.2

Dockerのバージョン。

$ docker version
Client: Docker Engine - Community
 Version:           27.3.1
 API version:       1.47
 Go version:        go1.22.7
 Git commit:        ce12230
 Built:             Fri Sep 20 11:41:00 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          27.3.1
  API version:      1.47 (minimum version 1.24)
  Go version:       go1.22.7
  Git commit:       41ca978
  Built:            Fri Sep 20 11:41:00 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.23
  GitCommit:        57f17b0a6295a39009d861b89e3b3b87b005ca27
 runc:
  Version:          1.1.14
  GitCommit:        v1.1.14-0-g2c9f560
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

MicroK8sのregistryアドオンを使う

それでは、MicroK8sのregistryアドオンを使ってみましょう。

まずはregistryアドオンを有効にします。

$ microk8s enable registry

ログ。

Infer repository core for addon registry
Infer repository core for addon hostpath-storage
Enabling default storage class.
WARNING: Hostpath storage is not suitable for production environments.
         A hostpath volume can grow beyond the size limit set in the volume claim manifest.

deployment.apps/hostpath-provisioner created
storageclass.storage.k8s.io/microk8s-hostpath created
serviceaccount/microk8s-hostpath created
clusterrole.rbac.authorization.k8s.io/microk8s-hostpath created
clusterrolebinding.rbac.authorization.k8s.io/microk8s-hostpath created
Storage will be available soon.
The registry will be created with the size of 20Gi.
Default storage class will be used.
namespace/container-registry created
persistentvolumeclaim/registry-claim created
deployment.apps/registry created
service/registry created
configmap/local-registry-hosting configured

レジストリーの容量は、デフォルトで20Gです。

もっと大きなレジストリーとしたい場合は以下のように指定するみたいです。

$ microk8s enable registry:size=40Gi

microk8s statusで見てみると

$ microk8s status

registryが現れます。32000ポートでリッスンしているようです。

addons:
  enabled:
    dns                  # (core) CoreDNS
    ha-cluster           # (core) Configure high availability on the current node
    helm                 # (core) Helm - the package manager for Kubernetes
    helm3                # (core) Helm 3 - the package manager for Kubernetes
    hostpath-storage     # (core) Storage class; allocates storage from host directory
    registry             # (core) Private image registry exposed on localhost:32000
    storage              # (core) Alias to hostpath-storage add-on, deprecated

では、プライベートレジストリーにpushするイメージを作成します。nginxをベースにしましょう。

Dockerfile

FROM nginx:1.27.2-bookworm

といっても、イメージ名を変えたくらいですが。イメージ名のドメイン名の部分はlocalhost:32000にします。

$ docker image build -t localhost:32000/nginx:1.27.2-bookworm .

作成できました。

$ docker image ls
REPOSITORY              TAG               IMAGE ID       CREATED       SIZE
localhost:32000/nginx   1.27.2-bookworm   60c8a892f36f   6 weeks ago   192MB

pushしてみましょう。

$ docker image push localhost:32000/nginx:1.27.2-bookworm

insecure-registriesの設定を入れていませんが、pushできてしまいました…。

The push refers to repository [localhost:32000/nginx]
61ef4e878aac: Pushed
a0c145a29c8d: Pushed
a1fe8b721bb1: Pushed
19b722697f76: Pushed
ffe4285e2906: Pushed
7dca41ff1486: Pushed
c3548211b826: Pushed
1.27.2-bookworm: digest: sha256:2ebf3d369d813bcc6a531ba43e1859bd91ad5c8217ae33b821f5ffada06a6cd4 size: 1778

ドキュメントでは、一部のDockerのバージョンでは失敗する可能性があるくらいに書かれていましたが…。

Pushing to this insecure registry may fail in some versions of Docker unless the daemon is explicitly configured to trust this registry.

今回は気にせず進めてみましょう。

pushしたイメージを使って、Deploymentを作成。

$ microk8s kubectl create deployment nginx --image=localhost:32000/nginx:1.27.2-bookworm
deployment.apps/nginx created

Podが動いていることを確認。

$ microk8s kubectl get all
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-75fd58947c-fhtnj   1/1     Running   0          17s

NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.152.183.1   <none>        443/TCP   72m

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   1/1     1            1           17s

NAME                               DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-75fd58947c   1         1         1       17s

Serviceも作成しましょう。

$ microk8s kubectl expose deployment nginx --port 80
service/nginx exposed

確認。

$ microk8s kubectl get all
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-75fd58947c-fhtnj   1/1     Running   0          72s

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.152.183.1     <none>        443/TCP   73m
service/nginx        ClusterIP   10.152.183.208   <none>        80/TCP    29s

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   1/1     1            1           72s

NAME                               DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-75fd58947c   1         1         1       72s

アクセスしてみます。

$ curl 10.152.183.208
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

OKですね。

後片付け。

$ microk8s kubectl delete service nginx
$ microk8s kubectl delete deployment nginx


$ microk8s kubectl get all
NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.152.183.1   <none>        443/TCP   84m

オマケ

今回は不要でしたが、insecure-registriesを設定する場合は/etc/docker/daemon.jsonに以下のように設定して

/etc/docker/daemon.json

{
  "insecure-registries" : ["localhost:32000"]
}

Dockerデーモンを再起動します。

$ sudo systemctl restart docker

おわりに

MicroK8sのregistryアドオンを使って、組み込みレジストリー(プライベートレジストリー)を使ってみました。

かなり簡単に導入できたので驚きました。Kubernetesを使ってちょっと確認をしたい時や開発用途などで便利なのではないでしょうか。