salt 入门讲解

Python017

salt 入门讲解,第1张

master和minion各自干了哪些活:

master:

minion:

master and minion 有两种方式push and pull, 下面讲解一下master and minion install及pull方式的例子, here master and minion install 在同一台机器, install system is centos 7.

1.1 Update the system

Use the sudo user to log into the SaltStack master server, then update the system to the latest stable status:

After the reboot completes, use the same sudo user to log in.

1.2 Install and configure the salt-master program

Use the SaltStack official YUM repo to install the latest salt-master program:

After the installation finishes, modify the configuration file as below:

Find:

Replace the line with:  master ip value

Find:

Replace the line with:

Save and quit:

Start and enable the salt-master service:

Step 2: Operations on the SaltStack agent server

2.1 Update the system

Use the sudo user to log in the SaltStack agent server. Again, update the system to the latest stable status:

After the reboot, use the same sudo user to log in.

2.2 Install and configure the salt-minion program

Use the SaltStack official YUM repo to install the latest salt-minion program:

After the installation, modify the configuration file as below:

Find:

Replace the line with: master ip value

Find:

Replace the line with:

Save and quit:

Start and enable the salt-minion service:

After starting up, the salt-minion service will send off a signal to find the SaltStack server.

If you have more SaltStack agent servers, you need to setup them in the same fashion.

Step 3: Test your setup on the SaltStack master server

Return to the SSH connection to the SaltStack master server, input the following command to show all available agents:

sudo salt-key -L

If everything was successful, you will see the agent server "minion1" listed in the "Unaccepted Keys" segment.

Accept "minion hostname" using this command :

salt-key --accept=ip-10-29-76-235.ec2.internal

Or accept all of the agent servers:

salt-key -A

Finally, you can test your setup using the example commands below:

Example 1:

sudo salt  ip-10-29-76-235.ec2.internal test.ping

The output show:

Example 2:

sudo salt ip-10-29-76-235.ec2.internal cmd.run pwd

The output show:

That's it. You can learn more about SaltStack on its official website. Enjoy it!

 1.1 salt     #主要管理命令

   命令格式:salt [options]  <target> [arguments]

    例:salt ‘*’ test.ping

 1.2 salt-key #证书管理

 1.3 salt-run #管理minion

 1.4 salt-cp #将master文件复制到minion,不支持复制目录

 1.5 salt-ssh    

   #通过ssh连接被管理端,被管理端不用安装minion,管理端也不用安装master,salt-ssh是一个独立的包,安装后即可使用saltstack大部分功能,没有通讯机制ZeroMQ,命令执行速度会下降。一般没有客户端没有安装minion时候才考虑先用salt-ssh批量安装minion。

   # apt-get install salt-ssh sshpass   #salt-ssh用的sshpass进行密码交互,必须要安装

   1.5.1 salt-ssh常用参数

 -r,-raw-shell :执行shell命令  

    --key-deploy   :配置keys

    -i,-ignore-host-keys  :当ssh连接时,忽略keys

     -passwd      :指定默认密码

     -roster-file   :指定roster文件

   1.5.2 salt-ssh使用

    1.5.2.1 sat-ssh通过调用roster配置文件实现,所以先定义roster,让salt-ssh生效,就可以执行操作了

    1.5.2.1 测试

    1.5.2.3 执行shell命令及salt本身的模块

    #第一次运行时会提示是否接受秘钥,如果不想再提示可以加入—key-deploy参数

2、Pillar

 上节讲过Salt State,Salt状态系统的核心SLS,也可叫做配置管理,SLS描述了系统的目标状态,由简单的格式来包含这些数据

 Pillar是Salt最重要的系统之一,可用于提供开发接口,用于在master端定义数据,然后再minion中使用,一般传输敏感的数据,例如ssh key,加密证书等。

 pillar和states建立方式类似,由sls文件组成,有一个入口文件top.sls,通过这个文件关联其他sls文件,默认路径在/srv/pillar,可通过/etc/salt/master里面pillar_roots:指定位置。

 pillar到底什么作用呢?那么下面介绍一个简单的例子,你就明白了。

 用zabbix监控新上架的服务器(10台),需要将zabbix_agentd.conf分发到被监控主机,这个文件中hostname的ip每台都不同,我们不可能写10分配置文件吧!那么如何让hostname在分发的时候就根据被监控主机IP,修改成自己的呢?这时就用到渲染了,默认渲染器是jinja,支持for in循环判断,格式是{%...%}{% end* %},这样一来salt会先让jinja渲染,然后交给yaml处理。

 2.1 创建pillar目录和top.sls文件

 # mkdir /srv/pillar

 # vi /srv/pillar/top.sls

 2.2 先通过pillar获取minion主机IP

#刷新pillar数据到minion

#写完后,执行sls命令,可以看到已经获取到IP

 2.3 随后写个sate文件,将文件分发到minion上

 # mkdir /srv/salt/zabbix

 # vi /srv/salt/zabbix/agentd_conf.sls

 2.4 修改zabbix_agentd.conf要渲染的IP

2.5执行单sls命令,不用将sls文件关联到top.sls文件                         

 #这时再通过命令查看,已经更新成功

pillar相关命令:

#刷新pillar数据到minion

# salt "*" saltutil.refresh_pillar

#查看所有pillar信息

# salt "*" pillar.items

#查看某个pillar信息

# salt "*" pillar.item ip

既然grains与pillar类似,就说下区别:

1.grains是minion每次加载时获取本地系统信息数据,是静态的,固定的,而pillar是动态加载数据,随时变化的,比grains更灵活。

2.grains数据存储在minion本地,pillar存储在master。

脚本安装系统是Centos.

First step:

Run this script testminion.sh above.

Notes: please pay attention to remove blank line when you copy content into machine.

解析:

test:    //test environment

  'G@service:test':   //G stands for grain, service name equals test

    - test    //   call /srv/pillar/test.sls

test pillar value:  ()

start saltmaster

[email protected]:~ · 10:17 AM Fri Jan 17 · 

granis configure

pillar

支柱

双语对照

词典结果:

pillar

[英][ˈpɪlə(r)][美][ˈpɪlɚ]

n.柱,台柱,顶梁柱墩,柱脚(组织、制度、信仰等的)核心

复数:pillars

以上结果来自金山词霸

例句:

1.

This three pillar structure would produce three tangible benefits.

这三个支柱结构将产生三项实实在在的益处。

-----------------------------------

如有疑问欢迎追问!

满意请点击右上方【选为满意回答】按钮