本文是swarm docker集羣的實施文檔php
環境: centos 7html
192.168.1.23 swarm-manager rethinkdb controller swarm-agent consul-c1 registratorpython
192.168.1.45 registrator swarm-agent consul-s3linux
192.168.1.65 registrator swarm-agent consul-s2nginx
192.168.1.66 registrator swarm-agent consul-s2c++
192.168.1.70 registrator consul-c2 consul-template (nginx、haproxy)git
192.168.1.71 registrator consul-c2 consul-template (nginx、haproxy)web
docker-engine 1.12.4redis
1、安裝 docker-engine 1.12.4 docker
一、先檢查是否安裝舊版本docker
1
2
3
|
rpm -qa|
grep
docker
若是有先卸載
yum remove docker*
|
2.編輯docker.repo文件,寫入以下內容
1
2
3
4
5
6
7
|
cat
>>
/etc/yum
.repos.d
/docker-main
.repo << EOF
name=Docker main Repository
baseurl=https:
//yum
.dockerproject.org
/repo/main/centos/7
enabled=1
gpgcheck=1
gpgkey=
EOF
|
3.安裝docker
1
|
yum -y
install
docker-engine
|
4.關閉防火牆和selinux
1
2
3
4
5
|
systemctl stop firewalld.service
systemctl disable firewalld.service
關閉selinux
sed
-i
's/SELINUX=.*/SELINUX=disabled/g'
/etc/selinux/config
重啓
|
5.增長tcp監聽端口
1
2
|
修改
/lib/systemd/system/docker
.service
sed
-i
's/ExecStart=.*/ExecStart=\/usr\/bin\/dockerd -H unix\:\/\/\/var\/run\/docker.sock -D -H tcp\:\/\/0.0.0.0\:2375/g'
/lib/systemd/system/docker
.service
|
6.重啓docker
1
2
3
4
|
systemctl
enable
docker.service
systemctl restart docker
ps
-ef|
grep
docker
能看到docker啓動及2375端口
|
七、安裝pip及docker api
1
2
3
|
yum -y
install
epel-release
yum -y
install
python-pip
pip
install
docker-py docker-compose
|
2、配置consul cluster
一、建立 consul 用戶及組
1
2
|
groupadd -g 1000 consul
useradd
-u 100 -g 1000 -s
/sbin/nologin
consul
|
二、建立consul 數據存儲文件夾
1
2
|
mkdir
-p
/opt/consul/
{data,conf}
chown
-R consul:
/opt/consul
|
三、拉取consul鏡像
1
2
|
docker pull progrium
/consul
提示:目錄沒有官方出consul鏡像,以上consul鏡像是星數最高的,也是consul官方推薦的第三方docker image
|
四、啓動consul server 192.168.1.66
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
docker run -d \
-p 8300:8300 \
-p 8301:8301 \
-p 8301:8301
/udp
\
-p 8302:8302 \
-p 8302:8302
/udp
\
-p 8400:8400 \
-p 8500:8500 \
-p 8600:53 \
-p 8600:53
/udp
\
-
v
/opt/consul/data
:
/data
\
-h consul-s1 \
--restart=always \
--name=consul-s1 \
progrium
/consul
\
-server -bootstrap-expect=1 \
-ui-
dir
=
/ui
\
-client 0.0.0.0 \
-advertise 192.168.1.66
|
五、啓動consul server 192.168.1.65
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
docker run -d \
-p 8300:8300 \
-p 8301:8301 \
-p 8301:8301
/udp
\
-p 8302:8302 \
-p 8302:8302
/udp
\
-p 8400:8400 \
-p 8500:8500 \
-p 8600:53 \
-p 8600:53
/udp
\
-
v
/opt/consul/data
:
/data
\
-h consul-s2 \
--restart=always \
--name=consul-s2 \
progrium
/consul
\
-server \
-ui-
dir
=
/ui
\
-client 0.0.0.0 \
-advertise 192.168.1.65 -
join
192.168.1.66
|
六、啓動consul server 192.168.1.45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
docker run -d \
-p 8300:8300 \
-p 8301:8301 \
-p 8301:8301
/udp
\
-p 8302:8302 \
-p 8302:8302
/udp
\
-p 8400:8400 \
-p 8500:8500 \
-p 8600:53 \
-p 8600:53
/udp
\
-
v
/opt/consul/data
:
/data
\
-h consul-s3 \
--restart=always \
--name=consul-s3 \
progrium
/consul
\
-server \
-ui-
dir
=
/ui
\
-client 0.0.0.0 \
-advertise 192.168.1.45 -
join
192.168.1.66
|
七、啓動consul Client 192.168.1.23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
docker run -d -h consul-c1 \
-p 8300:8300 \
-p 8301:8301 \
-p 8301:8301
/udp
\
-p 8302:8302 \
-p 8302:8302
/udp
\
-p 8400:8400 \
-p 8500:8500 \
-p 8600:53 \
-p 8600:53
/udp
\
-
v
/opt/consul/data
:
/data
\
--restart=always \
--name=consul-c1 \
progrium
/consul
-advertise 192.168.1.23 -
join
192.168.1.66
|
八、啓動consul Client 192.168.1.70
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
docker run -d -h consul-c2 \
-p 8300:8300 \
-p 8301:8301 \
-p 8301:8301
/udp
\
-p 8302:8302 \
-p 8302:8302
/udp
\
-p 8400:8400 \
-p 8500:8500 \
-p 8600:53 \
-p 8600:53
/udp
\
-
v
/opt/consul/data
:
/data
\
--restart=always \
--name=consul-c2 \
progrium
/consul
-advertise 192.168.1.70 -
join
192.168.1.66
|
九、啓動consul Client 192.168.1.71
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
docker run -d -h consul-c3 \
-p 8300:8300 \
-p 8301:8301 \
-p 8301:8301
/udp
\
-p 8302:8302 \
-p 8302:8302
/udp
\
-p 8400:8400 \
-p 8500:8500 \
-p 8600:53 \
-p 8600:53
/udp
\
-
v
/opt/consul/data
:
/data
\
--restart=always \
--name=consul-c3 \
progrium
/consul
-advertise 192.168.1.71 -
join
192.168.1.66
|
十、關閉consul-s1並刪除容器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
docker
rm
-f consul-s1
從新啓動一個新容器
docker run -d \
-p 8300:8300 \
-p 8301:8301 \
-p 8301:8301
/udp
\
-p 8302:8302 \
-p 8302:8302
/udp
\
-p 8400:8400 \
-p 8500:8500 \
-p 8600:53 \
-p 8600:53
/udp
\
-
v
/opt/consul/data
:
/data
\
-h consul-s2 \
--restart=always \
--name=consul-s2 \
progrium
/consul
\
-server \
-ui-
dir
=
/ui
\
-client 0.0.0.0 \
-advertise 192.168.1.66 -
join
192.168.1.65
進入容器運行 consul info 查詢是否實現自動選舉Leader
|
3、registrator狀態獲取
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
依次啓動
docker run \
-d \
--restart=always \
--name=registrator \
--net=host \
-
v
/var/run/docker
.sock:
/tmp/docker
.sock \
gliderlabs
/registrator
\
-ip 192.168.1.66 \
consul:
//192
.168.1.66:8500
docker run \
-d \
--restart=always \
--name=registrator \
--net=host \
-
v
/var/run/docker
.sock:
/tmp/docker
.sock \
gliderlabs
/registrator
\
-ip 192.168.1.65 \
consul:
//192
.168.1.65:8500
docker run \
-d \
--restart=always \
--name=registrator \
--net=host \
-
v
/var/run/docker
.sock:
/tmp/docker
.sock \
gliderlabs
/registrator
\
-ip 192.168.1.45 \
consul:
//192
.168.1.45:8500
docker run \
-d \
--restart=always \
--name=registrator \
--net=host \
-
v
/var/run/docker
.sock:
/tmp/docker
.sock \
gliderlabs
/registrator
\
-ip 192.168.1.23 \
consul:
//192
.168.1.23:8500
docker run \
-d \
--restart=always \
--name=registrator \
--net=host \
-
v
/var/run/docker
.sock:
/tmp/docker
.sock \
gliderlabs
/registrator
\
-ip 192.168.1.70 \
consul:
//192
.168.1.70:8500
docker run \
-d \
--restart=always \
--name=registrator \
--net=host \
-
v
/var/run/docker
.sock:
/tmp/docker
.sock \
gliderlabs
/registrator
\
-ip 192.168.1.71 \
consul:
//192
.168.1.71:8500
|
4、安裝 Shipyard+Swarm
一、 192.168.1.23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
docker run -tid \
-p 3375:3375 \
--restart=always \
--name shipyard-swarm-manager \
swarm:latest \
manage --host tcp:
//0
.0.0.0:3375 consul:
//192
.168.1.66:8500
docker run -tid \
--restart=always \
--name=shipyard-rethinkdb \
-p 28015:28015 \
-p 29015:29015 \
-
v
/data/rethinkdb
:
/data
\
index.tenxcloud.com
/docker_library/rethinkdb
docker run -tid \
--restart=always \
--name shipyard-controller \
--link shipyard-rethinkdb:rethinkdb \
--link shipyard-swarm-manager:swarm \
-p 8080:8080 \
dockerclub
/shipyard
:latest \
server \
-d tcp:
//swarm
:3375
# 中文鏡像
docker run -tid \
--restart=always \
--name shipyard-swarm-agent \
swarm:latest \
join
--addr 192.168.1.23:2375 consul:
//192
.168.1.66:8500
|
二、安裝swarm-agent (192.168.1.66,192.168.1.65,192.168.1.45)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
docker run -tid \
--restart=always \
--name shipyard-swarm-agent \
swarm:latest \
join
--addr 192.168.1.66:2375 consul:
//192
.168.1.66:8500
docker run -tid \
--restart=always \
--name shipyard-swarm-agent \
swarm:latest \
join
--addr 192.168.1.65:2375 consul:
//192
.168.1.66:8500
docker run -tid \
--restart=always \
--name shipyard-swarm-agent \
swarm:latest \
join
--addr 192.168.1.45:2375 consul:
//192
.168.1.66:8500
|
5、安裝haproxy or nginx (192.168.1.70,192.168.1.71)
一、安裝haproxy
http://www.haproxy.org/download/1.7/src/haproxy-1.7.0.tar.gz
1
2
3
4
5
6
7
8
9
10
11
|
yum -y
install
git patch gcc gcc-c++ readline-devel zlib-devel libffi-devel \
openssl openssl-devel
make
autoconf automake libtool bison libxml2 \
libxml2-devel libxslt-devel libyaml-devel python python-docutils \
cmake imake expat-devel libaio libaio-devel bzr ncurses-devel wget \
libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel \
pcre-devel curl-devel libmcrypt libmcrypt-devel
cd
/tmp
wget http:
//www
.haproxy.org
/download/1
.7
/src/haproxy-1
.7.0.
tar
.gz
tar
-xvf
/tmp/haproxy-1
.7.0.
tar
.gz
make
TARGET=linux31 PREFIX=
/opt/haproxy
make
install
PREFIX=
/opt/haproxy
|
二、配置/opt/haproxy/conf/haproxy.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
global
log 127.0.0.1 local0
#log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 50000
chroot
/opt/haproxy
uid 99
gid 99
daemon
nbproc 2
pidfile
/opt/haproxy/run/haproxy
.pid
#debug
#quiet
defaults
mode tcp
option dontlognull
option forwardfor
option redispatch
retries 2
balance static-rr
stats
enable
stats uri
/ha
?stats
#haproxy運行狀態查看 自定義uri
timeout connect 3000
timeout client 50000
timeout server 50000
listen admin_stat
# 監聽端口
bind *:8888
# http的7層模式
mode http
#log global
# 統計頁面自動刷新時間
stats refresh 30s
# 統計頁面URL
stats uri
/admin
?stats
# 統計頁面密碼框上提示文本
stats realm Haproxy\ Statistics
# 統計頁面用戶名和密碼設置
stats auth admin:admin
# 隱藏統計頁面上HAProxy的版本信息
#stats hide-version
listen login
bind *:9999
mode tcp
balance roundrobin
option httpchk
#maxconn 50000
#log 127.0.0.1 local0 debug
|
三、haproxy 啓動腳本 /etc/init.d/haproxy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
#! /bin/bash
# chkconfig: - 85 15
# description: haproxy is a World Wide Web server. It is used to serve
PROGDIR=
/opt/haproxy
PROGNAME=haproxy
DAEMON=$PROGDIR
/sbin/
$PROGNAME
CONFIG=$PROGDIR
/conf/
$PROGNAME.conf
PIDFILE=$PROGDIR
/run/
$PROGNAME.pid
DESC=
"HAProxy daemon"
SCRIPTNAME=
/opt/haproxy/init
.d/$PROGNAME
# Gracefully exit if the package has been removed.
test
-x $DAEMON ||
exit
0
start()
{
echo
-n
"Starting $DESC: $PROGNAME"
$DAEMON -f $CONFIG
echo
"."
}
stop()
{
echo
-n
"Stopping $DESC: $PROGNAME"
cat
$PIDFILE |
xargs
kill
echo
"."
}
reload()
{
echo
-n
"reloading $DESC: $PROGNAME"
$DAEMON -f $CONFIG -p $PIDFILE -sf $(
cat
$PIDFILE)
}
case
"$1"
in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
*)
echo
"Usage: $SCRIPTNAME {start|stop|reload}"
>&2
exit
1
;;
esac
exit
0
|
四、啓動 haproxy 並加入開機啓動
1
2
3
|
chmod
+x haproxy
chkconfig haproxy on
service haproxy start
|
五、安裝nginx 並支支持數字證書
https://www.openssl.org/source/openssl-1.1.0c.tar.gz
http://nginx.org/download/nginx-1.11.7.tar.gz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
yum -y
install
git patch gcc gcc-c++ readline-devel zlib-devel libffi-devel \
openssl openssl-devel
make
autoconf automake libtool bison libxml2 \
libxml2-devel libxslt-devel libyaml-devel python python-docutils \
cmake imake expat-devel libaio libaio-devel bzr ncurses-devel wget \
libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel \
pcre-devel curl-devel libmcrypt libmcrypt-devel
下載安裝openssl
cd
/tmp
wget https:
//www
.openssl.org
/source/openssl-1
.1.0c.
tar
.gz
tar
-xvf openssl-1.1.0c.
tar
.gz
cd
/tmp/openssl-1
.1.0c
.
/config
--openssldir=
/usr/local/ssl
make
&&
make
install
.
/config
shared --openssldir=
/usr/local/ssl
make
clean
make
&&
make
install
下載安裝 nginx
cd
/tmp
wget http:
//nginx
.org
/download/nginx-1
.11.7.
tar
.gz
groupadd -r nginx
useradd
-g nginx -r nginx
tar
-xvf nginx-1.11.7.
tar
.gz
cd
/tmp/nginx-1
.11.7
.
/configure
--prefix=
/usr/local/nginx
\
--sbin-path=
/usr/sbin/nginx
\
--conf-path=
/etc/nginx/nginx
.conf \
--error-log-path=
/var/log/nginx/error
.log \
--http-log-path=
/var/log/nginx/access
.log \
--pid-path=
/var/run/nginx
.pid \
--lock-path=
/var/run/nginx
.lock \
--http-client-body-temp-path=
/var/cache/nginx/client_temp
\
--http-proxy-temp-path=
/var/cache/nginx/proxy_temp
\
--http-fastcgi-temp-path=
/var/cache/nginx/fastcgi_temp
\
--http-uwsgi-temp-path=
/var/cache/nginx/uwsgi_temp
\
--http-scgi-temp-path=
/var/cache/nginx/scgi_temp
\
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-threads \
--with-stream \
--with-openssl=
/tmp/openssl-1
.1.0c \
# openssl 源碼路徑
--with-stream_ssl_module \
--with-http_slice_module \
--with-mail \
--with-mail_ssl_module \
--with-
file
-aio \
--with-http_v2_module \
--with-ipv6
mkdir
-pv
/var/cache/nginx/
{client_temp,proxy_temp,fastcgi_temp,uwsgi_temp,scgi_temp}
make
&&
make
install
|
六、nginx 配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
修改
/etc/nginx/nginx
.conf
user nginx;
worker_processes 1;
error_log
/var/log/nginx/error
.log warn;
pid
/var/run/nginx
.pid;
events {
worker_connections 1024;
}
http {
include
/etc/nginx/mime
.types;
default_type application
/octet-stream
;
log_format main
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
;
access_log
/var/log/nginx/access
.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip
on;
include
/etc/nginx/conf
.d/*.conf;
}
建立
/etc/nginx/conf
.d 文件夾
mkdir
-p
/etc/nginx/conf
.d
添加nginx默認web配置文件
/etc/nginx/conf
.d
/default
.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root
/usr/local/nginx/html
;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504
/50x
.html;
location =
/50x
.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root
/usr/local/nginx/html
;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* ^.+\.(jpg|jpeg|gif|png|bmp)$ {
access_log off;
root opencart;
expires 30d;
break
;
}
}
|
七、建立nginx 啓動腳本 /etc/init.d/nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# chkconfig: 2345 10 90
# description: Start and Stop redis
PATH=
/usr/local/bin
:
/sbin
:
/usr/bin
:
/bin
EXEC=
/usr/sbin/nginx
PIDFILE=
/var/run/nginx
.pid
CONF=
"/etc/nginx/nginx.conf"
AUTH=
"1234"
case
"$1"
in
start)
if
[ -f $PIDFILE ]
then
echo
"$PIDFILE exists, process is already running or crashed."
else
echo
"Starting nginx server..."
$EXEC &
fi
if
[
"$?"
=
"0"
]
then
echo
"nginx is running..."
fi
;;
stop)
if
[ ! -f $PIDFILE ]
then
echo
"$PIDFILE exists, process is not running."
else
PID=$(
cat
$PIDFILE)
echo
"Stopping..."
kill
-9 $PID
PID=$(pidof nginx)
kill
-9 $PID
rm
-rf
/var/run/nginx
.pid
sleep
2
while
[ -x $PIDFILE ]
do
echo
"Waiting for nginx to shutdown..."
sleep
1
done
echo
"nginx stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo
"Usage: /etc/init.d/nginx {start|stop|restart|force-reload}"
>&2
exit
1
esac
|
八、設置nginx開機啓動並啓動nginx
1
2
3
|
chmod
+x nginx
chkconfig nginx on
service nginx start
|
6、安裝consul-template 實現服務器自動發現
一、下載consul-template
https://releases.hashicorp.com/consul-template/0.16.0/consul-template_0.16.0_linux_amd64.zip
1
2
3
4
|
cd
/tmp
wget https:
//releases
.hashicorp.com
/consul-template/0
.16.0
/consul-template_0
.16.0_linux_amd64.zip
yum -y
install
unzip
unzip
/tmp/consul-template_0
.16.0_linux_amd64.zip -d
/usr/bin/
|
二、consul-template haproxy配置
1
2
3
4
5
6
7
8
9
|
cat
>
/opt/consul/conf/haproxy_ctmpl
.json << EOF
consul =
"127.0.0.1:8500"
template {
source
=
"/opt/haproxy/conf/haproxy.ctmpl"
destination =
"/opt/haproxy/conf/haproxy.conf"
command
=
"/etc/init.d/haproxy reload"
}
EOF
|
三、 /opt/haproxy/conf/haproxy.ctmpl 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
global
log 127.0.0.1 local0
#log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 50000
chroot
/opt/haproxy
uid 99
gid 99
daemon
nbproc 2
pidfile
/opt/haproxy/run/haproxy
.pid
#debug
#quiet
defaults
mode tcp
option dontlognull
option forwardfor
option redispatch
retries 2
balance static-rr
stats
enable
stats uri
/ha
?stats
timeout connect 3000
timeout client 50000
timeout server 50000
listen admin_stat
bind *:8888
mode http
#log global
stats refresh 30s
stats uri
/admin
?stats
stats realm Haproxy\ Statistics
stats auth admin:admin
#stats hide-version
frontend www
bind *:80
mode http
acl apache hdr(HOST) apache.zone.com
acl nginx hdr(HOST) nginx.zone.com
use_backend apache.qkazone.com
if
apache
use_backend nginx.qkazone.com
if
nginx
backend apache.zone.com
balance roundrobin
mode http
{{range service
"apache-php-80"
}}
server apache {{.Address}}:{{.Port}} check {{end}}
backend nginx.zone.com
mode http
balance roundrobin
{{range service
"nginx-80"
}}
server nginx {{.Address}}:{{.Port}} check {{end}}
listen login
bind *:9999
mode tcp
balance roundrobin
#log 127.0.0.1 local0 debug
{{range service
"centos7"
}}
server
ssh
{{.Address}}:{{.Port}} check {{end}}
|
四、配置consul-template haproxy 啓動腳本 /etc/init.d/haproxy_ctmpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#!/bin/bash
# chkconfig: 2345 10 90
# description: Start and Stop redis
PATH=
/usr/local/bin
:
/sbin
:
/usr/bin
:
/bin
EXEC=
/usr/bin/consul-template
CONF=
"/opt/consul/conf/haproxy_ctmpl.json"
case
"$1"
in
start)
PID=$(
ps
-ef |
grep
-
v
grep
|
grep
"$EXEC -config $CONF"
|
awk
'{print $2}'
)
if
[ -n
"$PID"
]
then
echo
"haproxy_ctmpl is running..."
else
echo
"Starting haproxy_ctmpl server..."
$EXEC -config $CONF >
/tmp/haproxy_ctmpl
.out 2>&1 &
fi
;;
stop)
PID=$(
ps
-ef |
grep
-
v
grep
|
grep
"$EXEC -config $CONF"
|
awk
'{print $2}'
)
if
[ -n
"$PID"
]
then
echo
"Stopping..."
kill
-9 $PID
sleep
2
else
echo
"haproxy_ctmpl exists, process is not running."
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo
"Usage: /etc/init.d/tmpl {start|stop|restart|force-reload}"
>&2
exit
1
esac
|
五、設置開機啓動,啓動
1
2
3
|
chmod
+x haproxy_ctmpl
chkconfig haproxy_ctmpl on
service haproxy_ctmpl start
|
六、consul-template nginx配置 /opt/consul/conf/nginx_ctmpl.json
1
2
3
4
5
6
7
8
9
|
cat
>
/opt/consul/conf/nginx_ctmpl
.json << EOF
consul =
"127.0.0.1:8500"
template {
source
=
"/etc/nginx/conf.d/nginx_web.ctmpl"
destination =
"/etc/nginx/conf.d/nginx_web.conf"
command
=
"/usr/sbin/nginx -s reload"
}
EOF
|
七、/etc/nginx/conf.d/nginx_web.ctmpl 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
upstream apache {
ip_hash;
# Refer: http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream
# least_conn;
# least_time;
{{range service
"apache-php-80"
}}
server {{.Address}}:{{.Port}} fail_timeout=0;
{{end}}
keepalive 64;
}
server {
listen 80;
server_name apache.zone.com;
location / {
client_max_body_size 0;
proxy_connect_timeout 300s;
proxy_send_timeout 900;
proxy_read_timeout 900;
proxy_buffer_size 32k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_redirect off;
proxy_hide_header Vary;
proxy_set_header Accept-Encoding
''
;
proxy_set_header Host $host;
proxy_set_header Referer $http_referer;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_pass http:
//apache/
;
}
}
upstream nginx {
ip_hash;
# Refer: http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream
# least_conn;
# least_time;
{{range service
"nginx-80"
}}
server {{.Address}}:{{.Port}} fail_timeout=0;
{{end}}
keepalive 64;
}
server {
listen 80;
server_name nginx.zone.com;
location / {
client_max_body_size 0;
proxy_connect_timeout 300s;
proxy_send_timeout 900;
proxy_read_timeout 900;
proxy_buffer_size 32k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_redirect off;
proxy_hide_header Vary;
proxy_set_header Accept-Encoding
''
;
proxy_set_header Host $host;
proxy_set_header Referer $http_referer;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_pass http:
//nginx/
;
}
}
|
八、配置consul-template nginx啓動腳本 /etc/init.d/nginx_ctmpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#!/bin/bash
# chkconfig: 2345 10 90
# description: Start and Stop redis
PATH=
/usr/local/bin
:
/sbin
:
/usr/bin
:
/bin
EXEC=
/usr/bin/consul-template
CONF=
"/opt/consul/conf/nginx_ctmpl.json"
case
"$1"
in
start)
PID=$(
ps
-ef |
grep
-
v
grep
|
grep
"$EXEC -config $CONF"
|
awk
'{print $2}'
)
if
[ -n
"$PID"
]
then
echo
"haproxy_ctmpl is running..."
else
echo
"Starting haproxy_ctmpl server..."
$EXEC -config $CONF >
/tmp/nginx_ctmpl
.out 2>&1 &
fi
;;
stop)
PID=$(
ps
-ef |
grep
-
v
grep
|
grep
"$EXEC -config $CONF"
|
awk
'{print $2}'
)
if
[ -n
"$PID"
]
then
echo
"Stopping..."
kill
-9 $PID
sleep
2
else
echo
"haproxy_ctmpl exists, process is not running."
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo
"Usage: /etc/init.d/tmpl {start|stop|restart|force-reload}"
>&2
exit
1
esac
|
九、設置開機啓動
1
2
3
|
chmod
+x nginx_ctmpl
chkconfig nginx_ctmpl on
service nginx_ctmpl start
|
7、測試是否自動發現
1
2
|
docker run -ti -d -p :80 eboraas
/apache-php
docker run -d -ti -p :80 nginx
|
一、打開 http://192.168.1.66:8500/ui/#/dc1/services
二、http://192.168.1.23:8080/#/containers
查看shipyard 管理平臺
帳號admin密碼 shipyard
三、haproxy
http://192.168.1.70:8888/admin?stats
http://192.168.1.71:8888/admin?stats
帳號:admin 密碼admin
最後:注意事項:consul registrator 是基於端口發現的不映射端口是發現不了的
consul 服務掛了整個宿主機運行應用將不可見。registrator 是基於鏡像名加端口服務
鏡像名稱爲nginx 多端口暴露 對外發現的服務名稱nginx-80 單個端口暴露就是鏡像名稱爲服務名
像集羣添加宿主機
consul registrator swarm-agent registrator 連接consul 端口請使用宿主機的consul 端口
使用其它機器端口若是其它機器掛了這臺宿主機的應用不可被發現
本文出自 「小葉寒笑」 博客,請務必保留此出處http://juestnow.blog.51cto.com/1515305/1882708