Skip to content

Instantly share code, notes, and snippets.

@gangsta
Last active November 22, 2017 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gangsta/ed8fddad6a8e03bb15396ac0823dbb0f to your computer and use it in GitHub Desktop.
Save gangsta/ed8fddad6a8e03bb15396ac0823dbb0f to your computer and use it in GitHub Desktop.
Install Consul Server on Centos 7
echo '
#! /bin/bash


####### yum update is optional #####
#yum -y update
yum install firewalld -y
systemctl start firewalld
firewall-cmd  --add-port=8300/tcp --add-port=8301/tcp --add-port=8302/tcp --add-port=8400/tcp --add-port=8500/tcp --add-port=80/tcp --add-port=443/tcp --permanent
firewall-cmd  --reload
yum -y install unzip wget

### Download Consul to Host ####
mkdir /tmp/bin
cd /tmp/bin
wget https://releases.hashicorp.com/consul/0.9.2/consul_0.9.2_linux_amd64.zip
unzip consul_0.9.2_linux_amd64.zip
rm *.zip


####  Create needed folders ####
mkdir /var/consul
mkdir -p /home/consul/www
mkdir -p /etc/consul.d/{server,bootstrap}
mv consul /usr/local/bin/

###### Optional !! if you have config then comment below section ###
touch /etc/consul.d/server/config.json


cat <<EOF > /etc/consul.d/server/config.json
{
    "advertise_addr": "[SERVER IP]",
    "bind_addr": "[SERVER IP]",
    "domain": "[DOMAIN NAME]",
    "bootstrap_expect": 3,
    "server": true,
    "datacenter": "[DATACENTRE ID]",
    "data_dir": "/var/consul",
    "encrypt": "ENCRYPT KEY",
    "dns_config": {
        "allow_stale": true,
        "max_stale": "15s"
    },
    "retry_join": [
        "[LIST OF OTHER CONSUL SERVER IP's]",
        "[LIST OF OTHER CONSUL SERVER IP's]"
    ],
    "retry_interval": "10s",
    "retry_max": 100,
    "skip_leave_on_interrupt": true,
    "leave_on_terminate": false,
    "ports": {
        "dns": 53,
        "http": 80
    },
    "recursor": "[IP FOR FORWARD DNS LOOKUPS]",
    "rejoin_after_leave": true,
    "addresses": {
        "http": "0.0.0.0",
        "dns": "0.0.0.0"
    }
}
EOF


#### Adding systemd service script ####


cat <<EOF > /etc/systemd/system/consul.service
[Unit]
Description=consul agent
Requires=network-online.target
After=network-online.target

[Service]
EnvironmentFile=-/etc/sysconfig/consul
Environment=GOMAXPROCS=2
Restart=on-failure
ExecStart=/usr/local/bin/consul agent -config-dir=/etc/consul.d/server -rejoin -ui -data-dir=/var/consul
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGTERM

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl start consul
systemctl enable consul


### Consul service will not run due config above . please fill it up with your enviromental variables and restart consul service ####

' >> ./master.sh
chmod +x ./master.sh
sh ./master.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment