背景
我已成功在 Kubernetes 集群中运行 Jenkins。它还连接到集群以创建部署。
我正在尝试使用 SSH 代理插件部署某些内容。我的理解是,我需要它通过 SSH 连接到运行集群主节点的实际机器,然后我可以使用以下命令执行部署:
kubectl create -f deployment.yaml
到目前为止的进展
我已经安装了 SSH Agent 插件并将 SSH 私钥存储在 Jenkins 中。
我还将适当的公钥放入集群主节点的 /home/pi/.ssh 文件夹和authorized_keys 文件中。
我能够从另一台机器成功通过 SSH 连接到它。
问题
当 Pipeline 执行时,它表示正在将 SSH-Key 添加到从属 SSH Agent pod 中。
[ssh-agent] Using credentials pi (SSH credentials for the master node.)
[ssh-agent] Looking for ssh-agent implementation...
[ssh-agent] Exec ssh-agent (binary ssh-agent on a remote machine)
...
Running ssh-add (command line suppressed)
Identity added: /home/jenkins/agent/workspace/Deployment@tmp/private_key_123123123123132.key (pi@pi1)
[ssh-agent] Started.
但是当我尝试从 Jenkins 从机(SSH 代理)进行 SSH 时,它说无法验证密钥。
+ ssh [email protected] id
Host key verification failed.
请求
有人能指点我如何解决这个问题吗?我做错了什么?
其他详细信息
我正在使用这样的精简管道进行测试:
// Start the Pipeline
pipeline {
// Define the agent where it will run
agent {
// kubernetes = kubernetes cloud in Jenkins
kubernetes{
}
}
// Start declaring the stages of the pipeline
stages {
// Stage #3 - Deploy the image to the production kubernetes cluster using an SSH agent
stage('Deploy to Kubernetes Cluster'){
steps {
sshagent(['RPi-SSH']) {
script {
sh 'id'
sh 'ssh [email protected] id'
sh 'ssh [email protected] ls'
}
}
}
}
}
}
通过这个管道,我可以看到第一个 id 是 SSH Agent 节点中“jenkins”的 id。 当它尝试通过 SSH 连接到主节点时,它就失败了。
您尝试连接的主机可能不在您的
known_hosts
文件中。理想情况下它们应该是这样,但实际上没有人会为此烦恼,只需在第一次连接时通过将此开关添加到您的 ssh
命令来添加它们即可:
ssh -oStrictHostKeyChecking=accept-new [email protected] id
您将找到将
StrictHostKeyChecking
设置为 no
的建议。在这种情况下,这可能并不重要,因为我们正在处理瞬态容器,一旦管道完成,它们的 known_hosts
文件就会消失,但是一旦您使用它,其他开发人员就会将其复制粘贴到其他上下文中重要,所以...就这样吧。