|
1 | 1 | # Use a local registry with KinD
|
2 | 2 |
|
3 |
| -A local registry can save on bandwidth costs and means your OpenFaaS functions don't leave your local computer when running `faas-cli up` |
| 3 | +Whilst a remote registry is the easiest way to get started when developing functions, a local registry can be faster for development and testing. |
4 | 4 |
|
5 |
| -Not only is it much quicker, but it's also simple to configure if you're using KinD. |
| 5 | +Using a local registry is an optimisation, which requires some additional tooling and configuration. |
6 | 6 |
|
7 | 7 | ## Prerequisite:
|
8 | 8 |
|
9 |
| -You need to have **Docker** installed on your machine. |
| 9 | +You need to have **Docker** installed on your machine, arkade is also recommended for installing the necessary tools, however you can install them manually if you prefer. |
10 | 10 |
|
11 | 11 | ### Install arkade
|
12 | 12 |
|
@@ -48,31 +48,55 @@ The example below was copied from the [KinD documentation](https://kind.sigs.k8s
|
48 | 48 | #!/bin/sh
|
49 | 49 | set -o errexit
|
50 | 50 |
|
51 |
| -# create registry container unless it already exists |
| 51 | +# 1. Create registry container unless it already exists |
52 | 52 | reg_name='kind-registry'
|
53 | 53 | reg_port='5001'
|
54 | 54 | if [ "$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)" != 'true' ]; then
|
55 | 55 | docker run \
|
56 |
| - -d --restart=always -p "127.0.0.1:${reg_port}:5000" --name "${reg_name}" \ |
| 56 | + -d --restart=always -p "127.0.0.1:${reg_port}:5000" --network bridge --name "${reg_name}" \ |
57 | 57 | registry:2
|
58 | 58 | fi
|
59 | 59 |
|
60 |
| -# create a cluster with the local registry enabled in containerd |
| 60 | +# 2. Create kind cluster with containerd registry config dir enabled |
| 61 | +# TODO: kind will eventually enable this by default and this patch will |
| 62 | +# be unnecessary. |
| 63 | +# |
| 64 | +# See: |
| 65 | +# https://github.com/kubernetes-sigs/kind/issues/2875 |
| 66 | +# https://github.com/containerd/containerd/blob/main/docs/cri/config.md#registry-configuration |
| 67 | +# See: https://github.com/containerd/containerd/blob/main/docs/hosts.md |
61 | 68 | cat <<EOF | kind create cluster --config=-
|
62 | 69 | kind: Cluster
|
63 | 70 | apiVersion: kind.x-k8s.io/v1alpha4
|
64 | 71 | containerdConfigPatches:
|
65 | 72 | - |-
|
66 |
| - [plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:${reg_port}"] |
67 |
| - endpoint = ["http://${reg_name}:5000"] |
| 73 | + [plugins."io.containerd.grpc.v1.cri".registry] |
| 74 | + config_path = "/etc/containerd/certs.d" |
68 | 75 | EOF
|
69 | 76 |
|
70 |
| -# connect the registry to the cluster network if not already connected |
| 77 | +# 3. Add the registry config to the nodes |
| 78 | +# |
| 79 | +# This is necessary because localhost resolves to loopback addresses that are |
| 80 | +# network-namespace local. |
| 81 | +# In other words: localhost in the container is not localhost on the host. |
| 82 | +# |
| 83 | +# We want a consistent name that works from both ends, so we tell containerd to |
| 84 | +# alias localhost:${reg_port} to the registry container when pulling images |
| 85 | +REGISTRY_DIR="/etc/containerd/certs.d/localhost:${reg_port}" |
| 86 | +for node in $(kind get nodes); do |
| 87 | + docker exec "${node}" mkdir -p "${REGISTRY_DIR}" |
| 88 | + cat <<EOF | docker exec -i "${node}" cp /dev/stdin "${REGISTRY_DIR}/hosts.toml" |
| 89 | +[host."http://${reg_name}:5000"] |
| 90 | +EOF |
| 91 | +done |
| 92 | + |
| 93 | +# 4. Connect the registry to the cluster network if not already connected |
| 94 | +# This allows kind to bootstrap the network but ensures they're on the same network |
71 | 95 | if [ "$(docker inspect -f='{{json .NetworkSettings.Networks.kind}}' "${reg_name}")" = 'null' ]; then
|
72 | 96 | docker network connect "kind" "${reg_name}"
|
73 | 97 | fi
|
74 | 98 |
|
75 |
| -# Document the local registry |
| 99 | +# 5. Document the local registry |
76 | 100 | # https://github.com/kubernetes/enhancements/tree/master/keps/sig-cluster-lifecycle/generic/1755-communicating-a-local-registry
|
77 | 101 | cat <<EOF | kubectl apply -f -
|
78 | 102 | apiVersion: v1
|
@@ -136,15 +160,20 @@ Make sure Docker registry is running.
|
136 | 160 | $ docker logs -f kind-registry
|
137 | 161 | ```
|
138 | 162 |
|
139 |
| -### Deploy OpenFaaS |
| 163 | +### Deploy OpenFaaS Standard or OpenFaaS For Enterprises |
140 | 164 |
|
141 |
| -Deploy OpenFaaS and its CLI: |
| 165 | +Deploy one of the OpenFaaS Pro editions along with faas-cli: |
142 | 166 |
|
143 | 167 | ```bash
|
144 |
| -$ arkade install openfaas |
145 | 168 | $ arkade get faas-cli
|
146 | 169 | ```
|
147 | 170 |
|
| 171 | +```bash |
| 172 | +$ arkade install openfaas --license-file ~/.openfaas/LICENSE |
| 173 | +``` |
| 174 | + |
| 175 | +Alternatively, install [OpenFaaS Pro](https://docs.openfaas.com/deployment/pro/) with helm, creating the required `openfaas-license` secret, and setting `openfaasPro: true`. |
| 176 | + |
148 | 177 | Then log in and port-forward OpenFaaS using the instructions given, or run `arkade info openfaas` to get them a second time.
|
149 | 178 |
|
150 | 179 | ### Create a Function
|
@@ -236,4 +265,4 @@ $ echo "advocate" | faas-cli invoke pydict
|
236 | 265 |
|
237 | 266 | Now that you have a local registry, you can speed up your local development of functions by keeping the container images within your local computer.
|
238 | 267 |
|
239 |
| -> This tutorial is based upon the KinD docs and [a post by Yankee Maharjan](https://dev.to/yankee/deploy-your-serverless-python-function-locally-with-openfaas-in-kubernetes-18jf). |
| 268 | +> This tutorial is based upon the KinD docs and [a post by Yankee Maharjan](https://dev.to/yankee/deploy-your-serverless-python-function-locally-with-openfaas-in-kubernetes-18jf). |
0 commit comments