Skip to content
This repository has been archived by the owner on Apr 3, 2023. It is now read-only.

document local minikube. Fixes https://github.com/tilt-dev/tilt/issues/3193 #1

Merged
merged 1 commit into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,68 @@
# minikube-local

The best way to set minikube up for local development

[![Build Status](https://circleci.com/gh/tilt-dev/minikube-local/tree/master.svg?style=shield)](https://circleci.com/gh/tilt-dev/minikube-local)

When using Tilt with a [Minikube](https://minikube.sigs.k8s.io/docs/) cluster,
we recommend using a local registry for faster image pushing and pulling.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

faster image pushing and pulling.

seems like you joinked this from a doc where the tradeoff was local registry vs. remote registry -- the main Q for minikube is about local registry vs. hooking into minikube docker, I think the docs should discuss that instead

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you overestimate how well most minikube users are going to understand the minikube feature set. simplified the explanation here a bit


This repo documents the best way to set it up.

## Why use Minikube with a local registry?

Minikube offers many different ways to get your app into the cluster.

Using a local registry is the best method for iterative app development.

- Unlike with a remote registry, the image stays local to your machine, with no
network traffic to wait on or credentials to setup.

- Unlike with an in-cluster builder, you can reset the cluster without deleting
the image cache.

- Unlike with loading into the container runtime, docker will skip pushing any
layers that already exist in the registry.

Over all these approaches, a local registry has good speed, incremental caching,
and few footguns. But setting it up is awkward and fiddly. This script makes it
easy.

## How to Try It

1) Install [Minikube](https://minikube.sigs.k8s.io/docs/)

2) Copy the [minikube-with-registry.sh](minikube-with-registry.sh) script somewhere on your path.

3) Create a cluster with `minikube-with-registry.sh`. Currently it creates the registry at port 5000.

```
minikube-with-registry.sh
```

4) Try pushing an image.

```
docker tag alpine localhost:5000/alpine
docker push localhost:5000/alpine
```

You can now use the image name `localhost:5000/alpine` in any resources you deploy to the cluster.

[Tilt](https://tilt.dev) will automatically detect the local registry created by this script.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought Tilt's default was to hook into the minikube docker daemon -- if it detects a local registry, does that behavior override that behavior?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope!

we should change tilt to prefer the local registry over the docker daemon.

HOWEVER! This script uses containerd, so Tilt will detect that there's no docker daemon connect to

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh clever! Nice. Can you open an issue/story for the local-registry-over-minikube-docker behavior?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


## Thanks to

High five to [MicroK8s](https://github.com/ubuntu/microk8s) for the initial local registry feature
that inspired a lot of this work.

The Kind team ran with this, writing up documentation and hooks for how to [set up a local registry](https://kind.sigs.k8s.io/docs/user/local-registry/) with Kind.

This repo adapts the Kind team's approach and applies the local registry configmap, so that tools
like Tilt can discover the local-registry. This protocol is a [Kubernetes Enhancement Proposal](https://github.com/kubernetes/enhancements/issues/1755).

## License

Copyright 2020 Windmill Engineering

Licensed under [the Apache License, Version 2.0](LICENSE)
60 changes: 60 additions & 0 deletions minikube-with-registry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/sh
#
# Adapted from:
# https://github.com/kubernetes-sigs/kind/commits/master/site/static/examples/kind-with-registry.sh
#
# Copyright 2020 The Kubernetes Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit

# desired profile name; default is ""
MINIKUBE_PROFILE_NAME="${MINIKUBE_PROFILE_NAME:-minikube}"

reg_name='minikube-registry'
reg_port='5000'

# create registry container unless it already exists
running="$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)"
if [ "${running}" != 'true' ]; then
docker run \
-d --restart=always -p "${reg_port}:5000" --name "${reg_name}" \
registry:2
fi

reg_host="$(docker inspect -f '{{.NetworkSettings.IPAddress}}' "${reg_name}")"
echo "Registry Host: ${reg_host}"

# create a cluster
minikube start -p "$MINIKUBE_PROFILE_NAME" --driver=docker --container-runtime=containerd

# patch the container runtime
# this is the most annoying sed expression i've ever had to write
minikube ssh sudo sed "\-i" "s,\\\[plugins.cri.registry.mirrors\\\],[plugins.cri.registry.mirrors]\\\n\ \ \ \ \ \ \ \ [plugins.cri.registry.mirrors.\\\"localhost:${reg_port}\\\"]\\\n\ \ \ \ \ \ \ \ \ \ endpoint\ =\ [\\\"http://${reg_host}:${reg_port}\\\"]," /etc/containerd/config.toml

# restart the container runtime
minikube ssh sudo systemctl restart containerd

# document the registry
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ConfigMap
metadata:
name: local-registry-hosting
namespace: kube-public
data:
localRegistryHosting.v1: |
host: "localhost:${reg_port}"
help: "https://github.com/tilt-dev/minikube-local"
EOF
13 changes: 13 additions & 0 deletions test/pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Pod
metadata:
name: minikube-test
labels:
app: minikube-test
spec:
containers:
- name: minikube-test
image: localhost:5000/busybox
command: ["sh", "-c", "busybox httpd -f -p 8000"]
ports:
- containerPort: 8000
14 changes: 14 additions & 0 deletions test/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
#
# Make sure the local registry works as expected.

set -ex

cd $(dirname $0)

docker pull busybox
docker tag busybox localhost:5000/busybox
docker push localhost:5000/busybox
kubectl delete -f pod.yaml --ignore-not-found
kubectl create -f pod.yaml
kubectl wait --for=condition=ready pod/minikube-test --timeout=60s