Microk8s: Check availability of port 8080 in snap install hook

Created on 6 Jun 2018  路  14Comments  路  Source: ubuntu/microk8s

If 8080 isn't available, increment and try again up to some reasonable limit. In other words, find an available port for the apiserver to use instead of just quietly failing if 8080 is already in use.

enhancement inactive

Most helpful comment

This really needs fixing, choosing 8080 as the default port just doesn't make sense. I think it'd be great if you change it to _something_ else before the team starts working on a well thought-out configuration option.

All 14 comments

Also, 8080 is quite of a commonly used port :) it'd be great if microk8s would have an env variable / config where users could specify on what port to bind.

Yes, that is true.

We cannot pass parameters during snap install. What we could do is to select an open port from a range during the installation (eg first available port in [8080, 8090]) and/or have a microk8s.init command where you would be asked this kind of questions (similar to what for example lxd is doing with lxd init).

@ktsakalozos I also like the way kubectl manages things, where you can do a config set / get to update parameters -- so something like microk8s.config set APISERVER_PORT 9876 would be nice :)

This is interesting. This configuration approach might extend to addons as well, for example we could do microk8s.config set REGISTRY_SIZE 80G to set the size of the registry in the respective addon.

Yes! I'm new to k8s. I just spent an hour figuring out that microk8s is crashing because Syncthing also uses 8080 by default. (I forgot about that.)
Then I spent the next two hours figuring out, how to change the port of the API server. I gave up when I ran into the fact that I cannot edit kubelet.config or use "microk8s.kubectl set command" because microk8s was installed in a snap. I finally changed the port of Syncthing.
At least a sensible error message should be printed. Following the microk8s video all I got was:

microk8s.kubectl get all
error: the server doesn't have a resource type "all"

Here's a quick workaround for running the api-server on a different port:

# define our new port number
API_PORT=8081

# update kube-apiserver args with the new port
echo "--insecure-port=$API_PORT" | sudo tee -a /var/snap/microk8s/current/args/kube-apiserver 

# tell other services about the new port
sudo sed -i "s/:8080/:$API_PORT/" /var/snap/microk8s/current/args/* 

# create new, updated copies of our kubeconfig for kubelet and kubectl to use
mkdir -p ~/.kube && microk8s.config | sed "s/:8080/:$API_PORT/" | sudo tee /var/snap/microk8s/current/kubelet.config > ~/.kube/microk8s.config

# tell kubelet about the new kubeconfig
sudo sed -i 's#${SNAP}/configs/kubelet.config#${SNAP_DATA}/kubelet.config#' /var/snap/microk8s/current/args/kubelet

# disable and enable the microk8s snap to restart all services
sudo snap disable microk8s && sudo snap enable microk8s

At this point you can run microk8s.inspect to confirm all services are running properly. All that remains is to make sure kubectl is using our new kubeconfig file.

We can't tell microk8s.kubectl to use an external kubeconfig, so we'll switch to using the standalone kubectl snap, which will work just fine with microk8s:

snap install kubectl
KUBECONFIG=~/.kube/microk8s.config kubectl get all

Success!

The above procedure will also make microk8s.status to return the cluster as not running because it will use kubectl with the wrong kubeconfig too.

Otherwise it seems to work nicely.

Default secure port for the Kubernetes is 6443. Assuming 6080 as insecure port for consistency, why not chose 60080 and 60443 for microk8s to avoid collision with an existing Kubernetes API endpoint?

Those ports aren't likely to be used otherwise.

This is unfortunately a blocker for me :-(
Tomcat uses port 8080 by default and I certainly will not reconfigure all my development tomcats or spring boot apps ...
The workaround of @tvansteenburgh is unfortunately only a crutch. So I'll stay with Minikube for the time being, in the hope that a reasonable solution will be found here soon ;-)

How about a microk8s.config tool that helps to reconfigure?
Something like microk8s.config --api-insecure-port 60080

You'll also need to ~update~ rebind the /snap/microk8s/current/client.config file. Otherwise things like microk8s.enable dashboard will try to use the old port.
The /snap directory is read-only, even as root, so you'll have to rebind it like this:

sudo mount --bind -o nodev,ro ~/.kube/microk8s.config /snap/microk8s/current/client.config

When this get's fixed, this line will need to updated as well:
https://github.com/ubuntu/microk8s/blob/c181919402622e7303258c2f5edd818f228074b4/microk8s-resources/actions/common/utils.sh#L78
The fact that it gets the default config is wrong.

This really needs fixing, choosing 8080 as the default port just doesn't make sense. I think it'd be great if you change it to _something_ else before the team starts working on a well thought-out configuration option.

This may have been resolved recently鈥擨 did a fresh installation and the API server is now listening on https://192.168.x.y:16443. #323?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings