容器创建步骤:
1.从etcd读取docker的host信息,包括appname与ip。
2.由confd生成haproxy配置文件,读取etcd的变量信息。
3.最后由Python API操作创建docker容器。
介绍
Etcd是一个高可用的 Key/Value 存储系统,主要用于分享配置和服务发现。 简单:支持 curl 方式的用户 API 。
Confd介绍
Confd是一个轻量级的配置管理工具。通过查询Etcd,结合配置模板引擎,保持本地配置最新,同时具备定期探测机制,配置变更自动reload。
安装
宿主机安装
yum -y install docker-io
etcd安装
wget https://github.com/coreos/etcd/releases/download/v0.4.6/etcd-v0.4.6-linux-amd64.tar.gz
tar -zxvf etcd-v0.4.6-linux-amd64.tar.gz
cd etcd-v0.4.6-linux-amd64
cp etcd* /bin/
confd与haproxy
yum -y install haproxy
wget https://github.com/kelseyhightower/confd/releases/download/v0.6.3/confd-0.6.3-linux-amd64
mv confd /usr/local/bin/confd
chmod +x /usr/local/bin/confd
etcd部分
启动
#-peer-addr:与其他节点通讯socket
#-addr:服务监听socket
#-data-dir:存储目录
/bin/etcd -name etcdserver -peer-addr 192.168.1.21:7001 -addr 192.168.1.21:4001 -data-dir /data/etcd -peer-bind-addr 0.0.0.0:7001 -bind-addr 0.0.0.0:4001 &
实例
#设置key
curl -L http://192.168.1.21:4001/v2/keys/testkey -XPUT -d value="this is testkey"
#获取key
curl -L http://192.168.1.21:4001/v2/keys/testkey
#删除key
curl -L http://192.168.1.21:4001/v2/keys/testkey -XDELETE
confd + haproxy部分
/etc/confd/conf.d/ haproxy.toml
[template]
src = "haproxy.cfg.tmpl"
dest = "/etc/haproxy/haproxy.cfg"
keys = [
"/app/servers",
]
reload_cmd = "/etc/init.d/haproxy reload"
/etc/confd/templates/haproxy.cfg.tmpl
global
log 127.0.0.1 local3
maxconn 5000
uid 99
gid 99
daemon
defaults
log 127.0.0.1 local3
mode http
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen frontend 0.0.0.0:80
mode http
balance roundrobin
maxconn 2000
option forwardfor
{{range gets "/app/servers/*"}}
server {{base .Key}} {{.Value}} check inter 5000 fall 1 rise 2
{{end}}
stats enable
stats uri /admin-status
stats auth admin:123456
stats admin if TRUE
语法见http://golang.org/pkg/text/template/
启动
#interval探测频率,node监控socket,confdir配置目录
/usr/local/bin/confd -verbose -interval 10 -node '192.168.1.21:4001' -confdir /etc/confd > /var/log/confd.log &
docker启动程序实例(by 刘天斯)
import docker
import etcd
import sys
Etcd_ip="192.168.1.21"
Server_ip="192.168.1.22"
App_port="80"
App_protocol="tcp"
Image="yorko/webserver:v3"
Port=""
Name=""
idict={}
rinfo={}
try:
c = docker.Client(base_url='tcp://'+Server_ip+':2375',version='1.14',timeout=15)
except Exception,e:
print "Connection docker server error:"+str(e)
sys.exit()
try:
rinfo=c.create_container(image=Image,stdin_open=True,tty=True,command="/usr/bin/supervisord -c /etc/supervisord.conf",volumes=['/data','/etc/httpd/conf','/etc/httpd/conf.d
','/etc/localtime'],ports=[80,22],name=None)
containerId=rinfo['Id']
except Exception,e:
print "Create docker container error:"+str(e)
sys.exit()
try:
c.start(container=containerId, binds={'/data':{'bind': '/data','ro': False},'/etc/httpd/conf':{'bind': '/etc/httpd/conf','ro': True},'/etc/httpd/conf.d':{'bind': '/etc/htt
pd/conf.d','ro': True},'/etc/localtime':{'bind': '/etc/localtime','ro': True}}, port_bindings={80:None,22:None}, lxc_conf=None,publish_all_ports=True, links=None, privileged=F
alse,dns='172.17.42.1', dns_search=None, volumes_from=None, network_mode=None,restart_policy=None, cap_add=None, cap_drop=None)
except Exception,e:
print "Start docker container error:"+str(e)
sys.exit()
try:
idict=c.inspect_container(containerId)
Name=idict["Name"][1:]
skey=App_port+'/'+App_protocol
for _key in idict["NetworkSettings"]["Ports"].keys():
if _key==skey:
Port=idict["NetworkSettings"]["Ports"][skey][0]["HostPort"]
except Exception,e:
print "Get docker container inspect error:"+str(e)
sys.exit()
if Name!="" and Port!="":
try:
client = etcd.Client(host=Etcd_ip, port=4001)
client.write('/app/servers/'+Name, Server_ip+":"+str(Port))
print Name+" container run success!"
except Exception,e:
print "set etcd key error:"+str(e)
else:
print "Get container name or port error."