一. etcd服务的安装和使用
1.安装etcd应用:
wget https://github.com/coreos/etcd/releases/download/v2.2.5/etcd-v2.2.5-linux-amd64.tar.gz -O etcd-v2.2.5-linux-amd64.tar.gz
tar -zxvf etcd-v2.2.5-linux-amd64.tar.g
cp etcd etcdctl /usr/local/bin/
2.启动etcd服务:
mkdir -p /data/etcd #创建数据存储目录
nohup etcd -name auto_scale --data-dir /data/etcd/ \
--listen-peer-urls 'http://172.16.1.211:2380,http://172.16.1.211:7001' \
--listen-client-urls 'http://172.16.1.211:2379,http://172.16.1.211:4001' \
--advertise-client-urls 'http://172.16.1.211:2379,http://172.16.1.211:4001' &
3.提交key到etcd中:
curl -s http://172.16.1.211:2379/v2/keys/message -XPUT -d value="hello world" | python -m json.tool #结果通过python的json模块转义输出,增加可读性。
4.获取刚才提交的key值:
curl -s http://172.16.1.211:2379/v2/keys/message | python -m json.tool
5.删除刚才提交的key:
curl -s http://172.16.1.211:2379/v2/keys/message -XDELETE | python -m json.tool
6.提交带10秒过期时间的key:
curl -s http://172.16.1.211:2379/v2/keys/ttl_use -XPUT -d value="hello world 1" -d ttl=10 | python -m json.tool
二. 实现Salt自动化让Haproxy扩容
1.配置salt的pillar连接etcd:
yum install python-pip
pip install python-etcd #安装python的etcd包
vim /etc/salt/master
#底部添加
etcd_pillar_config:
etcd.host: 172.16.1.211
etcd.port: 4001
ext_pillar:
- etcd: etcd_pillar_config root=/salt/haproxy/ #root参数是指定etcd里面的目录
/etc/init.d/salt-master restart
2.测试通过salt获取pillar:
curl -s http://172.16.1.211:2379/v2/keys/salt/haproxy/backend_www_wmj_com/web-node1 -XPUT -d value="172.16.1.213:8080" | python -m json.tool
salt '*' pillar.item
3.让salt模板自动添加haproxy的backend:
vim /srv/salt/prod/cluster/files/haproxy-outside.cfg
backend backend_www_example_com
option forwardfor header X-REAL-IP
option httpchk HEAD / HTTP/1.0
balance roundrobin
server web-node1 172.16.1.213:8080 check inter 2000 rise 30 fall 15
server web-node2 172.16.1.214:8080 check inter 2000 rise 30 fall 15
#使用for循环获取etcd的key值
{% for web,web_ip in pillar.backend_www_wmj_com.iteritems() %}
server {{ web }} {{ web_ip }} check inter 2000 rise 30 fall 15
{% endfor %}
4.添加一台haproxy的节点:
# curl -s http://172.16.1.211:2379/v2/keys/salt/haproxy/backend_www_wmj_com/web-node3 -XPUT -d value="172.16.1.215:8080" | python -m json.tool
# salt '*' state.sls cluster.haproxy-outside env=prod
5.简单的自动化扩容脚本:
#!/bin/bash
create_host(){
echo "create host"
}
deploy_service(){
salt '*' state.sls nginx.install env=prod
}
deploy_code(){
echo "deploy code ok"
}
service_check(){
STATUS=$(curl -s --head http://172.16.1.213:8080/ | grep '200 OK')
if [ -n "$STATUS" ];then
echo "HTTP ok"
else
echo "HTTP not ok"
exit 1
fi
}
etcd_key(){
curl -s http://172.16.1.211:2379/v2/keys/salt/haproxy/backend_www_wmj_com/web-node4 -XPUT -d value="172.16.1.213:8080"
}
sync_state(){
salt '*' state.sls cluster.haproxy-outside env=prod
}
main(){
create_host
deploy_service
deploy_code
service_check
etcd_key
sync_state
}
main
三.Salt-cloud实现Openstack管理
官方手册: https://docs.saltstack.com/en/latest/topics/cloud/openstack.html
安装环境:
172.16.1.211 salt-master服务器
172.16.1.240 openstack控制节点
ps: 下面都在salt-master操作
1.安装插件:
yum install salt-cloud python-libcloud
2.创建salt-cloud配置文件:
mkdir /etc/salt/cloud.providers.d/
vim /etc/salt/cloud.providers.d/openstack.conf
my-openstack-config:
# Set the location of the salt-master
#
minion:
master: 172.16.1.211
# Configure the OpenStack driver
#
identity_url: http://172.16.1.240:5000/v2.0/tokens
compute_name: nova
protocol: ipv4
compute_region: RegionOne
# Configure Openstack authentication credentials
#
user: demo
password: demo
# tenant is the project name
tenant: demo
driver: openstack
provider: openstack
# skip SSL certificate validation (default false)
insecure: false
3.查看镜像列表:
salt-cloud --list-images openstack #查看镜像列表
salt-cloud --list-size openstack #查看云主机类型
4.创建Salt-cloud虚拟机模板文件:
vim /etc/salt/cloud.profiles.d/web.conf
web-node: #虚拟机模板名称
provider: my-openstack-config #前面配置文件定义的
size: m1.tiny #云主机类型
image: cirros #镜像名称
ssh_key_file: /root/.ssh/id_rsa #公钥文件
ssh_key_name: mykey #密钥对名称
ssh_interface: private_ips
networks:
- fixed:
- 69200e49-0f8b-47b6-9bb5-2db9bca9a393 #网络的ID
minion: #下面是自动给虚拟机安装salt-minion并配置
master: 172.16.1.211
grains:
role: webserver-01
4.通过Salt-cloud创建Openstack虚拟机:
salt-cloud -p web-node web-test1 -l debug
# -p: 虚拟机模板名称
# web-test1: 创建虚拟机的名称
# -l debug: 打印debug