回顾:
1 安装etcd
[root@linux-node1 ~]# pip install python-etcd 安装etcd软件
2 修改salt-master的配置文件,加配置,并重新启动salt-master
[root@linux-node1 ~]# 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 添加节点
[root@linux-node1 ~]# 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模版标志
[root@linux-node1 ~]# vim /srv/salt/prod/cluster/haproxy-outside.sls
- template: jinja
5 修改配置文件
[root@linux-node1 ~]# 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 %}
脚本实现自动添加机器:
[root@linux-node1 ~]# 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
欢迎交流
本文出自 “linux” 博客,请务必保留此出处http://chenjisong.blog.51cto.com/7858025/1714341
原文地址:http://chenjisong.blog.51cto.com/7858025/1714341