執行的命令寫入mysqlhtml
[root@node1]#yum install MySQL-python -ynode
[root@node1]#vim /etc/salt/masterpython
master_job_cache: mysql ###把master的job寫如mysql裏面 mysql.host: '10.240.17.103' ###mysql地址 mysql.user: 'salt' ###鏈接mysql用戶名 mysql.pass: 'salt' ###mysql的密碼 mysql.db: 'salt' ###鏈接mysql的庫
mysql.port: 3306 ###mysql的鏈接端口
[root@node1]#yum install mysql-servermysql
[root@node1]#/etc/init,d/mysqld startlinux
[root@node1]#mysql_secure_installation ##安全初始化nginx
第一次直接回車git
第二次是否爲root配置密碼 Y 輸入密碼github
第三次是否刪除匿名用戶 Yweb
第四次是否禁止root遠程登陸 Yredis
第五次是否刪除test數據庫 Y
第六次是否刷新受權表 Y
[root@node1]#mysql -u root -p 123.com
https://www.unixhot.com/docs/saltstack/ref/returners/all/salt.returners.mysql.html#module-salt.returners.mysql ##官網地址 CREATE DATABASE `salt` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci; USE `salt`; -- -- Table structure for table `jids` -- DROP TABLE IF EXISTS `jids`; CREATE TABLE `jids` ( `jid` varchar(255) NOT NULL, `load` mediumtext NOT NULL, UNIQUE KEY `jid` (`jid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE INDEX jid ON jids(jid) USING BTREE; -- -- Table structure for table `salt_returns` -- DROP TABLE IF EXISTS `salt_returns`; CREATE TABLE `salt_returns` ( `fun` varchar(50) NOT NULL, `jid` varchar(255) NOT NULL, `return` mediumtext NOT NULL, `id` varchar(255) NOT NULL, `success` varchar(10) NOT NULL, `full_ret` mediumtext NOT NULL, `alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, KEY `id` (`id`), KEY `jid` (`jid`), KEY `fun` (`fun`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- Table structure for table `salt_events` -- DROP TABLE IF EXISTS `salt_events`; CREATE TABLE `salt_events` ( `id` BIGINT NOT NULL AUTO_INCREMENT, `tag` varchar(255) NOT NULL, `data` mediumtext NOT NULL, `alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, `master_id` varchar(255) NOT NULL, PRIMARY KEY (`id`), KEY `tag` (`tag`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; grant all on salt.* to salt@10.240.17.103 identified by 'salt';
[root@node1]#/etc/init.d/salt-master restart
[root@node1]#mysql -h 10.240.17.103 -u salt -p salt
use salt; select * from salt_returns; [root@node1]#salt '*' test.ping select * from salt_returns; ##發現salt-master的命令已經存入數據庫
https://www.unixhot.com/docs/saltstack/topics/targeting/globbing.html salt-master平常操做方法
salt '*' network.active_tcp ###獲取網絡信息 salt '*' network.arp ###獲取ARP信息 salt '*' network.connect archlinux.org 80 ###連通性測試 到這個域名的80端口 https://www.unixhot.com/docs/saltstack/ref/modules/all/salt.modules.network.html#module-salt.modules.network
salt 'node1' state.show_top ##查看node1的都有那些狀態
include: ##引用web.下的httpd.sls來作配置
- web.httpd
###############################################
salt-run manage.status #####作狀態管理
salt- run manage.versions ###查看版本是什麼
################################################
自動化安裝zabbix-agent
[root@node1 base]#mkdir init zabbix logstash
[root@node1 base]#cd init && mkdir files &&cd files
[root@node1 files]# wget http://mirrors.aliyun.com/repo/epel-7.repo
[root@node1 init]# vim yum_repo.sls
/etc/yum.repos.d/epel-7.repo: ##狀態名 直接是路徑名 file.managed: - source: salt://init/files/epel-7.repo - user: root - group: root - mode: 644
[root@node1 base]#cd zabbix && mkdir files && vim zabbix-agent.sls
include:
- init.yum_repo
zabbix-agent: pkg.installed: - name: zabbix22-agent
- require:
- file: /etc/yum.repos.d/epel-7.repo ##依賴這個名稱ID
file.managed: - name: /etc/zabbix/zabbix_agentd.conf - source: salt://zabbix/files/zabbix_agentd.conf - user: root - group: root - mode: 644 - template: jinja ###變成jinja模版 - defaults: ## 聲明如下變量 ZABBIX_SERVER: 10.240.17.103
AGENT_HOSTNAME: {{ grains['fqdn'] }} ###經過grains來獲取主機名
- require: ###依賴與zabbix-agent,若是安裝成功才執行file.managed
- pkg: zabbix-agent
service.running: ######狀態管理
- name: zabbix-agent
- enable: True
- watch: ###依賴於
- file: zabbix-agent ##文件變動就重啓
- pkg: zabbix-agent ##包變動就重啓
zabbix_agentd.conf.d:
file.directory:
- name: /etc/zabbix/zabbix_agentd.conf.d
- watch_in:
- service: zabbix-agent
- require:
- pkg: zabbix-agent ##依賴包 zabbix-agent
- file: zabbix-agent ##依賴文件zabbix-agent
[root@node1 zabbix]#sz /etc/zabbix_agentd.conf
[root@node1 files]#vim zabbix_agentd.conf
Server={{ ZABBIX_SERVER }}
Hostname={{ AGENT_HOSTNAME }}
INCLUDE=/etc/zabbix_agentd.conf.d/
[root@node1 prod]# mkdir -p modules/{apache,haproxy,keeplived,mysql,redis}
[root@node1 prod]# mkdir redis-cluster
[root@node1 prod]# cd modules && cd redis
[root@node1 redis]# vim redis-install.sls ######redis安裝
redis-install: pkg.installed: - name: redis
[root@node1 redis-cluster]# mkdir files && vim redis-master.sls
include: - modules.redis.redis-install redis-master-config: file.managed: - name: /etc/redis.conf - source: salt://redis-cluseter/files/redis-master.conf - user: root - group: root - mode: 644 - template: jinja - defaults: REDIS_MEM: 1G redis-master-reivice: service.running: - name: redis - enable: True - wath: - file: redis-master-config
[root@node1 redis-cluster]# cd files && sz redis.conf && mv redis.conf redis-master.conf
[root@node1 files]# vim redis-master.conf
61 bind 0.0.0.0
128 daemonize yes
537 maxmemory {{ REDIS_MEM }}
[root@node1 prod]#salt 'node1' state.sls redis-cluster.redis-master test=True saltenv=prod
redis-cli -h 10.240.17.100
https://github.com/unixhot/saltbook-code/tree/master/salt/prod/keepalived
salt-ssh配置
[root@node1 ~]#yum install salt-ssh -y
[root@node1 ~]#vim /etc/salt/roster
node1: host: 10.240.17.100 user: root passwd: 123.com port: 22 node2: host: 10.240.17.103 user: root passwd: 123.com port: 22
[root@node1 ~]#salt-ssh '*' test.ping -i
[root@node1 ~]#salt-ssh '*' -r 'w'
https://www,unixhot.com/docs/saltstack/ref/netapi/all/salt.netapi.rest_cherrypy.html#a--rest-api-for-salt
#########salt-api
[root@node1 ~]#yum install salt-api
[root@node1 ~]# rpm -qa |grep cherry
[root@node1 ~]# yum install pyOpenSSL
[root@node1 ~]# salt-call --local tls.create_self_signed_cert
[root@node1 ~]# vim /etc/salt/master
12 default_include: master.d/*.conf
[root@node1 salt]#mkdir master.d && master.d
[root@node1 master.d]vim api.conf
rest_cherrypy: host: 10.240.17.103 port: 8000 ssl_crt: /etc/pki/tls/certs/localhost.crt ssl_key: /etc/pki/tls/certs/localhost.key
[root@node1 salt]#useradd -M -s /sbin/nologin saltapi ##不建立家目錄
[root@node1 salt]#echo "saltapi" |passwd saltapi --stdin ##建立非交互的密碼
[root@node1 master.d]#vim auth.conf
external_auth: pam: saltapi: - .* - '@wheel' - '@runner' - '@jobs'
[root@node1 salt]# /etc/init.d/salt-master restart
[root@node1 salt]#/etc/init.d/salt-api restart
[root@node1 salt]# netstat- ntlp |grep 8000
[root@node1 salt] #curl -sSk https://10.240.17.103:8000/login \
-H 'Accept: application/x-yaml' \
-d username='saltapi' \
-d password='saltapi' \
-d eauth='pam'
[root@node2 ~]# curl -sSk https://10.240.17.103:8000 -H 'Accept: application/x-yaml' -H 'X-Auth-Token: 3eb37a72cc8ba6ade1be9bb73753514e746658c4' -d client=local -d tgt='*' -d fun=test.ping
[root@node2 ~]# curl -sSk https://10.240.17.103:8000 -H 'Accept: application/x-yaml' -H 'X-Auth-Token: 3eb37a72cc8ba6ade1be9bb73753514e746658c4' -d client=local -d tgt='*' -d fun=cmd.run -d arg='uptime'
########salt-master 集羣架構
[root@node1]#vim /etc/master
master:
- 10.240.17.100
- 10.240.17.103
[root@node2]# yum install salt-master -y
[root@node1]#sz /etc/salt/master 傳到salt2 /etc/salt/
[root@node1]#yum install nfs-utils -y
[root@node1]#vim /etc/export
/etc/salt/pki/master 10.240.17.100 *(rw,sync,no_root_squash,no_all_squash)
/srv/salt 10.240.17.100 *(rw,sync,no_root_squash,no_all_squash)
[root@node1]# /etc/init.d/nfs start
[root@node2]#showmount -e 10.240.17.103
[root@node2]#mkdir /etc/salt/pki/master
[root@node2]#mkdir /srv/salt
[root@node2]#mount -t nfs 10.240.17.103:/etc/salt/pki/master /etc/salt/pki/master
[root@node2]#mount -t nfs 10.240.17.103:/srv/salt /srv/salt
[root@node2]# systemctl start salt-master
1.master配置文件同樣 2.master file_roots同樣 3.master公鑰和私鑰同樣4.修改salt-minion配置,設置2個master並重啓 5.在另一臺master上贊成設置
altStack生產實踐建議 1.不建議使用Salt的File模塊 目錄管理 進行代碼部署 命令編排的狀態管理 壓縮包,file.managed cmd.run 執行部署 2.配置管理,不建議使用salt管理項目代碼的配置文件。 分層管理,salt只管理服務例如Nginx Tomcat Apache 3.若是你有固定的文件服務器,可使用source: salt:// http:// ftp:// 4.SLS版本化 1.建立一個git項目。 2.找一個測試環境,編寫SLS,測試,git commit && git push到版本倉庫 3.生產環境git pull,測試。所有執行。 1.誰 什麼時間 幹了什麼 輸出是什麼 5. 使用Master Job Cache保存job的輸出到SQL 停機維護: 1.登陸到xxx機器 檢查:執行hostname檢查主機 執行ifconfig檢查ip地址 2.執行xxx。關閉xxx 檢查:執行netstat -ntlp | grep 80檢查端口 執行ps aux | grep nginx檢查進程 SaLtStack 二次開發 1.Master Job cache將全部的job輸出保存到MySQL 2.若是作管理平臺,能夠將User id和Jid作關聯 3.使用List作目標選擇 回憶過去: 1.SLS YAML ID NAME 2.狀態模塊 pkg file service cmd 3.狀態間關係 require require_in watch watch_in unless 4.include minion_id怎麼改 1.中止minion服務 2.salt-key -d minionid 刪除minion 3.rm -f /etc/salt/minion_id 4.rm -rf /etc/salt/pki 5.修改配置文件id: 6.啓動minion. 系統初始化: 1.DNS file.managed 2.防火牆 service.dead 3.selinux file.managed 4.limit設置 file.managed 5.SSH useDNS設置,改端口 file.managed 6.systctl 內核參數調優 systctl 7.關閉不須要的服務 service 8.時間同步 file.managed cron 9.基礎軟件包 pkg.installed 10.yum源 file.managed 生產環境按項目走 modules/ 基礎狀態 - files redis-cluster 公用服務 - files sms/ - redis-cluster/redis-master.sls redis-slave.sls - mysql-cluster/mysql-master.sls mysql-slave.sls - nginx shop/ user/