#################################################
###
saltstack安装etcd
#################################################
[[email protected] salt]# mkdir -p /srv/salt/etcd/files/etcd
[[email protected] salt]# cd /srv/salt/etcd/files/etcd
[[email protected]]# tar xf etcd-v3.1.14-linux-arm64.tar.gz
[[email protected]]# mv etcd-v3.1.14-linux-amd64/etcd /srv/salt/etcd/files/etcd
[[email protected]]# mv etcd-v3.1.14-linux-amd64/etcdctl /srv/salt/etcd/files/etcd
[[email protected]]# \rm -rf etcd-v3.1.14-linux-amd64 etcd-v3.1.14-linux-amd64.tar.gz
#查看下etcd目录结构
[[email protected] files]# tree /srv/salt/prod/etcd/
/srv/salt/etcd/
├── etcd-install.sls
└── files
└── etcd
├── etcd
└── etcdctl
#salt etcd主配置文件
[[email protected] etcd]# cat /srv/salt/etcd/etcd-install.sls
etcd-install:
file.recurse:
- name: /usr/local/etcd
- source: salt://etcd/files/etcd
cmd.run: - names:
- chmod +x /usr/local/etcd/etcd
- chmod +x /usr/local/etcd/etcdctl
- ln -s /usr/local/etcd/etcd /usr/local/bin/etcd
- ln -s /usr/local/etcd/etcdctl /usr/local/bin/etcdctl
#客户端启动进程
[[email protected] etcd]# cat /usr/local/etcd/runport.sh
#!/bin/sh
nohup etcd --name atuo_scale --data-dir /data/tecd/ --listen-peer-urls ‘http://192.168.44.7:2380, http://192.168.44.7:7001‘ --listen-client-urls ‘http://192.168.44.7:2379, http://192.168.44.7:4001‘ --advertise-client-urls ‘http://192.168.44.7:2379, http://192.168.44.7:4001‘ &
#查看一下调用的端口
[[email protected] ~]# netstat -luntp|grep etcd
tcp 0 0 192.168.44.7:4001 0.0.0.0: LISTEN 21543/etcd
tcp 0 0 192.168.44.7:2379 0.0.0.0: LISTEN 21543/etcd
tcp 0 0 192.168.44.7:2380 0.0.0.0: LISTEN 21543/etcd
tcp 0 0 192.168.44.7:7001 0.0.0.0: LISTEN 21543/etcd
#设置一个message的key
curl -s http://192.168.44.7:2379/v2/keys/message -XPUT -d value="Hello world" |python -m json.tool
#下载message的key
curl -s http://192.168.44.7:2379/v2/keys/message |python -m json.tool
#删除message的key
curl -s http://192.168.44.7:2379/v2/keys/message -XDELETE |python -m json.tool
#设置一个message的key,60秒后自动删除
curl -s http://192.168.44.7:2379/v2/keys/message -XPUT -d value="Hello world" -d ttl=60 |python -m json.tool
[[email protected] etcd]# yum install python-pip -y
[[email protected] etcd]# pip install python-etcd
#添加如下配置文件
[[email protected] ~]# tail -6 /etc/salt/master
etcd_pillar_config:
etcd.host: 192.168.44.7
etcd.prot: 4001
ext_pillar:
- etcd: etcd_pillar_config root=/salt/haproxy
#重启master
[[email protected] ~]# /etc/init.d/salt-master restart
#创建key web01
[[email protected] etcd]# curl -s http://192.168.44.7:2379/v2/keys/salt/haproxy/backend_www_yehaixiao_com/web01 -XPUT -d value="192.168.44.7:8080" | python -m json.tool
[[email protected] salt]# salt ‘WEB0?‘ pillar.items
WEB02:
backend_www_yehaixiao_com:
----------
web01:
192.168.44.7:8080
#修改cfg的配置文件
[[email protected] ~]# cat /srv/salt/cluster/files/haproxy-outside.cfg
global
maxconn 100000
chroot /usr/local/haproxy
uid 99
gid 99
daemon
nbproc 1
pidfile /usr/local/haproxy/logs/haproxy.pid
log 127.0.0.1 local3 info
defaults
mode http
option http-keep-alive
maxconn 100000
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
listen status
mode http
bind 0.0.0.0:8888
stats enable
stats uri /haproxy-status
stats auth haproxy:saltstack
frontend frontend_www_yehaixiao_com
bind 192.168.44.91:80
mode http
option httplog
log global
default_backend backend_www_yehaixiao_com
backend backend_www_yehaixiao_com
option forwardfor header X-REAL-IP
option httpchk HEAD / HTTP/1.0
#balance source # 根据请求源IP,建议使用
balance roundrobin # 轮询,软负载均衡基本都具备这种算法
#server WEB01 192.168.44.7:8080 check inter 2000 rise 30 fall 15
#server WEB02 192.168.44.8:8080 check inter 2000 rise 30 fall 15
{% for web,web_ip in pillar.backend_www_yehaixiao_com.iteritems() %}
server {{web}} {{web_ip}} check inter 2000 rise 30 fall 15
{% endfor %}
#修改内容支持jinja模板
[[email protected] ~]# cat /srv/salt/cluster/haproxy-outside.sls
include:
- haproxy.haproxy-install
haproxy-service:
file.managed:
- name: /etc/haproxy/haproxy.cfg
- source: salt://cluster/files/haproxy-outside.cfg
- user: root
- group: root
- mode: 644
- template: jinja # 加这1行
service.running: - name: haproxy
- enable: True
- reload: True
- require:
- cmd: haproxy-install
- watch:
- file: haproxy-service
#登录后只有web01
http://192.168.44.7:8888/haproxy-status
#创建key web02
[[email protected] etcd]# curl -s http://192.168.44.7:2379/v2/keys/salt/haproxy/backend_www_yehaixiao_com/web02 -XPUT -d value="192.168.44.8:8080" | python -m json.tool
#获取一下WEB02的pillar值
[[email protected] ~]# salt ‘WEB02‘ pillar.items
WEB02:
backend_www_yehaixiao_com:
web01:
192.168.44.7:8080
web02:
192.168.44.8:8080
zabbix-agent:
----------
Zabbix_Server:
192.168.44.81
#执行一下或重启一下客户端的haproxy服务
[[email protected] salt]# salt ‘WEB0?‘ state.sls cluster.haproxy-outside
#删除key web02
[[email protected] etcd]# curl -s http://192.168.44.7:2379/v2/keys/salt/haproxy/backend_www_yehaixiao_com/web02 -XDELETE | python -m json.tool
#获取一下WEB02的pillar值
[[email protected] ~]# salt ‘WEB02‘ pillar.items
WEB02:
backend_www_yehaixiao_com:
web01:
192.168.44.7:8080
#此时http://192.168.44.7:8888/haproxy-status 的状态没有变
#执行一下或重启一下客户端的haproxy服务
[[email protected] salt]# salt ‘WEB0?‘ state.sls cluster.haproxy-outside
#自动添加虚拟主机的脚本
[[email protected] cluster]# cat /srv/salt/cluster/auto_add_haproxy.sh
#!/bin/bash
MAIN_ADD_HOST=$1
create_host(){
echo ‘create host ok‘
}
deploy_service(){
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 ‘status check ok‘
else
echo ‘status check not ok‘
exit
fi
}
etcd_key(){
ADD_HOST=$1
curl http://192.168.44.7:2379/v2/keys/salt/haproxy/backend_www_yehaixiao_com/$ADD_HOST -XPUT -d value="192.168.44.7:$ADD_HOST_PORT"
}
sync_state(){
salt ‘WEB0?‘ state.sls cluster.haproxy-outside
}
main(){
create_host;
deploy_service;
deploy_code;
etcd_key $MAIN_ADD_HOST;
sync_state;
}
main $1
#自动添加虚拟主机的方法
[[email protected] cluster]# sh /srv/salt/cluster/auto_add_haproxy.sh web07
create host ok
deploy code ok
{"action":"set","node":{"key":"/salt/haproxy/backend_www_yehaixiao_com/web07","value":"192.168.44.7:8080","modifiedIndex":11,"createdIndex":11}}
#查询下自动添加的结果
[[email protected] salt]# salt ‘WEB0?‘ pillar.items
WEB01:
backend_www_yehaixiao_com:
----------
web01:
192.168.44.7:8080
web02:
192.168.44.8:8080
web06:
192.168.44.7:8080
web07:
192.168.44.7:8080
#执行一下或重启一下客户端的haproxy服务
[[email protected] salt]# salt ‘WEB0?‘ state.sls cluster.haproxy-outside
原文地址:http://blog.51cto.com/yehaixiao/2125107