use .localhost as a default registry name

This commit is contained in:
Florian Klein 2020-02-10 10:04:03 +01:00
parent 0ec51e1318
commit 34bf23ef85
No known key found for this signature in database
GPG Key ID: CE56CABFF7F0816C
4 changed files with 18 additions and 28 deletions

View File

@ -331,7 +331,7 @@ func CreateCluster(c *cli.Context) error {
exists, err := registryNameExists.Exists()
if !exists || err != nil {
log.Printf("Make sure you have an alias in your /etc/hosts file like '127.0.0.1 %s'", clusterSpec.RegistryName)
log.Printf("Make sure %s resolves to '127.0.0.1' (using /etc/hosts f.e)", clusterSpec.RegistryName)
}
}

View File

@ -103,27 +103,30 @@ If you don't want k3d to manage your registry, you can start it with some `docke
```shell script
docker volume create local_registry
docker container run -d --name registry.local -v local_registry:/var/lib/registry --restart always -p 5000:5000 registry:2
docker container run -d --name registry.localhost -v local_registry:/var/lib/registry --restart always -p 5000:5000 registry:2
```
These commands will start you registry in `registry.local:5000`. In order to push to this registry, you will
These commands will start you registry in `registry.localhost:5000`. In order to push to this registry, you will
need to add the line at `/etc/hosts` as we described in [the previous section ](#etc-hosts). Once your
registry is up and running, we will need to add it to your [`registries.yaml` configuration file](#registries-file).
Finally, you must connect the registry network to the k3d cluster network:
`docker network connect k3d-k3s-default registry.local`. And then you can
`docker network connect k3d-k3s-default registry.localhost`. And then you can
[check you local registry](#testing).
### <a name="etc-hosts"></a>Pushing to your local registry address
The registry will be located, by default, at `registry.local:5000` (customizable with the `--registry-name`
The registry will be located, by default, at `registry.localhost:5000` (customizable with the `--registry-name`
and `--registry-port` parameters). All the nodes in your k3d cluster can resolve this hostname (thanks to the
DNS server provided by the Docker daemon) but, in order to be able to push to this registry, this hostname
but also be resolved from your host.
The easiest solution for this is to add an entry in your `/etc/hosts` file like this:
Luckily, (NSS-myhostname)[http://man7.org/linux/man-pages/man8/nss-myhostname.8.html] should be installed on a desktop environment
and should resolve `*.localhost` automatically to `127.0.0.1`.
If it's not the case, you can add an entry in your `/etc/hosts` file like this:
```shell script
127.0.0.1 registry.local
127.0.0.1 registry.localhost
```
Once again, this will only work with k3s >= v0.10.0 (see the [section below](#k3s-old)
@ -135,7 +138,7 @@ You should test that you can
* push to your registry from your local development machine.
* use images from that registry in `Deployments` in your k3d cluster.
We will verify these two things for a local registry (located at `registry.local:5000`) running
We will verify these two things for a local registry (located at `registry.localhost:5000`) running
in your development machine. Things would be basically the same for checking an external
registry, but some additional configuration could be necessary in your local machine when
using an authenticated or secure registry (please refer to Docker's documentation for this).
@ -144,8 +147,8 @@ Firstly, we can download some image (like `nginx`) and push it to our local regi
```shell script
docker pull nginx:latest
docker tag nginx:latest registry.local:5000/nginx:latest
docker push registry.local:5000/nginx:latest
docker tag nginx:latest registry.localhost:5000/nginx:latest
docker push registry.localhost:5000/nginx:latest
```
Then we can deploy a pod referencing this image to your cluster:
@ -170,7 +173,7 @@ spec:
spec:
containers:
- name: nginx-test-registry
image: registry.local:5000/nginx:latest
image: registry.localhost:5000/nginx:latest
ports:
- containerPort: 80
EOF
@ -208,8 +211,8 @@ sandbox_image = "{{ .NodeConfig.AgentConfig.PauseImage }}"
# Added section: additional registries and the endpoints
[plugins.cri.registry.mirrors]
[plugins.cri.registry.mirrors."<b>registry.local:5000</b>"]
endpoint = ["http://<b>registry.local:5000</b>"]
[plugins.cri.registry.mirrors."<b>registry.localhost:5000</b>"]
endpoint = ["http://<b>registry.localhost:5000</b>"]
</pre>
and then mount it at `/var/lib/rancher/k3s/agent/etc/containerd/config.toml.tmpl` (where

View File

@ -14,7 +14,7 @@ import (
// defaultK3sImage specifies the default image being used for server and workers
const defaultK3sImage = "docker.io/rancher/k3s"
const defaultK3sClusterName string = "k3s-default"
const defaultRegistryName = "registry.local"
const defaultRegistryName = "registry.localhost"
const defaultRegistryPort = 5000
// main represents the CLI application

View File

@ -8,9 +8,7 @@ source "$CURR_DIR/common.sh"
#########################################################################################
REGISTRY_NAME="registry.local"
REGISTRY_PORT="5000"
REGISTRY="$REGISTRY_NAME:$REGISTRY_PORT"
REGISTRY="registry.localhost:5000"
TEST_IMAGE="nginx:latest"
FIXTURES_DIR=$CURR_DIR/fixtures
@ -21,17 +19,6 @@ REGISTRIES_YAML=$FIXTURES_DIR/01-registries-empty.yaml
#########################################################################################
info "Checking that $REGISTRY_NAME is resolvable"
grep -q $REGISTRY_NAME /etc/hosts
if [ $? -ne 0 ] ; then
[ "$CI" = "true" ] || abort "$REGISTRY_NAME is not in /etc/hosts: please add an entry manually."
info "Adding '127.0.0.1 $REGISTRY_NAME' to /etc/hosts"
echo "127.0.0.1 $REGISTRY_NAME" | sudo tee -a /etc/hosts
else
passed "... good: $REGISTRY_NAME is in /etc/hosts"
fi
info "Creating two clusters (with a registry)..."
$EXE create --wait 60 --name "c1" --api-port 6443 --enable-registry || failed "could not create cluster c1"
$EXE create --wait 60 --name "c2" --api-port 6444 --enable-registry --registries-file "$REGISTRIES_YAML" || failed "could not create cluster c2"