自动化运维工具SaltStack - 实际问题记录(集群中部分minion无法连接master)

最近在进行 saltstack 的过程中发现,在一个集群中,集群有 20 台服务器,其中只有 5 台能成功连接到 master ,其他的怎么都连接不上,因此进行记录下。

1、首先当时确定的是,我集群中的所有服务器,使用 hostname 都能看到主机名,并且 /etc/sysconfig/network 中的也标记了主机名
2、minion 也能正常启动
3、在 master 中发现一个奇怪的现象,即接收的服务器中始终有一个 localhost.localdomain(我在master端已经去掉了的),按照正常道理是不可能有这个的,事出反常即为妖。
4、minion 部分服务器报错

[CRITICAL] The Salt Master has rejected this minion's public key!
To repair this issue, delete the public key for this minion on the Salt Master and restart this minion.
Or restart the Salt Master in open mode to clean out the keys. The Salt Minion will now exit.

基于以上情况进行分析。
1、拿起我们的 salt 的 debug 进行详细输出调试。

 sudo salt-minion -l debug 

输出的部分内容如下:

[DEBUG   ] Reading configuration from /etc/salt/minion
[INFO    ] Using cached minion ID from /etc/salt/minion_id: localhost.localdomain
[DEBUG   ] Configuration file path: /etc/salt/minion
[INFO    ] Setting up the Salt Minion "localhost.localdomain"
[DEBUG   ] Created pidfile: /var/run/salt-minion.pid
[DEBUG   ] Reading configuration from /etc/salt/minion

看到这个输出,如果你已经启动过了,它会直接去读取 /etc/salt/minion_id 的缓存的,所以首先做的一件事情就是,删除这个缓存

sudo rm /etc/salt/minion

最后再次 debug 尝试,输出结果如下:

[DEBUG   ] Reading configuration from /etc/salt/minion
[DEBUG   ] Guessing ID. The id can be explicitly in set /etc/salt/minion
[INFO    ] Found minion id from getfqdn(): localhost.localdomain
[DEBUG   ] Configuration file path: /etc/salt/minion
[INFO    ] Setting up the Salt Minion "localhost.localdomain"
[DEBUG   ] Created pidfile: /var/run/salt-minion.pid
[DEBUG   ] Reading configuration from /etc/salt/minion

注意:从 debug 中我们能看到 minion 获取 minion id 是通过 getfqdn 方法的,getfqdn 方法获取到的主机名居然是 localhost.localdomain ,尼玛,不对啊,赶紧翻阅下 getfqdn 的源码。并且自己编写了一个测试脚本测试了下结果,测试结果如下:

#!/usr/bin/python env
import socket

myname = socket.getfqdn(socket.gethostname())
myaddr = socket.gethostbyname(myname)
print "myname= %s" % myname
print "myaddr= %s" % myaddr

测试结果如下:

myname= localhost.localdomain
myaddr= 127.0.0.1

然后查看了下该 minion 的 /etc/hosts 文件,发现如下配置

127.0.0.1 localhost.localdomain localhost
127.0.0.1 hostname

问题出在 127.0.0.1 hostname 上面,需要修改下把上面的修改成

主机IP 主机名
: 注:或者不配置 /etc/hosts 也不会有这有的问题

注:你们在修正了 /etc/hosts 中的配置之后,一定要先删除 /etc/salt/minion_id 这个缓存。

注:如果你把上面这行注释掉的话,程序会报错如下

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    myaddr = socket.gethostbyname(myname)
socket.gaierror: [Errno -2] Name or service not known

注:下面是官方文档中关于这方面的相关解析

当minion启动后,它会产生一个 id 值,除非已经在之前的运行过程中产生过并且缓存在配置路径下,默认是 /etc/salt 。minion用这个值作为名称尝试去master进行验证。尝试下面几步操作,以便找到一个不是 localhost 的值:

  1. 运行Python函数”socket.getfqdn()”
  2. 核对”/etc/hostname”(仅针对非Windows系统)
  3. 核对”/etc/hosts”(在Windows主机上是”%WINDIR%system32driversetchosts”) 上的包括”127.0.0.0/8″在内的所有主机名。
    如果以上都不能产生除”localhost”以外的id,那么就会按顺序检测minion上的IP地址列表(排除”127.0.0.0/8″在内)。如果存在,就会使用第一个公网路由IP地址,否则就会使用第一个私网路由IP地址。

如果所有这些都失败了,那么就会使用”localhost”作为备选。

: 注解:覆盖”id”值,minion的id也可以通过minion配置文件中 :conf_minion:id选项手动指定。如果指定这个配置值,它会覆盖所有其他来源的”id”值。现在minion已经运行了,它会产生秘钥对并且尝试连接master。下一步就是折回master服务器接受新minion的公钥。

困扰了一天的问题终于解决,PS:主要还是需要加强熟悉下 SaltStack 的整体处理流程,后续加强多读读源码。。问题就是进步的原动力。

    原文作者:yexiaobai
    原文地址: https://segmentfault.com/a/1190000000505397
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞