杨梅冲
每天在想什么呢?
# 环境准备
192.168.0.110 centos 7.9  hostname:Master
192.168.0.111 centos 7.9  hostname:node1
# 注意点:
服务器时间
防火墙和selinux

一、slatstack安装

https://repo.saltproject.io/

rpm --import https://repo.saltproject.io/py3/redhat/7/x86_64/latest/SALTSTACK-GPG-KEY.pub
curl -fsSL https://repo.saltproject.io/py3/redhat/7/x86_64/latest.repo | sudo tee /etc/yum.repos.d/salt.repo
salt master安装启动
yum install -y salt-master salt-minion
systemctl enable salt-master systemctl start salt
-master salt minion安装 yum install -y salt-minion

修改/etc/salt/minion配置文件
master: 192.168.0.110 # 在minion上标注出master是谁
# id 默认和主机名一样,可以自己有需要进行修改,启动后生成/etc/salt/minion_id,如果需要改id,需要删除这个文件以及master服务器中公钥存放目录/etc/salt/pki/master/minions_pre对应的id文件,已经同意的删除对应目录公钥

systemctl enable salt-minion systemctl start salt
-minion

#配置文件
[root@Master salt]# grep -v ^# /etc/salt/master|grep -v ^$
interface: 0.0.0.0  #绑定到本地的0.0.0.0地址
publish_port: 4505  #管理端口,命令发送
user: root      #运行salt进程的用户
worker_threads: 5  #salt运行线程数,线程越多处理速度越快,不要超过cpu个数
ret_port: 4506  #执行结果返回端口
pidfile: /var/run/salt-master.pid #pid文件位置
log_file: /var/log/salt/master  #日志文件地址

#自动接收minion的key
auto_accept: True  # 如果对minion信任可以配置master自动接收
# 查看同意的key节点:salt-key命令用于管理mionion秘钥
[root@Master minions_pre]# salt-key
Accepted Keys: 已接收的key
Denied Keys:   已拒绝的key
Unaccepted Keys:  未加入的key
Master
node1
Rejected Keys:  吊销的key

# salt-key -h  常用参数
-A 许可所有的公钥
-a 许可指定公钥
-r 注销掉指定key(该key未被认证)
-R 拒绝所有公钥
-d 根据公钥名称删除指定公钥
-D 删除所有
-L 查看key状态 [root@Master minions_pre]# salt
-key -A The following keys are going to be accepted: Unaccepted Keys: Master node1 Proceed? [n/Y] Y Key for minion Master accepted. Key for minion node1 accepted. [root@Master minions_pre]# salt-key Accepted Keys: Master node1 Denied Keys: Unaccepted Keys: Rejected Keys:

# minion上生成文件/etc/salt/pki/minion/minion_master.pub 双向交换秘钥进行验证

#验证

[root@Master ~]# salt '*' test.ping
node1:
True
Master:
True

# master 默认端口为 4505和4506,4505端口为发送消息端口,所有minion都会接收到,4506为返回消息端口
# zeromq消息队列
[root@Master salt]# lsof -n -i :4505
COMMAND     PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
salt-mast 83600 root   18u  IPv4  869809      0t0  TCP *:4505 (LISTEN)
salt-mast 83600 root   20u  IPv4 1028703      0t0  TCP 192.168.0.110:4505->192.168.0.111:60802 (ESTABLISHED)
salt-mast 83600 root   21u  IPv4 1032782      0t0  TCP 192.168.0.110:4505->192.168.0.110:41460 (ESTABLISHED)
salt-mini 92885 root   22u  IPv4 1028705      0t0  TCP 192.168.0.110:41460->192.168.0.110:4505 (ESTABLISHED)

命令执行,状态模块

saltsatck模块:https://www.unixhot.com/docs/saltstack/salt-modindex.html

# 固定写法
# cmd.run 执行命令模块,所有的shell命令都可以执行
[root@Master salt]# salt Master cmd.run 'w'
Master:
     09:46:07 up 12:21,  2 users,  load average: 0.00, 0.04, 0.06
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    root     tty1                      01:10    8:35m  0.01s  0.01s -bash
    root     pts/1    192.168.0.125    01:34    7.00s  1.22s  0.86s /usr/bin/python3 /usr/bin/salt Master cmd.run w

 列出所有的sys模块

#与系统交互的sys模块
[root@Master ~]# salt 'node1' sys.list_modules

service远程管理服务模块

# 管理服务是系统管理员的重要任务,通过salt管理minion服务会很简单,使用service模块
[root@Master base]# salt 'node1' service.status "sshd"
node1:
    True

service.running #确保服务处于运行状态,如果没用运行就启动
service.enabled # 确保服务是开机启动
service.disabled # 确保服务开机不启动
service.dead # 确保服务当前没有运行,如果运行就停止

pkg安装模块

logstash:
  pkg.installed  # 安装模块
    - fromrepo: ppa:wolfnet/logstash  #指定yum源
    - pkgs:
      - php-fpm  # 列出需要安装的包

pkg.latest #确保软件包是最新版本,如果不是,进行升级
pkg.remove # 确保软件包已卸载,如果之前已安装,进行卸载
pkg.purge # 除remove外,也会删除其配置文件

file文件管理

# 常用方法
file.managed #保证文件存在并为对应状态
file.recurse  # 保证目录存在并且为对应状态
file.absent   # 保证文件不存在,如果存在就删除

requisites处理状态间关系

# 常用方法
require # 我依赖某个状态,例如安装不成功,就不继续往下走
require_in # 我被某个状态依赖
watch # 我关注某个状态,例如:如果某个服务的配置文件改变,自动重启服务或者reload;查看lnmp例子
watch_in # 我被某个状态关注

二、YAML讲解

salt的配置文件是yaml配置文件,不能用tab
saltstack,k8s,ansible都用的yaml格式配置文件


语法规则
    大小写敏感
    使用缩进表示层级关系,连续的项目通过减号“-”来表示   
    缩进时禁止tab键,只能空格
    缩进的空格数不重要,相同层级的元素左侧对其即可
    # 表示注释行
yaml支持的数据结构
    对象: 键值对,也称作映射 mapping 哈希hashes 字典 dict    冒号表示 key: value   key冒号后必须有
    数组: 一组按次序排列的值,又称为序列sequence 列表list     短横线  - list1
    纯量: 单个不可再分的值

1.字串不一定要用双引号标识;
2.在缩排中空白字符的数目并不是非常重要,只要相同阶层的元素左侧对齐就可以了(不过不能使用TAB字符);
3.允许在文件中加入选择性的空行,以增加可读性;
4.在一个档案中,可同时包含多个文件,并用“——”分隔;
5.选择性的符号“...”可以用来表示档案结尾(在利用串流的通讯中,这非常有用,可以在不关闭串流的情况下,发送结束讯号)。
# 修改配置文件,标注yaml文件目录
vim /etc/salt/master +677
file_roots:
# 根据环境来定义,如果写了多个目录就会挨个找
  base:
     - /srv/salt
# 开发环境
  dev:
     - /srv/salt/dev

[root@Master salt]# mkdir -p /srv/salt/{base,dev,test,prod} # 客户端也执行下
[root@Master salt]# systemctl restart salt-master

apache安装yaml编写

# 唯一标识,id声明,不能重复
apache-install:
  # pkg状态模块,installed状态模块的一个方法
  pkg.installed:
    # name状态:这台机器应该有一个httpd,如果有就不安装,没有就安装
    - name: httpd

apache-service:
# service模块,running方法 service.running: # 检查httpd是否有启动,如果没有启动就启动起来 - name: httpd # 开机自动启动 - enable: True

命令执行:
# master执行:state为模块,apache后面不需要加xls
[root@Master base]# salt 'node1' state.sls apache
node1:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True # 返回结果
     Comment: The following packages were installed/updated: httpd
     Started: 22:40:06.200137
    Duration: 97472.676 ms
     Changes:   # 改变
              ----------
              apr:
                  ----------
                  new:
                      1.4.8-7.el7
                  old:
              apr-util:
                  ----------
                  new:
                      1.5.2-6.el7
                  old:
              httpd:
                  ----------
                  new:
                      2.4.6-97.el7.centos.1
                  old:
              httpd-tools:
                  ----------
                  new:
                      2.4.6-97.el7.centos.1
                  old:
              mailcap:
                  ----------
                  new:
                      2.1.41-2.el7
                  old:
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd has been enabled, and is running
     Started: 22:41:43.686190
    Duration: 272.505 ms
     Changes:   
              ----------
              httpd:
                  True

Summary for node1
------------
Succeeded: 2 (changed=2)
Failed:    0
------------
Total states run:     2
Total run time:  97.745 s


# master执行中在node1上查询,可以看到下面的状态
[root@node1 ~]# ps -ef | grep yum
root      20293  20250  1 22:40 ?        00:00:00 /usr/bin/python /usr/bin/yum --quiet --assumeyes check-update --setopt=autocheck_running_kernel=false
root      20324  20109  0 22:40 pts/0    00:00:00 grep --color=auto yum
[root@node1 ~]# ps -ef | grep yum
root      20364  20250  0 22:41 ?        00:00:00 /usr/bin/python /usr/bin/yum -y install httpd
root      20376  20109  0 22:41 pts/0    00:00:00 grep --color=auto yum


# master执行完成后查看node1是否运行这个服务
[root@Master base]# salt 'node1' service.status "httpd"
node1:
    True

[root@Master base]# salt 'node1' service.stop "httpd"
node1:
True
[root@Master base]# salt 'node1' service.status "httpd"
node1:
False

三、配置管理

sls可以根据用途或项目进行细分,高级状态

# 多级目录执行方法
mkdir /srv/salt/base/web
# 命令执行方法
[root@Master base]# salt 'node1' state.sls web.apache
# top.sls 定义每台机器做什么
base: # 环境
  'node1': # 节点
    - web.apache  #执行状态
  'node2':
    - web.apache

# 执行top.sls方法,生产中使用比较多
# state.highstate 执行高级状态,默认去base目录下找到top.sls并读取,告诉salt那个minon该执行什么状态
[root@Master base]# salt 'node1' state.highstate  # 高级状态依靠的就是topfile

 安装一个文件需要3个模块:pkg软件包、file配置文件、服务service

状态模块:https://www.unixhot.com/docs/saltstack/ref/states/all/index.html

四、Saltstack配置管理之LNMP状态管理

# 正式运行之前可以先,在一台试验机上yum安装一次看看有什么问题没

lamp-install:
  pkg.installed:   # 同一个文件环境下只能有一个这种模块
    - pkgs:
      - httpd
      - php
      - php-fpm
      - php-cli
      - php-mysql
      - php-common
      - php-pdo

apache-config:
  file.managed:  #file 文件管理模块,管理文件和目录
    - name: /etc/httpd/conf/httpd.conf  # 文件下载到的minion路径
    - source: salt://web/httpd.conf    # 代表从当前环境master所在路径,salt://代表/srv/salt/base/目录,web目录下有一个httpd.conf文件,也可以放到FTP上
    - user: root   # 所属用户
    - group: root  # 所属组
    - mode: 644   # 文件权限
- require: # 依赖lamp-install执行成功
- pkg: lamp-install
# 状态判断
apache-auth: pkg.installed: - name: httpd-tools - require_in: - cmd: apache-auth cmd.run: - name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin
- unless: test -f /etc/httpd/conf/htpasswd_file
# 判断文件是否存在;unless判断是否为真,如果为真就不执行

apache-conf: file.recurse: - name: /etc/httpd/conf.d # 目录文件同步,详情请看后面描述 - source: salt://web/files/apache-conf.d
- watch_in:
- service: lamp-service # 如果我变了,通知服务重启
php-config:   # 如果
  file.managed:
    - name: /etc/php.ini 
    - source: salt://web/php.ini
    - user: root
    - group: root
    - mode: 644

/etc/php-fpm.conf:
file.managed:
- source: salt://web/files/php-fpm.conf
- user: root
- group: root
- mode: 644

lamp-service:
service.running:
- name: httpd
- enable: True
- reload: True # 配置文件有改变服务进行reload,如果没有这一行就是重启
- watch:
- file: apache-config
- file: php-config

# 有pkg.installed安装的文件没有安装也会有报错,并提示那些安装了,那些没有安装
第一次:
Comment: The following packages failed to install/update: php-curl
6 targeted packages were installed/updated.
The following packages were already installed: httpd
第二次: php-curl安装失败
Comment: The following packages failed to install/update: php-curl
The following packages were already installed: httpd, php, php-fpm, php-cli, php-mysql, php-common, php-pdo

# 运行时配置文件编写错误
node1:、
Data failed to compile:
----------
Too many functions declared in state 'file' in SLS 'web.lamp'

# 还有一种执行没成功是因为缓存原因,一般再修改一次再用
清理对应客户端缓存:/var/cache/salt/minion/files/base/web/lamp.sls

配置文件也可以单独建立目录放置,但是sls配置里也需要修改为对应目录

file模块管理目录:
# 将本地/srv/salt/base/web/files/apache-conf.d/目录下的所有文件都传送到节点上

apache-conf:
  file.recurse:
    - name: /etc/httpd/conf.d
    - source: salt://web/files/apache-conf.d

# 先找一个节点测试:
#    服务端新建一个文件,修改一个文件;客户端新建一个文件,修改一个文件
# test=True 不改变客户端,只显示需要变动的地方
[root@Master apache-conf.d]# salt 'node1'  state.highstate test=True
# 执行结果:
     Changes:   
              ----------
              /etc/httpd/conf.d/README:
                  ----------
                  diff:
                      --- 
                      +++ 
                      @@ -7,4 +7,3 @@
                       
                       Files are processed in alphabetical order.
                       
                      -ddd
              /etc/httpd/conf.d/kk:
                  ----------
                  newfile:
                      /etc/httpd/conf.d/kk

Succeeded: 5 (unchanged=1, changed=1)

# 正式执行:
[root@Master apache-conf.d]# salt 'node1'  state.highstate
# 执行结果:客户端新增了一个文件kk,但是客户端目录里自己加的那个文件也没有消失,修改的文件被服务端覆盖

# file模块操作环境变量追加

# 追加:可以用户环境变量里追加
/etc/profile:
  file.append:
    - text:
      - "#aaa"
      - "#bb"

 tomcat操作

# tomcat.sls
jdk-install:
  pkg.installed:
    - pkgs:
      - java-1.8.0-openjdk

tomcat-install:
  file.managed:
    - name: /usr/local/src/apache-tomcat-8.5.72.tar.gz
    - source: salt://web/files/apache-tomcat-8.5.72.tar.gz
    - user: root
    - group: root
    - mode: 755
  cmd.run:
    - name: cd /usr/local/src && tar zxf apache-tomcat-8.5.72.tar.gz && mv apache-tomcat-8.5.72 /usr/local/ && ln -s /usr/local/apache-tomcat-8.5.72 /usr/local/tomcat
    - unless: test -L /usr/local/tomcat && test -d /usr/local/apache-tomcat-8.5.72

saltstack数据系统-Grains

Grains 是saltstack组件中非常重要之一,在配置部署时候回经常使用,Grains记录minion的静态信息,比如常用属性,CPU、内存、磁盘、网络信息等。
Minions的Grains信息是Minion启动时采集汇报给Master的Grains是以 key  value形式存储的数据库,可以看做Host的元数据(metadata)Grains保存着收集到的客户端的详细信息如果slave机器数据变化,grains就过期了
在生产环境中需要自定义Grains,可以通过
Minion配置文件
Grains相关模块定义
Python脚本定义salt 'node1' sys.doc grains#查看grains的命令用法
用途:1.数据采集静态信息 2.匹配minion(例如:匹配特定系统执行) 3.信息查询

salt 'node1' grains.ls 列出所有的grains
salt 'node1' grains.items 可以看到节点的很多信息
salt -G 'os:CentOS' cmd.run 'uptime' 在所有的Centos系统中执行uptime
salt 'node1' grains.item fqdn_ipv4
salt 'node1' grains.item os id host

模板文件里面变量使用{{ 名称 }} 

# 举例lnmp中端口使用变量名,master修改
# httpd.conf 修改
第一种:Listen 80 修改为Listen {{ PORT }}
第二种:Listen {{ IPADDR  }}:{{ PORT }}

# lnmp.sls
apache-config:
  file.managed:
    - name: /etc/httpd/conf/httpd.conf
    - source: salt://web/files/httpd.conf
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - defaults:
      PORT: 8080   # 替换{{ PORT }}
      IPADDR: {{ grains['fqdn_ipv4'][0]  }}
    - require:
      - pkg: lamp-install

 

posted on 2021-11-04 22:30  杨梅冲  阅读(170)  评论(0编辑  收藏  举报