Terraform - Deploy Server - Multiple Servers

现实中不可能只有单一的Server,会弹性运行多个Server来保证有足够的Server可用。

Auto Scaling Group

可以利用ASG来实现多个Web Servers。

  1. 创建Config文件
    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
    variable "server_port" {
    description = "The port the server will use for HTTP requests"
    default = 80
    }

    data "aws_availability_zones" "all" {}

    provider "aws" {
    region = "ap-northeast-1"
    }

    resource "aws_autoscaling_group" "yonfeiuall_scaling_group" {
    launch_configuration = "${aws_launch_configuration.yongfeiuall_launch_config.id}"
    availability_zones = ["${data.aws_availability_zones.all.names}"]
    min_size = 2
    max_size = 10
    tag {
    key = "Name"
    value = "yongfeiuall-asg"
    propagate_at_launch = true
    }
    }

    resource "aws_launch_configuration" "yongfeiuall_launch_config" {
    image_id = "ami-28ddc154"
    instance_type = "t2.micro"

    user_data = <<-EOF
    #!/bin/bash
    yum update -y
    yum install -y httpd
    service httpd start
    echo '<html><h1> configurable web server from terraform </h1></html>' > /var/www/html/index.html
    EOF

    lifecycle {
    create_before_destroy = true
    }
    security_groups = ["${aws_security_group.http.id}"]
    }

    resource "aws_security_group" "http" {
    name = "yonfeiuall_single_web"

    # HTTP access from anywhere
    ingress {
    from_port = "${var.server_port}"
    to_port = "${var.server_port}"
    protocol = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
    }

    # Outbound internet access
    egress {
    from_port = 0
    to_port = 0
    protocol = "-1"
    cidr_blocks = ["0.0.0.0/0"]
    }
    lifecycle {
    create_before_destroy = true
    }
    }
  • lifecycle block to any resource to configure how that resource should be created, updated, or destroyed.
  • A data source represents a piece of read-only information that is fetched from the provider every time you run Terraform. To use the data source, you reference it using the following syntax: "${data.TYPE.NAME.ATTRIBUTE}".
  1. 执行命令terraform planterraform apply
  2. 验证成功

Elastic Load Balancer

ASG能实现多个Servers,但是有多个IP啊,可能通过ELB来实现对外只有一个DNS。

  1. 创建Config文件

    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
    variable "server_port" {
    description = "The port the server will use for HTTP requests"
    default = 80
    }
    output "elb_dns_name" {
    value = "${aws_elb.yongfeiuall_elb.dns_name}"
    }

    data "aws_availability_zones" "all" {}

    provider "aws" {
    region = "ap-northeast-1"
    }

    # elb
    resource "aws_elb" "yongfeiuall_elb" {
    name = "yongfeiuall-asg-elb"
    availability_zones = ["${data.aws_availability_zones.all.names}"]
    security_groups = ["${aws_security_group.http.id}"]

    listener {
    lb_port = "${var.server_port}"
    lb_protocol = "http"
    instance_port = "${var.server_port}"
    instance_protocol = "http"
    }

    health_check {
    healthy_threshold = 2
    unhealthy_threshold = 2
    timeout = 3
    interval = 30
    target = "HTTP:${var.server_port}/"
    }
    }

    # asg
    resource "aws_autoscaling_group" "yonfeiuall_scaling_group" {
    launch_configuration = "${aws_launch_configuration.yongfeiuall_launch_config.id}"
    availability_zones = ["${data.aws_availability_zones.all.names}"]
    load_balancers = ["${aws_elb.yongfeiuall_elb.name}"]

    min_size = 2
    max_size = 10
    tag {
    key = "Name"
    value = "yongfeiuall-asg"
    propagate_at_launch = true
    }
    }

    # launch configuration
    resource "aws_launch_configuration" "yongfeiuall_launch_config" {
    image_id = "ami-28ddc154"
    instance_type = "t2.micro"

    user_data = <<-EOF
    #!/bin/bash
    yum update -y
    yum install -y httpd
    service httpd start
    echo '<html><h1> configurable web server from terraform </h1></html>' > /var/www/html/index.html
    EOF

    lifecycle {
    create_before_destroy = true
    }
    security_groups = ["${aws_security_group.http.id}"]
    }

    # security group
    resource "aws_security_group" "http" {
    name = "yonfeiuall_single_web"

    # HTTP access from anywhere
    ingress {
    from_port = "${var.server_port}"
    to_port = "${var.server_port}"
    protocol = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
    }

    # Outbound internet access
    egress {
    from_port = 0
    to_port = 0
    protocol = "-1"
    cidr_blocks = ["0.0.0.0/0"]
    }
    lifecycle {
    create_before_destroy = true
    }
    }
  2. 执行命令terraform planterraform apply

  3. 验证成功

Clean Up

这个非常简单:

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
D:\terraform\Example>terraform destroy
aws_security_group.http: Refreshing state... (ID: sg-3a619542)
data.aws_availability_zones.all: Refreshing state...
aws_elb.yongfeiuall_elb: Refreshing state... (ID: yongfeiuall-asg-elb)
aws_launch_configuration.yongfeiuall_launch_config: Refreshing state... (ID: terraform-201804240854003
23000000001)
aws_autoscaling_group.yonfeiuall_scaling_group: Refreshing state... (ID: tf-asg-2018042408541147700000
0002)

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
- destroy

Terraform will perform the following actions:

- aws_autoscaling_group.yonfeiuall_scaling_group

- aws_elb.yongfeiuall_elb

- aws_launch_configuration.yongfeiuall_launch_config

- aws_security_group.http


Plan: 0 to add, 0 to change, 4 to destroy.

Do you really want to destroy?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.

Enter a value:

唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!