快速开始
本文档将帮助您快速了解如何创建 Git Connector 并使用它来安全地克隆仓库,而无需直接处理凭据。
目录
介绍
适用场景
Git Connector 允许您通过以下方式安全地执行代码克隆操作:
- 集中管理凭据,而不是将其硬编码在工作负载中
- 在克隆过程中自动注入认证信息
- 为团队间的私有仓库访问提供受控权限
该方法特别适用于:
- 需要安全访问私有仓库的 CI/CD 流水线
- 团队共享仓库访问权限而不共享凭据
- 需要集中管理 Git 凭据的环境
预计阅读时间
15 分钟
注意事项
- Git connector 使用 CSI 驱动集成来安全注入 Git 凭据。
- Connector 生成的配置在 30 分钟后过期。
前提条件
- 已安装 Connectors 系统(Operator、Core 和 Git 组件)的 Kubernetes 集群。有关安装这些组件的详细信息,请参见安装指南。
- 已配置 kubectl 以与您的集群通信
- 拥有带有效凭据(用户名/密码或令牌)的 Git 仓库
- 具备 Kubernetes 资源的基础知识
流程概览
序号 | 操作步骤 | 说明 |
---|
1 | 创建 Namespace | 创建一个专用的命名空间用于演示 |
2 | 创建 Git 凭据和 Connector | 设置用于 Git 访问的凭据和 connector |
3 | 配置 RBAC 权限 | 授予使用 connector 的适当权限 |
4 | 创建克隆 Job | 部署一个使用 connector 克隆仓库的 Job |
5 | 验证操作 | 检查仓库是否成功克隆 |
操作步骤
步骤 1:创建 Namespace
为本次演示创建一个专用的命名空间:
kubectl create ns git-connector-demo
步骤 2:创建 Git 凭据和 Connector
创建包含 Git 凭据的 Secret 和 Git Connector 资源。有关创建和配置 connector 的详细信息,请参阅Connectors 快速开始指南。
cat <<EOF | kubectl apply -f -
kind: Secret
apiVersion: v1
metadata:
name: git-auth
namespace: git-connector-demo
type: kubernetes.io/basic-auth
stringData:
username: your-username # 替换为您的 Git 用户名
password: your-token # 替换为您的 Git 密码或令牌
---
apiVersion: connectors.alauda.io/v1alpha1
kind: Connector
metadata:
name: git-connector
namespace: git-connector-demo
spec:
connectorClassName: git
address: https://github.com # 替换为您的 Git 服务器地址
auth:
name: basicAuth
secretRef:
name: git-auth
params:
- name: repository
value: your-org/your-repo.git # 替换为您的仓库路径
EOF
验证 connector 是否处于 "Ready" 状态:
kubectl get connector git-connector -n git-connector-demo
输出应显示:
NAME CLASS ADDRESS READY AGE
git-connector git https://github.com True 1m
步骤 3:创建克隆 Job
创建一个使用 connector 克隆仓库的 Job:
cat <<EOF | kubectl apply -f -
apiVersion: batch/v1
kind: Job
metadata:
name: git-clone
namespace: git-connector-demo
spec:
template:
spec:
restartPolicy: Never
containers:
- name: git
image: bitnami/git:2.47.1
imagePullPolicy: IfNotPresent
command:
- "git"
args: [ "clone", "--progress", "https://github.com/your-org/your-repo.git", "/tmp/repo" ] # 替换为您的仓库地址
volumeMounts:
- name: gitconfig
mountPath: /root/
volumes:
- name: gitconfig
csi:
readOnly: true
driver: connectors-csi
volumeAttributes:
connector.name: "git-connector"
configuration.names: "gitconfig"
EOF
关键参数说明:
connector.name
:您的 Git connector 名称
configuration.names
:设置为 "gitconfig",引用 Git ConnectorClass 中定义的特定配置模板。该模板用于生成包含认证和 URL 重写设置的 ".gitconfig" 文件。
mountPath
:必须设置为 "/root/",因为容器以 root 用户运行,Git 会在 "/root/.gitconfig" 路径查找配置
步骤 4:验证操作
查看 Job 日志,确认仓库已成功克隆:
kubectl logs -f job/git-clone -n git-connector-demo
您应看到 Git 克隆操作成功完成且无认证错误。
预期结果
完成所有步骤后,您将看到:
-
Git connector 处于 "Ready" 状态:
NAME CLASS ADDRESS READY AGE
git-connector git https://github.com True 5m
-
Job 日志中显示成功的 Git 克隆操作:
Cloning into '/tmp/repo'...
remote: Enumerating objects: 1324, done.
remote: Counting objects: 100% (1324/1324), done.
remote: Compressing objects: 100% (712/712), done.
remote: Total 1324 (delta 612), reused 1324 (delta 612), pack-reused 0
Receiving objects: 100% (1324/1324), 2.56 MiB | 4.25 MiB/s, done.
Resolving deltas: 100% (612/612), done.
工作原理
Git Connector 的工作流程:
- 用代理服务 URL 替换原始 Git 仓库 URL
- 在请求代理服务时注入认证信息
- 代理服务在转发请求到 Git 服务器时添加必要的凭据
查看生成的配置:
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: inspect-git-config
namespace: git-connector-demo
spec:
restartPolicy: Never
containers:
- name: git
image: bitnami/git:2.47.1
command: ["sleep", "3600"]
volumeMounts:
- name: gitconfig
mountPath: /root/
volumes:
- name: gitconfig
csi:
readOnly: true
driver: connectors-csi
volumeAttributes:
connector.name: "git-connector"
configuration.names: "gitconfig"
EOF
查看生成的配置内容:
kubectl exec -it inspect-git-config -n git-connector-demo -- cat /root/.gitconfig
示例输出:
[http]
extraHeader = Authorization: Basic OmV5Smhixxxxxxxxx==
[url "http://c-git-connector.git-connector-demo.svc]
insteadOf = https://github.com
故障排查
如果克隆操作失败,请检查以下内容:
-
Connector 状态:确保 connector 处于 "Ready" 状态:
kubectl describe connector git-connector -n git-connector-demo
-
RBAC 权限:确认 RoleBinding 配置正确。
-
Job 配置:
- 确认卷挂载路径正确(/root/)
- 验证仓库 URL 与 connector 中配置的一致
后续步骤
成功使用 Git Connector 克隆第一个仓库后,您可以:
- 将此方法集成到您的 CI/CD 流水线中
- 在其他 Git 操作中使用该 connector,如 push、pull 和 fetch
- 为不同的 Git 服务创建更多 connector