Help us understand the problem. What is going on with this article?

k3dでPrivateRepositoryを使う

公式ドキュメント

https://github.com/rancher/k3d/blob/master/docs/examples.md

  • LinuxとMacとで、ホームディレクトリのパスは読み換える

環境

  • Mac

やること

Docker上でPrivateRegistryを起動して、k3dでのPodデプロイ時にPrivateRegistryからPullする

PrivateRegisty用のボリューム作成

docker volume create local_registry
❯ docker volume ls | grep local_registrylocal
local_registry

PrivateRegistryコンテナ起動

  • レジストリ名はregistry.local
  • ポートは5000 コンテナ側もローカル側も
docker container run -d --name registry.local -v local_registry:/var/lib/registry --restart always -p 5000:5000 registry:2

ローカルにコンフィグファイル置き場を作成

mkdir -p ~/.k3d

k3dコンフィグをローカルに作成

vim ~/.k3d/config.toml.tmpl
~/.k3d/config.toml.tmpl
# Original section: no changes
[plugins.opt]
path = "{{ .NodeConfig.Containerd.Opt }}"
[plugins.cri]
stream_server_address = "{{ .NodeConfig.AgentConfig.NodeName }}"
stream_server_port = "10010"
{{- if .IsRunningInUserNS }}
disable_cgroup = true
disable_apparmor = true
restrict_oom_score_adj = true
{{ end -}}
{{- if .NodeConfig.AgentConfig.PauseImage }}
sandbox_image = "{{ .NodeConfig.AgentConfig.PauseImage }}"
{{ end -}}
{{- if not .NodeConfig.NoFlannel }}
  [plugins.cri.cni]
    bin_dir = "{{ .NodeConfig.AgentConfig.CNIBinDir }}"
    conf_dir = "{{ .NodeConfig.AgentConfig.CNIConfDir }}"
{{ end -}}

# Added section: additional registries and the endpoints
[plugins.cri.registry.mirrors]
  [plugins.cri.registry.mirrors."registry.local:5000"]
    endpoint = ["http://registry.local:5000"]

k3d起動

CLUSTER_NAME=k3s-default
k3d create \
    --name ${CLUSTER_NAME} \
    --wait 0 \
    --auto-restart \
    --volume ~/.k3d/config.toml.tmpl:/var/lib/rancher/k3s/agent/etc/containerd/config.toml.tmpl

kubectlコマンドをk3sを使用するための環境変数をセット

export KUBECONFIG="$(k3d get-kubeconfig --name='k3s-default')"

k3dとDockerとNetworkを連結

docker network connect k3d-k3s-default registry.local

hostsへPrivateRegistry名を定義

❯ tail -n 1 /etc/hosts
127.0.0.1 registry.local

イメージがPushできるか確認

docker pull nginx:latest
docker tag nginx:latest registry.local:5000/nginx:latest
docker push registry.local:5000/nginx:latest

PrivateRegistryからPullしてくるDeployment

cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-test-registry
  labels:
    app: nginx-test-registry
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-test-registry
  template:
    metadata:
      labels:
        app: nginx-test-registry
    spec:
      containers:
      - name: nginx-test-registry
        image: registry.local:5000/nginx:latest
        ports:
        - containerPort: 80
EOF

Pullできたか確認

~ ❯❯❯ kubectl get pod
NAME                                   READY   STATUS    RESTARTS   AGE
nginx-test-registry-7f6c7b79fb-mhxpm   1/1     Running   0          5m36s
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away