3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

k3dでPrivateRepositoryを使う

Last updated at Posted at 2019-09-18

公式ドキュメント

  • 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
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?