Skip to content

Commit e58698b

Browse files
authored
feat: add limitrange, resourcequota and networkpolicy features for k8s namespace (#147)
1 parent b361fcb commit e58698b

26 files changed

+434
-186
lines changed

README.md

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ You can find more about this project in Anton Babenko stream:
4747
- [Why you should use this boilerplate](#why-you-should-use-this-boilerplate)
4848
- [Description](#description)
4949
- [Table of contents](#table-of-contents)
50+
- [FAQ: Frequently Asked Questions](#faq-frequently-asked-questions)
5051
- [Architecture diagram](#architecture-diagram)
5152
- [Current infrastructure cost](#current-infrastructure-cost)
52-
- [EKS Upgrading](#eks-upgrading)
5353
- [Namespace structure in the K8S cluster](#namespace-structure-in-the-k8s-cluster)
5454
- [Useful tools](#useful-tools)
5555
- [Useful VSCode extensions](#useful-vscode-extensions)
@@ -77,6 +77,10 @@ You can find more about this project in Anton Babenko stream:
7777
- [TFSEC](#tfsec)
7878
- [Contributing](#contributing)
7979

80+
## FAQ: Frequently Asked Questions
81+
82+
[FAQ](docs/FAQ.md): Frequently Asked Questions
83+
8084
## Architecture diagram
8185

8286
![aws-base-diagram](docs/aws-base-diagrams-Infrastracture-v6.svg)
@@ -124,20 +128,6 @@ This diagram describes the default infrastructure:
124128
| | | | | Total | 216.8 |
125129

126130
> The cost is indicated without counting the amount of traffic for Nat Gateway Load Balancer and S3
127-
128-
## EKS Upgrading
129-
To upgrade k8s cluster to a new version, please use [official guide](https://docs.aws.amazon.com/eks/latest/userguide/update-cluster.html) and check changelog/breaking changes.
130-
Starting from v1.18 EKS supports K8S add-ons. We use them to update things like vpc-cni, kube-proxy, coredns. To get the latest add-ons versions, run:
131-
```bash
132-
aws eks describe-addon-versions --kubernetes-version 1.21 --query 'addons[].[addonName, addonVersions[0].addonVersion]'
133-
```
134-
where 1.21 - is a k8s version on which we are updating.
135-
DO NOT FORGET!!! to update cluster-autoscaler too. It's version must be the same as the cluster version.
136-
Also ***IT'S VERY RECOMMENDED*** to check that deployed objects have actual apiVersions that won't be deleted after upgrading. There is a tool [*pluto*](https://github.com/FairwindsOps/pluto) that can help to do it.
137-
```bash
138-
Switch to the correct cluster
139-
Run `pluto detect-helm -o markdown --target-versions k8s=v1.22.0`, where `k8s=v1.22.0` is a k8s version we want to update to.
140-
```
141131
## Namespace structure in the K8S cluster
142132

143133
![aws-base-namespaces](docs/aws-base-diagrams-Namespaces-v3.svg)

docs/FAQ.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
## EKS Upgrading
2+
To upgrade k8s cluster to a new version, please use [official guide](https://docs.aws.amazon.com/eks/latest/userguide/update-cluster.html) and check changelog/breaking changes.
3+
Starting from v1.18 EKS supports K8S add-ons. We use them to update things like vpc-cni, kube-proxy, coredns. To get the latest add-ons versions, run:
4+
```bash
5+
aws eks describe-addon-versions --kubernetes-version 1.21 --query 'addons[].[addonName, addonVersions[0].addonVersion]'
6+
```
7+
where `1.21` - is a k8s version on which we are updating.
8+
DO NOT FORGET!!! to update cluster-autoscaler too. Its version must be the same as the cluster version.
9+
Also ***IT'S VERY RECOMMENDED*** to check that deployed objects have actual apiVersions that won't be deleted after upgrading. There is a tool [*pluto*](https://github.com/FairwindsOps/pluto) that can help to do it.
10+
```bash
11+
Switch to the correct cluster
12+
Run `pluto detect-helm -o markdown --target-versions k8s=v1.22.0`, where `k8s=v1.22.0` is a k8s version we want to update to.
13+
```
14+
15+
## K8S namespace features:
16+
We strongly recommend using our terraform module `kubernetes-namespace` to manage (create) k8s namespaces. It provides additional functionalities.
17+
* **LimitRange**: By default, containers run with unbounded compute resources on a Kubernetes cluster. This module has a policy [**LimitRange**](https://kubernetes.io/docs/concepts/policy/limit-range/) to constrain resource allocations (to Pods or Containers) in a namespace. Default value is:
18+
```
19+
{
20+
type = "Container"
21+
default = {
22+
cpu = "150m"
23+
memory = "128Mi"
24+
}
25+
default_request = {
26+
cpu = "100m"
27+
memory = "64Mi"
28+
}
29+
}
30+
```
31+
If you don't specify requests or limits for containers these default values will be applied.
32+
33+
* **ResourceQuota**: When several users or teams share a cluster with a fixed number of nodes, there is a concern that one team could use more than its fair share of resources. Using this module you can define [**ResourceQuota**](https://kubernetes.io/docs/concepts/policy/resource-quotas/) to provide constraints that limit aggregate resource consumption per namespace. It can limit the quantity of objects that can be created in a namespace by type, as well as the total amount of compute resources that may be consumed by resources in that namespace. Default value is empty (No any resource quotas)
34+
35+
* **NetworkPolicy**: If you want to control traffic flow at the IP address or port level (OSI layer 3 or 4), then you might consider using Kubernetes NetworkPolicies for particular applications in your cluster. [**NetworkPolicies**](https://kubernetes.io/docs/concepts/services-networking/network-policies/) are an application-centric construct which allow you to specify how a pod is allowed to communicate with various network "entities" (we use the word "entity" here to avoid overloading the more common terms such as "endpoints" and "services", which have specific Kubernetes connotations) over the network.
36+
37+
The entities that a Pod can communicate with are identified through a combination of the following 3 identifiers:
38+
39+
Other pods that are allowed (exception: a pod cannot block access to itself)
40+
Namespaces that are allowed
41+
IP blocks (exception: traffic to and from the node where a Pod is running is always allowed, regardless of the IP address of the Pod or the node)
42+
Default value is empty (No any NetworkPolicies - all traffic is allowed)
43+
44+
Example of configuring namespace LimitRange, ResourceQuota and NetworkPolicy:
45+
```
46+
module "test_namespace" {
47+
source = "../modules/kubernetes-namespace"
48+
name = "test"
49+
limits = [
50+
{
51+
type = "Container"
52+
default = {
53+
cpu = "200m"
54+
memory = "64Mi"
55+
}
56+
default_request = {
57+
cpu = "100m"
58+
memory = "32Mi"
59+
}
60+
max = {
61+
cpu = "2"
62+
}
63+
},
64+
{
65+
type = "Pod"
66+
max = {
67+
cpu = "4"
68+
}
69+
}
70+
]
71+
resource_quotas = [
72+
{
73+
name = "compute-resources"
74+
hard = {
75+
"requests.cpu" = 1
76+
"requests.memory" = "1Gi"
77+
"limits.cpu" = 2
78+
"limits.memory" = "2Gi"
79+
}
80+
scope_selector = {
81+
scope_name = "PriorityClass"
82+
operator = "NotIn"
83+
values = ["high"]
84+
}
85+
},
86+
{
87+
name = "object-counts"
88+
hard = {
89+
configmaps = 10
90+
persistentvolumeclaims = 4
91+
pods = 4
92+
replicationcontrollers = 20
93+
secrets = 10
94+
services = 10
95+
"services.loadbalancers" = 2
96+
}
97+
}
98+
]
99+
network_policies = [
100+
{
101+
name = "allow-this-namespace"
102+
policy_types = ["Ingress"]
103+
ingress = {
104+
from = [
105+
{
106+
namespace_selector = {
107+
match_labels = {
108+
name = "test"
109+
}
110+
}
111+
}
112+
]
113+
}
114+
},
115+
{
116+
name = "allow-from-ingress-namespace"
117+
policy_types = ["Ingress"]
118+
ingress = {
119+
from = [
120+
{
121+
namespace_selector = {
122+
match_labels = {
123+
name = "ing"
124+
}
125+
}
126+
}
127+
]
128+
}
129+
},
130+
{
131+
name = "allow-egress-to-dev"
132+
policy_type = ["Egress"]
133+
egress = {
134+
ports = [
135+
{
136+
port = "80"
137+
protocol = "TCP"
138+
}
139+
]
140+
to = [
141+
{
142+
namespace_selector = {
143+
match_labels = {
144+
name = "dev"
145+
}
146+
}
147+
}
148+
]
149+
}
150+
}
151+
]
152+
}
153+
```
154+

terraform/layer2-k8s/eks-aws-node-termination-handler.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ resource "helm_release" "aws_node_termination_handler" {
33
chart = "aws-node-termination-handler"
44
version = var.aws_node_termination_handler_version
55
repository = local.helm_repo_eks
6-
namespace = kubernetes_namespace.sys.id
6+
namespace = module.sys_namespace.name
77
wait = false
88
max_history = var.helm_release_history_size
99

terraform/layer2-k8s/eks-network-policy.tf renamed to terraform/layer2-k8s/eks-calico.tf

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,3 @@ resource "helm_release" "calico_daemonset" {
1515
data.template_file.calico_daemonset.rendered,
1616
]
1717
}
18-
19-
#tfsec:ignore:kubernetes-network-no-public-egress tfsec:ignore:kubernetes-network-no-public-ingress
20-
module "dev_ns_network_policy" {
21-
source = "../modules/kubernetes-network-policy-namespace"
22-
namespace = kubernetes_namespace.dev.metadata[0].name
23-
allow_from_namespaces = [module.ing_namespace.labels_name]
24-
25-
depends = [helm_release.calico_daemonset]
26-
}

terraform/layer2-k8s/eks-cert-manager.tf

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ resource "helm_release" "cert_manager" {
1010
name = "cert-manager"
1111
chart = "cert-manager"
1212
repository = local.helm_repo_certmanager
13-
namespace = kubernetes_namespace.certmanager.id
13+
namespace = module.certmanager_namespace.name
1414
version = var.cert_manager_version
1515
wait = true
1616
max_history = var.helm_release_history_size
@@ -20,10 +20,9 @@ resource "helm_release" "cert_manager" {
2020
]
2121
}
2222

23-
resource "kubernetes_namespace" "certmanager" {
24-
metadata {
25-
name = "certmanager"
26-
}
23+
module "certmanager_namespace" {
24+
source = "../modules/kubernetes-namespace"
25+
name = "certmanager"
2726
}
2827

2928
#tfsec:ignore:aws-iam-no-policy-wildcards

terraform/layer2-k8s/eks-cluster-autoscaler.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ resource "helm_release" "cluster_autoscaler" {
1414
chart = "cluster-autoscaler"
1515
repository = local.helm_repo_cluster_autoscaler
1616
version = var.cluster_autoscaler_chart_version
17-
namespace = kubernetes_namespace.sys.id
17+
namespace = module.sys_namespace.name
1818
max_history = var.helm_release_history_size
1919

2020
values = [

terraform/layer2-k8s/eks-cluster-issuer.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ data "template_file" "cluster_issuer" {
1111
resource "helm_release" "cluster_issuer" {
1212
name = "cluster-issuer"
1313
chart = "../../helm-charts/cluster-issuer"
14-
namespace = kubernetes_namespace.certmanager.id
14+
namespace = module.certmanager_namespace.name
1515
wait = false
1616
max_history = var.helm_release_history_size
1717

@@ -20,5 +20,5 @@ resource "helm_release" "cluster_issuer" {
2020
]
2121

2222
# This dep needs for correct apply
23-
depends_on = [helm_release.cert_manager, kubernetes_namespace.certmanager]
23+
depends_on = [helm_release.cert_manager]
2424
}

terraform/layer2-k8s/eks-external-dns.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ resource "helm_release" "external_dns" {
1414
chart = "external-dns"
1515
repository = local.helm_repo_bitnami
1616
version = var.external_dns_version
17-
namespace = kubernetes_namespace.dns.id
17+
namespace = module.dns_namespace.name
1818
max_history = var.helm_release_history_size
1919

2020
values = [

terraform/layer2-k8s/eks-external-secrets.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ resource "helm_release" "external_secrets" {
1212
chart = "kubernetes-external-secrets"
1313
repository = local.helm_repo_external_secrets
1414
version = var.external_secrets_version
15-
namespace = kubernetes_namespace.sys.id
15+
namespace = module.sys_namespace.name
1616
max_history = var.helm_release_history_size
1717

1818
values = [
@@ -25,7 +25,7 @@ resource "helm_release" "reloader" {
2525
chart = "reloader"
2626
repository = local.helm_repo_stakater
2727
version = var.reloader_version
28-
namespace = kubernetes_namespace.sys.id
28+
namespace = module.sys_namespace.name
2929
wait = false
3030
max_history = var.helm_release_history_size
3131
}

terraform/layer2-k8s/eks-kube-prometheus-stack.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ resource "helm_release" "prometheus_operator" {
3030
name = "kube-prometheus-stack"
3131
chart = "kube-prometheus-stack"
3232
repository = local.helm_repo_prometheus_community
33-
namespace = kubernetes_namespace.monitoring.id
33+
namespace = module.monitoring_namespace.name
3434
version = var.prometheus_operator_version
3535
wait = false
3636
max_history = var.helm_release_history_size

0 commit comments

Comments
 (0)