安装前的准备

如果本机是首次使用docker-compose环境部署,需要在在/deploy/sorts创建以下脚本

vi pre_install.sh
#!/bin/bash
###关闭selinux
sed -i 's/SELINUXTYPE=targeted/SELINUXTYPE=disabled/g' /etc/selinux/config
setenforce 0
###卸载旧版本docker,安装docker-ce
#yum remove docker \
# docker-client \
# docker-client-latest \
# docker-common \
# docker-latest \
# docker-latest-logrotate \
# docker-logrotate \
# docker-engine
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum -y install docker-ce
###配置拉取国内镜像仓库
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://5sr5uink.mirror.aliyuncs.com",
"https://docker.mirrors.ustc.edu.cn",
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"
],
"insecure-registries": ["harbor.xxx.com"]
}
EOF
###启动docker
systemctl start docker
systemctl enable docker
###配置阿里云epel 源
cd /etc/yum.repo.d
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
###安装docker-compose
yum -y install epel-release
yum install -y docker-compose
docker-compose -version

创建目录

mkdir -p /deploy/sorts/consul

创建子目录

cd /deploy/sorts/consul
mkdir conf data log

编辑docker-compose.yml文件

version: '3.1'
services:
consul:
image: harbor.xxx.com/xxx/consul:1.7.3
volumes:
- ./data:/consul/data ##保存数据目录
- ./conf:/consul/config ##保存配置目录
- ./log:/var/log/consul ##保存日志目录
ports:
- 8300:8300
- 8301:8301
- 8301:8301/udp
- 8302:8302
- 8302:8302/udp
- 8400:8400
- 8500:8500
- 53:53/udp
network_mode: "host"
command: agent -server -bind=10.10.10.203 -client=0.0.0.0 -node=consul -bootstrap-expect=1 -ui -log-file=/var/log/consul ##绑定地址需要改成安装主机的IP

编辑启动和停止脚本

启动脚本

vi up.sh
#!/bin/bash

set -e

tm=`date +%Y%m%d-%H%M%S`
echo "====================================================="
echo "Starting consul."
echo "Starting <${tm}>."
echo "====================================================="
echo "Please ensure the config files right."
echo "====================================================="

docker-compose -p consul up -d

停止脚本

vi down.sh
#!/bin/bash

set -e

tm=`date +%Y%m%d-%H%M%S`
echo "====================================================="
echo "Starting consul."
echo "Starting <${tm}>."
echo "====================================================="
echo "Please ensure the config files right."
echo "====================================================="

docker-compose -p consul down

启动和查看状态

up表示已经启动

sh up.sh
[root@test_docker_compose_10 consul]# docker-compose ps
Name Command State Ports
----------------------------------------------------------------
consul_consul_1 docker-entrypoint.sh agent ... Up

查看日志

docker-compose logs consul