从C#docker访问kubernetes服务
我正在尝试使用kuberentes服务中的C#docker访问Kubernetes服务 我有一个PythonDocker YAML文件,并希望使用在PythonDocker的同一集群中运行的c#Dotnet core docker中的相同YAML以编程方式创建pod。我找到了用于dotnet core的Kubernetes api。我为下面的列表pods创建了代码从C#docker访问kubernetes服务,c#,docker,kubernetes,.net-core,kubernetes-apiserver,C#,Docker,Kubernetes,.net Core,Kubernetes Apiserver,我正在尝试使用kuberentes服务中的C#docker访问Kubernetes服务 我有一个PythonDocker YAML文件,并希望使用在PythonDocker的同一集群中运行的c#Dotnet core docker中的相同YAML以编程方式创建pod。我找到了用于dotnet core的Kubernetes api。我为下面的列表pods创建了代码 using System; using k8s; namespace simple { internal class Po
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!");
}
}
}
}
此代码获取错误禁止(“操作返回无效的状态代码‘禁止’”)。
使用BuildConfigFromConfigFile而不是InClusterConfig的代码在本地环境中工作。是否遗漏了任何内容
编辑
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'
},
InClusterConfig
使用部署pod的命名空间的default
服务帐户。默认情况下,该服务帐户将不会有任何导致禁止的
错误
它在本地环境中工作的原因是,它使用来自kubeconfig
文件的凭据,该文件大部分时间是对集群具有根级别RBAC权限的管理员凭据
您需要定义一个角色
,并使用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
InClusterConfig
使用部署pod的命名空间的default
服务帐户。默认情况下,该服务帐户将不会有任何导致禁止的
错误
它在本地环境中工作的原因是,它使用来自kubeconfig
文件的凭据,该文件大部分时间是对集群具有根级别RBAC权限的管理员凭据
您需要定义一个角色
,并使用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)创建了角色。在我检查了您提到的命令后,仍然是否。我在我的名称空间中尝试了此链接。我使用您提到的上述命令进行了检查。是的。但仍然出现了禁止的错误。和阿伦一样,我也遵循了这一点,虽然can-I命令返回“是”,但我仍然被禁止进入领奖台。正如阿伦在链接中所建议的那样,ClusterAdmin补充道,这就成功了。当我有机会计算出所需的特定权限时,我会尽量记住更新此权限!我在默认名称空间中使用上述yaml(kubectl delete-f tt.yaml)创建了角色。在我检查了您提到的命令后,仍然是否。我在我的名称空间中尝试了此链接。我使用您提到的上述命令进行了检查。是的。但仍然出现了禁止的错误。和阿伦一样,我也遵循了这一点,虽然can-I命令返回“是”,但我仍然被禁止进入领奖台。正如阿伦在链接中所建议的那样,ClusterAdmin补充道,这就成功了。当我有机会计算出所需的特定权限时,我会尽量记住更新此权限!您的应用程序能否列出“api”命名空间中的吊舱<代码>变量列表=client.ListNamespacedPod(“api”)代码>你的应用程序可以从“api”名称空间列出吊舱吗<代码>变量列表=client.ListNamespacedPod(“api”)代码>