Use a local registry with KinD

A local registry can save on bandwidth costs and means your OpenFaaS functions don’t leave your local computer when running faas-cli up

Not only is it much quicker, but it’s also simple to configure if you’re using KinD.

Prerequisite:

You need to have Docker installed on your machine.

Install arkade

We will use arkade to install and deploy apps and services to Kubernetes.

  1. # Download only, install yourself with sudo
  2. $ curl -sLS https://dl.get-arkade.dev | sh
  3. # Download and install
  4. $ curl -sLS https://dl.get-arkade.dev | sudo sh

arkade commands:

  • use arkade get to download CLI tools and applications.
  • use arkade install to install applications using helm charts or vanilla YAML files.
  • use arkade info to get back info about an app you’ve installed

Install kubectl

kubectl is a command line tool that talks to the Kubernetes API for performing actions on our cluster.

  1. $ arkade get kubectl

Create the KinD cluster with a local registry enabled

We will set up our local Kubernetes cluster using KinD (Kubernetes in Docker).

Install KinD

These instructions are adapted from the KinD documentation. Our goal is to keep everything locally including a local Docker registry.

The official KinD docs provides a shell script to create a Kubernetes cluster with local Docker registry enabled.

  1. #!/bin/sh
  2. set -o errexit
  3. # create registry container unless it already exists
  4. reg_name='kind-registry'
  5. reg_port='5000'
  6. running="$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)"
  7. if [ "${running}" != 'true' ]; then
  8. docker run \
  9. -d --restart=always -p "${reg_port}:5000" --name "${reg_name}" \
  10. registry:2
  11. fi
  12. # create a cluster with the local registry enabled in containerd
  13. cat <<EOF | kind create cluster --config=-
  14. kind: Cluster
  15. apiVersion: kind.x-k8s.io/v1alpha4
  16. containerdConfigPatches:
  17. - |-
  18. [plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:${reg_port}"]
  19. endpoint = ["http://${reg_name}:${reg_port}"]
  20. EOF
  21. # connect the registry to the cluster network
  22. docker network connect "kind" "${reg_name}"
  23. # tell https://tilt.dev to use the registry
  24. # https://docs.tilt.dev/choosing_clusters.html#discovering-the-registry
  25. for node in $(kind get nodes); do
  26. kubectl annotate node "${node}" "kind.x-k8s.io/registry=localhost:${reg_port}";
  27. done

View the shell script in the KinD docs


Note:

You can find similar solutions for other local Kubernetes distributions:


Make the script executable:

  1. $ chmod +x kind-with-registry.sh

Run it to create your local cluster with registry:

  1. $ ./kind-with-registry.sh

Make sure the kubectl context is set to the newly created cluster:

  1. $ kubectl config current-context

If the result is not kind-kind then execute:

  1. $ kubectl config use kind-kind

Make sure the cluster is running:

  1. $ kubectl cluster-info

Make sure Docker registry is running.

  1. $ docker logs -f kind-registry

Deploy OpenFaaS

Deploy OpenFaaS and its CLI:

  1. $ arkade install openfaas
  2. $ arkade get faas-cli

Then log in and port-forward OpenFaaS using the instructions given, or run arkade info openfaas to get them a second time.

Create a Function

We will take an example of a simple function; a dictionary that returns the meaning of word you query. We will be using the PyDictionary module for this setup.

Pull python language template from store:

  1. $ faas-cli template store pull python3-flask

We will be using the python3-flask-debian template.

Setup your OPENFAAS_PREFIX variable to configure the address of your registry:

  1. export OPENFAAS_PREFIX=localhost:5000

Note: Docker for Mac users may need to change “localhost” to the IP address of their LAN or WiFi adapter as shown on ifconfig such as 192.168.0.14

Create a new function using the template:

  1. $ export FN=pydict
  2. $ faas-cli new $FN --lang python3-flask-debian

This will create a directory for your function and a YAML config file with the function name you provided:

  • pydict/
  • pydict.yml

Add dependency to the pydict/requirements.txt file:

  1. PyDictionary

Update handler.py with the following code.

  1. from PyDictionary import PyDictionary
  2. dictionary = PyDictionary()
  3. def handle(word):
  4. return dictionary.meaning(word)

Our minimal function is complete.

Stack file

You will see that the OpenFaaS stack YAML file pydict.yml has localhost:5000 in its image destination.

  1. version: 1.0
  2. provider:
  3. name: openfaas
  4. gateway: http://127.0.0.1:8080
  5. functions:
  6. pydict:
  7. lang: python3-flask-debian
  8. handler: ./pydict
  9. image: localhost:5000/pydict:latest

Build Push Deploy

With our setup ready; we can now build our image, push it to the registry, and deploy it to Kubernetes. And using faas-cli it is possible with a single command!

  1. faas-cli up -f pydict.yml

Test the function

We can invoke our function from CLI using faas-cli or curl.

  1. $ echo "advocate" | faas-cli invoke pydict
  2. {"Noun":["a person who pleads for a cause or propounds an idea","a lawyer who pleads cases in court"],"Verb":["push for something","speak, plead, or argue in favor of"]}

Wrapping Up

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.

This tutorial is based upon the KinD docs and a post by Yankee Maharjan.