QQ叫紫珊 发表于 2018-7-31 14:17:27

基于etcd加saltstack的自动化扩容

  # 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/
  # /etc/init.d/salt-master restart
  Stopping salt-master daemon:                               [确定]
  Starting salt-master daemon:                               [确定]
  此处设置一个key:
  # curl -s http://10.0.0.7:2379/v2/keys/salt/haproxy/backend_www_oldboyedu_com/web-node1 -XPUT -d value="10.0.0.7:8080" | python -m json.tool
  {
  "action": "set",
  "node": {
  "createdIndex": 15,
  "key": "/salt/haproxy/backend_www_oldboyedu_com/web-node1",
  "modifiedIndex": 15,
  "value": "10.0.0.7:8080"
  }
  }
  获取key:
  # salt '*' pillar.items
  linux-node1.example.com:
  ----------
  zabbix-agent:
  ----------
  Zabbix_Server:
  10.0.0.7
  linux-node2.example.com:
  ----------
  zabbix-agent:
  ----------
  Zabbix_Server:
  10.0.0.7
  此处没有获取到:
  查看日志:
  # tailf /var/log/salt/master
  CommandExecutionError: (unable to import etcd, module most likely not installed)             不能导入etcd,需要安装一个依赖包
  # yum install python-pip -y                                              需要安装python-pip
  # pip install python-etcd                                                       安装etcd软件
  # salt '*' pillar.items
  linux-node1.example.com:
  ----------
  backend_www_oldboyedu_com:
  ----------
  web-node1:
  10.0.0.7:8080
  zabbix-agent:
  ----------
  Zabbix_Server:
  10.0.0.7
  linux-node2.example.com:
  ----------
  backend_www_oldboyedu_com:
  ----------
  web-node1:
  10.0.0.7:8080
  zabbix-agent:
  ----------
  Zabbix_Server:
  10.0.0.7
  此处获取到值了:
  backend_www_oldboyedu_com:
  ----------
  web-node1:
  10.0.0.7:8080
  # 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 %}
  # vim /srv/salt/prod/cluster/haproxy-outside.sls            ====================>在文件里面指定为jinja模版
  - template: jinja
  修改之后执行高级模块:
  # salt '*' state.highstate
  # salt '*' pillar.items
  linux-node2.example.com:
  ----------
  backend_www_oldboyedu_com:
  ----------
  web-node1:
  10.0.0.7:8080
  zabbix-agent:
  ----------
  Zabbix_Server:
  10.0.0.7
  linux-node1.example.com:
  ----------
  backend_www_oldboyedu_com:
  ----------
  web-node1:
  10.0.0.7:8080
  zabbix-agent:
  ----------
  Zabbix_Server:
  10.0.0.7
  两个节点都获取到了key value:
  backend_www_oldboyedu_com:
  ----------
  web-node1:
  10.0.0.7:8080
  例子:
  # curl -s http://10.0.0.7:2379/v2/keys/salt/haproxy/backend_www_oldboyedu_com/web-node2 -XPUT -d value="10.0.0.7:8080" | python -m json.tool
  {
  "action": "set",
  "node": {
  "createdIndex": 16,
  "key": "/salt/haproxy/backend_www_oldboyedu_com/web-node2",
  "modifiedIndex": 16,
  "value": "10.0.0.7:8080"
  }
  }
  # curl -s http://10.0.0.7:2379/v2/keys/salt/haproxy/backend_www_oldboyedu_com/web-node3 -XPUT -d value="10.0.0.7:8080" | python -m json.tool
  {
  "action": "set",
  "node": {
  "createdIndex": 17,
  "key": "/salt/haproxy/backend_www_oldboyedu_com/web-node3",
  "modifiedIndex": 17,
  "value": "10.0.0.7:8080"
  }
  }
  # 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
  {
  "action": "set",
  "node": {
  "createdIndex": 18,
  "key": "/salt/haproxy/backend_www_oldboyedu_com/web-node4",
  "modifiedIndex": 18,
  "value": "10.0.0.7:8080"
  }
  }
  # curl -s http://10.0.0.7:2379/v2/keys/salt/haproxy/backend_www_oldboyedu_com/web-node5 -XPUT -d value="10.0.0.7:8080" | python -m json.tool
  {
  "action": "set",
  "node": {
  "createdIndex": 19,
  "key": "/salt/haproxy/backend_www_oldboyedu_com/web-node5",
  "modifiedIndex": 19,
  "value": "10.0.0.7:8080"
  }
  }
  # curl -s http://10.0.0.7:2379/v2/keys/salt/haproxy/backend_www_oldboyedu_com/web-node55 -XPUT -d value="10.0.0.7:8080" | python -m json.tool
  {
  "action": "set",
  "node": {
  "createdIndex": 34,
  "key": "/salt/haproxy/backend_www_oldboyedu_com/web-node55",
  "modifiedIndex": 34,
  "value": "10.0.0.7:8080"
  }
  }
  # salt '*' state.highstate                  =================>执行高级状态
  再次查看获取的数值:
  # salt '*' pillar.items
  linux-node1.example.com:
  ----------
  backend_www_oldboyedu_com:
  ----------
  web-node1:
  10.0.0.7:8080
  web-node2:
  10.0.0.7:8080
  web-node3:
  10.0.0.7:8080
  web-node4:
  10.0.0.7:8080
  web-node5:
  10.0.0.7:8080
  web-node55:
  10.0.0.7:8080
  zabbix-agent:
  ----------
  Zabbix_Server:
  10.0.0.7
  linux-node2.example.com:
  ----------
  backend_www_oldboyedu_com:
  ----------
  web-node1:
  10.0.0.7:8080
  web-node2:
  10.0.0.7:8080
  web-node3:
  10.0.0.7:8080
  web-node4:
  10.0.0.7:8080
  web-node5:
  10.0.0.7:8080
  web-node55:
  10.0.0.7:8080
  zabbix-agent:
  ----------
  Zabbix_Server:
  10.0.0.7
  此时在浏览器中查看已添加了N个节点
  老男孩网址:http://www.etiantian.org
  qq:406564728
  欢迎交流
页: [1]
查看完整版本: 基于etcd加saltstack的自动化扩容