{"id":6153,"date":"2022-08-22T14:41:41","date_gmt":"2022-08-22T05:41:41","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=6153"},"modified":"2022-08-22T16:36:03","modified_gmt":"2022-08-22T07:36:03","slug":"consul-cluster-%ea%b5%ac%ec%84%b1%ed%95%98%ea%b8%b0-2","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=6153","title":{"rendered":"Consul cluster \uad6c\uc131\ud558\uae30"},"content":{"rendered":"<h1>Consul cluster \uad6c\uc131\ud558\uae30<\/h1>\n<h2>\ud3f4\ub354 \uc0dd\uc131<\/h2>\n<pre><code class=\"language-bash\">mkdir consul_server_cluster\ncd consul_server_cluster<\/code><\/pre>\n<h2>AMI \uc544\uc774\ub514 \ucc3e\uae30<\/h2>\n<p>AMI \uc544\uc774\ub514\ub294 \uc544\ub798\ucc98\ub7fc \ucc3e\uc744 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<p><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/wp-content\/uploads\/2022\/08\/2022-08-21-01.png\"><img decoding=\"async\" src=\"https:\/\/www.skyer9.pe.kr\/wordpress\/wp-content\/uploads\/2022\/08\/2022-08-21-01.png\" alt=\"\" \/><\/a><\/p>\n<h2>\ubcc0\uc218\uc124\uc815 \ubc0f \uc11c\ubc84 \uc2e4\ud589<\/h2>\n<pre><code class=\"language-bash\">vi variables.tf\n-----------------------------\nvariable &quot;stack_name&quot; {\n  description = &quot;The name to prefix onto resources.&quot;\n  type        = string\n  default     = &quot;my&quot;\n}\n\nvariable &quot;owner_name&quot; {\n  description = &quot;Your name so resources can be easily assigned.&quot;\n  type        = string\n  default     = &quot;skyer9&quot;\n}\n\nvariable &quot;owner_email&quot; {\n  description = &quot;Your email so you can be contacted about resources.&quot;\n  type        = string\n  default     = &quot;skyer9@gmail.com&quot;\n}\n\nvariable &quot;region&quot; {\n  description = &quot;The AWS region to deploy into.&quot;\n  type        = string\n  default     = &quot;ap-northeast-2&quot;\n}\n\nvariable &quot;availability_zones&quot; {\n  description = &quot;The AWS region AZs to deploy into.&quot;\n  type        = list(string)\n  default     = [&quot;ap-northeast-2a&quot;, &quot;ap-northeast-2b&quot;, &quot;ap-northeast-2c&quot;]\n}\n\nvariable &quot;ami&quot; {\n  description = &quot;The AMI to use, preferably built by the supplied Packer scripts.&quot;\n  type        = string\n  default     = &quot;ami-0ea5eb4b05645aa8a&quot;\n}\n\nvariable &quot;consul_server_instance_type&quot; {\n  description = &quot;The EC2 instance type to launch for Consul servers.&quot;\n  type        = string\n  default     = &quot;t3a.micro&quot;\n}\n\nvariable &quot;server_instance_type&quot; {\n  description = &quot;The EC2 instance type to launch for Nomad servers.&quot;\n  type        = string\n  default     = &quot;t3a.micro&quot;\n}\n\nvariable &quot;client_instance_type&quot; {\n  description = &quot;The EC2 instance type to launch for Nomad clients.&quot;\n  type        = string\n  default     = &quot;t3a.small&quot;\n}\n\nvariable &quot;consul_server_count&quot; {\n  description = &quot;The number of Consul servers to run.&quot;\n  type        = number\n  default     = 1\n}\n\nvariable &quot;server_count&quot; {\n  description = &quot;The number of Nomad servers to run.&quot;\n  type        = number\n  default     = 1\n}\n\nvariable &quot;client_count&quot; {\n  description = &quot;The number of Nomad clients to run.&quot;\n  type        = number\n  default     = 1\n}\n\nvariable &quot;root_block_device_size&quot; {\n  description = &quot;The number of GB to assign as a block device on instances.&quot;\n  type        = number\n  default     = 8\n}\n\nvariable &quot;retry_join&quot; {\n  description = &quot;The retry join configuration to use.&quot;\n  type        = string\n  default     = &quot;provider=aws tag_key=ConsulAutoJoin tag_value=auto-join&quot;\n}\n\nvariable &quot;allowlist_ip&quot; {\n  description = &quot;A list of IP address to grant access via the LBs.&quot;\n  type        = list(string)\n  default     = [&quot;0.0.0.0\/0&quot;]\n}\n\nvariable &quot;access_key&quot; {\n  description = &quot;AWS_ACCESS_KEY_ID&quot;\n  type        = string\n  default     = &quot;XXXXXXXXXXXXXXX&quot;\n}\n\nvariable &quot;secret_access_key&quot; {\n  description = &quot;AWS_SECRET_ACCESS_KEY&quot;\n  type        = string\n  default     = &quot;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&quot;\n}\n-----------------------------<\/code><\/pre>\n<pre><code class=\"language-bash\">vi main.tf\n-----------------------------\nprovider &quot;aws&quot; {\n  region  = var.region\n}\n\nresource &quot;aws_instance&quot; &quot;consul_server&quot; {\n  ami                    = var.ami\n  instance_type          = var.consul_server_instance_type\n  \/\/ key_name               = var.key_name\n  \/\/ vpc_security_group_ids = [aws_security_group.consul_lb.id]\n  count                  = var.consul_server_count\n  \/\/ iam_instance_profile   = aws_iam_instance_profile.consul_server.name\n\n  tags = {\n    Name           = &quot;${var.stack_name}-consul_server-${count.index + 1}&quot;\n    ConsulAutoJoin = &quot;auto-join&quot;\n    OwnerName      = var.owner_name\n    OwnerEmail     = var.owner_email\n  }\n\n  root_block_device {\n    volume_type           = &quot;gp2&quot;\n    volume_size           = var.root_block_device_size\n    delete_on_termination = &quot;true&quot;\n  }\n\n  \/\/ user_data            = data.template_file.user_data_consul_server.rendered\n}\n-----------------------------<\/code><\/pre>\n<pre><code class=\"language-bash\">terraform init\nterraform validate\nterraform plan<\/code><\/pre>\n<p>\ud14c\uc2a4\ud2b8\ub85c \uc778\uc2a4\ud134\uc2a4\ub97c \uc0dd\uc131\ud574 \ubd05\ub2c8\ub2e4.<br \/>\n\ud604\uc7ac\uc0c1\ud0dc\uc5d0\uc11c\ub294 \uc544\ubb34\ub7f0 \uae30\ub2a5\ub3c4 \uc5c6\uace0,<br \/>\n\ub2e8\uc9c0 EC2 \uc778\uc2a4\ud134\uc2a4\ub97c \uc0dd\uc131\ud574 \ubcf4\uace0 \uc0ad\uc81c\ud569\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-bash\">terraform apply\nterraform show\nterraform destroy<\/code><\/pre>\n<h2>\uad8c\ud55c\uc124\uc815<\/h2>\n<pre><code class=\"language-bash\">vi private.tf\n-----------------------------\nvariable &quot;key_name&quot; {\n  description = &quot;The EC2 key pair to use for EC2 instance SSH access.&quot;\n  type        = string\n  default     = &quot;aws_key&quot;                   # \ub0b4 \ud0a4\ud398\uc5b4\n}\n\nvariable &quot;my_ip&quot; {\n  description = &quot;A list of IP address to grant access via the LBs.&quot;\n  type        = list(string)\n  default     = [&quot;183.101.XXX.XXX\/32&quot;]      # \ub0b4 \uc544\uc774\ud53c\n}\n-----------------------------<\/code><\/pre>\n<pre><code class=\"language-bash\">vi sg.tf\n-----------------------------\ndata &quot;aws_vpc&quot; &quot;default&quot; {\n  default = true\n}\n\nresource &quot;aws_security_group&quot; &quot;consul_lb&quot; {\n  name   = &quot;${var.stack_name}-consul-lb&quot;\n  vpc_id = data.aws_vpc.default.id\n\n  # Consul HTTP API &amp; UI.\n  ingress {\n    from_port   = 8300\n    to_port     = 8600\n    protocol    = &quot;tcp&quot;\n    cidr_blocks = var.my_ip\n  }\n\n  ingress {\n    from_port   = 22\n    to_port     = 22\n    protocol    = &quot;tcp&quot;\n    cidr_blocks = var.my_ip\n  }\n\n  egress {\n    from_port   = 0\n    to_port     = 0\n    protocol    = &quot;-1&quot;\n    cidr_blocks = [&quot;0.0.0.0\/0&quot;]\n  }\n}\n\nresource &quot;aws_security_group_rule&quot; &quot;consul_to_consul_ingress&quot; {\n  type        = &quot;ingress&quot;\n  from_port   = 1\n  to_port     = 65535\n  protocol    = &quot;tcp&quot;\n  security_group_id = aws_security_group.consul_lb.id\n  source_security_group_id = aws_security_group.consul_lb.id\n}\n-----------------------------<\/code><\/pre>\n<pre><code class=\"language-bash\">vi iam.tf\n-----------------------------\nresource &quot;aws_iam_instance_profile&quot; &quot;consul_server&quot; {\n  name_prefix = var.stack_name\n  role        = aws_iam_role.consul_server.name\n}\n\nresource &quot;aws_iam_role&quot; &quot;consul_server&quot; {\n  name_prefix        = var.stack_name\n  assume_role_policy = data.aws_iam_policy_document.consul_server_assume.json\n}\n\nresource &quot;aws_iam_role_policy&quot; &quot;consul_server&quot; {\n  name   = &quot;nomad-server&quot;\n  role   = aws_iam_role.consul_server.id\n  policy = data.aws_iam_policy_document.consul_server.json\n}\n\ndata &quot;aws_iam_policy_document&quot; &quot;consul_server_assume&quot; {\n  statement {\n    effect  = &quot;Allow&quot;\n    actions = [&quot;sts:AssumeRole&quot;]\n\n    principals {\n      type        = &quot;Service&quot;\n      identifiers = [&quot;ec2.amazonaws.com&quot;]\n    }\n  }\n}\n\ndata &quot;aws_iam_policy_document&quot; &quot;consul_server&quot; {\n  statement {\n    effect = &quot;Allow&quot;\n\n    actions = [\n      &quot;ec2:DescribeInstances&quot;,\n      &quot;ec2:DescribeTags&quot;,\n    ]\n\n    resources = [&quot;*&quot;]\n  }\n}\n-----------------------------<\/code><\/pre>\n<pre><code class=\"language-bash\">vi main.tf\n-----------------------------\nresource &quot;aws_instance&quot; &quot;consul_server&quot; {\n  \/\/ ......\n  key_name               = var.key_name\n  vpc_security_group_ids = [aws_security_group.consul_lb.id]\n  iam_instance_profile   = aws_iam_instance_profile.consul_server.name\n  \/\/ ......\n-----------------------------<\/code><\/pre>\n<p>\ub2e4\uc2dc \uc778\uc2a4\ud134\uc2a4\ub97c \uc0dd\uc131\ud574 \ubd05\ub2c8\ub2e4.<br \/>\n\ud558\uc9c0\ub9cc \uc544\uc9c1 Consul \uc11c\ubc84\ub85c\uc11c \uae30\ub2a5\ud558\uc9c0\ub294 \uc54a\uc2b5\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-bash\">terraform validate\nterraform plan\n\nterraform apply\nterraform show\nterraform destroy<\/code><\/pre>\n<h2>Consul server \uc124\uc815<\/h2>\n<pre><code class=\"language-bash\">mkdir files\n\nvi files\/user-data-consul-server.sh\n-----------------------------\n#!\/bin\/bash\n\nset -e\n\nsudo mkdir -p \/ops\ncd \/ops\/\n\nsudo wget https:\/\/github.com\/skyer9\/TerraformOnAws\/raw\/main\/files\/setup.sh\nsudo wget https:\/\/github.com\/skyer9\/TerraformOnAws\/raw\/main\/files\/net.sh\nsudo wget https:\/\/github.com\/skyer9\/TerraformOnAws\/raw\/main\/files\/consul-server.sh\nsudo wget https:\/\/github.com\/skyer9\/TerraformOnAws\/raw\/main\/files\/consul-server.hcl\nsudo wget https:\/\/github.com\/skyer9\/TerraformOnAws\/raw\/main\/files\/consul.service\n\nsudo chmod +x \/ops\/setup.sh\nsudo chmod +x \/ops\/net.sh\nsudo chmod +x \/ops\/consul-server.sh\n\nsudo bash -c &quot;\/ops\/consul-server.sh \\&quot;${server_count}\\&quot; \\&quot;${retry_join}\\&quot;&quot;\n# rm -rf \/ops\/\n-----------------------------<\/code><\/pre>\n<pre><code class=\"language-bash\">vi templates.tf\n-----------------------------\ndata &quot;template_file&quot; &quot;user_data_consul_server&quot; {\n  template = file(&quot;${path.module}\/files\/user-data-consul-server.sh&quot;)\n\n  vars = {\n    server_count  = var.consul_server_count\n    region        = var.region\n    retry_join    = var.retry_join\n  }\n}\n-----------------------------<\/code><\/pre>\n<pre><code class=\"language-bash\">vi main.tf\n-----------------------------\nresource &quot;aws_instance&quot; &quot;consul_server&quot; {\n  \/\/ ......\n  user_data            = data.template_file.user_data_consul_server.rendered\n}\n-----------------------------<\/code><\/pre>\n<pre><code class=\"language-bash\">terraform init -upgrade\n\nterraform apply\nterraform show\nterraform destroy<\/code><\/pre>\n<p>http:\/\/&lt;\uc11c\ubc84 \uc544\uc774\ud53c&gt;:8500\/ \uc5d0 \uc811\uc18d\ud558\uc5ec \ud655\uc778\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Consul cluster \uad6c\uc131\ud558\uae30 \ud3f4\ub354 \uc0dd\uc131 mkdir consul_server_cluster cd consul_server_cluster AMI \uc544\uc774\ub514 \ucc3e\uae30 AMI \uc544\uc774\ub514\ub294 \uc544\ub798\ucc98\ub7fc \ucc3e\uc744 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \ubcc0\uc218\uc124\uc815 \ubc0f \uc11c\ubc84 \uc2e4\ud589 vi variables.tf &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; variable &quot;stack_name&quot; { description = &quot;The name to prefix onto resources.&quot; type = string default = &quot;my&quot; } variable &quot;owner_name&quot; { description = &quot;Your name so resources can be easily\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=6153\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-6153","post","type-post","status-publish","format-standard","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/6153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6153"}],"version-history":[{"count":3,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/6153\/revisions"}],"predecessor-version":[{"id":6166,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/6153\/revisions\/6166"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}