案例:當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
然後進行檢視節點狀態以及個數: