Skip to content

Commit

Permalink
Implement resource constraints on nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
aojea committed Oct 1, 2019
1 parent 161112b commit 2026ebd
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
11 changes: 11 additions & 0 deletions pkg/apis/config/v1alpha3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ type Node struct {
// ExtraPortMappings describes additional port mappings for the node container
// binded to a host Port
ExtraPortMappings []cri.PortMapping `json:"extraPortMappings,omitempty"`

// Constraints describe the node resources constraints
Constraints NodeResources `json:"constraints,omitempty"`
}

// NodeRole defines possible role for nodes in a Kubernetes cluster managed by `kind`
Expand Down Expand Up @@ -138,3 +141,11 @@ type PatchJSON6902 struct {
// Patch should contain the contents of the json patch as a string
Patch string `json:"patch"`
}

// NodeResources represents the node resources (CPU/Memory)
type NodeResources struct {
// The maximum amount of memory the node can use.
Memory string `json:"memory,omitempty"`
// Specify how much of the available CPU resources a node can use
Cpus string `json:"cpus,omitempty"`
}
11 changes: 11 additions & 0 deletions pkg/internal/apis/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ type Node struct {
// ExtraPortMappings describes additional port mappings for the node container
// binded to a host Port
ExtraPortMappings []cri.PortMapping

// Constraints describe the node resources constraints
Constraints NodeResources
}

// NodeRole defines possible role for nodes in a Kubernetes cluster managed by `kind`
Expand Down Expand Up @@ -137,3 +140,11 @@ type PatchJSON6902 struct {
// Patch should contain the contents of the json patch as a string
Patch string
}

// NodeResources represents the node resources (CPU/Memory)
type NodeResources struct {
// The maximum amount of memory the node can use.
Memory string
// Specify how much of the available CPU resources a node can use.
Cpus string
}
17 changes: 16 additions & 1 deletion pkg/internal/cluster/providers/docker/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,10 @@ func runArgsForNode(node *config.Node, name string, args []string) []string {
args...,
)

// convert mounts and port mappings to container run args
// convert mounts, port mappings and resource constraints to container run args
args = append(args, generateMountBindings(node.ExtraMounts...)...)
args = append(args, generatePortMappings(node.ExtraPortMappings...)...)
args = append(args, generateNodeConstraints(node.Constraints)...)

// finally, specify the image to run
return append(args, node.Image)
Expand Down Expand Up @@ -293,3 +294,17 @@ func generatePortMappings(portMappings ...cri.PortMapping) []string {
}
return args
}

// generateNodeConstraints converts the nodesConstraints to a list of args for docker
// ref https://docs.docker.com/config/containers/resource_constraints/
func generateNodeConstraints(resources config.NodeResources) []string {
var args []string
if len(resources.Cpus) > 0 {
args = append(args, fmt.Sprintf("--cpus=%s", (resources.Cpus)))
}
if len(resources.Memory) > 0 {
args = append(args, fmt.Sprintf("--memory=%s", (resources.Memory)))
}

return args
}
22 changes: 22 additions & 0 deletions site/content/docs/user/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,27 @@ nodes:
- role: worker
```

#### Limit node resources
`kind` implements [docker resource constraints][docker resource constraints],
so you can limit the memory and CPU used by any cluster node.

```yaml
kind: Cluster
apiVersion: kind.sigs.k8s.io/v1alpha3
nodes:
# the control plane node
- role: control-plane
constraints:
memory: "200m"
cpu: "2"
- role: worker
- role: worker
constraints:
memory: "100m"
cpu: "1"
```


### Configure kind to use a proxy
If you are running kind in an environment that requires a proxy, you may need to configure kind to use it.

Expand Down Expand Up @@ -409,3 +430,4 @@ kind, the Kubernetes cluster itself, etc.
[Private Registries]: /docs/user/private-registries
[customize control plane with kubeadm]: https://kubernetes.io/docs/setup/independent/control-plane-flags/
[docker enable ipv6]: https://docs.docker.com/v17.09/engine/userguide/networking/default_network/ipv6/
[docker resource constraints]: https://docs.docker.com/config/containers/resource_constraints/

0 comments on commit 2026ebd

Please sign in to comment.