1
+ #! /bin/sh
2
+ set -o errexit
3
+
4
+ # 1. Create registry container unless it already exists
5
+ reg_name=' kind-registry'
6
+ reg_port=' 5001'
7
+ if [ " $( docker inspect -f ' {{.State.Running}}' " ${reg_name} " 2> /dev/null || true) " != ' true' ]; then
8
+ docker run \
9
+ -d --restart=always -p " 127.0.0.1:${reg_port} :5000" --name " ${reg_name} " \
10
+ registry:2
11
+ fi
12
+
13
+ # 3. Add the registry config to the nodes
14
+ #
15
+ # This is necessary because localhost resolves to loopback addresses that are
16
+ # network-namespace local.
17
+ # In other words: localhost in the container is not localhost on the host.
18
+ #
19
+ # We want a consistent name that works from both ends, so we tell containerd to
20
+ # alias localhost:${reg_port} to the registry container when pulling images
21
+ REGISTRY_DIR=" /etc/containerd/certs.d/localhost:${reg_port} "
22
+ for node in $( kind get nodes -A) ; do
23
+ docker exec " ${node} " mkdir -p " ${REGISTRY_DIR} "
24
+ cat << EOF | docker exec -i "${node} " cp /dev/stdin "${REGISTRY_DIR} /hosts.toml"
25
+ [host."http://${reg_name} :5000"]
26
+ EOF
27
+ done
28
+
29
+ # 4. Connect the registry to the cluster network if not already connected
30
+ # This allows kind to bootstrap the network but ensures they're on the same network
31
+ if [ " $( docker inspect -f=' {{json .NetworkSettings.Networks.kind}}' " ${reg_name} " ) " = ' null' ]; then
32
+ docker network connect " kind" " ${reg_name} "
33
+ fi
34
+
35
+ # 5. Document the local registry
36
+ # https://github.com/kubernetes/enhancements/tree/master/keps/sig-cluster-lifecycle/generic/1755-communicating-a-local-registry
37
+ cat << EOF | kubectl apply -f -
38
+ apiVersion: v1
39
+ kind: ConfigMap
40
+ metadata:
41
+ name: local-registry-hosting
42
+ namespace: kube-public
43
+ data:
44
+ localRegistryHosting.v1: |
45
+ host: "localhost:${reg_port} "
46
+ help: "https://kind.sigs.k8s.io/docs/user/local-registry/"
47
+ EOF
0 commit comments