Skip to content

Commit bd1eaf6

Browse files
committed
Single node ES instance
- No load balancers - No autoscaling - Output set to single node IP address fixed issue with singlenode instance being provisioned in clustered mode Single-node network interface attachment to ensure connectivity without load balancer updated gitignore Kibana image restored file
1 parent 53691d2 commit bd1eaf6

16 files changed

+193
-53
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ terraform.tfvars
66
.terraform/
77
.gcp*
88
cluster_bootstrap_state
9+
terraform-aws/cluster_bootstrap_state
910
gcp-account.json
1011
*.iml

Diff for: assets/node-init.json

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"ec2:DescribeInstances",
66
"ec2:DescribeVolumes",
77
"ec2:AttachVolume",
8+
"ec2:AttachNetworkInterface",
9+
"ec2:DescribeNetworkInterfaces",
810
"ec2:DescribeTags",
911
"autoscaling:DescribeAutoScalingGroups"
1012
],

Diff for: assets/scripts/aws/autoattach-network.sh

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Required variables
2+
# - aws_region
3+
# - es_cluster
4+
# - elasticsearch_data_dir
5+
6+
AV_ZONE="$(ec2metadata --availability-zone)"
7+
INSTANCE_ROLE="$(aws ec2 describe-tags --region $aws_region --filters Name=resource-id,Values=$(ec2metadata --instance-id) | jq -r '.Tags[] | select(.Key == "Role") | .Value')"
8+
echo "AV_ZONE: $AV_ZONE"
9+
echo "INSTANCE_ROLE: $INSTANCE_ROLE"
10+
11+
while true; do
12+
echo "UNATTACHED_ENI_ID: $eni_id"
13+
14+
aws ec2 attach-network-interface --instance-id=$(ec2metadata --instance-id) --device-index 1 --network-interface-id ${eni_id} --region "$aws_region"
15+
if [ "$?" != "0" ]; then
16+
sleep 10
17+
continue
18+
fi
19+
20+
ATTACHMENTS_COUNT="$(aws ec2 describe-network-interfaces --region $aws_region --filters Name=network-interface-id,Values=${eni_id} | jq -r '.NetworkInterfaces[0].Attachment | length')"
21+
if [ "$ATTACHMENTS_COUNT" != "0" ]; then break; fi
22+
done
23+
24+
echo "Updating network configuration"
25+
26+
cat <<EOF >/etc/netplan/51-ens6.yaml
27+
network:
28+
version: 2
29+
renderer: networkd
30+
ethernets:
31+
ens6:
32+
addresses:
33+
- ${eni_ipv4}/20
34+
dhcp4: no
35+
routes:
36+
- to: 0.0.0.0/0
37+
via: 172.31.16.1 # Default gateway
38+
table: 1000
39+
- to: ${eni_ipv4}
40+
via: 0.0.0.0
41+
scope: link
42+
table: 1000
43+
routing-policy:
44+
- from: ${eni_ipv4}
45+
table: 1000
46+
EOF
47+
48+
sleep 5
49+
50+
netplan apply
51+

Diff for: assets/scripts/singlenode.sh

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,76 @@
11
#!/bin/bash
22
set +e
33

4+
echo "Testing AMI Builder if it works properly"
5+
6+
7+
echo "Running common env script"
48
. /opt/cloud-deploy-scripts/common/env.sh
5-
. /opt/cloud-deploy-scripts/$cloud_provider/env.sh
9+
10+
if [ -e /opt/cloud-deploy-scripts/$cloud_provider/env.sh ]; then
11+
echo "Running ${cloud_provider} env script"
12+
. /opt/cloud-deploy-scripts/$cloud_provider/env.sh
13+
fi
614

715
# It is required to bind to all interfaces for load balancer on GCP to work
816
if [ "$cloud_provider" == "gcp" ]; then
917
export BIND_TO_ALL="true"
1018
fi
1119

20+
echo "Running EBS volume autoattach script"
1221
/opt/cloud-deploy-scripts/$cloud_provider/autoattach-disk.sh
1322

23+
echo "Running ENI autoattach script"
24+
/opt/cloud-deploy-scripts/$cloud_provider/autoattach-network.sh
25+
26+
echo "Running config-es script"
1427
/opt/cloud-deploy-scripts/common/config-es.sh
28+
29+
echo "Running config-beats script"
1530
/opt/cloud-deploy-scripts/common/config-beats.sh
1631

32+
echo "Running ${cloud_provider}/config-es script"
1733
/opt/cloud-deploy-scripts/$cloud_provider/config-es.sh
34+
35+
echo "Running ${cloud_provider}/config-es-discovery script"
1836
/opt/cloud-deploy-scripts/$cloud_provider/config-es-discovery.sh
1937

38+
echo "Creating elasticsearch.yml file"
2039
cat <<'EOF' >>/etc/elasticsearch/elasticsearch.yml
2140
node.master: true
2241
node.data: true
2342
node.ingest: true
2443
discovery.type: single-node
2544
EOF
2645

46+
echo "Running config/clients script"
47+
2748
/opt/cloud-deploy-scripts/common/config-clients.sh
2849

2950
# add bootstrap.password to the keystore, so that config-cluster scripts can run
3051
# only done on bootstrap and singlenode nodes, before starting ES
3152
if [ "${security_enabled}" == "true" ]; then
53+
echo "Configuring elasticsearch keystore"
3254
echo "${client_pwd}" | /usr/share/elasticsearch/bin/elasticsearch-keystore add --stdin bootstrap.password
3355
fi
3456

57+
#Fix IP Address
58+
echo "Rewriting ENI IP Address in elasticsearch.yml"
59+
sed -i -re "s/_ec2:privateIpv4_/${eni_ipv4}/ig" /etc/elasticsearch/elasticsearch.yml
60+
3561
# Start Elasticsearch
62+
echo "Starting elasticsearch service"
63+
3664
systemctl daemon-reload
3765
systemctl enable elasticsearch.service
3866
systemctl start elasticsearch.service
3967

68+
echo "Running config-cluster script"
4069
/opt/cloud-deploy-scripts/common/config-cluster.sh
41-
/opt/cloud-deploy-scripts/$cloud_provider/config-cluster.sh
70+
71+
72+
echo "Running ${cloud_provider}/config-cluster script"
73+
/opt/cloud-deploy-scripts/$cloud_provider/config-cluster.sh
74+
75+
76+

Diff for: templates/aws_user_data.sh

+2
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,7 @@ export bootstrap_node="${bootstrap_node}"
3030
export ca_cert="${ca_cert}"
3131
export node_cert="${node_cert}"
3232
export node_key="${node_key}"
33+
export eni_id="${eni_id}"
34+
export eni_ipv4="${eni_ipv4}"
3335

3436
/opt/cloud-deploy-scripts/${startup_script}

Diff for: terraform-aws/alb.tf

+32-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
resource "aws_security_group" "elasticsearch-alb-sg" {
2-
name = "${var.es_cluster}-alb-sg"
2+
name = "${var.environment}-${var.es_cluster}-alb-sg"
33
description = "ElasticSearch Ports for ALB Access"
44
vpc_id = var.vpc_id
55

@@ -47,7 +47,9 @@ resource "aws_security_group" "elasticsearch-alb-sg" {
4747
#-----------------------------------------------------
4848

4949
resource "aws_lb_target_group" "esearch-p9200-tg" {
50-
name = "${var.es_cluster}-p9200-tg"
50+
count = local.singlenode_mode ? 0 : 1
51+
52+
name = "${var.environment}-${var.es_cluster}-p9200-tg"
5153
port = 9200
5254
protocol = "HTTP"
5355
vpc_id = var.vpc_id
@@ -64,7 +66,9 @@ resource "aws_lb_target_group" "esearch-p9200-tg" {
6466
}
6567

6668
resource "aws_lb_target_group" "kibana-p5601-tg" {
67-
name = "${var.es_cluster}-p5601-tg"
69+
count = local.singlenode_mode ? 0 : 1
70+
71+
name = "${var.environment}-${var.es_cluster}-p5601-tg"
6872
port = 5601
6973
protocol = "HTTP"
7074
vpc_id = var.vpc_id
@@ -81,7 +85,9 @@ resource "aws_lb_target_group" "kibana-p5601-tg" {
8185
}
8286

8387
resource "aws_lb_target_group" "grafana-p3000-tg" {
84-
name = "${var.es_cluster}-p3000-tg"
88+
count = local.singlenode_mode ? 0 : 1
89+
90+
name = "${var.environment}-${var.es_cluster}-p3000-tg"
8591
port = 3000
8692
protocol = "HTTP"
8793
vpc_id = var.vpc_id
@@ -98,7 +104,9 @@ resource "aws_lb_target_group" "grafana-p3000-tg" {
98104
}
99105

100106
resource "aws_lb_target_group" "cerebro-p9000-tg" {
101-
name = "${var.es_cluster}-p9000-tg"
107+
count = local.singlenode_mode ? 0 : 1
108+
109+
name = "${var.environment}-${var.es_cluster}-p9000-tg"
102110
port = 9000
103111
protocol = "HTTP"
104112
vpc_id = var.vpc_id
@@ -115,7 +123,9 @@ resource "aws_lb_target_group" "cerebro-p9000-tg" {
115123
}
116124

117125
resource "aws_lb" "elasticsearch-alb" {
118-
name = "${var.es_cluster}-alb"
126+
count = local.singlenode_mode ? 0 : 1
127+
128+
name = "${var.environment}-${var.es_cluster}-alb"
119129
internal = ! var.public_facing
120130
load_balancer_type = "application"
121131
security_groups = [aws_security_group.elasticsearch-alb-sg.id]
@@ -130,46 +140,54 @@ resource "aws_lb" "elasticsearch-alb" {
130140
#-----------------------------------------------------
131141

132142
resource "aws_lb_listener" "esearch" {
133-
load_balancer_arn = aws_lb.elasticsearch-alb.arn
143+
count = local.singlenode_mode ? 0 : 1
144+
145+
load_balancer_arn = aws_lb.elasticsearch-alb[0].arn
134146
port = "9200"
135147
protocol = "HTTP"
136148

137149
default_action {
138150
type = "forward"
139-
target_group_arn = aws_lb_target_group.esearch-p9200-tg.arn
151+
target_group_arn = aws_lb_target_group.esearch-p9200-tg[0].arn
140152
}
141153
}
142154

143155
resource "aws_lb_listener" "kibana" {
144-
load_balancer_arn = aws_lb.elasticsearch-alb.arn
156+
count = local.singlenode_mode ? 0 : 1
157+
158+
load_balancer_arn = aws_lb.elasticsearch-alb[0].arn
145159
port = "5601"
146160
protocol = "HTTP"
147161

148162
default_action {
149163
type = "forward"
150-
target_group_arn = aws_lb_target_group.kibana-p5601-tg.arn
164+
target_group_arn = aws_lb_target_group.kibana-p5601-tg[0].arn
151165
}
152166
}
153167

154168
resource "aws_lb_listener" "grafana" {
155-
load_balancer_arn = aws_lb.elasticsearch-alb.arn
169+
count = local.singlenode_mode ? 0 : 1
170+
171+
load_balancer_arn = aws_lb.elasticsearch-alb[0].arn
156172
port = "3000"
157173
protocol = "HTTP"
158174

159175
default_action {
160176
type = "forward"
161-
target_group_arn = aws_lb_target_group.grafana-p3000-tg.arn
177+
target_group_arn = aws_lb_target_group.grafana-p3000-tg[0].arn
162178
}
163179
}
164180

165181
resource "aws_lb_listener" "cerebro" {
166-
load_balancer_arn = aws_lb.elasticsearch-alb.arn
182+
count = local.singlenode_mode ? 0 : 1
183+
184+
load_balancer_arn = aws_lb.elasticsearch-alb[0].arn
167185
port = "9000"
168186
protocol = "HTTP"
169187

170188
default_action {
171189
type = "forward"
172-
target_group_arn = aws_lb_target_group.cerebro-p9000-tg.arn
190+
target_group_arn = aws_lb_target_group.cerebro-p9000-tg[0].arn
173191
}
174192
}
175193

Diff for: terraform-aws/client.tf

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ data "template_file" "client_userdata_script" {
77
}
88

99
resource "aws_launch_template" "client" {
10-
name_prefix = "elasticsearch-${var.es_cluster}-client-nodes"
10+
name_prefix = "elasticsearch-${var.environment}-${var.es_cluster}-client-nodes"
1111
image_id = data.aws_ami.kibana_client.id
1212
instance_type = var.master_instance_type
1313
user_data = base64encode(data.template_file.client_userdata_script.rendered)
@@ -35,7 +35,7 @@ resource "aws_launch_template" "client" {
3535
resource "aws_autoscaling_group" "client_nodes" {
3636
count = length(keys(var.clients_count))
3737

38-
name = "elasticsearch-${var.es_cluster}-client-nodes-${keys(var.clients_count)[count.index]}"
38+
name = "elasticsearch-${var.environment}-${var.es_cluster}-client-nodes-${keys(var.clients_count)[count.index]}"
3939
max_size = var.clients_count[keys(var.clients_count)[count.index]]
4040
min_size = var.clients_count[keys(var.clients_count)[count.index]]
4141
desired_capacity = var.clients_count[keys(var.clients_count)[count.index]]
@@ -45,10 +45,10 @@ resource "aws_autoscaling_group" "client_nodes" {
4545
vpc_zone_identifier = local.clients_subnet_ids[keys(var.clients_count)[count.index]]
4646

4747
target_group_arns = [
48-
aws_lb_target_group.esearch-p9200-tg.arn,
49-
aws_lb_target_group.kibana-p5601-tg.arn,
50-
aws_lb_target_group.grafana-p3000-tg.arn,
51-
aws_lb_target_group.cerebro-p9000-tg.arn,
48+
aws_lb_target_group.esearch-p9200-tg[0].arn,
49+
aws_lb_target_group.kibana-p5601-tg[0].arn,
50+
aws_lb_target_group.grafana-p3000-tg[0].arn,
51+
aws_lb_target_group.cerebro-p9000-tg[0].arn,
5252
]
5353

5454
launch_template {

Diff for: terraform-aws/cluster_bootstrap_state

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0
1+
1

Diff for: terraform-aws/datas.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ data "template_file" "data_userdata_script" {
77
}
88

99
resource "aws_launch_template" "data" {
10-
name_prefix = "elasticsearch-${var.es_cluster}-data-nodes"
10+
name_prefix = "elasticsearch-${var.environment}-${var.es_cluster}-data-nodes"
1111
image_id = data.aws_ami.elasticsearch.id
1212
instance_type = var.data_instance_type
1313
user_data = base64encode(data.template_file.data_userdata_script.rendered)
@@ -36,7 +36,7 @@ resource "aws_launch_template" "data" {
3636
resource "aws_autoscaling_group" "data_nodes" {
3737
count = length(keys(var.datas_count))
3838

39-
name = "elasticsearch-${var.es_cluster}-data-nodes-${keys(var.datas_count)[count.index]}"
39+
name = "elasticsearch-${var.environment}-${var.es_cluster}-data-nodes-${keys(var.datas_count)[count.index]}"
4040
max_size = var.datas_count[keys(var.datas_count)[count.index]]
4141
min_size = var.datas_count[keys(var.datas_count)[count.index]]
4242
desired_capacity = var.datas_count[keys(var.datas_count)[count.index]]
@@ -51,7 +51,7 @@ resource "aws_autoscaling_group" "data_nodes" {
5151
]
5252

5353
target_group_arns = [
54-
aws_lb_target_group.esearch-p9200-tg.arn,
54+
aws_lb_target_group.esearch-p9200-tg[0].arn,
5555
]
5656

5757
launch_template {

Diff for: terraform-aws/disks.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ resource "aws_ebs_volume" "master" {
2121
encrypted = var.volume_encryption
2222

2323
tags = {
24-
Name = "elasticsearch-${var.es_cluster}-master-${jsondecode(each.value)["name"]}"
24+
Name = "elasticsearch-${var.environment}-${var.es_cluster}-master-${jsondecode(each.value)["name"]}"
2525
ClusterName = "${var.es_cluster}"
2626
VolumeIndex = jsondecode(each.value)["index"]
2727
AutoAttachGroup = "master"
@@ -37,7 +37,7 @@ resource "aws_ebs_volume" "data" {
3737
encrypted = var.volume_encryption
3838

3939
tags = {
40-
Name = "elasticsearch-${var.es_cluster}-data-${jsondecode(each.value)["name"]}"
40+
Name = "elasticsearch-${var.environment}-${var.es_cluster}-data-${jsondecode(each.value)["name"]}"
4141
ClusterName = "${var.es_cluster}"
4242
VolumeIndex = jsondecode(each.value)["index"]
4343
AutoAttachGroup = "data"
@@ -53,7 +53,7 @@ resource "aws_ebs_volume" "singlenode" {
5353
encrypted = var.volume_encryption
5454

5555
tags = {
56-
Name = "elasticsearch-${var.es_cluster}-singlenode"
56+
Name = "elasticsearch-${var.environment}-${var.es_cluster}-singlenode"
5757
ClusterName = "${var.es_cluster}"
5858
VolumeIndex = "0"
5959
AutoAttachGroup = "singlenode"

0 commit comments

Comments
 (0)