【问题标题】:kubectl apply Error from server (Forbidden) Authentication required - Jenkinskubectl apply 来自服务器的错误(禁止)需要身份验证 - Jenkins
【发布时间】:2020-04-22 01:42:46
【问题描述】:

我在 Windows 10 上安装了 Jenkins,minikube 集群是 Virtual Box VM

在 minikube 集群上,我使用这个 yaml 创建了服务帐户:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: jenkins
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: jenkins
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["create","delete","get","list","patch","update","watch"]
- apiGroups: [""]
  resources: ["pods/exec"]
  verbs: ["create","delete","get","list","patch","update","watch"]
- apiGroups: [""]
  resources: ["pods/log"]
  verbs: ["get","list","watch"]
- apiGroups: [""]
  resources: ["secrets"]
  verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: jenkins
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: jenkins
subjects:
- kind: ServiceAccount
  name: jenkins

列出sa:

kubectl get sa
NAME      SECRETS   AGE
default   1         128m
jenkins   1         99m

kubectl describe sa jenkins
Name:                jenkins
Namespace:           default
Labels:              <none>
Annotations:         kubectl.kubernetes.io/last-applied-configuration:
                       {"apiVersion":"v1","kind":"ServiceAccount","metadata":{"annotations":{},"name":"jenkins","namespace":"default"}}
Image pull secrets:  <none>
Mountable secrets:   jenkins-token-rk2mg
Tokens:              jenkins-token-rk2mg
Events:              <none>

我使用该帐户的令牌并在 Jenkins 上配置了 Kubernetes 插件,连接成功

在 Jenkins 文件中,我添加了获取 kubectl 版本的阶段:

stage('Check kubectl version') {
         steps {
                 sh 'kubectl version'
          }
      }

我得到了:

+ kubectl version
Client Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.0", GitCommit:"70132b0f130acc0bed193d9ba59dd186f0e634cf", GitTreeState:"clean", BuildDate:"2019-12-07T21:20:10Z", GoVersion:"go1.13.4", Compiler:"gc", Platform:"windows/amd64"}
Error from server (Forbidden): <html><head><meta http-equiv='refresh' content='1;url=/login?from=%2Fversion%3Ftimeout%3D32s'/><script>window.location.replace('/login?from=%2Fversion%3Ftimeout%3D32s');</script></head><body style='background-color:white; color:white;'>


    Authentication required
    <!--
    You are authenticated as: anonymous
    Groups that you are in:

    Permission you need to have (but didn't): hudson.model.Hudson.Read
     ... which is implied by: hudson.security.Permission.GenericRead
     ... which is implied by: hudson.model.Hudson.Administer
    -->

【问题讨论】:

  • 您是如何安装 jenkins 的?你有没有在 jenkins github 上一步步关注running locally with minikube?在stackoverflow 上找到了类似的问题,看看吧,也许有什么可以帮助你的。让我知道是否有帮助。
  • 从msi包安装的,没有按照说明,周末后试试

标签: jenkins kubernetes kubectl minikube


【解决方案1】:

我遇到了同样的问题。而且Jenkins里面有好几个k8s环境。

原来kubectl apply 命令是

kubectl apply -f <directory-or-file>

要解决这个问题,添加--context参数来添加特定的集群

kubectl apply -f <directory-or-file> --context <CLUSTER_NAME>

【讨论】:

    【解决方案2】:

    您的身份验证为:匿名

    您必须以您为 Jenkins 创建的 ServiceAccount jenkins 进行身份验证。

    在您的 Jenkinsfile 步骤/阶段中使用 withCredentials 并加载属于 jenkins 的 ServiceAccount 的令牌。您必须首先使用属于您生成的 ServiceAccount 的令牌来识别密钥。

    使用 kubectl 命令时,指定您要使用令牌进行身份验证,可能还需要 ApiServer 的服务器主机名。

    例如像这样:

    kubectl apply -f <diretory-or-file> --token $TOKEN_FROM_WITH_CREDENTIALS --server apiserver.hostname.local
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-14
      • 2020-01-15
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多