Skip to content

Commit 2268b21

Browse files
committed
support to create a new vpc and improve this module
1 parent 9b1fc70 commit 2268b21

File tree

8 files changed

+297
-224
lines changed

8 files changed

+297
-224
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
*.tfvars
1010
crash.log
1111
.idea/
12+
terraform.log

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## 1.1.0 (Unreleased)
2+
## 1.0.0 (December 3, 2019)
3+
4+
IMPROVEMENTS:
5+
6+
- support to create a new vpc and improve this module [GH-3](https://github.com/terraform-alicloud-modules/terraform-alicloud-managed-kubernetes/pull/3)
7+
- add managed kubernetes module [GH-1](https://github.com/terraform-alicloud-modules/terraform-alicloud-managed-kubernetes/pull/1)
8+
9+

README.md

Lines changed: 115 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
Alibaba Cloud terraform example for kubernetes cluster
1+
Alibaba Cloud Managed Kubernetes Cluster Module
2+
terraform-alicloud-managed-kubernetes
23
======================================================
34

4-
A terraform example to launching a kubernetes cluster in alibaba cloud.
5+
A terraform module used to launch a managed kubernetes cluster on Alibaba Cloud.
56

67
These types of the module resource are supported:
78

89
- [VPC](https://www.terraform.io/docs/providers/alicloud/r/vpc.html)
9-
- [Subnet](https://www.terraform.io/docs/providers/alicloud/r/vswitch.html)
10-
- [ECS Instance](https://www.terraform.io/docs/providers/alicloud/r/instance.html)
11-
- [Security Group](https://www.terraform.io/docs/providers/alicloud/r/security_group.html)
10+
- [VSwitch](https://www.terraform.io/docs/providers/alicloud/r/vswitch.html)
11+
- [EIP](https://www.terraform.io/docs/providers/alicloud/r/eip.html)
1212
- [Nat Gateway](https://www.terraform.io/docs/providers/alicloud/r/nat_gateway.html)
13-
- [ManagedKubernetes](https://www.terraform.io/docs/providers/alicloud/r/cs_managed_kubernetes.html)
14-
13+
- [Snat](https://www.terraform.io/docs/providers/alicloud/r/snat.html)
14+
- [SLS Project](https://www.terraform.io/docs/providers/alicloud/r/log_project.html)
15+
- [Managed Kubernetes](https://www.terraform.io/docs/providers/alicloud/r/cs_managed_kubernetes.html)
1516

1617
Usage
1718
-----
@@ -22,69 +23,126 @@ This example can specify the following arguments to create user-defined kubernte
2223
* region: The ID of region in which launching resources
2324
* k8s_name_prefix: The name prefix of kubernetes cluster
2425
* worker_number: The number of worker nodes in each kubernetes cluster
25-
* k8s_pod_cidr: The kubernetes pod cidr block. It cannot be equals to vpc's or vswitch's and cannot be in them. If vpc's cidr block is `172.16.XX.XX/XX`,
26-
it had better to `192.168.XX.XX/XX` or `10.XX.XX.XX/XX`
26+
* k8s_pod_cidr: The kubernetes pod cidr block. It cannot be equals to vpc's or vswitch's and cannot be in them.
2727
* k8s_service_cidr: The kubernetes service cidr block. Its setting rule is same as `k8s_pod_cidr`
2828
* Other kubernetes cluster arguments
2929

30-
**Note:** In order to avoid some needless error, you had better to set `new_nat_gateway` to `true`.
31-
Otherwise, you must you must ensure you specified vswitches can access internet before running the example.
32-
33-
Planning phase
34-
35-
terraform plan
36-
37-
Apply phase
38-
39-
terraform apply
40-
41-
42-
Destroy
43-
44-
terraform destroy
45-
46-
47-
Conditional creation
48-
--------------------
49-
This example can support the following creating kubernetes cluster scenario by setting different arguments.
50-
51-
### 1. Create a new vpc, vswitches and nat gateway for the cluster.
52-
53-
You can specify the following user-defined arguments:
54-
55-
* vpc_name: A new vpc name
56-
* vpc_cidr: A new vpc cidr block
57-
* vswitch_name_prefix: The name prefix of several vswitches
58-
* vswitch_cidrs: List of cidr blocks for several new vswitches
59-
60-
### 2. Using existing vpc and vswitches for the cluster.
61-
62-
You can specify the following user-defined arguments:
63-
64-
* vpc_id: A existing vpc ID
65-
* vswitch_ids: List of IDs for several existing vswitches
66-
67-
### 3. Using existing vpc, vswitches and nat gateway for the cluster.
68-
69-
You can specify the following user-defined arguments:
70-
71-
* vpc_id: A existing vpc ID
72-
* vswitch_ids: List of IDs for several existing vswitches
73-
* new_nat_gateway: Set it to false. But you must ensure you specified vswitches can access internet.
74-
In other words, you must set snat entry for each vswitch before running the example.
30+
Usage
31+
-----
7532

33+
This module used to create a managed kubernetes and it can meet several scenarios by specifying different parameters.
34+
35+
1. Create a new vpc, several new vswitches and a new nat gateway for the cluster.
36+
```hcl
37+
// Create a scaling group using autoscaling module at first.
38+
module "managed-k8s" {
39+
source = "terraform-alicloud-modules/managed-kubernetes/alicloud"
40+
profile = "Your-profile-name"
41+
42+
k8s_name_prefix = "my-managed-k8s-with-new-vpc"
43+
new_vpc = true
44+
vpc_cidr = "192.168.0.0/16"
45+
vswitch_cidrs = [
46+
"192.168.1.0/24",
47+
"192.168.2.0/24",
48+
"192.168.3.0/24",
49+
"192.168.4.0/24",
50+
]
51+
}
52+
```
53+
54+
In this scenario, the module will create a new vpc with `vpc_cidr`, several vswitches with `vswitch_cidrs`, a new nat gateway,
55+
a new EIP with `new_eip_bandwidth` and several snat entries for vswitches.
56+
57+
1. Using existing vpc and vswitches by specifying `vswitch_ids`. Setting `new_nat_gateway=true` to add a new nat gateway in the vswitches' vpc.
58+
```hcl
59+
// Create a scaling group using autoscaling module at first.
60+
module "managed-k8s" {
61+
source = "terraform-alicloud-modules/managed-kubernetes/alicloud"
62+
profile = "Your-profile-name"
63+
64+
k8s_name_prefix = "my-managed-k8s-with-new-vpc"
65+
new_vpc = false
66+
vswitch_ids = [
67+
"vsw-12345678",
68+
"vsw-09876537"
69+
]
70+
new_nat_gateway = true
71+
}
72+
```
73+
74+
In this scenario, if setting `new_nat_gateway=false`, you should ensure the specified vswitches can access internet.
75+
In other words, the specified vpc has a nat gateway and there are several snat entries to bind the vswitches and a EIP.
76+
77+
**NOTE:** This module using AccessKey and SecretKey are from `profile` and `shared_credentials_file`.
78+
If you have not set them yet, please install [aliyun-cli](https://github.com/aliyun/aliyun-cli#installation) and configure it.
79+
80+
## Conditional creation
81+
82+
This moudle can set [sls project](https://www.terraform.io/docs/providers/alicloud/r/log_project.html) config for this module
83+
84+
1. Create a new sls project with `new_sls_project`:
85+
```hcl
86+
new_sls_project = true
87+
```
88+
89+
1. Using existing sls project with `sls_project_name`:
90+
```hcl
91+
sls_project_name = "Your-sls-project-name"
92+
```
93+
94+
## Inputs
95+
96+
| Name | Description | Type | Default | Required |
97+
|------|-------------|:----:|:-----:|:-----:|
98+
| region | The region ID used to launch this module resources. If not set, it will be sourced from followed by ALICLOUD_REGION environment variable and profile | string | '' | no |
99+
| profile | The profile name as set in the shared credentials file. If not set, it will be sourced from the ALICLOUD_PROFILE environment variable. | string | '' | no |
100+
| shared_credentials_file | This is the path to the shared credentials file. If this is not set and a profile is specified, $HOME/.aliyun/config.json will be used. | string | '' | no |
101+
| skip_region_validation | Skip static validation of region ID. Used by users of alternative AlibabaCloud-like APIs or users w/ access to regions that are not public (yet). | bool | false | no |
102+
| new_vpc | Create a new vpc for this module | string | false | no |
103+
| vpc_cidr | The cidr block used to launch a new vpc | string | "192.168.0.0/16" | no |
104+
| vswitch_ids | List Ids of existing vswitch | string | [] | yes |
105+
| vswitch_cidrs | List cidr blocks used to create several new vswitches when 'new_vpc' is true | string | ["192.168.1.0/24"] | yes |
106+
| availability_zones | List available zone ids used to create several new vswitches when 'vswitch_ids' is not specified. If not set, data source `alicloud_zones` will return one automatically. | list | [] | no |
107+
| new_eip_bandwidth | The bandwidth used to create a new EIP when 'new_vpc' is true | int | 50 | no |
108+
| new_nat_gateway | Seting it to true can create a new nat gateway automatically in a existing VPC. If 'new_vpc' is true, it will be ignored | bool | false|
109+
| cpu_core_count | CPU core count is used to fetch instance types | int | 1 | no |
110+
| memory_size | Memory size used to fetch instance types | int | 2 | no |
111+
| worker_instance_types | The ecs instance type used to launch worker nodes. If not set, data source `alicloud_instance_types` will return one based on `cpu_core_count` and `memory_size` | list | ["ecs.n4.xlarge"] | no |
112+
| worker_disk_category | The system disk category used to launch one or more worker nodes| string | "cloud_efficiency" | no |
113+
| worker_disk_size | The system disk size used to launch one or more worker nodes| int | 40 |no |
114+
| ecs_password | The password of work nodes | string | "Abc12345" | no |
115+
| worker_number | The number of kubernetes cluster work nodes | int | 2 | no |
116+
| k8s_name_prefix | The name prefix used to create managed kubernetes cluster | string | "terraform-alicloud-managed-kubernetes" | no |
117+
| k8s_pod_cidr | The kubernetes pod cidr block. It cannot be equals to vpc's or vswitch's and cannot be in them. If vpc's cidr block is `172.16.XX.XX/XX`, it had better to `192.168.XX.XX/XX` or `10.XX.XX.XX/XX` | string | "172.20.0.0/16" | no |
118+
| k8s_service_cidr | The kubernetes service cidr block. It cannot be equals to vpc's or vswitch's or pod's and cannot be in them. Its setting rule is same as `k8s_pod_cidr` | string | "172.21.0.0/20" | no |
119+
| cluster_network_type | Network type, valid options are `flannel` and `terway` | string | "flannel" | no |
120+
| new_sls_project | Create a new sls project for this module | bool | false | no |
121+
| sls_project_name | Specify a existing sls project for this module | string | "" | no |
122+
123+
## Outputs
124+
125+
| Name | Description |
126+
|------|-------------|
127+
| this_k8s_id | The ID of managed kubernetes cluster |
128+
| this_k8s_name | The name of managed kubernetes cluster |
129+
| this_k8s_nodes | List worker nodes of managed kubernetes cluster |
130+
| this_vpc_id | The ID of VPC |
131+
| this_vswitch_ids | List Ids of vswitches |
132+
| this_security_group_id | ID of the Security Group used to deploy kubernetes cluster |
133+
| this_sls_project_name | The sls project name used to configure cluster |
76134
77135
Terraform version
78136
-----------------
79-
Terraform version 0.11.0 or newer and Provider version 1.57.2 or newer are required for this example to work.
137+
Terraform version 0.12.0 or newer and Provider version 1.57.2 or newer are required for this example to work.
80138
81139
Authors
82140
-------
83-
Created and maintained by Meng Xiaobing(@menglingwei, menglingwei@gmail.com)
141+
Created and maintained by Meng Xiaobing(@menglingwei, menglingwei@gmail.com, @xiaozhu36, heguimin36@163.com)
84142
85143
License
86144
-------
87-
Mozilla Public License 2.0. See LICENSE for full details.
145+
Apache 2 Licensed. See LICENSE for full details.
88146
89147
Reference
90148
---------

locals.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
locals {
2+
k8s_name = substr(join("-", [var.k8s_name_prefix, "", random_uuid.this.result]), 0, 63)
3+
new_vpc_name = "for-${local.k8s_name}"
4+
new_vpc_tags = {
5+
Created = "Terraform"
6+
For = "modules/terraform-alicloud-managed-kubernetes"
7+
K8s = local.k8s_name
8+
}
9+
vswitch_ids = length(var.vswitch_ids) > 0 ? var.vswitch_ids : alicloud_vswitch.new.*.id
10+
sls_project = var.sls_project_name == "" ? alicloud_log_project.new.0.id : var.sls_project_name
11+
instance_types = length(var.worker_instance_types) > 0 ? var.worker_instance_types : [data.alicloud_instance_types.default.ids.0]
12+
}
13+
14+
resource "random_uuid" "this" {}

main.tf

Lines changed: 19 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,36 @@
11
// Provider specific configs
22
provider "alicloud" {
3-
version = ">=1.57.2"
4-
region = var.region != "" ? var.region : null
5-
configuration_source = "terraform-alicloud-modules/kubernetes"
3+
version = ">=1.57.2"
4+
profile = var.profile != "" ? var.profile : null
5+
shared_credentials_file = var.shared_credentials_file != "" ? var.shared_credentials_file : null
6+
region = var.region != "" ? var.region : null
7+
skip_region_validation = var.skip_region_validation
8+
configuration_source = "terraform-alicloud-modules/managed-kubernetes"
69
}
710

8-
// Instance_types data source for instance_type
9-
data "alicloud_instance_types" "default" {
10-
cpu_core_count = var.cpu_core_count
11-
memory_size = var.memory_size
12-
}
13-
14-
// Zones data source for availability_zone
15-
data "alicloud_zones" "default" {
16-
available_instance_type = data.alicloud_instance_types.default.instance_types[0].id
17-
}
18-
19-
// If there is not specifying vpc_id, the module will launch a new vpc
20-
resource "alicloud_vpc" "vpc" {
21-
count = var.vpc_id == "" ? 1 : 0
22-
cidr_block = var.vpc_cidr
23-
name = var.vpc_name == "" ? var.example_name : var.vpc_name
24-
}
25-
26-
// According to the vswitch cidr blocks to launch several vswitches
27-
resource "alicloud_vswitch" "vswitches" {
28-
count = length(var.vswitch_ids) > 0 ? 0 : length(var.vswitch_cidrs)
29-
vpc_id = var.vpc_id == "" ? join("", alicloud_vpc.vpc.*.id) : var.vpc_id
30-
cidr_block = var.vswitch_cidrs[count.index]
31-
availability_zone = data.alicloud_zones.default.zones[count.index % length(data.alicloud_zones.default.zones)]["id"]
32-
name = var.vswitch_name_prefix == "" ? format(
33-
"%s-%s",
34-
var.example_name,
35-
format(var.number_format, count.index + 1),
36-
) : format(
37-
"%s-%s",
38-
var.vswitch_name_prefix,
39-
format(var.number_format, count.index + 1),
40-
)
41-
}
42-
43-
resource "alicloud_nat_gateway" "default" {
44-
count = var.new_nat_gateway == "true" ? 1 : 0
45-
vpc_id = var.vpc_id == "" ? join("", alicloud_vpc.vpc.*.id) : var.vpc_id
46-
name = var.example_name
47-
}
48-
49-
resource "alicloud_eip" "default" {
50-
count = var.new_nat_gateway == "true" ? 1 : 0
51-
bandwidth = 100
52-
}
53-
54-
resource "alicloud_eip_association" "default" {
55-
count = var.new_nat_gateway == "true" ? 1 : 0
56-
allocation_id = alicloud_eip.default[0].id
57-
instance_id = alicloud_nat_gateway.default[0].id
58-
}
59-
60-
resource "alicloud_snat_entry" "default" {
61-
count = var.new_nat_gateway == "false" ? 0 : length(var.vswitch_ids) > 0 ? length(var.vswitch_ids) : length(var.vswitch_cidrs)
62-
snat_table_id = alicloud_nat_gateway.default[0].snat_table_ids
63-
source_vswitch_id = length(var.vswitch_ids) > 0 ? split(",", join(",", var.vswitch_ids))[count.index % length(split(",", join(",", var.vswitch_ids)))] : length(var.vswitch_cidrs) < 1 ? "" : split(",", join(",", alicloud_vswitch.vswitches.*.id))[count.index % length(split(",", join(",", alicloud_vswitch.vswitches.*.id)))]
64-
snat_ip = alicloud_eip.default[0].ip_address
65-
}
66-
67-
resource "alicloud_log_project" "log" {
68-
name = var.k8s_name_prefix == "" ? format(
69-
"%s-managed-sls",
70-
var.example_name,
71-
) : format(
72-
"%s-managed-sls",
73-
var.k8s_name_prefix,
74-
)
11+
resource "alicloud_log_project" "new" {
12+
count = var.new_sls_project == true ? 1 : 0
13+
name = "for-${local.k8s_name}"
7514
description = "created by terraform for managedkubernetes cluster"
7615
}
7716

78-
resource "alicloud_cs_managed_kubernetes" "k8s" {
79-
count = 1
80-
name = var.k8s_name_prefix == "" ? format(
81-
"%s-%s",
82-
var.example_name,
83-
format(var.number_format, count.index + 1),
84-
) : format(
85-
"%s-%s",
86-
var.k8s_name_prefix,
87-
format(var.number_format, count.index + 1),
88-
)
89-
vswitch_ids = [length(var.vswitch_ids) > 0 ? split(",", join(",", var.vswitch_ids))[count.index%length(split(",", join(",", var.vswitch_ids)))] : length(var.vswitch_cidrs) < 1 ? "" : split(",", join(",", alicloud_vswitch.vswitches.*.id))[count.index%length(split(",", join(",", alicloud_vswitch.vswitches.*.id)))]]
90-
new_nat_gateway = false
17+
resource "alicloud_cs_managed_kubernetes" "this" {
18+
count = length(local.vswitch_ids) > 0 ? 1 : 0
19+
name = local.k8s_name
20+
vswitch_ids = local.vswitch_ids
21+
new_nat_gateway = var.new_vpc == true ? false : var.new_nat_gateway
9122
worker_disk_category = var.worker_disk_category
9223
password = var.ecs_password
9324
pod_cidr = var.k8s_pod_cidr
9425
service_cidr = var.k8s_service_cidr
95-
slb_internet_enabled = true
26+
slb_internet_enabled = true
9627
install_cloud_monitor = true
9728
cluster_network_type = var.cluster_network_type
98-
99-
depends_on = [alicloud_snat_entry.default]
10029
worker_instance_types = var.worker_instance_types
101-
worker_number = var.worker_number
30+
worker_number = var.worker_number
10231
log_config {
103-
type = "SLS"
104-
project = alicloud_log_project.log.name
32+
type = local.sls_project == "" ? null : "SLS"
33+
project = local.sls_project == "" ? null : local.sls_project
10534
}
35+
depends_on = [alicloud_snat_entry.new]
10636
}

0 commit comments

Comments
 (0)