添加Agent和应用服务器之间的SSH互信

背景及概念

CICD已经成为了各公司敏捷开发的重点,而CICD中除了开发部分,自动部署也是极为重要的一环。
之前的博文也介绍了Atlassian公司的自动化发布工具Bamboo,运用它可以大大减少运维人员在发布应用过程中的重复操作。首先我们要了解什么是Agent服务器,什么是应用服务器。

What is Bamboo Agent?

A Bamboo agent is a service that can run job builds. There are the following types of Bamboo agents:
local agents run as part of the Bamboo server.
remote agents run on computers, other than the Bamboo server, that run the remote agent tool.
elastic agents run in the Amazon Elastic Compute Cloud (EC2).
Local agents run in the Bamboo server’s process, i.e. in the same JVM as the server. Each remote agent runs in its own process, i.e. has its own JVM.
Each agent has a defined set of capabilities and can only run builds for jobs whose requirements match the agent’s capabilities.

简而言之,Bamboo Agent就是执行自动化步骤的服务器,应用服务器就是运行ERP、OA等企业应用的服务器。
在这里插入图片描述

添加互信

在本文中agent和应用服务器均已获得root权限,本文中我们以root账号为例来进行演示。
首先我们在agent上生成公钥、私钥对:

ssh-keygen

此时我们可以尝试在agent上拷贝公钥文件到应用服务器上:

ssh-copy-id -i /root/.ssh/id_rsa.pub root@server_ip

此时会提示输入应用服务器的root密码,但是在输入正确的密码后仍然可能会报出一个错误:

permission denied

分析原因,可能是应用服务器上对ssh做了限制,可以进行如下四步操作(注:一般只需要做第一步即可,如果第一步不生效,再执行后面的步骤):
(1)修改vim /etc/ssh/ssh_config中参数:

StrictHostKeyChecking no

(2)修改vim /etc/ssh/sshd_config中参数,在最后加入一行

AllowUsers bamboo root

这里加入bamboo root是因为服务器已经加入了AD域管理,需要手动添加允许bamboo和root用户进行访问。
这一步是根据Linux模板的不同而调整的,如果Linux模板中没有集成AD域管理,默认允许所有用户访问,那么就不需要加上这一行。如果此时加上这一行后,其他的AD域账号反而不能正常登录了。
测试Linux有没有集成AD域管理,可以添加一个本地账户,看是否能够登陆Linux,如果能登陆说明没有集成AD域管理,如果不能说明集成了。另一种方法是查看/etc/shadow文件,看其中有没有ldap这一行,如果有也说明集成了AD域管理。
(3)修改vim /etc/hosts.allow中参数,在最后加入一行

sshd:ALL

这一句是强制允许所有的ssh远程访问。
(4)修改vim /etc/ssh/sshd_config中参数:

RhostsRSAAuthentication yes

当以上所有ssh的配置修改好后,需要重启sshd:

service sshd restart

此时,再次执行ssh公钥拷贝命令:

ssh-copy-id -i /root/.ssh/id_rsa.pub root@server_ip

便可以成功拷贝公钥。
拷贝成功后,在agent上执行:

ssh root@server_ip "command"

便可在agent上ssh登录应用服务器,并执行命令。

发布了79 篇原创文章 · 获赞 322 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_15329947/article/details/98063237