80327065 2020-06-08
SaltStack是基于Python语言实现、结合轻量级消息队列与Python第三方模块构建的一款服务器基础架构集中化管理平台,主要功能为配置管理、远程执行、监控等。
我们可以通过部署SaltStack达到在成千万台服务器上批量执行命令的效果并可以根据不同业务进行配置集中化管理、分发文件、采集服务器数据、操作系统基础及软件包管理。
SaltStack有利于运维人员提高工作效率、规范业务配置与操作
准备两台Centos7虚拟机,配置IP地址和hostname,关闭防火墙和selinux,同步系统时间,配置IP地址和hostname映射
hostname | ip |
---|---|
master | 192.168.29.132 |
node | 192.168.29.134 |
两结点安装epel源
[ ~]# yum install epel-release -y [ ~]# yum install epel-release -y
master结点安装配置salt-master和salt-minion
[ ~]# yum install salt-master salt-minion -y [ ~]# vi /etc/salt/master interface: 0.0.0.0 file_roots: #设定调用文件的根目录 base: - /srv/salt #启动服务 [ ~]# systemctl start salt-master
node结点安装配置salt-minion
[ ~]# yum install salt-minion -y #修改配置文件 [ ~]# vi /etc/salt/minion master: 192.168.29.132 id: slave #启动服务 [ ~]# systemctl start salt-minion
密钥认证
#master查看密钥列表 [ ~]# salt-key -L Accepted Keys: Denied Keys: Unaccepted Keys: slave Rejected Keys: #master查看密钥匹配 [ ~]# salt-key -f slave #node结点请求密钥匹配 [ ~]# salt-call --local key.finger #master结点接收密钥 [ ~]# salt-key -a slave #再次查看密钥列表 [ ~]# salt-key -L #探测node结点主机状态 [ ~]# salt ‘slave‘ test.ping slave: True
saltstack架构部署完成
生成模块目录
[ ~]# tree /srv/ /srv/ └── salt ├── httpd │ ├── httpd.sls │ └── index.php ├── mysql │ ├── my.sh │ └── mysql.sls ├── php │ └── php.sls └── top.sls 4 directories, 6 files
编写httpd.sls
[ ~]# vi /srv/salt/httpd/httpd.sls #安装httpd httpd-install: pkg.installed: - name: httpd #传输本地主页文件到远端 httpd-index: file.managed: - name: /var/www/html/index.php - source: salt://httpd/index.php #开启服务 httpd-service: service.running: - name: httpd - enable: True
编写httpd主页文件
[ ~]# vi /srv/salt/httpd/index.php <?php phpinfo(); ?>
编写mysql.sls
[ ~]# vi /srv/salt/mysql/mysql.sls mysql-install: pkg.installed: - pkgs: - mariadb-server - mariadb mysql-service: service.running: - name: mariadb - enable: True #传送脚本并执行脚本 mysql-script: file.managed: - name: /tmp/my.sh - source: salt://mysql/my.sh cmd.run: - name: cd /tmp/ && chmod +x my.sh && ./my.sh
编写执行脚本
[ ~]# vi /srv/salt/mysql/my.sh #设置密码 mysqladmin password "your_password" #创建数据库 mysql -u root --password="your_password" -e "create database if not exists mydb"
编写php.sls
[ ~]# vi /srv/salt/php/php.sls php-install: pkg.installed: - pkgs: - php - php-gd - php-ldap - php-odbc - php-pear - php-xml - php-xmlrpc - php-mbstring - php-snmp - php-soap - curl - libcurl-devel - php-bcmath #重启httpd服务 httpd-stop: cmd.run: - name: systemctl restart httpd
编写top.sls
[ ~]# vi /srv/salt/top.sls base: ‘slave‘: - httpd.httpd - mysql.mysql - php.php
执行top.sls
[ ~]# salt ‘slave‘ state.highstate #执行成功显示如下 Succeeded: 9 Failed: 0
查看数据库
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mydb | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.01 sec)
浏览器访问http://node
自此saltstack部署LAMP架构完成