-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathk8s-load-balancer.tf
More file actions
204 lines (174 loc) · 7.05 KB
/
k8s-load-balancer.tf
File metadata and controls
204 lines (174 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# Infrastructure for Yandex Cloud Managed Service for Kubernetes cluster
#
# RU: https://cloud.yandex.ru/docs/managed-kubernetes/operations/create-load-balancer
# EN: https://cloud.yandex.com/en/docs/managed-kubernetes/operations/create-load-balancer
# Set the configuration of the Managed Service for Kubernetes cluster:
locals {
folder_id = "" # Set your cloud folder ID.
k8s_version = "" # Set the Kubernetes version.
sa_name = "" # Set the service account name
# The following settings are predefined. Change them only if necessary.
network_name = "k8s-network" # Name of the network
subnet_name = "subnet-a" # Name of the subnet
zone_a_v4_cidr_blocks = "10.1.0.0/16" # CIDR block for the subnet in the ru-central1-a availability zone
master_v4_cidr_blocks = "10.2.0.0/16" # CIDR block for the cluster
node_v4_cidr_blocks = "10.3.0.0/16" # CIDR block for the cluster node group
main_security_group_name = "k8s-main-sg" # Name of the main security group of the cluster
public_services_sg_name = "k8s-public-services" # Name of the public services security group for node groups
k8s_cluster_name = "k8s-cluster" # Name of the Kubernetes cluster
k8s_node_group_name = "k8s-node-group" # Name of the Kubernetes node group
}
resource "yandex_vpc_network" "k8s-network" {
description = "Network for the Managed Service for Kubernetes cluster"
name = local.network_name
}
resource "yandex_vpc_subnet" "subnet-a" {
description = "Subnet in ru-central1-a availability zone"
name = local.subnet_name
zone = "ru-central1-a"
network_id = yandex_vpc_network.k8s-network.id
v4_cidr_blocks = [local.zone_a_v4_cidr_blocks]
}
resource "yandex_vpc_security_group" "k8s-main-sg" {
description = "Security group ensure the basic performance of the cluster. Apply it to the cluster and node groups."
name = local.main_security_group_name
network_id = yandex_vpc_network.k8s-network.id
ingress {
description = "The rule allows availability checks from a range of load balancer addresses. It is required for operation of a fault-tolerant cluster and load balancer services."
protocol = "TCP"
predefined_target = "loadbalancer_healthchecks"
from_port = 0
to_port = 65535
}
ingress {
description = "The rule allows the master-node and node-node interaction within the security group"
protocol = "ANY"
predefined_target = "self_security_group"
from_port = 0
to_port = 65535
}
ingress {
description = "The rule allows receipt of debugging ICMP packets from internal subnets"
protocol = "ICMP"
v4_cidr_blocks = ["10.0.0.0/8"]
}
ingress {
description = "The rule allows access to Kubernetes API, kubectl, and other utilities"
protocol = "TCP"
v4_cidr_blocks = ["0.0.0.0/0"]
port = 443
}
ingress {
description = "The rule allows access to Kubernetes API, kubectl, and other utilities"
protocol = "TCP"
v4_cidr_blocks = ["0.0.0.0/0"]
port = 6443
}
egress {
description = "The rule allows all outgoing traffic between a master and nodes"
protocol = "ANY"
predefined_target = "self_security_group"
from_port = 0
to_port = 65535
}
}
resource "yandex_vpc_security_group" "k8s-public-services" {
description = "Security group allows connections to services from the internet. Apply the rules only for node groups."
name = local.public_services_sg_name
network_id = yandex_vpc_network.k8s-network.id
ingress {
description = "The rule allows the pod-pod and service-service interaction. Specify the subnets of your cluster and services."
protocol = "ANY"
v4_cidr_blocks = [local.master_v4_cidr_blocks,local.node_v4_cidr_blocks]
from_port = 0
to_port = 65535
}
ingress {
description = "The rule allows incoming traffic from the internet to the NodePort port range. Add ports or change existing ones to the required ports."
protocol = "TCP"
v4_cidr_blocks = ["0.0.0.0/0"]
from_port = 30000
to_port = 32767
}
egress {
description = "The rule allows all outgoing traffic. Nodes can connect to Yandex Container Registry, Object Storage, Docker Hub, and more."
protocol = "ANY"
v4_cidr_blocks = ["0.0.0.0/0"]
from_port = 0
to_port = 65535
}
}
resource "yandex_iam_service_account" "k8s-sa" {
description = "Service account for Kubernetes cluster"
name = local.sa_name
}
# Assign "editor" role to Kubernetes service account
resource "yandex_resourcemanager_folder_iam_binding" "editor" {
folder_id = local.folder_id
role = "editor"
members = [
"serviceAccount:${yandex_iam_service_account.k8s-sa.id}"
]
}
# Assign "container-registry.images.puller" role to Kubernetes service account
resource "yandex_resourcemanager_folder_iam_binding" "images-puller" {
folder_id = local.folder_id
role = "container-registry.images.puller"
members = [
"serviceAccount:${yandex_iam_service_account.k8s-sa.id}"
]
}
resource "yandex_kubernetes_cluster" "k8s-cluster" {
description = "Managed Service for Kubernetes cluster"
name = local.k8s_cluster_name
network_id = yandex_vpc_network.k8s-network.id
cluster_ipv4_range = local.master_v4_cidr_blocks
service_ipv4_range = local.node_v4_cidr_blocks
master {
version = local.k8s_version
master_location {
zone = yandex_vpc_subnet.subnet-a.zone
subnet_id = yandex_vpc_subnet.subnet-a.id
}
public_ip = true
security_group_ids = [yandex_vpc_security_group.k8s-main-sg.id]
}
service_account_id = yandex_iam_service_account.k8s-sa.id # Cluster service account ID
node_service_account_id = yandex_iam_service_account.k8s-sa.id # Node group service account ID
depends_on = [
yandex_resourcemanager_folder_iam_binding.editor,
yandex_resourcemanager_folder_iam_binding.images-puller
]
}
resource "yandex_kubernetes_node_group" "k8s-node-group" {
description = "Node group for the Managed Service for Kubernetes cluster"
name = local.k8s_node_group_name
cluster_id = yandex_kubernetes_cluster.k8s-cluster.id
version = local.k8s_version
scale_policy {
fixed_scale {
size = 1 # Number of hosts
}
}
allocation_policy {
location {
zone = yandex_vpc_subnet.subnet-a.zone
}
}
instance_template {
platform_id = "standard-v2" # Intel Cascade Lake
network_interface {
nat = true
subnet_ids = [yandex_vpc_subnet.subnet-a.id]
security_group_ids = [yandex_vpc_security_group.k8s-main-sg.id,yandex_vpc_security_group.k8s-public-services.id]
}
resources {
memory = 4 # RAM quantity in GB
cores = 4 # Number of CPU cores
}
boot_disk {
type = "network-hdd"
size = 64 # Disk size in GB
}
}
}