【问题标题】:cannot query kubernetes (unauthorized): endpoints is forbidden: User cannot list endpoints in the namespace无法查询 kubernetes(未经授权):端点被禁止:用户无法在命名空间中列出端点
【发布时间】:2018-04-17 15:34:56
【问题描述】:

我在我的 gke 集群上运行 kubernetes 1.9.4

我有两个 pod,gate 正在尝试连接到 coolapp,两者都写在 elixir

我正在使用libcluster 连接我的节点 我收到以下错误:

[libcluster:app_name] cannot query kubernetes (unauthorized): endpoints is forbidden: User "system:serviceaccount:staging:default" cannot list endpoints in the namespace "staging": Unknown user "system:serviceaccount:staging:default"

这是我在 gateconfig/prod 下的配置:

 config :libcluster,
 topologies: [
   app_name: [
     strategy: Cluster.Strategy.Kubernetes,
     config: [
       kubernetes_selector: "tier=backend",
       kubernetes_node_basename: System.get_env("MY_POD_NAMESPACE") || "${MY_POD_NAMESPACE}"]]]

这是我的配置:

vm-args

## Name of the node
-name ${MY_POD_NAMESPACE}@${MY_POD_IP}
## Cookie for distributed erlang
-setcookie ${ERLANG_COOKIE}
# Enable SMP automatically based on availability
-smp auto

创造秘密:

kubectl create secret generic erlang-config --namespace staging --from-literal=erlang-cookie=xxxxxx
kubectl create configmap vm-config --namespace staging --from-file=vm.args

gate/deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: gate
  namespace: staging
spec:
  replicas: 1
  revisionHistoryLimit: 1
  strategy:
      type: RollingUpdate
  template:
    metadata:
      labels:
        app: gate
        tier: backend
    spec:
      securityContext:
        runAsUser: 0
        runAsNonRoot: false
      containers:
      - name: gate
        image: gcr.io/development/gate:0.1.7
        args:
          - foreground
        ports:
        - containerPort: 80
        volumeMounts:
        - name: config-volume
          mountPath: /beamconfig
        env:
        - name: MY_POD_NAMESPACE
          value: staging
        - name: MY_POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: RELEASE_CONFIG_DIR
          value: /beamconfig
        - name: ERLANG_COOKIE
          valueFrom:
            secretKeyRef:
              name: erlang-config
              key: erlang-cookie
      volumes:
      - name: config-volume
        configMap:
          name: vm-config

coolapp/deployment.yaml:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: coolapp
  namespace: staging
spec:
  replicas: 1
  revisionHistoryLimit: 1
  strategy:
      type: RollingUpdate
  template:
    metadata:
      labels:
        app: coolapp
        tier: backend
    spec:
      securityContext:
        runAsUser: 0
        runAsNonRoot: false
     # volumes
      volumes:
      - name: config-volume
        configMap:
          name: vm-config
      containers:
      - name: coolapp
        image: gcr.io/development/coolapp:1.0.3
        volumeMounts:
        - name: secrets-volume
          mountPath: /secrets
          readOnly: true
        - name: config-volume
          mountPath: /beamconfig
        ports:
        - containerPort: 80
        args:
          - "foreground"
        env:
        - name: MY_POD_NAMESPACE
          value: staging
        - name: MY_POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: REPLACE_OS_VARS
          value: "true"
        - name: RELEASE_CONFIG_DIR
          value: /beamconfig
        - name: ERLANG_COOKIE
          valueFrom:
            secretKeyRef:
              name: erlang-config
              key: erlang-cookie
        # proxy_container
      - name: cloudsql-proxy
        image: gcr.io/cloudsql-docker/gce-proxy:1.11
        command: ["/cloud_sql_proxy", "--dir=/cloudsql",
            "-instances=staging:us-central1:com-staging=tcp:5432",
            "-credential_file=/secrets/cloudsql/credentials.json"]
        volumeMounts:
          - name: cloudsql-instance-credentials
            mountPath: /secrets/cloudsql
            readOnly: true
          - name: cloudsql
            mountPath: /cloudsql

【问题讨论】:

    标签: erlang kubernetes elixir google-kubernetes-engine


    【解决方案1】:

    staging 命名空间的默认 service account(显然您使用 libcluster 的 Pod 正在其中运行)缺少在该命名空间中获取端点RBAC 权限。

    您的应用程序可能需要许多其他权限(上述错误消息中未提及)才能正常工作;识别所有此类权限超出了 SO 的范围。

    解决此问题的一种方法是授予该服务帐户的超级用户权限。这不是一个安全的解决方案,而是一个权宜之计。

    $ kubectl create clusterrolebinding make-staging-sa-cluster-admin \
        --serviceaccount=staging:default \
        --clusterrole=cluster-admin
    
    clusterrolebinding "make-staging-sa-cluster-admin" created
    

    要授予特定权限(在暂存命名空间中获取端点),您需要先创建一个Role

    apiVersion: rbac.authorization.k8s.io/v1
    kind: Role
    metadata:
      name: some-permissions
      namespace: staging
    rules:
    - apiGroups: [""]
      resources: ["endpoints"]
      verbs: ["get", "list", "watch"]
    

    并为暂存命名空间中的默认服务帐户创建一个RoleBinding

    apiVersion: rbac.authorization.k8s.io/v1
    kind: RoleBinding
    metadata:
      name: give-default-sa-some-permissions
      namespace: staging
    subjects:
    - kind: ServiceAccount
      name: default
      namespace: staging
    roleRef:
      kind: Role
      name: some-permissions
      apiGroup: rbac.authorization.k8s.io
    

    【讨论】:

    • 我收到Error from server (Forbidden): error when creating "some-permissions.yaml": roles.rbac.authorization.k8s.io "some-permissions" is forbidden: attempt to grant extra privileges:
    • 您没有指定您的环境,但如果您正在使用 GKE,请检查 stackoverflow.com/a/46316672/371954 。否则,我建议你开始一个新问题。
    【解决方案2】:

    不是 erlang/elixir 或 libcluster 用户,但它似乎正在尝试使用命名空间的默认服务帐户来尝试向 master 查询集群中可用的端点列表。

    libcluster 的自述文件说了这么多:

    如果设置为 Cluster.Strategy.Kubernetes,它将使用 Kubernetes API 根据基本名称和标签选择器查询端点,使用 注入每个 pod 的令牌和命名空间;一旦它有一个列表 端点,它使用该列表形成一个集群,并保持它 日期。

    阅读code for the kubernetes.ex in libcluster 并确认您得到的错误。

    您需要为暂存命名空间中的服务帐户设置 ClusterRole 和 RoleBinding。这将允许 libcluster 动态查询 master 以发现集群/命名空间中的其他 erlang 节点。

    以下是一些方便的后续阅读资源:

    【讨论】:

      猜你喜欢
      • 2021-06-24
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      相关资源
      最近更新 更多