saltstack实现自动化扩容
案例:当nginx的并发达到3000,并持续了一段时间时,通过自动化创建一台虚拟机,部署应用最后添加到集群提供服务:
zabbix监控(nginx并发量)-------》action-------》创建了一台主机/docker容器-------》部署服务--------》部署应用代码-------》测试状态--------》加入到集群---------》加入监控----------》通知
简单实现上面中的某些步骤:为集群添加一个后端节点以提供服务
为了实现上面功能,这里采用salstack+etcd
安装etcd:
[root@node1 src]# tar xf etcd-v3.2.9-linux-amd64.tar.gz cd etcd-v3.2.9-linux-amd64 cp etcd etcdctl /usr/local/bin/
然后开启etcd集群:
1、首先创建数据目录:mkdir /data/etcd -p
2、开启服务:
nohup etcd --name auto_scale --data-dir /data/etcd/ --listen-peer-urls http://192.168.44.134:2380,http://192.168.44.134:7001 --listen-client-urls http://192.168.44.134:2379,http://192.168.44.134:4001 --advertise-client-urls http://192.168.44.134:2379,http://192.168.44.134:4001 &
[root@node1 ~]# netstat -tunlp|grep etcd tcp 0 0 192.168.44.134:2379 0.0.0.0:* LISTEN 52094/etcd tcp 0 0 192.168.44.134:2380 0.0.0.0:* LISTEN 52094/etcd tcp 0 0 192.168.44.134:7001 0.0.0.0:* LISTEN 52094/etcd tcp 0 0 192.168.44.134:4001 0.0.0.0:* LISTEN 52094/etcd
1、创建一个key/value
[root@node1 ~]# curl -s http://192.168.44.134:2379/v2/keys/key1 -XPUT -d value="Hello world"
2、获取创建的key/value
[root@node1 ~]# curl -s http://192.168.44.134:2379/v2/keys/salt/haproxy/backend_www/www1
3、删除创建的key/value
[root@node1 ~]# curl -s http://192.168.44.134:2379/v2/keys/key1 -XDELETE
或者将上面的输出结果以json格式输出:
[root@node1 ~]# curl -s http://192.168.44.134:2379/v2/keys/salt/haproxy/backend_www/www1|python -m json.tool { "action": "get", "node": { "createdIndex": 9, "key": "/salt/haproxy/backend_www/www1", "modifiedIndex": 9, "value": "192.168.44.134:8080" } }
将etcd配置在saltstack中,结合使用:
1、首先需要安装依赖包:
yum install python-pip
pip install python-etcd
2、将etcd配置在salt中:在master配置文件中设置
####config etcd my_etcd_config: etcd.host: 192.168.44.134 etcd.port: 4001 ext_pillar: - etcd: my_etcd_config root=/salt/haproxy
3、重启master
[root@node1 ~]# /etc/init.d/salt-master restart
结尾一直报错:
查找原因发现salt是python2.7安装的,而etcd是python2.6安装的,所以一直调用出错:
于是使用python2.7安装一遍etcd服务:
1、mv /usr/bin/python /usr/bin/python.bak 2、ln -s /usr/bin/python2.7 /usr/bin/python 3、修改/usr/bin/yum第一行为:#!/usr/bin/python2.6 4、wget https://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg --no-check-certificate 5、chmod +x setuptools-0.6c11-py2.7.egg 6、sh setuptools-0.6c11-py2.7.egg 7、wget https://pypi.python.org/packages/source/p/pip/pip-1.3.1.tar.gz --no-check-certificate 8、tar xf pip-1.3.1.tar.gz 9、cd pip-1.3.1 10、python setup.py install 11、/usr/bin/pip-2.7 install python-etcd
试着使用python2.7导入模块etcd,报错如下:
解决办法:
https://pypi.python.org/pypi/backports.ssl_match_hostname tar xf backports.ssl_match_hostname-3.5.0.1.tar.gz cd backports.ssl_match_hostname-3.5.0.1 python setup.py build python setup.py install
然后重启salt-master服务,查看设置的pillar:
当删除掉etcd中的key时,那么pillar中的item也将会被删除掉:
[root@node1 ~]# curl -s http://192.168.44.134:2379/v2/keys/salt/haproxy/backend_www/www1 -XDELETE {"action":"delete","node":{"key":"/salt/haproxy/backend_www/www1","modifiedIndex":12,"createdIndex":11},"prevNode":{"key":"/salt/haproxy/backend_www/www1","value":"192.168.44.134:8080","modifiedIndex":11,"createdIndex":11}}
[root@node1 ~]# salt '*' pillar.items node2: ---------- backend_www: ---------- zabbix-agent: ---------- Zabbix_Server: 192.168.44.134 zabbix-agent-host: ---------- zabbix_host: node2
现在通过添加etcd的key来增加haproxy后端的节点服务器:
1、设置etcd的key
curl -s http://192.168.44.134:2379/v2/keys/salt/haproxy/backend_www/www1 -XPUT -d value="192.168.44.134:8081"|python -m json.tool
2、查看pillar
[root@node1 ~]# salt '*' pillar.items node1: ---------- backend_www: ---------- www1: 192.168.44.134:8081
3、修改haproxy配置文件:vim /srv/salt/prod/cluster/files/haproxy-outside.cfg
{% for www,www_ip in pillar.backend_www.iteritems() %} server {{ www }} {{ www_ip }} check inter 1000 {% endfor %}
4、修改haproxy状态配置文件:vim /srv/salt/prod/cluster/haproxy-outside.sls
haproxy-service: file.managed: - name: /etc/haproxy/haproxy.cfg - source: salt://cluster/files/haproxy-outside.cfg - user: root - group: root - mode: 644 - template: jinja 新增一行,使用jinja模板,使用变量
测试并验证:
由于etcd仅仅只是设置了一个key:
www1:192.168.44.134:8081
所以后端只有一个节点:
现在为haproxy后端新增节点www2和www3:
[root@node1 ~]# curl -s http://192.168.44.134:2379/v2/keys/salt/haproxy/backend_www/www2 -XPUT -d value="192.168.44.134:8080"|python -m json.tool { "action": "set", "node": { "createdIndex": 14, "key": "/salt/haproxy/backend_www/www2", "modifiedIndex": 14, "value": "192.168.44.134:8080" } } [root@node1 ~]# curl -s http://192.168.44.134:2379/v2/keys/salt/haproxy/backend_www/www3 -XPUT -d value="192.168.44.135:8080"|python -m json.tool { "action": "set", "node": { "createdIndex": 15, "key": "/salt/haproxy/backend_www/www3", "modifiedIndex": 15, "value": "192.168.44.135:8080" } }
查看设置的pillar:
[root@node1 ~]# salt '*' pillar.items node2: ---------- backend_www: ---------- www1: 192.168.44.134:8081 www2: 192.168.44.134:8080 www3: 192.168.44.135:8080
执行salt状态配置文件:
添加完成后,默认不会进行增加,需要执行状态配置文件(随着配置文件修改会reload服务)
salt '*' state.highstate
然后进行查看节点状态以及个数: