Skip to content

Commit e8d14ca

Browse files
movrayschnatterer
authored andcommitted
add binding for k8s exposed services to 127.0.0.1 instead of default 0.0.0.0
1 parent 5a93ece commit e8d14ca

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
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

scripts/init-cluster.sh

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,28 @@ function createCluster() {
8989
# Internal Docker registry must be on localhost. Otherwise docker will use HTTPS, leading to errors on docker push
9090
# in the example application's Jenkins Jobs.
9191
K3D_ARGS+=(
92-
"-p ${BIND_REGISTRY_PORT}:30000@server:0:direct"
92+
# Note that binding to 127.0.0.1 (instead of the default 0.0.0.0, i.e. ALL networks) is much more secure!
93+
"-p 127.0.0.1:${BIND_REGISTRY_PORT}:30000@server:0"
9394
)
9495
else
9596
# User wants us to choose an arbitrary port.
9697
# The port must then be passed when applying the playground as --internal-registry-port (printed after creation)
9798
K3D_ARGS+=(
98-
'-p 30000@server:0:direct'
99+
'-p 127.0.0.1::30000@server:0'
99100
)
100101
fi
101102

102103
# Bind ingress port only when requested by parameter.
103104
# 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
105+
if [[ "${BIND_INGRESS_PORT}" == '0' ]]; then
105106
# User wants us to choose an arbitrary port.
106107
# The port must then be passed when applying the playground as --base-url=localhost:PORT (printed after creation)
107108
K3D_ARGS+=(
108-
'-p 80@server:0:direct'
109+
'-p 127.0.0.1::80@server:0'
109110
)
110111
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
112112
K3D_ARGS+=(
113-
"-p ${BIND_INGRESS_PORT}:80@server:0:direct"
113+
"-p 127.0.0.1:${BIND_INGRESS_PORT}:80@server:0"
114114
)
115115
fi
116116

@@ -121,20 +121,27 @@ function createCluster() {
121121

122122
for portBinding in "${portBindings[@]}"; do
123123
K3D_ARGS+=(
124-
"-p ${portBinding}@server:0:direct"
124+
"-p 127.0.0.1:${portBinding}@server:0"
125125
)
126126
done
127127
fi
128128
fi
129129

130130
echo "Creating cluster '${CLUSTER_NAME}'"
131-
k3d cluster create ${CLUSTER_NAME} ${K3D_ARGS[*]} >/dev/null
132-
131+
#k3d cluster create ${CLUSTER_NAME} ${K3D_ARGS[*]} >/dev/null
132+
cat <<EOF | k3d cluster create ${CLUSTER_NAME} ${K3D_ARGS[*]} --config - > /dev/null
133+
apiVersion: k3d.io/v1alpha5
134+
kind: Simple
135+
kubeAPI:
136+
hostIP: "127.0.0.1"
137+
EOF
138+
139+
133140
if [[ ${BIND_REGISTRY_PORT} != '30000' ]]; then
134141
local registryPort
135142
registryPort=$(docker inspect \
136143
--format='{{ with (index .NetworkSettings.Ports "30000/tcp") }}{{ (index . 0).HostPort }}{{ end }}' \
137-
k3d-${CLUSTER_NAME}-server-0)
144+
k3d-${CLUSTER_NAME}-serverlb)
138145
echo "Bound internal registry port 30000 to localhost port ${registryPort}."
139146
echoHightlighted "Make sure to pass --internal-registry-port=${registryPort} when applying the playground."
140147
fi
@@ -143,7 +150,7 @@ function createCluster() {
143150
local ingressPort
144151
ingressPort=$(docker inspect \
145152
--format='{{ with (index .NetworkSettings.Ports "80/tcp") }}{{ (index . 0).HostPort }}{{ end }}' \
146-
k3d-${CLUSTER_NAME}-server-0)
153+
k3d-${CLUSTER_NAME}-serverlb)
147154
echo "Bound ingress port to localhost:${ingressPort}."
148155
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."
149156
fi

0 commit comments

Comments
 (0)