Skip to content

Commit

Permalink
Support publishing ports for the worker nodes
Browse files Browse the repository at this point in the history
All ports exposed by --publish will also be exported for all worker
nodes. The host port will be auto indexed based worker id.

For example: with the following command option:

k3d create --publish  80:80  --publish 90:90/udp --workers 1

The exposed ports will be:

host TCP port 80  -> k3s server TCP 80
host TCP port 90  -> k3s server TCP 90
host UDP port 81 -> k3s worker 0 UDP 80
host UDP port 91 -> k3s worker 0 UDP 90
  • Loading branch information
andyz-dev committed May 8, 2019
1 parent 988fbdb commit 65f2820
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func CreateCluster(c *cli.Context) error {
strings.Split(c.String("volume"), ","),
i,
c.String("port"),
publishedPorts,
)
if err != nil {
return fmt.Errorf("ERROR: failed to create worker node for cluster %s\n%+v", c.String("name"), err)
Expand Down
7 changes: 6 additions & 1 deletion cli/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ func createServer(verbose bool, image string, port string, args []string, env []
}

// createWorker creates/starts a k3s agent node that connects to the server
func createWorker(verbose bool, image string, args []string, env []string, name string, volumes []string, postfix int, serverPort string) (string, error) {
func createWorker(verbose bool, image string, args []string, env []string, name string, volumes []string,
postfix int, serverPort string, pPorts *PublishedPorts) (string, error) {
containerLabels := make(map[string]string)
containerLabels["app"] = "k3d"
containerLabels["component"] = "worker"
Expand All @@ -201,11 +202,14 @@ func createWorker(verbose bool, image string, args []string, env []string, name

env = append(env, fmt.Sprintf("K3S_URL=https://k3d-%s-server:%s", name, serverPort))

workerPublishedPorts := pPorts.Offset(postfix + 1)

hostConfig := &container.HostConfig{
Tmpfs: map[string]string{
"/run": "",
"/var/run": "",
},
PortBindings: workerPublishedPorts.PortBindings,
Privileged: true,
}

Expand All @@ -226,6 +230,7 @@ func createWorker(verbose bool, image string, args []string, env []string, name
Image: image,
Env: env,
Labels: containerLabels,
ExposedPorts: workerPublishedPorts.ExposedPorts,
}

id, err := startContainer(verbose, config, hostConfig, networkingConfig, containerName)
Expand Down

0 comments on commit 65f2820

Please sign in to comment.