Skip to content

Commit 11a8ec8

Browse files
committed
feat: 解决方案deploy-nginx-services-through-ingress tf文件完成
1 parent 7233118 commit 11a8ec8

File tree

5 files changed

+308
-0
lines changed

5 files changed

+308
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## Introduction
2+
<!-- DOCS_DESCRIPTION_CN -->
3+
本示例用于实现解决方案[部署 Nginx 并通过 Ingress 暴露服务](https://www.aliyun.com/solution/tech-solution/nginx-ingress), 涉及到专有网络(VPC)、交换机(VSwitch)、云服务器(ECS)、 容器服务 Kubernetes(ACK)、日志服务(SLS)。
4+
<!-- DOCS_DESCRIPTION_CN -->
5+
6+
<!-- DOCS_DESCRIPTION_EN -->
7+
This example demonstrates the implementation of the solution [Deploy Nginx and expose the service through Ingress](https://www.aliyun.com/solution/tech-solution/nginx-ingress). It involves the creation, configuration, and deployment of resources such as Virtual Private Cloud (Vpc), Virtual Switch (VSwitch), Elastic Compute Service (Ecs), Container Service for Kubernetes (ACK), and Simple Log Service (SLS).
8+
<!-- DOCS_DESCRIPTION_EN -->
9+
10+
11+
<!-- BEGIN_TF_DOCS -->
12+
## Providers
13+
14+
| Name | Version |
15+
|------|---------|
16+
| <a name="provider_alicloud"></a> [alicloud](#provider\_alicloud) | n/a |
17+
18+
## Modules
19+
20+
No modules.
21+
22+
## Resources
23+
24+
| Name | Type |
25+
|------|------|
26+
| [alicloud_cs_kubernetes_node_pool.node_pool](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/cs_kubernetes_node_pool) | resource |
27+
| [alicloud_cs_managed_kubernetes.ack](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/cs_managed_kubernetes) | resource |
28+
| [alicloud_ram_role.role](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/ram_role) | resource |
29+
| [alicloud_ram_role_policy_attachment.attach](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/ram_role_policy_attachment) | resource |
30+
| [alicloud_vpc.vpc](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vpc) | resource |
31+
| [alicloud_vswitch.vsw](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vswitch) | resource |
32+
| [alicloud_ack_service.open_ack](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/ack_service) | data source |
33+
| [alicloud_instance_types.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/instance_types) | data source |
34+
| [alicloud_log_service.open_sls](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/log_service) | data source |
35+
| [alicloud_ram_roles.roles](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/ram_roles) | data source |
36+
| [alicloud_zones.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/zones) | data source |
37+
38+
## Inputs
39+
40+
| Name | Description | Type | Default | Required |
41+
|------|-------------|------|---------|:--------:|
42+
| <a name="input_ack_name"></a> [ack\_name](#input\_ack\_name) | 集群名称:The name must be 1 to 63 characters in length and can contain letters, Chinese characters, digits, and hyphens (-). | `string` | `"cluster-for-nginx-test"` | no |
43+
| <a name="input_common_name"></a> [common\_name](#input\_common\_name) | Common Name | `string` | `"ack-for-nginx"` | no |
44+
| <a name="input_region"></a> [region](#input\_region) | 地域 | `string` | `"cn-hangzhou"` | no |
45+
<!-- END_TF_DOCS -->
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
# 开通sls服务
2+
data "alicloud_log_service" "open_sls" {
3+
enable = "On"
4+
}
5+
6+
# 开通ack pro
7+
data "alicloud_ack_service" "open_ack" {
8+
enable = "On"
9+
type = "propayasgo"
10+
}
11+
12+
# 查询实例实例规格
13+
data "alicloud_instance_types" "default" {
14+
instance_type_family = "ecs.g7"
15+
sorted_by = "CPU"
16+
memory_size = "16"
17+
}
18+
19+
# 查询实例规格支持的可用区
20+
data "alicloud_zones" "default" {
21+
available_instance_type = data.alicloud_instance_types.default.ids.0
22+
available_slb_address_type = "classic_internet"
23+
}
24+
25+
resource "alicloud_vpc" "vpc" {
26+
cidr_block = "192.168.0.0/16"
27+
vpc_name = "${var.common_name}-vpc"
28+
}
29+
30+
resource "alicloud_vswitch" "vsw" {
31+
vpc_id = alicloud_vpc.vpc.id
32+
cidr_block = "192.168.0.0/24"
33+
zone_id = data.alicloud_zones.default.ids.0
34+
vswitch_name = "${var.common_name}-${data.alicloud_zones.default.ids.0}-vsw"
35+
}
36+
37+
resource "alicloud_cs_managed_kubernetes" "ack" {
38+
depends_on = [data.alicloud_log_service.open_sls, data.alicloud_ack_service.open_ack, alicloud_ram_role_policy_attachment.attach]
39+
name = var.ack_name
40+
cluster_spec = "ack.pro.small"
41+
vswitch_ids = [alicloud_vswitch.vsw.id]
42+
pod_vswitch_ids = [alicloud_vswitch.vsw.id]
43+
service_cidr = "172.16.0.0/16"
44+
new_nat_gateway = true
45+
slb_internet_enabled = false
46+
is_enterprise_security_group = false
47+
48+
addons {
49+
name = "security-inspector"
50+
}
51+
addons {
52+
name = "terway-eniip"
53+
config = jsonencode({
54+
IPVlan = "false"
55+
NetworkPolicy = "false"
56+
ENITrunking = "false"
57+
})
58+
}
59+
addons {
60+
name = "csi-plugin"
61+
}
62+
addons {
63+
name = "csi-provisioner"
64+
}
65+
addons {
66+
name = "storage-operator"
67+
config = jsonencode({
68+
CnfsOssEnable = "false"
69+
CnfsNasEnable = "false"
70+
})
71+
}
72+
addons {
73+
name = "logtail-ds"
74+
config = jsonencode({
75+
IngressDashboardEnabled = "true"
76+
})
77+
}
78+
addons {
79+
name = "nginx-ingress-controller"
80+
config = jsonencode({
81+
IngressSlbNetworkType = "internet"
82+
IngressSlbSpec = "slb.s2.small"
83+
})
84+
}
85+
addons {
86+
name = "ack-node-local-dns"
87+
}
88+
addons {
89+
name = "arms-prometheus"
90+
}
91+
addons {
92+
name = "ack-node-problem-detector"
93+
config = jsonencode({
94+
sls_project_name = ""
95+
})
96+
}
97+
}
98+
99+
resource "alicloud_cs_kubernetes_node_pool" "node_pool" {
100+
node_pool_name = "${var.common_name}-nodepool"
101+
cluster_id = alicloud_cs_managed_kubernetes.ack.id
102+
vswitch_ids = [alicloud_vswitch.vsw.id]
103+
instance_types = [data.alicloud_instance_types.default.ids.0]
104+
system_disk_category = "cloud_essd"
105+
system_disk_size = 40
106+
desired_size = 3
107+
108+
runtime_name = "containerd"
109+
runtime_version = "1.6.20"
110+
}
111+
112+
# 定义本地变量,包含所有要创建的 RAM Role 及其策略
113+
locals {
114+
cs_roles = [
115+
{
116+
name = "AliyunCSManagedLogRole"
117+
policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
118+
description = "集群的日志组件使用此角色来访问您在其他云产品中的资源。"
119+
policy_name = "AliyunCSManagedLogRolePolicy"
120+
},
121+
{
122+
name = "AliyunCSManagedCmsRole"
123+
policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
124+
description = "集群的CMS组件使用此角色来访问您在其他云产品中的资源。"
125+
policy_name = "AliyunCSManagedCmsRolePolicy"
126+
},
127+
{
128+
name = "AliyunCSManagedCsiRole"
129+
policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
130+
description = "集群的存储组件使用此角色来访问您在其他云产品中的资源。"
131+
policy_name = "AliyunCSManagedCsiRolePolicy"
132+
},
133+
{
134+
name = "AliyunCSManagedCsiPluginRole"
135+
policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
136+
description = "集群的存储组件使用此角色来访问您在其他云产品中的资源。"
137+
policy_name = "AliyunCSManagedCsiPluginRolePolicy"
138+
},
139+
{
140+
name = "AliyunCSManagedCsiProvisionerRole"
141+
policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
142+
description = "集群的存储组件使用此角色来访问您在其他云产品中的资源。"
143+
policy_name = "AliyunCSManagedCsiProvisionerRolePolicy"
144+
},
145+
{
146+
name = "AliyunCSManagedVKRole"
147+
policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
148+
description = "ACK Serverless集群的VK组件使用此角色来访问您在其他云产品中的资源。"
149+
policy_name = "AliyunCSManagedVKRolePolicy"
150+
},
151+
{
152+
name = "AliyunCSServerlessKubernetesRole"
153+
policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
154+
description = "集群默认使用此角色来访问您在其他云产品中的资源。"
155+
policy_name = "AliyunCSServerlessKubernetesRolePolicy"
156+
},
157+
{
158+
name = "AliyunCSKubernetesAuditRole"
159+
policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
160+
description = "集群审计功能使用此角色来访问您在其他云产品中的资源。"
161+
policy_name = "AliyunCSKubernetesAuditRolePolicy"
162+
},
163+
{
164+
name = "AliyunCSManagedNetworkRole"
165+
policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
166+
description = "集群网络组件使用此角色来访问您在其他云产品中的资源。"
167+
policy_name = "AliyunCSManagedNetworkRolePolicy"
168+
},
169+
{
170+
name = "AliyunCSDefaultRole"
171+
policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
172+
description = "集群操作时默认使用此角色来访问您在其他云产品中的资源。"
173+
policy_name = "AliyunCSDefaultRolePolicy"
174+
},
175+
{
176+
name = "AliyunCSManagedKubernetesRole"
177+
policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
178+
description = "集群默认使用此角色来访问您在其他云产品中的资源。"
179+
policy_name = "AliyunCSManagedKubernetesRolePolicy"
180+
},
181+
{
182+
name = "AliyunCSManagedArmsRole"
183+
policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
184+
description = "集群Arms插件使用此角色来访问您在其他云产品中的资源。"
185+
policy_name = "AliyunCSManagedArmsRolePolicy"
186+
},
187+
{
188+
name = "AliyunCISDefaultRole"
189+
policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
190+
description = "容器服务(CS)智能运维使用此角色来访问您在其他云产品中的资源。"
191+
policy_name = "AliyunCISDefaultRolePolicy"
192+
},
193+
{
194+
name = "AliyunOOSLifecycleHook4CSRole"
195+
policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"oos.aliyuncs.com\"]}}],\"Version\":\"1\"}"
196+
description = "集群扩缩容节点池依赖OOS服务,OOS使用此角色来访问您在其他云产品中的资源。"
197+
policy_name = "AliyunOOSLifecycleHook4CSRolePolicy"
198+
},
199+
{
200+
name = "AliyunCSManagedAutoScalerRole"
201+
policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
202+
description = "集群的弹性伸缩组件使用此角色来访问您在其他云产品中的资源。"
203+
policy_name = "AliyunCSManagedAutoScalerRolePolicy"
204+
}
205+
]
206+
}
207+
208+
// 查询RAM角色列表
209+
data "alicloud_ram_roles" "roles" {
210+
policy_type = "Custom"
211+
name_regex = "^Aliyun.*Role$"
212+
}
213+
214+
locals {
215+
# 提取所有所需RAM角色name
216+
all_role_names = [for role in local.cs_roles : role.name]
217+
# 提取已存在的RAM角色name
218+
created_role_names = [for role in data.alicloud_ram_roles.roles.roles : role.name]
219+
# 计算补集:即找出还未创建的所需RAM角色
220+
complement_names = setsubtract(local.all_role_names, local.created_role_names)
221+
# 待创建的RAM角色
222+
complement_roles = [for role in local.cs_roles : role if contains(local.complement_names, role.name)]
223+
}
224+
225+
// 创建角色。
226+
resource "alicloud_ram_role" "role" {
227+
for_each = { for r in local.complement_roles : r.name => r }
228+
role_name = each.value.name
229+
assume_role_policy_document = each.value.policy_document
230+
description = each.value.description
231+
}
232+
233+
// 角色关联系统权限。
234+
resource "alicloud_ram_role_policy_attachment" "attach" {
235+
for_each = { for r in local.complement_roles : r.name => r }
236+
policy_name = each.value.policy_name
237+
policy_type = "System"
238+
role_name = each.value.name
239+
depends_on = [alicloud_ram_role.role]
240+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "ack_cluster_id" {
2+
value = alicloud_cs_managed_kubernetes.ack.id
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
provider "alicloud" {
2+
region = var.region
3+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
variable "region" {
2+
description = "地域"
3+
type = string
4+
default = "cn-hangzhou"
5+
}
6+
7+
variable "ack_name" {
8+
description = "集群名称:The name must be 1 to 63 characters in length and can contain letters, Chinese characters, digits, and hyphens (-)."
9+
type = string
10+
default = "cluster-for-nginx-test"
11+
}
12+
13+
variable "common_name" {
14+
description = "Common Name"
15+
type = string
16+
default = "ack-for-nginx"
17+
}

0 commit comments

Comments
 (0)