解决 FluxCD 报错 GitRepository auth failed: publickey permission denied 私有仓库认证失败修复
排查 FluxCD source-controller 拉取 GitLab / GitHub 私网代码报 failed to clone: ssh: handshake failed: publickey,分析 SSH 密钥与 known_hosts。
问题现象
FluxCD 控制器无法与 Git 仓库建立连接,GitRepository 资源报认证失败:
$ flux get sources git
NAME REVISION SUSPENDED READY MESSAGE
flux-system False False failed to clone: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain
触发根因
- Kubernetes Secret 中保存的
identity私钥未被 GitLab / GitHub 部署公钥(Deploy Key)信任; - 缺少
known_hosts远端服务器指纹,或服务器公钥指纹变更导致 SSH 握手保护性中断; - 私钥生成时带有密码保护(Passphrase),而 Flux 仅支持免密专用部署私钥。
解决步骤
生成一套专用的免密 ed25519 部署密钥对
在本地终端生成:
ssh-keygen -t ed25519 -C "flux-deploy-key" -f /tmp/flux-key -N ""
将生成的 /tmp/flux-key.pub 公钥添加至 Git 平台的 Deploy Keys 中。
使用 flux CLI 标准命令创建安全 Secret
自动获取已知主机指纹并创建 Secret:
flux create secret git flux-system \
--url=ssh://git@github.com/mycorp/gitops.git \
--private-key-file=/tmp/flux-key
验证确认
强制刷新 Git 源:
flux reconcile source git flux-system
SSH 双向握手瞬间建立,Git 分支最新 Commit 秒级拉取成功,READY 变为 True。


