背景:
在gitlib-ci 对接kubernetes的时候报错:
ERROR: Job failed (system failure): pods is forbidden: User “system:serviceaccount:dev:default” cannot create resource “pods” in API group “” in the namespace “dev”
原因:
k8s使用的RBAC权限访问控制,当前对namespace dev 没有操作权限
我在gitlab-ci.yml 文件里面 使用 --kubeconfig 带 kubeadm.conf 的admin权限管理文件去认证操作
一般存在于master节点的/root/.kube/config
kubectl --kubeconfig dev.conf -n dev get pod
我的集群使用的kubeadm安装的, 默认对每一个命名空间有一个默认的serviceaccount :default
如果在CI里面没有特别指定serviceaccount 那么将使用默认账户 system:serviceaccount:dev:default
最终的原因就是没有创建 对应 namespaces 的 集群角色绑定clusterrolebinding
解决办法:
执行一下命令,创建clusterrolebinding即可
kubectl create clusterrolebinding gitlab-cluster-admin --clusterrole=cluster-admin --group=system:serviceaccounts --namespace=dev
效果
附件:
gitlab-ci.yml
stages:
- build
- deploy
k8s-deploy:
stage: deploy
image: http://myharbor/kubectl:latest
only:
- ci-test
script:
- kubectl --kubeconfig dev.conf -n dev get pod
tags:
- k8s-runner
runner配置文件config.toml
[runners.kubernetes]
host = ""
bearer_token_overwrite_allowed = false
image = "docker:dind"
namespace = "dev"
namespace_overwrite_allowed = ""
privileged = true
pull_policy = "if-not-present"
helper_image = "http://myharbor/cfss/gitlab-runner-helper:x86_64-ac2a293c"
service_account_overwrite_allowed = ""
pod_annotations_overwrite_allowed = ""
参考:
https://gist.github.com/Machy8/da45ed785b0a14ab625f9b2614b9829b