回顾:
1 安装etcd
[[email protected] ~]# pip install python-etcd 安装etcd软件
2 修改salt-master的配置文件,加配置,并重新启动salt-master
[[email protected] ~]# vim /etc/salt/master
etcd_pillar_config:
etcd.host: 10.0.0.7
etcd.port: 4001
ext_pillar:
- etcd: etcd_pillar_config root=/salt/haproxy/
3 添加节点
[[email protected] ~]# curl -s http://10.0.0.7:2379/v2/keys/salt/haproxy/backend_www_oldboyedu_com/web-node4 -XPUT -d value="10.0.0.7:8080" | python -m json.tool
4 改haproxy-outside.sls文件添加jinja模版标志
[[email protected] ~]# vim /srv/salt/prod/cluster/haproxy-outside.sls
- template: jinja
5 修改配置文件
[[email protected] ~]# vim /srv/salt/prod/cluster/files/haproxy-outside.cfg
{% for web,web_ip in pillar.backend_www_oldboyedu_com.iteritems() -%}
server {{ web }} {{ web_ip }} check inter 2000 rise 30 fall 15
{% endfor %}
脚本实现自动添加机器:
[[email protected] ~]# cat auto.sh
#!/bin/bash
MAIN_ADD_HOST=$1
create_host(){
echo "create host ok"
}
deploy_service(){
#salt ‘linux-node2.example.com‘ state.sls nginx.install env=prod
ADD_HOST_PORT="8080"
}
deploy_code(){
echo "deploy code ok"
}
service_check(){
STATUS=$(curl -s --head http://"$ADD_HOST":"$ADD_HOST_PORT"/ | grep ‘200 OK‘)
if [ -n "$STATUS" ];then
echo "ok"
else
echo "not ok"
exit
fi
}
etcd_key(){
ADD_HOST=$1
curl http://10.0.0.7:2379/v2/keys/salt/haproxy/backend_www_oldboyedu_com/$ADD_HOST -XPUT -d value="10.0.0.8:${ADD_HOST_PORT}"
}
sync_state(){
salt ‘linux-node1.example.com‘ state.sls cluster.haproxy-outside env=prod
}
main(){
create_host;
deploy_service;
deploy_code;
etcd_key $MAIN_ADD_HOST;
sync_state;
}
main $1
老男孩网址:http://www.etiantian.org
qq:406564728
欢迎交流