Skip to content

Commit a19d91f

Browse files
authored
Merge pull request #214 from cloudogu/feature/allow-only-from-localhost
Limit accessibilty to localhost interface
2 parents 1b1f4b4 + d3954d5 commit a19d91f

File tree

3 files changed

+61
-13
lines changed

3 files changed

+61
-13
lines changed

Jenkinsfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ node('high-cpu') {
8989
String registryPort = sh(
9090
script: 'docker inspect ' +
9191
'--format=\'{{ with (index .NetworkSettings.Ports "30000/tcp") }}{{ (index . 0).HostPort }}{{ end }}\' ' +
92-
" k3d-${clusterName}-server-0",
92+
" k3d-${clusterName}-serverlb",
9393
returnStdout: true
9494
).trim()
9595

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,28 @@ You can apply the GitOps playground to
150150
* or almost any k8s cluster.
151151
Note that if you want to deploy Jenkins inside the cluster, you either need Docker as container runtime or set Jenkins up to run its build on an agent that provides Docker.
152152

153+
For the local cluster, you can avoid hitting DockerHub's rate limiting by using a mirror via the `--docker-io-registry-mirror` parameter.
154+
155+
For example:
156+
157+
```bash
158+
bash <(curl -s \
159+
https://raw.githubusercontent.com/cloudogu/gitops-playground/main/scripts/init-cluster.sh) --docker-io-registry-mirror https://mirror.gcr.io
160+
```
161+
162+
This parameter is passed on the containerd used by k3d.
163+
164+
In addition, the Jobs run by Jenkins are using the host's Docker daemon.
165+
To avoid rate limits there, you might have to configure a mirror there as well.
166+
This can be done in the `/etc/docker/daemon.json` or in the config of Docker Desktop.
167+
168+
For example:
169+
```json
170+
{
171+
"registry-mirrors": ["https://mirror.gcr.io"]
172+
}
173+
```
174+
153175
### Apply playground
154176

155177
You can apply the playground to your cluster using our container image `ghcr.io/cloudogu/gitops-playground`.

scripts/init-cluster.sh

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ function createCluster() {
7777
# Disable traefik (we roll our own ingress-controller)
7878
'--k3s-arg=--disable=traefik@server:0'
7979
)
80+
81+
REGISTRIES=""
82+
if [[ -n "$DOCKER_IO_REGISTRY_MIRROR" ]]; then
83+
REGISTRIES=$(cat <<EOF
84+
registries:
85+
config: |
86+
mirrors:
87+
docker.io:
88+
endpoint:
89+
- "$DOCKER_IO_REGISTRY_MIRROR"
90+
EOF
91+
)
92+
fi
8093

8194
if [[ ${BIND_LOCALHOST} == 'true' ]]; then
8295
K3D_ARGS+=(
@@ -89,52 +102,60 @@ function createCluster() {
89102
# Internal Docker registry must be on localhost. Otherwise docker will use HTTPS, leading to errors on docker push
90103
# in the example application's Jenkins Jobs.
91104
K3D_ARGS+=(
92-
"-p ${BIND_REGISTRY_PORT}:30000@server:0:direct"
105+
# Note that binding to 127.0.0.1 (instead of the default 0.0.0.0, i.e. ALL networks) is much more secure!
106+
"-p 127.0.0.1:${BIND_REGISTRY_PORT}:30000@server:0"
93107
)
94108
else
95109
# User wants us to choose an arbitrary port.
96110
# The port must then be passed when applying the playground as --internal-registry-port (printed after creation)
97111
K3D_ARGS+=(
98-
'-p 30000@server:0:direct'
112+
'-p 127.0.0.1::30000@server:0'
99113
)
100114
fi
101115

102116
# Bind ingress port only when requested by parameter.
103117
# On linux the pods can be reached without ingress via the k3d container's network address and the node port.
104-
if [[ "${BIND_REGISTRY_PORT}" == '0' ]]; then
118+
if [[ "${BIND_INGRESS_PORT}" == '0' ]]; then
105119
# User wants us to choose an arbitrary port.
106120
# The port must then be passed when applying the playground as --base-url=localhost:PORT (printed after creation)
107121
K3D_ARGS+=(
108-
'-p 80@server:0:direct'
122+
'-p 127.0.0.1::80@server:0'
109123
)
110124
elif [[ "${BIND_INGRESS_PORT}" != '-' ]]; then
111-
# Note that 127.0.0.1:$BIND_INGRESS_PORT would be more secure, but then requests to localhost fail
112125
K3D_ARGS+=(
113-
"-p ${BIND_INGRESS_PORT}:80@server:0:direct"
126+
"-p 127.0.0.1:${BIND_INGRESS_PORT}:80@server:0"
114127
)
115128
fi
116129

117-
if [ -n "$BIND_PORTS" ]; then
130+
if [[ -n "$BIND_PORTS" ]]; then
118131
IFS=","
119132
read -ra portBindings <<< "$BIND_PORTS"
120133
unset IFS
121134

122135
for portBinding in "${portBindings[@]}"; do
123136
K3D_ARGS+=(
124-
"-p ${portBinding}@server:0:direct"
137+
"-p 127.0.0.1:${portBinding}@server:0"
125138
)
126139
done
127140
fi
128141
fi
129142

130143
echo "Creating cluster '${CLUSTER_NAME}'"
131-
k3d cluster create ${CLUSTER_NAME} ${K3D_ARGS[*]} >/dev/null
132-
144+
#k3d cluster create ${CLUSTER_NAME} ${K3D_ARGS[*]} >/dev/null
145+
cat <<EOF | k3d cluster create ${CLUSTER_NAME} ${K3D_ARGS[*]} --config - > /dev/null
146+
apiVersion: k3d.io/v1alpha5
147+
kind: Simple
148+
kubeAPI:
149+
hostIP: "127.0.0.1"
150+
$REGISTRIES
151+
EOF
152+
153+
133154
if [[ ${BIND_REGISTRY_PORT} != '30000' ]]; then
134155
local registryPort
135156
registryPort=$(docker inspect \
136157
--format='{{ with (index .NetworkSettings.Ports "30000/tcp") }}{{ (index . 0).HostPort }}{{ end }}' \
137-
k3d-${CLUSTER_NAME}-server-0)
158+
k3d-${CLUSTER_NAME}-serverlb)
138159
echo "Bound internal registry port 30000 to localhost port ${registryPort}."
139160
echoHightlighted "Make sure to pass --internal-registry-port=${registryPort} when applying the playground."
140161
fi
@@ -143,7 +164,7 @@ function createCluster() {
143164
local ingressPort
144165
ingressPort=$(docker inspect \
145166
--format='{{ with (index .NetworkSettings.Ports "80/tcp") }}{{ (index . 0).HostPort }}{{ end }}' \
146-
k3d-${CLUSTER_NAME}-server-0)
167+
k3d-${CLUSTER_NAME}-serverlb)
147168
echo "Bound ingress port to localhost:${ingressPort}."
148169
echoHightlighted "Make sure to pass a base-url, e.g. --ingress-nginx --base-url=http://localhost$(if [ "${ingressPort}" -ne 80 ]; then echo ":${ingressPort}"; fi) when applying the playground."
149170
fi
@@ -166,6 +187,8 @@ function printParameters() {
166187
echo " | --bind-ingress-port=INT >> Bind the ingress controller to this localhost port. Defaults to 80. Set to - to disable."
167188
echo " | --bind-registry-port=INT >> Specify a custom port for the container registry to bind to localhost port. Only use this when port 30000 is blocked and --bind-localhost=true. Defaults to 30000 (default used by the playground)."
168189
echo " | --bind-portBindings=STRING >> A comma separated list of additional port bindings like 443:443,9090:9090. Ignored when --bind-localhost."
190+
191+
echo " | --docker-io-registry-mirror=STRING >> the hostname of a registry that mirrors DockerHub. Useful when encountering rate limits"
169192
echo
170193
echo " -x | --trace >> Debug + Show each command executed (set -x)"
171194
}
@@ -214,6 +237,7 @@ readParameters() {
214237
# Use default port for playground registry, because no parameter is required when applying
215238
BIND_REGISTRY_PORT="30000"
216239
BIND_PORTS=""
240+
DOCKER_IO_REGISTRY_MIRROR=""
217241
TRACE=false
218242

219243
while [ $# -gt 0 ]; do
@@ -230,6 +254,8 @@ readParameters() {
230254
if [[ "$1" == *"="* ]]; then shift; else shift 2; fi ;;
231255
--bind-ports*) BIND_PORTS=$(get_longopt_value "--bind-ports" "$@");
232256
if [[ "$1" == *"="* ]]; then shift; else shift 2; fi ;;
257+
--docker-io-registry-mirror*) DOCKER_IO_REGISTRY_MIRROR=$(get_longopt_value "--docker-io-registry-mirror" "$@");
258+
if [[ "$1" == *"="* ]]; then shift; else shift 2; fi ;;
233259
--) shift; break ;;
234260
*) break ;;
235261
esac

0 commit comments

Comments
 (0)