saltstack安裝etcd
###
saltstack安裝etcd
#################################################
[root@M01 salt]# mkdir -p /srv/salt/etcd/files/etcd
[root@M01 salt]# cd /srv/salt/etcd/files/etcd
[root@M01]# tar xf etcd-v3.1.14-linux-arm64.tar.gz
[root@M01]# mv etcd-v3.1.14-linux-amd64/etcd /srv/salt/etcd/files/etcd
[root@M01]# \rm -rf etcd-v3.1.14-linux-amd64 etcd-v3.1.14-linux-amd64.tar.gz
#查看下etcd目錄結構
[root@M01 files]# tree /srv/salt/prod/etcd/
/srv/salt/etcd/
├── etcd-install.sls
└── files
└── etcd
├── etcd
└── etcdctl
#salt etcd主配置文件
[root@M01 etcd]# cat /srv/salt/etcd/etcd-install.sls
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
#客戶端啟動進程
[root@WEB01 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‘ &
#查看一下調用的端口
[root@WEB01 ~]# 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
[root@M01 etcd]# yum install python-pip -y
[root@M01 etcd]# pip install python-etcd
#添加如下配置文件
[root@M01 ~]# 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
[root@M01 ~]# /etc/init.d/salt-master restart
#創建key web01
[root@M01 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
[root@M01 salt]# salt ‘WEB0?‘ pillar.items
WEB02:
backend_www_yehaixiao_com:
----------
web01:
192.168.44.7:8080
#修改cfg的配置文件
[root@M01 ~]# 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模板
[root@M01 ~]# 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
[root@M01 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值
[root@M01 ~]# 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服務
[root@M01 salt]# salt ‘WEB0?‘ state.sls cluster.haproxy-outside
#刪除key web02
[root@M01 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值
[root@M01 ~]# salt ‘WEB02‘ pillar.items
WEB02:
backend_www_yehaixiao_com:
web01:
192.168.44.7:8080
#此時http://192.168.44.7:8888/haproxy-status 的狀態沒有變
#執行一下或重啟一下客戶端的haproxy服務
[root@M01 salt]# salt ‘WEB0?‘ state.sls cluster.haproxy-outside
#自動添加虛擬主機的腳本
[root@M01 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
#自動添加虛擬主機的方法
[root@M01 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}}
#查詢下自動添加的結果
[root@M01 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服務
[root@M01 salt]# salt ‘WEB0?‘ state.sls cluster.haproxy-outside
saltstack安裝etcd