Cloud Virtual Machine Provisioning

    如果您手头没有 架构的PC、笔记本、Mac,使用即用即毁的云虚拟机可能是另一个不错的选择。

    是开源免费的 基础设施即代码 工具。您只需要声明好所需的云虚拟机、网络与安全组配置等,一键即可拉起对应的资源。

    在MacOS下安装Terraform,只需要执行brew install terraform即可。然后您需要有云厂商账号,并获取AccessKey与AccessSecret凭证,充点钱,就可以开始云端沙箱部署之旅啦。

    项目根目录 terraform/ 中提供了若干云厂商的 Terraform 资源定义文件,您可以使用这些模板快速在云上申请虚拟机资源用于部署Pigsty。这里以阿里云为例:

    1. provider "alicloud" {
    2. access_key = "xxxxxx"
    3. secret_key = "xxxxxx"
    4. region = "cn-beijing"
    5. }
    6. # use 10.10.10.0/24 cidr block as demo network
    7. resource "alicloud_vpc" "vpc" {
    8. vpc_name = "pigsty-demo-network"
    9. cidr_block = "10.10.10.0/24"
    10. }
    11. # add virtual switch for pigsty demo network
    12. resource "alicloud_vswitch" "vsw" {
    13. vpc_id = "${alicloud_vpc.vpc.id}"
    14. cidr_block = "10.10.10.0/24"
    15. zone_id = "cn-beijing-k"
    16. }
    17. # add default security group and allow all tcp traffic
    18. resource "alicloud_security_group" "default" {
    19. name = "default"
    20. vpc_id = "${alicloud_vpc.vpc.id}"
    21. }
    22. resource "alicloud_security_group_rule" "allow_all_tcp" {
    23. ip_protocol = "tcp"
    24. type = "ingress"
    25. nic_type = "intranet"
    26. policy = "accept"
    27. port_range = "1/65535"
    28. priority = 1
    29. security_group_id = "${alicloud_security_group.default.id}"
    30. cidr_ip = "0.0.0.0/0"
    31. }
    32. # https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/instance
    33. resource "alicloud_instance" "pg-meta-1" {
    34. instance_name = "pg-meta-1"
    35. host_name = "pg-meta-1"
    36. instance_type = "ecs.s6-c1m2.small"
    37. vswitch_id = "${alicloud_vswitch.vsw.id}"
    38. security_groups = ["${alicloud_security_group.default.id}"]
    39. image_id = "centos_7_8_x64_20G_alibase_20200914.vhd"
    40. password = "PigstyDemo4"
    41. private_ip = "10.10.10.10"
    42. internet_max_bandwidth_out = 40 # 40Mbps , alloc a public IP
    43. }
    44. resource "alicloud_instance" "pg-test-1" {
    45. instance_name = "pg-test-1"
    46. host_name = "pg-test-1"
    47. instance_type = "ecs.s6-c1m1.small"
    48. vswitch_id = "${alicloud_vswitch.vsw.id}"
    49. security_groups = ["${alicloud_security_group.default.id}"]
    50. image_id = "centos_7_8_x64_20G_alibase_20200914.vhd"
    51. password = "PigstyDemo4"
    52. private_ip = "10.10.10.11"
    53. }
    54. resource "alicloud_instance" "pg-test-2" {
    55. instance_name = "pg-test-2"
    56. host_name = "pg-test-2"
    57. instance_type = "ecs.s6-c1m1.small"
    58. vswitch_id = "${alicloud_vswitch.vsw.id}"
    59. security_groups = ["${alicloud_security_group.default.id}"]
    60. image_id = "centos_7_8_x64_20G_alibase_20200914.vhd"
    61. password = "PigstyDemo4"
    62. private_ip = "10.10.10.12"
    63. }
    64. resource "alicloud_instance" "pg-test-3" {
    65. instance_name = "pg-test-3"
    66. host_name = "pg-test-3"
    67. instance_type = "ecs.s6-c1m1.small"
    68. vswitch_id = "${alicloud_vswitch.vsw.id}"
    69. security_groups = ["${alicloud_security_group.default.id}"]
    70. image_id = "centos_7_8_x64_20G_alibase_20200914.vhd"
    71. password = "PigstyDemo4"
    72. private_ip = "10.10.10.13"
    73. }
    74. output "meta_ip" {
    75. value = "${alicloud_instance.pg-meta-1.public_ip}"
    76. }

    首先,使用terraform命令,创建上面定义的云资源(共享1C1G临时用用很便宜,按需付费)

    执行 apply 并输入 yes后,terraform会调用阿里云API创建对应的虚拟机资源。

    Terraform Apply执行结果

    1. Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
    2. + create
    3. Terraform will perform the following actions:
    4. # alicloud_instance.pg-meta-1 will be created
    5. + resource "alicloud_instance" "pg-meta-1" {
    6. + availability_zone = (known after apply)
    7. + credit_specification = (known after apply)
    8. + deletion_protection = false
    9. + dry_run = false
    10. + host_name = "pg-meta-1"
    11. + id = (known after apply)
    12. + image_id = "centos_7_8_x64_20G_alibase_20200914.vhd"
    13. + instance_charge_type = "PostPaid"
    14. + instance_name = "pg-meta-1"
    15. + instance_type = "ecs.s6-c1m2.small"
    16. + internet_max_bandwidth_in = (known after apply)
    17. + internet_max_bandwidth_out = 40
    18. + password = (sensitive value)
    19. + private_ip = "10.10.10.10"
    20. + public_ip = (known after apply)
    21. + role_name = (known after apply)
    22. + secondary_private_ip_address_count = (known after apply)
    23. + secondary_private_ips = (known after apply)
    24. + security_groups = (known after apply)
    25. + spot_strategy = "NoSpot"
    26. + status = "Running"
    27. + subnet_id = (known after apply)
    28. + system_disk_category = "cloud_efficiency"
    29. + system_disk_performance_level = (known after apply)
    30. + system_disk_size = 40
    31. + volume_tags = (known after apply)
    32. + vswitch_id = (known after apply)
    33. }
    34. # alicloud_instance.pg-test-1 will be created
    35. + resource "alicloud_instance" "pg-test-1" {
    36. + availability_zone = (known after apply)
    37. + credit_specification = (known after apply)
    38. + deletion_protection = false
    39. + dry_run = false
    40. + host_name = "pg-test-1"
    41. + id = (known after apply)
    42. + image_id = "centos_7_8_x64_20G_alibase_20200914.vhd"
    43. + instance_charge_type = "PostPaid"
    44. + instance_name = "pg-test-1"
    45. + instance_type = "ecs.s6-c1m1.small"
    46. + internet_max_bandwidth_in = (known after apply)
    47. + internet_max_bandwidth_out = 0
    48. + key_name = (known after apply)
    49. + password = (sensitive value)
    50. + private_ip = "10.10.10.11"
    51. + public_ip = (known after apply)
    52. + role_name = (known after apply)
    53. + secondary_private_ip_address_count = (known after apply)
    54. + secondary_private_ips = (known after apply)
    55. + security_groups = (known after apply)
    56. + spot_strategy = "NoSpot"
    57. + status = "Running"
    58. + subnet_id = (known after apply)
    59. + system_disk_category = "cloud_efficiency"
    60. + system_disk_performance_level = (known after apply)
    61. + system_disk_size = 40
    62. + volume_tags = (known after apply)
    63. + vswitch_id = (known after apply)
    64. }
    65. # alicloud_instance.pg-test-2 will be created
    66. + resource "alicloud_instance" "pg-test-2" {
    67. + availability_zone = (known after apply)
    68. + credit_specification = (known after apply)
    69. + deletion_protection = false
    70. + dry_run = false
    71. + host_name = "pg-test-2"
    72. + id = (known after apply)
    73. + image_id = "centos_7_8_x64_20G_alibase_20200914.vhd"
    74. + instance_charge_type = "PostPaid"
    75. + instance_name = "pg-test-2"
    76. + instance_type = "ecs.s6-c1m1.small"
    77. + internet_max_bandwidth_in = (known after apply)
    78. + internet_max_bandwidth_out = 0
    79. + key_name = (known after apply)
    80. + password = (sensitive value)
    81. + private_ip = "10.10.10.12"
    82. + public_ip = (known after apply)
    83. + role_name = (known after apply)
    84. + secondary_private_ip_address_count = (known after apply)
    85. + secondary_private_ips = (known after apply)
    86. + security_groups = (known after apply)
    87. + spot_strategy = "NoSpot"
    88. + status = "Running"
    89. + subnet_id = (known after apply)
    90. + system_disk_category = "cloud_efficiency"
    91. + system_disk_performance_level = (known after apply)
    92. + system_disk_size = 40
    93. + volume_tags = (known after apply)
    94. + vswitch_id = (known after apply)
    95. }
    96. # alicloud_instance.pg-test-3 will be created
    97. + resource "alicloud_instance" "pg-test-3" {
    98. + availability_zone = (known after apply)
    99. + credit_specification = (known after apply)
    100. + deletion_protection = false
    101. + dry_run = false
    102. + host_name = "pg-test-3"
    103. + id = (known after apply)
    104. + image_id = "centos_7_8_x64_20G_alibase_20200914.vhd"
    105. + instance_charge_type = "PostPaid"
    106. + instance_name = "pg-test-3"
    107. + instance_type = "ecs.s6-c1m1.small"
    108. + internet_max_bandwidth_in = (known after apply)
    109. + internet_max_bandwidth_out = 0
    110. + key_name = (known after apply)
    111. + password = (sensitive value)
    112. + private_ip = "10.10.10.13"
    113. + public_ip = (known after apply)
    114. + role_name = (known after apply)
    115. + secondary_private_ip_address_count = (known after apply)
    116. + secondary_private_ips = (known after apply)
    117. + security_groups = (known after apply)
    118. + spot_strategy = "NoSpot"
    119. + subnet_id = (known after apply)
    120. + system_disk_category = "cloud_efficiency"
    121. + system_disk_performance_level = (known after apply)
    122. + system_disk_size = 40
    123. + vswitch_id = (known after apply)
    124. }
    125. # alicloud_security_group.default will be created
    126. + resource "alicloud_security_group" "default" {
    127. + id = (known after apply)
    128. + inner_access = (known after apply)
    129. + inner_access_policy = (known after apply)
    130. + name = "default"
    131. + security_group_type = "normal"
    132. + vpc_id = (known after apply)
    133. }
    134. # alicloud_security_group_rule.allow_all_tcp will be created
    135. + resource "alicloud_security_group_rule" "allow_all_tcp" {
    136. + cidr_ip = "0.0.0.0/0"
    137. + id = (known after apply)
    138. + ip_protocol = "tcp"
    139. + nic_type = "intranet"
    140. + policy = "accept"
    141. + port_range = "1/65535"
    142. + priority = 1
    143. + security_group_id = (known after apply)
    144. + type = "ingress"
    145. }
    146. # alicloud_vpc.vpc will be created
    147. + resource "alicloud_vpc" "vpc" {
    148. + cidr_block = "10.10.10.0/24"
    149. + id = (known after apply)
    150. + ipv6_cidr_block = (known after apply)
    151. + name = (known after apply)
    152. + resource_group_id = (known after apply)
    153. + route_table_id = (known after apply)
    154. + router_id = (known after apply)
    155. + router_table_id = (known after apply)
    156. + status = (known after apply)
    157. + vpc_name = "pigsty-demo-network"
    158. }
    159. # alicloud_vswitch.vsw will be created
    160. + resource "alicloud_vswitch" "vsw" {
    161. + availability_zone = (known after apply)
    162. + cidr_block = "10.10.10.0/24"
    163. + id = (known after apply)
    164. + name = (known after apply)
    165. + status = (known after apply)
    166. + vpc_id = (known after apply)
    167. + vswitch_name = (known after apply)
    168. + zone_id = "cn-beijing-k"
    169. }
    170. Plan: 8 to add, 0 to change, 0 to destroy.
    171. Changes to Outputs:
    172. + meta_ip = (known after apply)
    173. Do you want to perform these actions?
    174. Terraform will perform the actions described above.
    175. Only 'yes' will be accepted to approve.
    176. Enter a value: yes
    177. alicloud_vpc.vpc: Creating...
    178. alicloud_vpc.vpc: Creation complete after 6s [id=vpc-2zed78z7n5z06o1dmydhj]
    179. alicloud_security_group.default: Creating...
    180. alicloud_vswitch.vsw: Creating...
    181. alicloud_security_group.default: Creation complete after 1s [id=sg-2ze7x7zu8tcdsefroofa]
    182. alicloud_security_group_rule.allow_all_tcp: Creating...
    183. alicloud_security_group_rule.allow_all_tcp: Creation complete after 0s [id=sg-2ze7x7zu8tcdsefroofa:ingress:tcp:1/65535:intranet:0.0.0.0/0:accept:1]
    184. alicloud_vswitch.vsw: Creation complete after 6s [id=vsw-2zejctjdr16ryz194jxz4]
    185. alicloud_instance.pg-test-3: Creating...
    186. alicloud_instance.pg-test-2: Creating...
    187. alicloud_instance.pg-test-1: Creating...
    188. alicloud_instance.pg-meta-1: Creating...
    189. alicloud_instance.pg-test-3: Still creating... [10s elapsed]
    190. alicloud_instance.pg-test-2: Still creating... [10s elapsed]
    191. alicloud_instance.pg-test-1: Still creating... [10s elapsed]
    192. alicloud_instance.pg-meta-1: Still creating... [10s elapsed]
    193. alicloud_instance.pg-meta-1: Creation complete after 16s [id=i-2zef4frw6kezb47339wr]
    194. alicloud_instance.pg-test-1: Still creating... [20s elapsed]
    195. alicloud_instance.pg-test-2: Still creating... [20s elapsed]
    196. alicloud_instance.pg-test-3: Still creating... [20s elapsed]
    197. alicloud_instance.pg-test-2: Creation complete after 23s [id=i-2zefzvz0fyl7mloc4v30]
    198. alicloud_instance.pg-test-1: Still creating... [30s elapsed]
    199. alicloud_instance.pg-test-3: Still creating... [30s elapsed]
    200. alicloud_instance.pg-test-3: Creation complete after 33s [id=i-2zeeyodo2pc8b1k2d167]
    201. alicloud_instance.pg-test-1: Creation complete after 33s [id=i-2zef4frw6kezb47339ws]

    其中,管理机将分配一个按量付费的公网IP,您也可以使用命令terraform output将其打印出来。

    1. # 创建 ~/.ssh/pigsty_terraform 文件,包含云端管理机器的SSH定义(可选,好用一点)
    2. cat > ~/.ssh/pigsty_terraform <<-EOF
    3. Host demo
    4. User root
    5. HostName ${public_ip}
    6. UserKnownHostsFile /dev/null
    7. StrictHostKeyChecking no
    8. PasswordAuthentication yes
    9. EOF
    10. chmod 0600 ~/.ssh/pigsty_terraform
    11. # 启用该配置
    12. if ! grep --quiet "Include ~/.ssh/pigsty_terraform" ~/.ssh/config ; then
    13. (echo 'Include ~/.ssh/pigsty_terraform' && cat ~/.ssh/config) > ~/.ssh/config.tmp;
    14. fi

    然后,您可以通过SSH别名demo访问该云端管理机了。

    然后,您就可以免密从本地访问该节点了,如果只需要进行单节点安装,这样就行了。接下来,在该元节点上完成标准安装

    阿里云虚拟机CentOS 7.8镜像中运行有 nscd ,锁死了 glibc 版本,会导致安装时出现RPM依赖错误。

    在所有机器上执行 即可解决此问题。