mylamour / blog

Your internal mediocrity is the moment when you lost the faith of being excellent. Just do it.
https://fz.cool
61 stars 14 forks source link

使用Terraform在AWS多区部署虚拟货币钱包节点 #31

Open mylamour opened 6 years ago

mylamour commented 6 years ago

Terrafrom多区部署虚拟货币的demo代码详见这里 目录结构如下: image

只需将每个货币的部署方式和脚本实现放在对应的不同文件夹下,然后在main.tf进行每个模块的调用即可。当然,这只是为了钱包节点的同步,而不是为了挖矿。采用多区同步用于确保尽可能的使节点能够同步到最新的区块。当然还可以在每个区设置多台,然后多区部署。

module "coinnode-us-east-1" {
  source = "coin/btc"
  region = "us-east-1"
}

module "coinnode-us-east-2" {
  source = "coin/btc"
  region = "us-east-2"
}

module "coinnode-us-west-1" {
 source = "coin/krb"
 region = "us-west-1"
}

module "coinnode-us-west-1" {
 source = "coin/krb"
 region = "us-west-1"
}

下面看一下单独的一个示例,采用krb作为示例研究:

// 设置实例

resource "aws_instance" "karbo" { ami = "${data.aws_ami.default.id}" instance_type = "t2.micro" vpc_security_group_ids = ["${aws_security_group.karbo.id}"] key_name = "${aws_key_pair.local.key_name}"

tags { Name = "coinnode-karbo" }

provisioner "local-exec" { command = "echo ${aws_instance.karbo.public_ip} >> ip_address.txt" }

connection { type = "ssh" user = "ubuntu" private_key = "${file("~/.ssh/id_rsa")}" }

provisioner "file" { source = "scripts/krb/install_krb.sh" destination = "/tmp/install_krb.sh" }

provisioner "remote-exec" { inline = [ "sudo bash /tmp/install_krb.sh" , "nohup karbowanecd &>/dev/null &" , "sleep 1", ] } }

* vpc.tf

```hcl

resource "aws_security_group" "karbo" {
    name = "karbo"
    description = "Karbo Coin Vpc Rules"

    ingress {
        from_port = 22
        to_port = 22
        protocol = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
    }
    ingress {//p2p
        from_port = 32347
        to_port = 32347
        protocol = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
    }
    ingress {//rpc
        from_port = 32348
        to_port = 32348
        protocol = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
    }
    egress {
        from_port = 0
        to_port = 65535
        protocol = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
    }

    tags {
        Name = "Karbo"
    }
}

Note