【问题标题】:Accessing kubernetes service from C# docker从 C# docker 访问 kubernetes 服务
【发布时间】:2020-11-23 16:46:09
【问题描述】:

我正在尝试使用 kuberentes 服务中的 C# docker 访问 Kubernetes 服务。

我有一个 python docker YAML 文件,并希望使用来自 c# Dotnet core docker 的相同 YAML 以编程方式创建 pod,该 docker 在同一 python docker 集群中运行。我找到了用于 dotnet core 的 Kubernetes api。我为下面的列表 pod 创建了代码。

using System;
using k8s;

namespace simple
{
    internal class PodList
    {
        private static void Main(string[] args)
        {
            var config = KubernetesClientConfiguration.InClusterConfig();
            IKubernetes client = new Kubernetes(config);
            Console.WriteLine("Starting Request!");

            var list = client.ListNamespacedPod("default");
            foreach (var item in list.Items)
            {
                Console.WriteLine(item.Metadata.Name);
            }

            if (list.Items.Count == 0)
            {
                Console.WriteLine("Empty!");
            }
        }
    }
}

此代码出现错误 Forbidden(“操作返回了无效的状态代码 'Forbidden'”)。 而不是 InClusterConfig 使用 BuildConfigFromConfigFile 代码在本地环境中工作。我错过了什么吗?

编辑

apiVersion: v1
kind: ServiceAccount
metadata:
  name: test-serviceaccount
  namespace: api

---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: api
  name: test-role
rules:
    - apiGroups: ["","apps","batch"]
      # "" indicates the core API group
      resources: ["deployments", "namespaces","cronjobs"]
      verbs: ["get", "list", "update", "patch","create"]  
  

  
---

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: test-binding
  namespace: api
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: test-role
subjects:
  - kind: ServiceAccount
    name: test-serviceaccount
    namespace: api

---


apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "4"
  creationTimestamp: "2019-07-04T16:05:43Z"
  generation: 4
  labels:
    app: test-console
    tier: middle-end
  name: test-console
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      app: test-console
      tier: middle-end
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: "2019-07-04T16:05:43Z"
      labels:
        app: test-console
        tier: middle-end
    spec:
      serviceAccountName: test-serviceaccount
      containers:
      - image: test.azurecr.io/tester:1.0.0
        imagePullPolicy: Always
        name: test-console
        ports:
        - containerPort: 80
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      imagePullSecrets:
      - name: pull
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      
C# code

  client.CreateNamespacedCronJob(jobmodel, "testnamesapce");
crone job
 'apiVersion': 'batch/v1beta1',
    'kind': 'CronJob',
    'metadata': {
        'creationTimestamp': '2020-08-04T06:29:19Z',
        'name': 'forcaster-cron',
        'namespace': 'testnamesapce'
    },

【问题讨论】:

  • 您的应用可以列出“api”命名空间中的 pod 吗? var list = client.ListNamespacedPod("api");

标签: c# docker kubernetes .net-core kubernetes-apiserver


【解决方案1】:

InClusterConfig 使用您正在部署 pod 的命名空间的 default 服务帐户。默认情况下,该服务帐户不会有任何RBAC 导致Forbidden 错误。

它在本地环境中工作的原因是因为它使用来自kubeconfig 文件的凭据,大部分时间是具有集群根级别 RBAC 权限的管理员凭据。

您需要定义一个Role 并使用RoleBinding 将该角色附加到服务帐户

因此,如果您在 default 命名空间中部署 pod,那么下面的 RBAC 应该可以工作。

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: myrole
  namespace: default
rules:
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - list
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: role-binding
  namespace: default
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: myrole
subjects:
- kind: ServiceAccount
  name: default
  namespace: default

一旦您在 RBAC 上申请,您可以使用以下命令检查服务帐户的权限

kubectl auth can-i list pods --as=system:serviceaccount:default:default -n default
yes

【讨论】:

  • 我在我的默认命名空间中使用上面的 yaml (kubectl delete -f tt.yaml) 创建了角色。在我检查你提到的命令后仍然没有。
  • 我在我的命名空间中尝试了这个链接。我检查了上面提到的命令。它是的。但我仍然遇到禁止错误。 github.com/fabric8io/fabric8/issues/6840#issuecomment-322984003
  • 和 Arun 一样,我跟着这个,虽然 can-i 命令返回是,但我仍然从 pod 内得到 Forbidden
  • 我按照 Arun 的链接中的建议添加了 ClusterAdmin,这样就成功了。当我有机会计算出所需的特定权限时,我会尽量记住更新它!
猜你喜欢
  • 2018-05-26
  • 1970-01-01
  • 2021-11-24
  • 2020-07-28
  • 2018-10-24
  • 1970-01-01
  • 2020-11-27
  • 2015-12-14
  • 1970-01-01
相关资源
最近更新 更多