IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> saltstack进阶 -> 正文阅读

[系统运维]saltstack进阶

循环语句

[root@master base]# cat test.sls 
{% for user in ["user001","user002","user003"] %}
{{ user }}:
  user.present
{% endfor %}

[root@node2 ~]# id user001
id: “user001”:无此用户
[root@node2 ~]# id user002
id: “user002”:无此用户
[root@node2 ~]# id user003
id: “user003”:无此用户


[root@master base]# salt 'node2' state.sls test
node2:
----------
          ID: user001
    Function: user.present
      Result: True
     Comment: New user user001 created
     Started: 09:51:27.512294
    Duration: 352.924 ms
     Changes:   
              ----------
              fullname:
              gid:
                  1001
              groups:
                  - user001
              home:
                  /home/user001
              homephone:
              name:
                  user001
              other:
              passwd:
                  x
              roomnumber:
              shell:
                  /bin/bash
              uid:
                  1001
              workphone:
----------
          ID: user002
    Function: user.present
      Result: True
     Comment: New user user002 created
     Started: 09:51:27.865420
    Duration: 291.818 ms
     Changes:   
              ----------
              fullname:
              gid:
                  1002
              groups:
                  - user002
              home:
                  /home/user002
              homephone:
              name:
                  user002
              other:
              passwd:
                  x
              roomnumber:
              shell:
                  /bin/bash
              uid:
                  1002
              workphone:
----------
          ID: user003
    Function: user.present
      Result: True
     Comment: New user user003 created
     Started: 09:51:28.157484
    Duration: 295.113 ms
     Changes:   
              ----------
              fullname:
              gid:
                  1003
              groups:
                  - user003
              home:
                  /home/user003
              homephone:
              name:
                  user003
              other:
              passwd:
                  x
              roomnumber:
              shell:
                  /bin/bash
              uid:
                  1003
              workphone:

Summary for node2
------------
Succeeded: 3 (changed=3)
Failed:    0
------------
Total states run:     3
Total run time: 939.855 ms

[root@node2 ~]# id user003
uid=1003(user003) gid=1003(user003)=1003(user003)
[root@node2 ~]# id user002
uid=1002(user002) gid=1002(user002)=1002(user002)
[root@node2 ~]# id user001
uid=1001(user001) gid=1001(user001)=1001(user001)

判断语句


[root@master base]# cat test.sls 
test_nginx_install:
  pkg.installed:
    {% if grains['os'] == 'CentOS Stream' %}
    - name: httpd
    {% elif grains['os'] == 'Ubuntu' %}
    - name: apache2
    {% endif %}

[root@master base]# salt 'node2' state.sls test
node2:
----------
          ID: test_nginx_install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: The following packages were installed/updated: httpd
     Started: 10:04:37.277457
    Duration: 10364.097 ms
     Changes:   
              ----------
              apr:
                  ----------
                  new:
                      1.6.3-11.el8
                  old:
              apr-util:
                  ----------
                  new:
                      1.6.1-6.el8
                  old:
              apr-util-bdb:
                  ----------
                  new:
                      1.6.1-6.el8
                  old:
              apr-util-openssl:
                  ----------
                  new:
                      1.6.1-6.el8
                  old:
              centos-logos-httpd:
                  ----------
                  new:
                      85.8-1.el8
                  old:
              httpd:
                  ----------
                  new:
                      2.4.37-40.module_el8.5.0+852+0aafc63b
                  old:
              httpd-filesystem:
                  ----------
                  new:
                      2.4.37-40.module_el8.5.0+852+0aafc63b
                  old:
              httpd-tools:
                  ----------
                  new:
                      2.4.37-40.module_el8.5.0+852+0aafc63b
                  old:
              mod_http2:
                  ----------
                  new:
                      1.15.7-3.module_el8.4.0+778+c970deab
                  old:

Summary for node2
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  10.364 s


[root@node2 ~]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port      Peer Address:Port   Process   
LISTEN   0        128              0.0.0.0:22             0.0.0.0:*                
LISTEN   0        128                 [::]:22                [::]:*                
[root@node2 ~]# systemctl start httpd
[root@node2 ~]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port      Peer Address:Port   Process   
LISTEN   0        128              0.0.0.0:22             0.0.0.0:*                
LISTEN   0        128                    *:80                   *:*                
LISTEN   0        128                 [::]:22                [::]:*   

应用场景

  • master 与 minion 网络不通或通信有延迟,即网络不稳定
  • 想在 minion 端直接执行状态

传统的 SaltStack 是需要通过 master 来执行状态控制 minion
从而实现状态的管理,但是当网络不稳定的时候,当想在minion本地执行状态的时候,当在只有一台主机的时候,想执行状态该怎么办呢?这就需要用到masterless
了。

有了masterless,即使你只有一台主机,也能玩saltstack,而不需要你有N台主机架构。

masterless配置

修改配置文件minion

  • 注释master行
  • 取消注释file_client并设其值为local
  • 设置file_roots
  • 设置pillar_roots

配置salt-minion

[root@node1 ~]# vim /etc/salt/minion
# resolved, then the minion will fail to start.
master: salt   //取消注释

# minion in masterless mode.
file_client: local   //取消此行注释并将值设为local

 file_roots:  //设置file_roots的路径和环境,可有多套环境
   base:
     - /srv/salt/base  
     
pillar_roots:
  base:
    - /srv/pillar/base   //设置pillar_roots的路径和环境
     

//创建文件夹 
[root@node1 ~]# mkdir -p /srv/salt/base /srv/pillar/base
[root@node1 ~]# tree /srv
/srv
├── pillar
│   └── base
└── salt
    └── base

4 directories, 0 files

关闭salt-minion服务

使用 masterless 模式时是不需要启动任何服务的,包括salt-master和salt-minion。

[root@node2 ~]# systemctl stop salt-minion

salt-call
masterless模式执行模块或状态时需要使用salt-call命令,而不再是salt或者salt-ssh。需要注意的是要使用salt-call的–local选项。

[root@node1 ~]# salt-call --local cmd.run 'date'
local:
    Wed Jul 21 09:29:08 EDT 2021
[root@node1 ~]# salt-call --local cmd.run 'ls -l /root'
local:
    total 4
    -rw-------. 1 root root 1030 Jun  3 14:36 anaconda-ks.cfg

masterless高可用

salt-master高可用配置
我们需要用salt来管理公司的所有机器,那么salt的master就不能宕机,否则就会整个瘫痪,所以我们必须要对salt进行高可用。salt的高可用配置非常简单,只需要改一下minion配置文件,将master用列表的形式列出即可。

salt-master高可用之数据同步

涉及到高可用时,数据的同步是个永恒的话题,我们必须保证高可用的2个master间使用的数据是一致的,包括:

  • /etc/salt/master配置文件
  • /etc/salt/pki目录下的所有key
  • /srv/下的salt和pillar目录下的所有文件

保障这些数据同步的方案有:

  • nfs挂载
  • rsync同步
  • 使用gitlab进行版本控制

安全相关:

为保证数据的同步与防止丢失,可将状态文件通过gitlab进行版本控制管理。

环境:

服务主机IP地址
mastermaster1192.168.100.10
mastermaster2192.168.100.77
minionnode1192.168.100.20
//复制主master的key到备master上
[root@master1 ~]# scp /etc/salt/master 192.168.100.10:/etc/salt/master
[root@master1 ~]# scp -r /etc/salt/pki 192.168.100.10:/etc/salt


//启动备master
[root@master2 ~]# systemctl start salt-master

//配置minion端连接到备master
[root@node1 ~]# vim /etc/salt/minion
master: 
  - 192.168.100.10
  - 192.168.100.77

//重启minion
[root@master2 ~]# systemctl start salt-master
//在备master上接受密钥key
[root@master2 ~]# salt-key -L
Accepted Keys:
node1
Denied Keys:
Unaccepted Keys:
Rejected Keys:


//这时两台master都可以控制minion
[root@master1 ~]# salt '*' test.ping
node1:
    True
[root@master2 ~]# salt '*' test.ping
node1:
    True



//配置故障转移
[root@node1 minion]# vim /etc/salt/minion

# beacons) without a master connection
master_type: failover  //取消注释改为failover

# of TCP connections, such as load balancers.)
master_alive_interval: 3    //当master1挂掉后,minion在3秒后自动切换master为master2

//测试两台mater是否能够控制minion
[root@master1 ~]# salt '*' test.ping
node1:
    True

[root@master2 ~]# salt '*' test.ping
node1:
    Minion did not return. [No response]

//模拟宕机
[root@master1 ~]# systemctl stop salt-master
[root@master2 ~]# salt '*' test.ping
node1:
    True
    //当master1宕机后,master2就能够控制minion了,实现高可用

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2021-07-22 14:38:04  更:2021-07-22 14:40:56 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/4 10:35:22-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码