授权Jenkins在其自己的K8S名称空间中列出pod

问题描述 投票:0回答:1

所以我使用Helm在我的K8S集群中部署Jenkins实例。

以下是我关注的流程:

1)创建名为jenkins-pipeline的命名空间。

kubectl get ns jenkins-pipeline -o yaml
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: 2018-09-15T16:58:33Z
  name: jenkins-pipeline
  resourceVersion: "25596"
  selfLink: /api/v1/namespaces/jenkins-pipeline
  uid: 9449b9e7-b908-11e8-a915-080027bfdbf9
spec:
  finalizers:
  - kubernetes
status:
  phase: Active

2)创建名为jenkins-admin INSIDE命名空间jenkins-pipeline的ServiceAccount。

kubectl get serviceaccounts -n jenkins-pipeline jenkins-admin -o yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  creationTimestamp: 2018-09-15T17:02:25Z
  name: jenkins-admin
  namespace: jenkins-pipeline
  resourceVersion: "25886"
  selfLink: /api/v1/namespaces/jenkins-pipeline/serviceaccounts/jenkins-admin
  uid: 1e921d43-b909-11e8-a915-080027bfdbf9
secrets:
- name: jenkins-admin-token-bhvdd

3)创建ClusterRoleBinding,将我的ServiceAccount jenkins-admin链接到ClusterRole cluster-admin。 (我知道这不是为我的部署分配那么多特权的最佳做法,但我只是在本地进行测试)。

kubectl get clusterrolebindings.rbac.authorization.k8s.io jenkins-cluster-role-binding -o yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  creationTimestamp: 2018-09-15T16:58:33Z
  name: jenkins-cluster-role-binding
  resourceVersion: "25597"
  selfLink: /apis/rbac.authorization.k8s.io/v1/clusterrolebindings/jenkins-cluster-role-binding
  uid: 944a4c18-b908-11e8-a915-080027bfdbf9
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: jenkins-admin
  namespace: jenkins-pipeline

4)在命名空间jenkins-pipeline中部署我的pod。

5)使用命名空间jenkins-pipeline中的服务公开部署。

Jenkins完全没问题,但是当我尝试测试我的Kuberenetes连接时,它失败了:

Error testing connection https://192.168.99.100:8443: Failure executing: GET at: https://192.168.99.100:8443/api/v1/namespaces/jenkins-pipeline/pods. Message: Forbidden!Configured service account doesn't have access. Service account may have been revoked. pods is forbidden: User "system:serviceaccount:jenkins-pipeline:default" cannot list pods in the namespace "jenkins-pipeline".

UI的片段如下所示:

enter image description here

我已经根据我的知识配置了这个。我在命名空间中创建了serviceaccount,并赋予了这个serviceaccount SUPER权限。然而,它无法在其自己的命名空间中列出pod。任何帮助将不胜感激。

我试图在Jenkins UI中更改命名空间,但我觉得它默认为jenkins-pipeline,即使我没有说明。

jenkins kubernetes jenkins-plugins
1个回答
1
投票

感谢David Maze的指示,我明白了。我错过了一个关键部分,即使我的部署使用新创建的ServiceAccount。

需要将它添加到spec.template.spec下的部署文件中:

  spec:
    containers:
    - env:
      - name: JAVA_OPTS
        value: -Djenkins.install.runSetupWizard=false
      image: jeunii/my-jenkins-base:1.0
      imagePullPolicy: IfNotPresent
    .
    .
    serviceAccount: jenkins-admin
    .
    .
© www.soinside.com 2019 - 2024. All rights reserved.