kubernetes-sigs / kind

Kubernetes IN Docker - local clusters for testing Kubernetes
https://kind.sigs.k8s.io/
Apache License 2.0
13.31k stars 1.54k forks source link

Kind extraPortMappings issue #3681

Open Ky4t opened 2 months ago

Ky4t commented 2 months ago

Creating cluster "k8s-proj" ... ✓ Ensuring node image (kindest/node:v1.30.0) 🖼 ✗ Preparing nodes 📦 📦 📦 Deleted nodes: ["k8s-proj-worker" "k8s-proj-worker2" "k8s-proj-control-plane"] ERROR: failed to create cluster: command "docker run --name k8s-proj-worker2 --hostname k8s-proj-worker2 --label io.x-k8s.kind.role=worker --privileged --security-opt seccomp=unconfined --security-opt apparmor=unconfined --tmpfs /tmp --tmpfs /run --volume /var --volume /lib/modules:/lib/modules:ro -e KIND_EXPERIMENTAL_CONTAINERD_SNAPSHOTTER --detach --tty --label io.x-k8s.kind.cluster=k8s-proj --net kind --restart=on-failure:1 --init=false --cgroupns=private --publish=127.0.0.1:80:30000/TCP --publish=127.0.0.1:443:31000/TCP --publish=127.0.0.1:15021:32000/TCP kindest/node:v1.30.0@sha256:047357ac0cfea04663786a612ba1eaba9702bef25227a794b52890dd8bcd692e" failed with error: exit status 125 Command Output: 90e96a94f270f108b9861949cb539484b7b9be34c1fad906d991e9373be7df0a docker: Error response from daemon: Ports are not available: exposing port TCP 127.0.0.1:80 -> 0.0.0.0:0: listen tcp 127.0.0.1:80: bind: An attempt was made

Here is the error occurred when I create a cluster with config file including extra port mappings.

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
  extraPortMappings:
   - containerPort: 30000
     hostPort: 80
     listenAddress: "127.0.0.1"
     protocol: TCP
   - containerPort: 31000
     hostPort: 443
     listenAddress: "127.0.0.1"
     protocol: TCP
   - containerPort: 32000
     hostPort: 15021
     listenAddress: "127.0.0.1"
     protocol: TCP
- role: worker
  extraPortMappings:
   - containerPort: 30000
     hostPort: 80
     listenAddress: "127.0.0.1"
     protocol: TCP
   - containerPort: 31000
     hostPort: 443
     listenAddress: "127.0.0.1"
     protocol: TCP
   - containerPort: 32000
     hostPort: 15021
     listenAddress: "127.0.0.1"
     protocol: TCP
stmcginnis commented 2 months ago

You have both workers trying to bind to the same ports on the host. That will not work.

What you probably want is to add the port mappings to the control-plane container, then use ingress to expose your applications running on the workers.

Ky4t commented 2 months ago

Can you give me some example for my config?

stmcginnis commented 2 months ago

As far as just creating the cluster, this should work:

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  extraPortMappings:
   - containerPort: 30000
     hostPort: 80
   - containerPort: 31000
     hostPort: 443
   - containerPort: 32000
     hostPort: 15021
- role: worker
- role: worker
Ky4t commented 2 months ago

Creating cluster "k8s-proj" ... ✓ Ensuring node image (kindest/node:v1.30.0) 🖼 ✗ Preparing nodes 📦 📦 📦 Deleted nodes: ["k8s-proj-worker" "k8s-proj-control-plane" "k8s-proj-worker2"] ERROR: failed to create cluster: command "docker run --name k8s-proj-control-plane --hostname k8s-proj-control-plane --label io.x-k8s.kind.role=control-plane --privileged --security-opt seccomp=unconfined --security-opt apparmor=unconfined --tmpfs /tmp --tmpfs /run --volume /var --volume /lib/modules:/lib/modules:ro -e KIND_EXPERIMENTAL_CONTAINERD_SNAPSHOTTER --detach --tty --label io.x-k8s.kind.cluster=k8s-proj --net kind --restart=on-failure:1 --init=false --cgroupns=private --publish=0.0.0.0:80:30000/TCP --publish=0.0.0.0:443:31000/TCP --publish=0.0.0.0:15021:32000/TCP --publish=127.0.0.1:35207:6443/TCP -e KUBECONFIG=/etc/kubernetes/admin.conf kindest/node:v1.30.0@sha256:047357ac0cfea04663786a612ba1eaba9702bef25227a794b52890dd8bcd692e" failed with error: exit status 125 Command Output: a93a88ffe4c358fa9eaabfade0a92e7d9f6e679d97dbc785d4d3d50d91163a7f docker: Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:80 -> 0.0.0.0:0: listen tcp 0.0.0.0:80: bind: An attempt was made to access a socket in a way forbidden by its access permissions.

Still showing the error

BenTheElder commented 2 months ago

something else on your host may already be using port 80?

setting listenAddress: "127.0.0.1" but otherwise the config above would be a good starting point.

also, if you don't have a specific use-case in mind, I'd highly recommend using a single node cluster and just doing:

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  extraPortMappings:
   - containerPort: 30000
     hostPort: 80
     listenAddress: "127.0.0.1"
   - containerPort: 31000
     hostPort: 443
     listenAddress: "127.0.0.1"
   - containerPort: 32000
     hostPort: 15021
     listenAddress: "127.0.0.1"