GitLab 连接已有 Kubernetes 集群

时间:2021-02-02 11:11:16   收藏:0   阅读:0

在 gitlab 的 Amin Area 页面,打开侧边栏的 Kubernetes ,点击 Integrate with a cluster certificates,选择 Connect existing cluster,输入以下信息

kubectl get secret     $(kubectl get secrets | grep default-token | awk ‘{print $1}‘)     -o jsonpath="{[‘data‘][‘ca\.crt‘]}" | base64 --decode

然后点击 "Add Kubernetes cluster" 按钮进行提交,提交时报错:

is blocked: Requests to the local network are not allowed

报错解决方法:在 Admin Area -> Settings -> Network 中选中 "Allow requests to the local network from web hooks and services" 并保存。

再次提交成功。

在 gitlab 服务器的 hosts 文件中添加 api url 用到的主机名 k8s-api 的解析(我们搭建的是高可用集群,所以这里用的是自定义主机名)。

配置完成,gitlab 却连不上 k8s 集群。

技术图片

登录到 gitlab 服务器,将之前拿到的证书保存到 ca.crt 文件,用下面的 curl 命令请求 api

curl --cacert ca.crt -H "Authorization: Bearer <service token>" https://k8s-api:6443/api/v1/namespaces/production/pods

返回的是 403 错误

{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {
    
  },
  "status": "Failure",
  "message": "pods is forbidden: User \"system:serviceaccount:kube-system:gitlab\" cannot list resource \"pods\" in API group \"\" in the namespace \"production\"",
  "reason": "Forbidden",
  "details": {
    "kind": "pods"
  },
  "code": 403
}

通过上面的错误信息找到了线索,请求 api 时使用的 service account 是 kube-system:gitlab ,但我们创建 ClusterRoleBinding 时用的是 --serviceaccount=default:gitlab,命名空间弄错了,通过下面的命令编辑 ClusterRoleBinding 配置文件,更正命名空间后问题就解决了。

kubectl edit clusterrolebinding gitlab-cluster-admin -n kube-system

gitlab 连接 k8s 集群成功!

技术图片

参考资料:

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!