You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行kamal setup部署Go后端与PostgreSQL时遇KeyError问题求助

解决Kamal 2部署到localhost时的SSH KeyError及Proxy访问问题

问题重现

执行kamal setup部署Go后端和PostgreSQL到localhost时,触发以下KeyError:

ERROR (KeyError): Exception while executing as ubuntu@localhost: key not found: #<SSHKit::Host:0x000059c7eb101ed8 @keys=[], @local=false, @use...

容器虽能启动,但无法通过kamal-proxy访问服务。

配置问题分析

当前config/deploy.yml存在两处关键冲突:

  1. localhost的SSH配置冗余:本地部署场景下,Kamal默认直接执行本地命令,无需SSH密钥认证,但配置中强制指定ssh.user: ubuntu和keys_only: true,导致SSHKit找不到对应密钥。
  2. 数据库Accessory的Host配置错误:accessories.db.host设置为ubuntu@localhost,强制通过SSH连接本地容器,这在本地环境下完全不必要。

修复步骤

1. 调整本地SSH规则

修改deploy.yml的ssh部分,为localhost单独配置本地访问逻辑,跳过SSH密钥检查:

ssh:
  default:
    keys: [~/abel-ec2-keypair.pem]
    user: ubuntu
    keys_only: true
  localhost:
    user: $USER  # 使用当前本地系统用户
    keys_only: false  # 禁用密钥强制认证

2. 修正数据库Host配置

将accessories.db.host改为纯localhost,移除SSH用户前缀:

accessories:
  db:
    image: postgres:15
    host: localhost  # 去掉ubuntu@前缀
    port: 5432
    # 其余配置保持不变

3. 校验Proxy健康检查路径

确认后端容器内/swagger/index.html可正常访问,若该路径不存在,替换为后端实际提供的健康检查接口(比如/health):

proxy:
  healthcheck:
    interval: 5
    path: /health  # 替换为有效健康检查路径
    timeout: 300

4. 重新执行部署

清理旧部署残留并重新部署:

kamal down
kamal setup
kamal deploy

验证

部署完成后执行kamal status,确认proxy和backend容器均处于running状态,再访问http://localhost(默认proxy端口为80)验证服务可用性。

内容的提问来源于stack exchange,提问作者Abel Tesfaye

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 08:19:56