rvn_tp22/install_nomad_consul.sh

172 lines
4 KiB
Bash
Raw Normal View History

2023-12-11 12:08:38 +00:00
#!/usr/bin/env bash
if [ -z "$BIND_ADDR" ]; then
BIND_ADDR=$(ip -4 -br addr show dev ip0 | awk '{print $NF}' | awk -F "/" '{print $1}' )
fi
if [ -z "$NODE_NAME" ]; then
read -p "Provide node_name: " NODE_NAME
fi
#read -p "node_name='$NODE_NAME', BIND_ADDR='$BIND_ADDR', ok?"
### CONSUL ###
echo "Installing dependencies..."
sudo apt-get update
sudo apt-get install -y unzip curl jq dnsutils
if [ ! -f /usr/bin/consul ]; then
CHECKPOINT_URL="https://checkpoint-api.hashicorp.com/v1/check"
if [ -z "$CONSUL_VERSION" ]; then
CONSUL_VERSION=$(curl -s "${CHECKPOINT_URL}"/consul | jq .current_version | tr -d '"')
fi
echo "Fetching Consul version ${CONSUL_VERSION} ..."
cd /tmp/
curl -s https://releases.hashicorp.com/consul/${CONSUL_VERSION}/consul_${CONSUL_VERSION}_linux_amd64.zip -o consul.zip
echo "Installing Consul version ${CONSUL_VERSION} ..."
unzip consul.zip
sudo chmod +x consul
sudo mv consul /usr/bin/consul
fi
# useradd : -U create a group same as username, -r system user
sudo mkdir /etc/consul.d
sudo chmod a+w /etc/consul.d
sudo useradd -Urd /var/consul/ consul
sudo mkdir -p /var/consul/data
sudo chown -R consul /var/consul/data
cat <<EOF | sudo tee /etc/consul.d/consul.hcl
node_name = "$NODE_NAME"
advertise_addr = "$BIND_ADDR"
bind_addr = "$BIND_ADDR"
server = true
bootstrap_expect = 3
# Generated by `consul keygen`
encrypt = "fMVZvH42OjraXuJhrPPLV+4XZpbcJjq32LoDFm1BgLs="
retry_join = [
"10.0.1.1",
"10.0.1.2",
"10.0.1.3"
]
ui_config {
enabled = true
}
datacenter = "dc1"
data_dir = "/var/consul/data"
log_level = "INFO"
client_addr = "127.0.0.1"
connect {
enabled = true
}
EOF
cat <<'EOF' | sudo tee /etc/systemd/system/consul.service
[Unit]
Description="HashiCorp Consul - A service mesh solution"
Documentation=https://www.consul.io/
Requires=network-online.target
After=network-online.target
ConditionFileNotEmpty=/etc/consul.d/consul.hcl
[Service]
EnvironmentFile=-/etc/consul.d/consul.env
User=consul
Group=consul
ExecStart=/usr/bin/consul agent -config-dir=/etc/consul.d/
ExecReload=/bin/kill --signal HUP ${MAINPID}
KillMode=process
KillSignal=SIGTERM
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
### NOMAD
if [ ! -f /usr/bin/nomad ]; then
if [ -z "$NOMAD_VERSION" ]; then
NOMAD_VERSION=$(curl -s "${CHECKPOINT_URL}"/nomad | jq .current_version | tr -d '"')
fi
echo "Fetching Nomad version ${NOMAD_VERSION} ..."
cd /tmp/
curl -s https://releases.hashicorp.com/nomad/${NOMAD_VERSION}/nomad_${NOMAD_VERSION}_linux_amd64.zip -o nomad.zip
echo "Installing Nomad version ${NOMAD_VERSION} ..."
unzip nomad.zip
sudo chmod +x nomad
sudo mv nomad /usr/bin/nomad
fi
# useradd : -U create a group same as username, -r system user
sudo mkdir /etc/nomad.d
sudo chmod a+w /etc/nomad.d
sudo useradd -Urd /var/nomad/ nomad
sudo mkdir -p /var/nomad/data
sudo chown -R nomad /var/nomad/data
cat <<EOF | sudo tee /etc/nomad.d/nomad.hcl
name = "$NODE_NAME"
bind_addr = "0.0.0.0"
#addresses {
# http = "127.0.0.1 $BIND_ADDR"
#}
#advertise {
# http = "127.0.0.1 $BIND_ADDR"
#}
server {
enabled = true
bootstrap_expect = 3
encrypt = "ESPWsUrvhho/BiwwKO9aftrniw+P3FUw5yvmeYHeQv8="
server_join {
retry_join = [
"10.0.1.1",
"10.0.1.2",
"10.0.1.3"
]
}
}
consul {
address = "$BIND_ADDR:8500"
}
datacenter = "dc1"
data_dir = "/var/nomad/data"
log_level = "INFO"
EOF
cat <<'EOF' | sudo tee /etc/systemd/system/nomad.service
[Unit]
Description="HashiCorp nomad"
Documentation=https://www.nomadproject.io/docs/
Requires=network-online.target
Wants=consul.service
After=consul.service
ConditionFileNotEmpty=/etc/nomad.d/nomad.hcl
[Service]
EnvironmentFile=-/etc/nomad.d/nomad.env
User=nomad
Group=nomad
ExecStart=/usr/bin/nomad agent -config /etc/nomad.d/
ExecReload=/bin/kill --signal HUP ${MAINPID}
KillMode=process
KillSignal=SIGTINT
Restart=on-failure
LimitNOFILE=65536
OOMScoreAdjust=-1000
[Install]
WantedBy=multi-user.target
EOF
### START SERVICES
sudo systemctl daemon-reload
sudo systemctl restart consul.service
sudo systemctl restart nomad.service