Skip to content

Commit 9b1fc70

Browse files
xiaobing.mengxiaozhu36
authored andcommitted
add managed kubernetes module
1 parent 4dafe1b commit 9b1fc70

File tree

6 files changed

+361
-2
lines changed

6 files changed

+361
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77

88
# .tfvars files
99
*.tfvars
10+
crash.log
11+
.idea/

README.md

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,95 @@
1-
# terraform-alicloud-managed-kubernetes
2-
Terraform module which create Managed Kubernetes cluster(s) on Alibaba Cloud.
1+
Alibaba Cloud terraform example for kubernetes cluster
2+
======================================================
3+
4+
A terraform example to launching a kubernetes cluster in alibaba cloud.
5+
6+
These types of the module resource are supported:
7+
8+
- [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)
12+
- [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+
15+
16+
Usage
17+
-----
18+
This example can specify the following arguments to create user-defined kuberntes cluster
19+
20+
* alicloud_access_key: The Alicloud Access Key ID
21+
* alicloud_secret_key: The Alicloud Access Secret Key
22+
* region: The ID of region in which launching resources
23+
* k8s_name_prefix: The name prefix of kubernetes cluster
24+
* 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`
27+
* k8s_service_cidr: The kubernetes service cidr block. Its setting rule is same as `k8s_pod_cidr`
28+
* Other kubernetes cluster arguments
29+
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.
75+
76+
77+
Terraform version
78+
-----------------
79+
Terraform version 0.11.0 or newer and Provider version 1.57.2 or newer are required for this example to work.
80+
81+
Authors
82+
-------
83+
Created and maintained by Meng Xiaobing(@menglingwei, menglingwei@gmail.com)
84+
85+
License
86+
-------
87+
Mozilla Public License 2.0. See LICENSE for full details.
88+
89+
Reference
90+
---------
91+
* [Terraform-Provider-Alicloud Github](https://github.com/terraform-providers/terraform-provider-alicloud)
92+
* [Terraform-Provider-Alicloud Release](https://releases.hashicorp.com/terraform-provider-alicloud/)
93+
* [Terraform-Provider-Alicloud Docs](https://www.terraform.io/docs/providers/alicloud/)
94+
95+

main.tf

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Provider specific configs
2+
provider "alicloud" {
3+
version = ">=1.57.2"
4+
region = var.region != "" ? var.region : null
5+
configuration_source = "terraform-alicloud-modules/kubernetes"
6+
}
7+
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+
)
75+
description = "created by terraform for managedkubernetes cluster"
76+
}
77+
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
91+
worker_disk_category = var.worker_disk_category
92+
password = var.ecs_password
93+
pod_cidr = var.k8s_pod_cidr
94+
service_cidr = var.k8s_service_cidr
95+
slb_internet_enabled = true
96+
install_cloud_monitor = true
97+
cluster_network_type = var.cluster_network_type
98+
99+
depends_on = [alicloud_snat_entry.default]
100+
worker_instance_types = var.worker_instance_types
101+
worker_number = var.worker_number
102+
log_config {
103+
type = "SLS"
104+
project = alicloud_log_project.log.name
105+
}
106+
}

outputs.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Output VPC
2+
output "vpc_id" {
3+
description = "The ID of the VPC."
4+
value = alicloud_cs_managed_kubernetes.k8s[0].vpc_id
5+
}
6+
7+
output "vswitch_ids" {
8+
description = "List ID of the VSwitches."
9+
value = [alicloud_cs_managed_kubernetes.k8s.*.vswitch_ids]
10+
}
11+
12+
//output "nat_gateway_id" {
13+
// value = alicloud_cs_managed_kubernetes.k8s[0].nat
14+
//}
15+
16+
//Output SLS
17+
output "sls_project_name" {
18+
value = alicloud_log_project.log.name
19+
}
20+
21+
// Output kubernetes resource
22+
output "cluster_id" {
23+
description = "ID of the kunernetes cluster."
24+
value = alicloud_cs_managed_kubernetes.k8s.*.id
25+
}
26+
27+
output "security_group_id" {
28+
description = "ID of the Security Group used to deploy kubernetes cluster."
29+
value = alicloud_cs_managed_kubernetes.k8s[0].security_group_id
30+
}
31+
32+
output "cluster_nodes" {
33+
description = "List nodes of cluster."
34+
value = alicloud_cs_managed_kubernetes.k8s.*.worker_nodes
35+
}

variables.tf

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# common variables
2+
3+
4+
variable "region" {
5+
description = "The region used to launch this module resources."
6+
default = "cn-beijing"
7+
}
8+
9+
variable "availability_zone" {
10+
description = "The available zone to launch ecs instance and other resources."
11+
default = ""
12+
}
13+
14+
variable "number_format" {
15+
description = "The number format used to output."
16+
default = "%02d"
17+
}
18+
19+
variable "example_name" {
20+
default = "tf-example-managed-kubernetes"
21+
}
22+
23+
# Instance typs variables
24+
variable "cpu_core_count" {
25+
description = "CPU core count is used to fetch instance types."
26+
default = 1
27+
}
28+
29+
variable "memory_size" {
30+
description = "Memory size used to fetch instance types."
31+
default = 2
32+
}
33+
34+
# VPC variables
35+
variable "vpc_name" {
36+
description = "The vpc name used to create a new vpc when 'vpc_id' is not specified. Default to variable `example_name`"
37+
default = ""
38+
}
39+
40+
variable "vpc_id" {
41+
description = "A existing vpc id used to create several vswitches and other resources."
42+
default = ""
43+
}
44+
45+
variable "vpc_cidr" {
46+
description = "The cidr block used to launch a new vpc when 'vpc_id' is not specified."
47+
default = "192.168.0.0/16"
48+
}
49+
50+
# VSwitch variables
51+
variable "vswitch_name_prefix" {
52+
description = "The vswitch name prefix used to create several new vswitches. Default to variable `example_name`"
53+
default = ""
54+
}
55+
56+
variable "vswitch_ids" {
57+
description = "List of existing vswitch id."
58+
type = list(string)
59+
default = []
60+
}
61+
62+
variable "vswitch_cidrs" {
63+
description = "List of cidr blocks used to create several new vswitches when 'vswitch_ids' is not specified."
64+
type = list(string)
65+
default = ["192.168.1.0/24"]
66+
}
67+
68+
variable "new_nat_gateway" {
69+
description = "Whether to create a new nat gateway. In this template, a new nat gateway will create a nat gateway, eip and server snat entries."
70+
default = "true"
71+
}
72+
73+
# Cluster nodes variables
74+
75+
variable "worker_instance_types" {
76+
description = "The ecs instance type used to launch worker nodes. Default from instance typs datasource."
77+
type = list(string)
78+
default = ["ecs.n4.xlarge"]
79+
}
80+
81+
variable "worker_disk_category" {
82+
description = "The system disk category used to launch one or more worker nodes."
83+
default = "cloud_efficiency"
84+
}
85+
86+
variable "worker_disk_size" {
87+
description = "The system disk size used to launch one or more worker nodes."
88+
default = "40"
89+
}
90+
91+
variable "ecs_password" {
92+
description = "The password of instance."
93+
default = "Abc12345"
94+
}
95+
96+
variable "worker_number" {
97+
description = "The number of kubernetes cluster."
98+
default = 2
99+
}
100+
101+
variable "k8s_name_prefix" {
102+
description = "The name prefix used to create several kubernetes clusters. Default to variable `example_name`"
103+
default = ""
104+
}
105+
106+
variable "k8s_pod_cidr" {
107+
description = "The kubernetes pod cidr block. It cannot be equals to vpc's or vswitch's and cannot be in them."
108+
default = "172.20.0.0/16"
109+
}
110+
111+
variable "k8s_service_cidr" {
112+
description = "The kubernetes service cidr block. It cannot be equals to vpc's or vswitch's or pod's and cannot be in them."
113+
default = "172.21.0.0/20"
114+
}
115+
116+
variable "cluster_network_type" {
117+
description = "Network type, valid options are flannel, terway"
118+
default = "flannel"
119+
}

versions.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
terraform {
3+
required_version = ">= 0.12"
4+
}

0 commit comments

Comments
 (0)