saltstack基本操做第一篇章

1、安裝saltstackhtml

1)官網安裝node

http://repo.saltstack.com/#rhel

  saltstack的模塊:   https://www.unixhot.com/docs/saltstack/ref/modules/all/python

 2)啓動服務mysql

安裝源,(全部機器)
yum install https://repo.saltstack.com/yum/redhat/salt-repo-latest.el7.noarch.rpm  -y

[root@k8s6 ~]# yum install salt-master salt-minion -y     # 安裝master和agent

[root@node01 ~]# yum install salt-minion -y            # 節點安裝agent

---
直接啓動master
[root@k8s6 ~]# systemctl start salt-master

在master修改salt-minion 配置文件
[root@k8s6 ~]# cd /etc/salt/
[root@k8s6 salt]# cp /etc/salt/minion /etc/salt/minion.bak
[root@k8s6 salt]# vim /etc/salt/minion
    master: 192.168.10.22    
[root@k8s6 salt]# systemctl start salt-minion    # 啓動agent
啓動會產生   minion_id 這個文件,內容默認爲主機名
[root@k8s6 salt]# cat minion_id
k8s6
---
節點服務器修改配置文件
[root@node01 salt]# vim /etc/salt/minion
    master: 192.168.10.22
[root@node01 salt]# systemctl start salt-minion

 3)祕鑰認證過程web

master和agent雖然都啓動了,但須要獲得master的承認才行
agent啓動後會產生祕鑰對
[root@node01 salt]# tree pki
pki
├── master
└── minion
    ├── minion.pem    私鑰
    └── minion.pub    公鑰

服務端的祕鑰對    
[root@k8s6 salt]# tree pki
pki
├── master
│   ├── master.pem    私鑰
│   ├── master.pub    公鑰
│   ├── minions
│   ├── minions_autosign
│   ├── minions_denied
│   ├── minions_pre        查看正在等待被管理的機器
│   │   ├── k8s6        
│   │   └── node01
│   └── minions_rejected
└── minion
    ├── minion.pem
    └── minion.pub
[root@k8s6 salt]# salt-key     # 列出全部的key
Accepted Keys:
Denied Keys:
Unaccepted Keys:
k8s6
node01
Rejected Keys:
[root@k8s6 salt]# salt-key -a k8s6    # 贊成
The following keys are going to be accepted:
Unaccepted Keys:
k8s6
Proceed? [n/Y] y
Key for minion k8s6 accepted.
[root@k8s6 salt]# salt-key         # 再次查看
Accepted Keys:
k8s6
Denied Keys:
Unaccepted Keys:
node01
Rejected Keys:
[root@k8s6 salt]# salt-key -a node*        #  使用通配符
The following keys are going to be accepted:
Unaccepted Keys:
node01
Proceed? [n/Y] y
Key for minion node01 accepted.

再次查看文件 ===》認證過程則是交換公鑰的過程
[root@k8s6 salt]# tree pki
pki
├── master
│   ├── master.pem
│   ├── master.pub
│   ├── minions
│   │   ├── k8s6
│   │   └── node01
│   ├── minions_autosign
│   ├── minions_denied
│   ├── minions_pre
│   └── minions_rejected
└── minion
    ├── minion_master.pub
    ├── minion.pem
    └── minion.pub
[root@node01 salt]# tree pki
pki
├── master
└── minion
    ├── minion_master.pub
    ├── minion.pem
    └── minion.pub
View Code

 2、執行命令sql

1) 直接命令行執行命令數據庫

[root@k8s6 ~]# salt '*' test.ping    # 查看機器在不在
k8s6:
    True
node01:
    True
[root@k8s6 ~]# salt 'node01' test.ping
node01:
    True
[root@k8s6 ~]# salt "node01" cmd.run "date"        執行命令
node01:
    Mon Mar 18 00:13:22 CST 2019
[root@k8s6 ~]# salt "node01" cmd.run "w"        新命令
node01:
     00:13:26 up 3 days,  5:17,  1 user,  load average: 0.00, 0.01, 0.05
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    192.168.10.22    Thu19    3days  0.10s  0.10s -bash
View Code

 2)以執行文本的形式去執行apache

2.1)修改配置服務vim

[root@k8s6 ~]# vim /etc/salt/master
#file_roots:
#  base:
#    - /srv/salt
===》改成
file_roots:
  base:
    - /srv/salt
vim /etc/salt/master

2.2)重啓服務bash

[root@k8s6 ~]# systemctl restart salt-master
[root@k8s6 ~]# mkdir -p /srv/salt/web

2.3)編輯apache.sls文件,安裝啓動httpd服務

[root@k8s6 ~]# cat /srv/salt/web/apache.sls
apache-install:
  pkg.installed:
    - names:
      - httpd
      - httpd-devel

apache-service:
  service.running:
    - name: httpd
    - enable: True
apache.sls

2.4)執行過程

[root@k8s6 ~]# salt '*' state.sls web.apache

執行中,發生的事情。master將編輯的文件發生給minion

[root@node01 ~]# cd /var/cache/salt/
[root@node01 salt]# tree
.
└── minion
    ├── extmods
    ├── files
    │   └── base
    │       └── web
    │           └── apache.sls
    └── proc
        └── 20190318150137078149

6 directories, 2 files
View Code

3)salt的高級狀態,指定哪臺機器執行什麼服務。(top file使用案例一)

3.1)編輯配置文件,讓k8s去執行web文件夾內的apache服務

[root@k8s6 ~]# cat /srv/salt/top.sls 
base:
  'k8s6':
    - web.apache

3.2)執行以前須要 檢測,防止出錯

[root@k8s6 ~]# salt 'k8s6' state.highstate test=True      # 先檢測
[root@k8s6 ~]# salt 'k8s6' state.highstate                # 再執行

3、salt的消息隊列

1)服務端監聽的端口

消息的發佈與訂閱
[root@k8s6 ~]# netstat -lntup|grep python
tcp        0      0 0.0.0.0:4505            0.0.0.0:*               LISTEN      20810/python           4505端口,消息發佈的監聽端口     
tcp        0      0 0.0.0.0:4506            0.0.0.0:*               LISTEN      20816/python
[root@k8s6 ~]# lsof -i:4505 -n
COMMAND     PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
salt-mini  4046 root   21u  IPv4 1964499      0t0  TCP 192.168.10.22:60516->192.168.10.22:4505 (ESTABLISHED)
salt-mast 20810 root   16u  IPv4 1960686      0t0  TCP *:4505 (LISTEN)
salt-mast 20810 root   18u  IPv4 1963647      0t0  TCP 192.168.10.22:4505->192.168.10.23:52670 (ESTABLISHED)
salt-mast 20810 root   19u  IPv4 1963648      0t0  TCP 192.168.10.22:4505->192.168.10.22:60516 (ESTABLISHED)

消息的返回值
[root@k8s6 ~]# lsof -i:4506 -n
COMMAND     PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
salt-mast 20816 root   24u  IPv4 1961475      0t0  TCP *:4506 (LISTEN)
View Code

2)安裝服務yum install python-setproctitle -y 。可查看具體進程跑的服務

[root@k8s6 ~]# ps aux|grep salt-master
root      2113  0.0  0.0 112724   992 pts/0    S+   17:52   0:00 grep --color=auto salt-master
root     20800  0.0  1.0 392220 41540 ?        Ss   14:13   0:01 /usr/bin/python /usr/bin/salt-master
root     20805  0.0  0.5 308908 20896 ?        S    14:13   0:00 /usr/bin/python /usr/bin/salt-master
root     20810  0.0  0.9 472932 35344 ?        Sl   14:13   0:00 /usr/bin/python /usr/bin/salt-master
root     20813  0.0  0.9 391320 35304 ?        S    14:13   0:00 /usr/bin/python /usr/bin/salt-master
root     20814  0.2  1.8 432108 73140 ?        S    14:13   0:27 /usr/bin/python /usr/bin/salt-master
root     20815  0.0  0.9 392088 35544 ?        S    14:13   0:00 /usr/bin/python /usr/bin/salt-master
root     20816  0.0  0.9 768944 36252 ?        Sl   14:13   0:00 /usr/bin/python /usr/bin/salt-master
root     20823  0.0  1.4 574448 54892 ?        Sl   14:13   0:00 /usr/bin/python /usr/bin/salt-master
root     20824  0.2  0.9 465952 36080 ?        Sl   14:13   0:28 /usr/bin/python /usr/bin/salt-master
root     20825  0.0  1.3 573184 53632 ?        Sl   14:13   0:00 /usr/bin/python /usr/bin/salt-master
root     20826  0.0  1.4 576084 56524 ?        Sl   14:13   0:01 /usr/bin/python /usr/bin/salt-master
root     20827  0.0  1.4 574816 55124 ?        Sl   14:13   0:00 /usr/bin/python /usr/bin/salt-master
root     20828  0.0  1.4 576896 57308 ?        Sl   14:13   0:01 /usr/bin/python /usr/bin/salt-master
[root@k8s6 ~]# yum install python-setproctitle -y
[root@k8s6 ~]# systemctl restart salt-master
[root@k8s6 ~]# ps aux|grep salt-master
root      2178  0.2  1.0 394288 41560 ?        Ss   17:53   0:00 /usr/bin/python /usr/bin/salt-master ProcessManager
root      2183  0.0  0.5 310976 20940 ?        S    17:53   0:00 /usr/bin/python /usr/bin/salt-master MultiprocessingLoggingQueue
root      2188  0.0  0.9 475004 35288 ?        Sl   17:53   0:00 /usr/bin/python /usr/bin/salt-master ZeroMQPubServerChannel
root      2191  0.0  0.8 393076 34812 ?        S    17:53   0:00 /usr/bin/python /usr/bin/salt-master EventPublisher
root      2192  0.3  1.2 410684 49456 ?        S    17:53   0:00 /usr/bin/python /usr/bin/salt-master Maintenance
root      2193  0.0  0.9 394156 35568 ?        S    17:53   0:00 /usr/bin/python /usr/bin/salt-master ReqServer_ProcessManager
root      2194  0.0  0.9 771012 36108 ?        Sl   17:53   0:00 /usr/bin/python /usr/bin/salt-master MWorkerQueue
root      2201  0.7  1.2 492756 50248 ?        Sl   17:53   0:00 /usr/bin/python /usr/bin/salt-master MWorker-0
root      2202  0.7  1.2 492764 50256 ?        Sl   17:53   0:00 /usr/bin/python /usr/bin/salt-master MWorker-1
root      2203  0.7  1.2 492768 50260 ?        Sl   17:53   0:00 /usr/bin/python /usr/bin/salt-master MWorker-2
root      2204  0.1  0.9 468020 35996 ?        Sl   17:53   0:00 /usr/bin/python /usr/bin/salt-master FileserverUpdate
root      2205  0.7  1.2 492768 50260 ?        Sl   17:53   0:00 /usr/bin/python /usr/bin/salt-master MWorker-3
root      2206  0.7  1.2 492768 50260 ?        Sl   17:53   0:00 /usr/bin/python /usr/bin/salt-master MWorker-4
root      3361  0.0  0.0 112724   992 pts/4    S+   17:54   0:00 grep --color=auto salt-master
yum install python-setproctitle -y

4、根據salt的組件角色來進行分組

1)根據內置組件判斷分組執行服務器的命令

[root@k8s6 ~]# salt 'k8s6' grains.ls        顯示全部的key
[root@k8s6 ~]# salt 'k8s6' grains.items        查看全部的key
[root@k8s6 ~]# salt 'k8s6' grains.item os    查看key裏面的值,如操做系統
[root@k8s6 ~]# salt -G 'os:CentOS' test.ping        目標選擇。根據操做系統來進行選擇執行的命令
k8s6:
    True
node01:
    True
[root@k8s6 ~]# salt -G 'os:CentOS' cmd.run 'echo hehe'
k8s6:
    hehe
node01:
    hehe
View Code

 2)自定義角色進行分組

[root@k8s6 ~]# vim /etc/salt/minion
#grains:
#  roles:
#    - webserver
#    - memcache
===》
grains:
  roles: apache
  [root@k8s6 ~]# salt '*' grains.item roles
k8s6:
    ----------
    roles:
node01:
    ----------
    roles:
[root@k8s6 ~]# 
[root@k8s6 ~]# systemctl restart salt-minion
[root@k8s6 ~]# salt '*' grains.item roles
k8s6:
    ----------
    roles:
        apache
node01:
    ----------
    roles:
View Code

執行命令,找到 有apache服務的,爲一個角色

[root@k8s6 ~]# netstat -lntup|grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      8279/httpd          
[root@k8s6 ~]# salt -G 'roles:apache' cmd.run 'systemctl stop httpd'
k8s6:
[root@k8s6 ~]# netstat -lntup|grep httpd
[root@k8s6 ~]# salt -G 'roles:apache' cmd.run 'systemctl start httpd'
k8s6:
[root@k8s6 ~]# 
View Code

 5、top案例使用方式二。根據角色來選擇

1)配置文件編寫,在服務端編寫角色

[root@k8s6 ~]# cat /srv/salt/top.sls
base:
  'k8s6':
    - web.apache
  'roles:apache':
    - match: grain      選擇匹配方式
    - web.apache

2)在nodes編寫角色

[root@node01 salt]# cat /etc/salt/grains
cloud: openstack

3)刷新出來角色(重啓或刷新)

[root@node01 salt]# systemctl restart salt-minion
master服務端獲取角色
[root@k8s6 ~]# salt '*' grains.item cloud
k8s6:
    ----------
    cloud:
node01:
    ----------
    cloud:
        openstack
================
能夠不用重啓,可刷新後,再獲取
[root@k8s6 ~]# salt '*' saltutil.sync_grains        服務端刷新
View Code

 6、開發一個grains。使用Python自定義item

1)腳本存放的固定位置文件夾

mkdir /srv/salt/_grains

2)Python腳本返回字典便可

[root@k8s6 ~]# cat /srv/salt/_grains/my_grains.py 
#!/usr/bin/env python
#-*- coding: utf-8 -*-

def my_grains():
    grains = {}
    grains["iaas"] = "openstack"
    grains["edu"] = "oldboy"
    return grains
my_grains.py

3)執行腳本同步命令

[root@k8s6 ~]# salt '*' saltutil.sync_grains
k8s6:
    - grains.my_grains
node01:
    - grains.my_grains
salt '*' saltutil.sync_grains

同步以後可在node節點中查看到該python腳本

[root@node01 ~]# cd /var/cache/salt/
[root@node01 salt]# tree
.
└── minion
    ├── accumulator
    ├── extmods
    │   └── grains
    │       ├── my_grains.py
    │       └── my_grains.pyc
    ├── files
    │   └── base
    │       ├── _grains
    │       │   └── my_grains.py
    │       ├── top.sls
    │       └── web
    │           └── apache.sls
    ├── highstate.cache.p
    ├── module_refresh
    ├── proc
    └── sls.p
cd /var/cache/salt/

查看效果

[root@k8s6 ~]# salt '*' grains.item iaas
k8s6:
    ----------
    iaas:
        openstack
node01:
    ----------
    iaas:
        openstack

 4)grians的優先級查找

Grians優先級
    1)系統自帶
    2)grains 文件寫的
    3)minion 配置文件寫的
    4)本身寫的

 7、另外一種動態配置管理pillar

1)啓動配置管理

[root@k8s6 ~]# salt "*" pillar.items    # 先查看,沒有內容
k8s6:
    ----------
node01:
    ----------
[root@k8s6 ~]# vim /etc/salt/master        # 修改配置
#pillar_opts: False
==》pillar_opts: True
[root@k8s6 ~]# systemctl restart salt-master
[root@k8s6 ~]# salt "*" pillar.items    # 再次查看,將出現不少值
salt "*" pillar.items

 展現的內容太多,不太好使用,仍是須要註釋掉

 2)使用過程

[root@k8s6 web]# cat /srv/pillar/web/apache.sls 
{% if grains['os']  == 'CentOS' %}
apache: httpd
{% elif grains['os'] == 'Debian' %}
apache: apache2
{% endif %}
[root@k8s6 srv]# cat /srv/pillar/top.sls 
base:
  'k8s6':
    - web.apache
[root@k8s6 srv]# tree pillar/
pillar/
├── top.sls
└── web
    └── apache.sls
[root@k8s6 ~]# salt '*' pillar.items apache
k8s6:
    ----------
    apache:
node01:
    ----------
    apache:
[root@k8s6 ~]# 
[root@k8s6 ~]# salt '*' saltutil.refresh_pillar        # 刷新
k8s6:
    True
node01:
    True
[root@k8s6 ~]# salt '*' pillar.items apache
k8s6:
    ----------
    apache:
        httpd
node01:
    ----------
    apache:
[root@k8s6 ~]# salt -I 'apache:httpd' test.ping    # 匹配到執行命令?? 
salt '*' pillar.items apache

 3)Grians和pillar的區別

Grians vs Pillar
            類型          數據採集方式            應用場景                    定義位置
Grians        靜態        minion啓動時收集    數據查詢,目標選擇,配置管理     minion
Pillar        動態        master自定義        目標選擇,配置管理,敏感數據     master

 8、執行命令解析

1)選擇目標機器的通配符

選擇目標機器的通配符
salt '*' test.ping
salt 'web01' test.ping
salt 'web0[1|2]' test.ping
salt 'web?.com' test.ping
salt 'web0[1-2]' test.ping
salt 'web0[!2]' test.ping
salt -L 'web01,web02' test.ping
salt -E 'web(01|02)' test.ping
salt -S 192.168.10.22 test.ping
salt -S 192.168.10.0/24 test.ping
salt -G 'os:RedHat' --batch-size 25% apache.singel restart  # --batch-size 25%,根據25%的比例重啓服務
View Code

2)自定義分組

[root@k8s6 ~]# vim /etc/salt/master    
#nodegroups:
#  group1: 'L@foo.domain.com,bar.domain.com,baz.domain.com or bl*.domain.com'
#  group2: 'G@os:Debian and foo.domain.com'
#  group3: 'G@os:Debian and N@group1'
#  group4:
#    - 'G@foo:bar'
#    - 'or'
#    - 'G@foo:baz'
nodegroups:                # 新增
  web: 'L@k8s6,node01'    # 分組的。新增
  
[root@k8s6 ~]# systemctl restart salt-master
[root@k8s6 ~]# salt -N web test.ping
View Code

 3)模塊解析執行命令

salt '*' network.active_tcp        # 返回全部的tcp鏈接
salt '*' network.arp            # 返回arp表
salt '*' network.get_hostname    # 返回主機名
salt '*' service.available sshd    # 查看服務是否在運行
salt '*' service.get_all        # 查看全部正在運行的服務
salt '*' service.status sshd    # 查看運行的狀態
salt-cp '*' /etc/hosts /tmp/hehe    # 拷貝文件
salt '*' state.show_top   # 查看在top裏面須要作什麼事
salt '*' state.single pkg.installed name=lsof    # 手動執行安裝
= salt '*' cmd.run 'yum install lsof -y'

 9、將返回結果寫入數據庫

https://www.unixhot.com/docs/saltstack/ref/returners/index.html        # 支持的返回的位置
salt '*' state.single pkg.installed name=MySQL-python  # 實質則是安裝mysql
https://www.unixhot.com/docs/saltstack/ref/returners/all/salt.returners.mysql.html  # 官網,所須要執行的步驟

 1)數據庫操做,建立庫,受權

1)建立salt數據庫
CREATE DATABASE  `salt`
  DEFAULT CHARACTER SET utf8
  DEFAULT COLLATE utf8_general_ci;

2)進入當前庫
    USE `salt`; 
    
3)建立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;

3)建立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;

4)建立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;

5)受權
grant all on salt.* to salt@'%' indentified by 'salt@pw';
flush privileges;

測試鏈接
mysql -h192.168.10.22 -usalt -psalt@pw
View Code

 2)修改配置文件,添加數據庫的鏈接方式

[root@k8s6 ~]# vim /etc/salt/minion
# 最後添加
mysql.host: '192.168.10.22'
mysql.user: 'salt'
mysql.pass: 'salt@pw'
mysql.db: 'salt'
mysql.port: 3306
[root@k8s6 ~]# systemctl restart salt-minion
vim /etc/salt/minion

 3)執行操做

[root@k8s6 ~]# systemctl restart salt-minion
[root@k8s6 ~]# tail -f /var/log/salt/minion     進行日誌監聽
[root@k8s6 ~]# salt '*' test.ping --return mysql
進入數據庫查看是否有數據

 10、編寫本身的執行模塊

存放位置: /srv/salt/_modules
命名:文件名就是模塊名
[root@k8s6 ~]# cat /srv/salt/_modules/my_disk.py 
def list():
    cmd = "df -h"
    ret = __salt__["cmd.run"](cmd)
    return ret
[root@k8s6 ~]# salt '*' saltutil.sync_modules    刷新
k8s6:
    - modules.my_disk
node01:
    - modules.my_disk
[root@k8s6 ~]# salt '*' my_disk.list    # 執行本身的模塊
相關文章
相關標籤/搜索