Skip to content

Instantly share code, notes, and snippets.

@strongant
Last active December 1, 2022 06:29
Show Gist options
  • Save strongant/7fcc6d288254d34059e454db4e1f1b90 to your computer and use it in GitHub Desktop.
Save strongant/7fcc6d288254d34059e454db4e1f1b90 to your computer and use it in GitHub Desktop.
自动安装consul,并创建对应的service并运行consul服务
echo '
#! /bin/bash
####### yum update is optional #####
#yum -y update
sudo yum install firewalld -y
sudo systemctl start firewalld
sudo 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
sudo firewall-cmd --reload
sudo yum -y install unzip wget
### Download Consul to Host ####
mkdir /tmp/bin
cd /tmp/bin
wget https://releases.hashicorp.com/consul/1.14.2/consul_1.14.2_linux_amd64.zip
unzip consul_1.14.2_linux_amd64.zip
rm *.zip
#### Create needed folders ####
sudo mkdir /var/consul
sudo mkdir -p /home/consul/www
sudo mkdir -p /etc/consul.d/{server,bootstrap}
sudo mv consul /usr/local/bin/
###### Optional !! if you have config then comment below section ###
sudo touch /etc/consul.d/server/config.json
sudo cat <<EOF > /etc/consul.d/server/config.json
{
"bind_addr": "0.0.0.0",
"bootstrap_expect": 1,
"server": true,
"data_dir": "/var/consul",
"dns_config": {
"allow_stale": true,
"max_stale": "0s"
},
"retry_join": [],
"retry_interval": "10s",
"retry_max": 100,
"skip_leave_on_interrupt": true,
"leave_on_terminate": false,
"rejoin_after_leave": true,
"addresses": {
"http": "0.0.0.0",
"dns": "0.0.0.0"
}
}
EOF
#### Adding systemd service script ####
sudo 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
sudo systemctl daemon-reload
sudo systemctl start consul
sudo systemctl enable consul
### Consul service will not run due config above . please fill it up with your enviromental variables and restart consul service ####
' >> ./consul-install.sh
chmod +x ./consul-install.sh
sh ./consul-install.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment