工欲善其事必先利其器,这篇是将武器(saltstack)进行实战。
一、实战架构图:
二、实验环境设置:(这里我仅仅使用master安装相关软件)
主机名 | IP地址 | 角色 |
---|---|---|
linux-node1.example.com | x.x.x.x | Master |
linux-node2.example.com | x.x.x.x | Minion、Memcached、Haproxy、Keepalived、Nginx+PHP |
三、SaltStack环境设置:
base环境用于存放初始化的功能,prod环境用于放置生产的配置管理功能
[root@linux-node1 ~]# vim /etc/salt/master
file_roots:
base:
- /srv/salt/base
dev: #develop 开发环境
- /srv/salt/dev
test: #测试环境
- /srv/salt/test
prod: #production 生产环境
- /srv/salt/prodpillar_roots:
base:
- /srv/pillar/base
prod:
- /srv/pillar/prod
四、系统初始化
当我们的服务器上架并安装好操作系统后,都会有一些基础的操作,所以生产环境中使用SaltStack,建议将所有服务器都会涉及的基础配置或者软件部署归类放在base环境下。此处,在base环境下创建一个init目录,将系统初始化配置的sls均放置到init目录下,称为“初始化模块”。
(4.1)需求分析和模块识别
初始化内容 | 模块使用 | 文件 |
---|---|---|
关闭SElinux | file.managed | /etc/selinux/config |
关闭默认firewalld | service.disabled | |
时间同步 | pkg.installed | |
文件描述符 | file.managed | /etc/security/limits.conf |
内核优化 | sysctl.present | |
SSH服务优化 | file.managed、service.running | |
精简开机系统服务 | service.dead | |
DNS解析 | file.managed | /etc/resolv.conf |
历史记录优化history | file.append | /etc/profile |
设置终端超时时间 | file.append | /etc/profile |
配置yum源 | file.managed | /etc/yum.repo.d/epel.repo |
安装各种agent | pkg.installed 、file.managed、service.running | |
基础用户 | user.present、group.present | |
常用基础命令 | pkg.installed、pkgs | |
用户登录提示、PS1的修改 | file.append | /etc/profile |
(4.2)需求实现
[root@linux-node1 base]# pwd
/srv/salt/base
[root@linux-node1 base]# mkdir init/files -p1、关闭selinux
#使用了file模块的managed方法
[root@linux-node1 init]# vim selinux.sls
selinux-config: #设定ID,只是一个标识而已
file.managed: #使用file模块,managed的方法
- name: /etc/selinux/config #设定文件推送的目的路径,minion上的路径
- source: salt://init/files/selinux-config #设定要把哪个文件发送到被控机
- user: root #设定被同步文件的所有者,默认是minion上运行salt的用户
- group: root #设定被同步文件的所属组,默认是minion上运行salt的组
- mode: 0644 #设置被同步的属性,只有:0644,0775,4664
[root@linux-node1 init]# cp /etc/selinux/config files/selinux-config2、关闭firewalld
#使用service模块的dead方法,直接关闭firewalld,并禁止开机启动
[root@linux-node1 init]# vim firewalld.sls
firewall-stop:
service.dead: #确保服务处于未运行状态
- name: firewalld.service #运行的服务名
- enable: False #运行状态关闭3、时间同步
#先使用pkg模块安装ntp服务,再使用cron模块加入计划任务
[root@linux-node1 init]# vim ntp.sls
ntp-install:
pkg.installed: #安装软件包
- name: ntpdate #软件包名称cron-ntpdate:
cron.present: #计划任务
- name: ntpdate time1.aliyun.com #计划任务内容
- user: root #执行的所有者权限
- minute: 5 #计划任务的周期4、修改文件描述符
#使用file模块的managed方法
[root@linux-node1 init]# vim limit.sls
limit-config:
file.managed: #使用file模块,managed的方法
- name: /etc/security/limits.conf #设定文件推送的目的路径,minion上的路径
- source: salt://init/files/limits.conf #设定要把哪个文件发送到被控机
- user: root #设定被同步文件的所有者,默认是minion上运行salt的用户
- group: root #设定被同步文件的所属组,默认是minion上运行salt的组
- mode: 0644 #设置被同步的属性,只有:0644,0775,4664
[root@linux-node1 init]# cp /etc/security/limits.conf files/
5、内核优化
#使用sysctl模块的present方法,此处演示一部分,这里没有使用name参数,所以id就相当于是name
[root@linux-node1 init]# vim sysctl.sls
net.ipv4.tcp_fin_timeout: #对象
sysctl.present: #赋值
- value: 2net.ipv4.tcp_tw_reuse:
sysctl.present:
- value: 1net.ipv4.tcp_tw_recycle:
sysctl.present:
- value: 1net.ipv4.tcp_syncookies:
sysctl.present:
- value: 1net.ipv4.tcp_keepalive_time:
sysctl.present:
- value: 6006、SSH服务优化
#使用file.managed和service.running以及watch,对ssh服务进行优化配置
[root@linux-node1 init]# vim sshd.sls
sshd-config:
file.managed:
- name: /etc/ssh/sshd_config
- source: salt://init/files/sshd_config
- user: root
- gourp: root
- mode: 0600
service.running:
- name: sshd
- enable: True
- reload: True
- watch:
- file: sshd-config
[root@linux-node1 init]# cp /etc/ssh/sshd_config files/
[root@linux-node1 init]# vim files/sshd_config
Port 8022
UseDNS no
PermitRootLogin no
PermitEmptyPasswords no
GSSAPIAuthentication no7、精简开机启动的系统服务
#举例关闭postfix开机自启动
[root@linux-node1 init]# vim thin.sls
postfix:
service.dead:
- enable: False8、DNS解析
[root@linux-node1 init]# vim dns.sls
dns-config:
file.managed:
- name: /etc/resolv.conf
- source: salt://init/files/resolv.conf
- user: root
- group: root
- mode: 644
[root@linux-node1 init]# cp /etc/resolv.conf files/9、历史记录优化history
#使用file.append扩展修改HISTTIMEFORMAT的值
[root@linux-node1 init]# vim history.sls
history-config:
file.append:
- name: /etc/profile
- text:
- export HISTTIMEFORMAT="%F %T `whoami` "
- export HISTSIZE=5
- export HISTFILESIZE=510、设置终端超时时间
#使用file.append扩展修改TMOUT环境变量的值
[root@linux-node1 init]# vim tty-timeout.sls
ty-timeout:
file.append:
- name: /etc/profile
- text:
- export TMOUT=30011、配置yum源
#拷贝yum源
[root@linux-node1 init]# vim yum-repo.sls
/etc/yum.repos.d/epel.repo:
file.managed:
- source: salt://init/files/epel.repo
- user: root
- group: root
- mode: 064412、安装各种agent(如安装zabbix-agent)
#相当于一个软件的安装、配置、启动,此处也使用了jinja模板和pillar
[root@linux-node1 base]# mkdir zabbix
[root@linux-node1 base]# vim zabbix/zabbix-agent.sls
zabbix-agent:
pkg.installed:
- name: zabbix22-agent
file.managed:
- name: /etc/zabbix_agentd.conf
- source: salt://zabbix/files/zabbix_agentd.conf
- template: jinja
- defaults:
ZABBIX-SERVER: {{ pillar['zabbix-agent']['Zabbix_Server'] }}
- require:
- pkg: zabbix-agent
service.running:
- enable: True
- watch:
- pkg: zabbix-agent
- file: zabbix-agent
zabbix_agent.conf.d:
file.directory:
- name: /etc/zabbix_agentd.conf.d
- watch_in:
- service: zabbix-agent
- require:
- pkg: zabbix-agent
- file: zabbix-agent
[root@linux-node1 srv]# vim pillar/base/zabbix.sls
zabbix-agent:
Zabbix_Server: 192.168.56.1113、基础用户
#增加基础管理用户www,使用user.present和group.present
[root@linux-node1 init]# vim user-www.sls
www-user-group:
group.present:
- name: www
- gid: 1000user.present:
- name: www
- fullname: www
- shell: /sbin/bash
- uid: 1000
- gid: 100014、常用基础命令
#这里因为各软件包会依赖源,所以使用include将yum源包含进来,并在pkg.installed最后增加require依赖
[root@linux-node1 init]# vim pkg-base.sls
include:
- init.yum-repo
base-install:
pkg.installed:
- pkgs:
- screen
- lrzsz
- tree
- openssl
- telnet
- iftop
- iotop
- sysstat
- wget
- dos2unix
- lsof
- net-tools
- mtr
- unzip
- zip
- vim
- bind-utils
- require:
- file: /etc/yum.repos.d/epel.repo15、用户登录提示、PS1的修改
[root@linux-node1 init]# vim tty-ps1.sls
/etc/bashrc:
file.append:
- text:
- export PS1=' [\u@\h \w]\$ '16、编写一个总的状态,并写入top file中
#将所有初始化所需要的功能编写完成,每个小功能都是一个sls文件,统一放在init目录下。此时再使用include把这些初始化的功能都包含进来。
[root@linux-node1 init]# vim init-all.sls
include: #而include块的实现,可以使得一个sls文件引用其他sls文件,通常放在sls文件顶部。include作为一个顶级声明,不能在一个文件中出现多次。
- init.dns
- init.yum-repo
- init.firewalld
- init.history
- init.limit
- init.ntp
- init.pkg-base
- init.selinux
- init.sshd
- init.sysctl
- init.thin
- init.tty-timeout
- init.tty-ps1
- init.user-www
#在top.sls里面给Minion指定状态并执行,强烈建议先测试,确定SaltStack会执行哪些操作然后再应用状态到服务器上
[root@linux-node1 base]# vim top.sls
base:
'*':
- init.init-all
[root@linux-node1 base]# salt '*' state.highstate test=True #测试
[root@linux-node1 base]# salt '*' state.highstate
5、安装MySQL
5.1 需求实现:
(1)在prod生产环境下载创建modules和mysql目录
[root@linux-node1 prod]# pwd
/srv/salt/prod
[root@linux-node1 prod]# mkdir -p modules/mysql
(2)配置安装和配置状态文件install.sls
[root@linux-node1 mysql]# cat install.sls
mysql-install:
pkg.installed:
- pkgs:
- mariadb
- mariadb-server# salt '*' state.sls prod.modules.mysql.install test=True
6、安装HAproxy 注意空格
(1)pkg配置管理
[root@linux-node1 modules]# mkdir pkg
[root@linux-node1 pkg]# vim pkg-init.sls
pkg-init:
pkg.installed:
- names:
- gcc
- gcc-c++
- glibc
- make
- autoconf
- openssl
- openssl-devel
[root@linux-node1 pkg]# salt '*' state.sls prod.modules.haproxy.pkg-init test=True
(2)haproxy配置管理
[root@linux-node1 modules]# mkdir haproxy/files -p
[root@linux-node1 haproxy]# cat haproxy.sls
include:
- pkg.pkg-inithaproxy-install:
file.managed:
- name: /usr/local/src/haproxy-1.5.3.tar.gz
- source: salt://modules/haproxy/files/haproxy-1.5.3.tar.gz
- user: root
- group: root
- mode: 755
cmd.run:
- name: cd /usr/local/src && tar -zxvf haproxy-1.5.3.tar.gz && cd haproxy-1.5.3 && make TARGET=linux26 PREFIX=/usr/local/haproxy && make install PREFIX=/usr/local/haproxy
- unless: test -d /usr/local/haproxy
- require:
- pkg: pkg-init
- file: haproxy-install/etc/init.d/haproxy:
file.managed:
- source: salt://modules/haproxy/files/haproxy.init
- user: root
- group: root
- mode: 755
- require:
- cmd: haproxy-installnet.ipv4.ip_nonlocal_bind:
sysctl.present:
- value: 1haproxy-config-dir:
file.directory:
- name: /etc/haproxy
- mode: 755
- user: root
- group: roothaproxy-init:
cmd.run:
- name: chkconfig --add haproxy
- unless: chkconfig --list | grep haproxy
- require:
- file: /etc/init.d/haproxy
[root@linux-node1 haproxy]# cp /usr/local/src/haproxy-1.5.3.tar.gz files/
[root@linux-node1 haproxy]# cp /usr/local/src/haproxy-1.5.3/examples/haproxy.init files/
上述可见,使用saltstack配置是有规律可循的。
参看文章:
https://blog.51cto.com/13399294/2161036
https://blog.51cto.com/jinlong/2066276
http://www.anrs.net/content/13016