在pod内为命令kubectl定义/设置默认名称空间的地方 [英] Where the default namespace is defined/setup for command kubectl inside of a pod

查看:72
本文介绍了在pod内为命令kubectl定义/设置默认名称空间的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:Azure AKS中有群集A和群集B.创建一个名为 Agent 的Pod,在集群A中的命名空间 test (这是非默认命名空间)中运行linux容器.在linux容器中,安装了pwsh和kubectl.

Background: There are Cluster A and Cluster B in Azure AKS. Create a pod called Agent running linux container in cluster A in namespace test (which is non-default namespace). In the linux container, pwsh and kubectl are installed.

操作:进入集群A中的pod/Agent(kubectl exec -it pod/agent-bash),并获取集群B的凭据,将使用集群名称和用户名,但没有名称空间.
从pod/Agent连接到集群B时,然后执行 kubectl get pods ,将返回名称空间 test 中的资源,而不是名称空间 default 中的资源.>.
由于群集B中没有名为 test 的名称空间,因此不会返回任何资源.

Operation: Get into the pod/Agent in cluster A (kubectl exec -it pod/agent -- bash), and get-credential of Cluster B, configfile will be setup with cluster name and user name, but NO namespace.
When connect to cluster B from pod/Agent, then execute kubectl get pods, the resource within namespace test is returned instead of the resources within namespace default.
Since, there is no namespace called test in cluster B, so no resource is returned.

因此,我想知道在Pod/Agent中将名称空间 test 定义/设置为默认名称空间的地方.

So I wonder where the namespace test is defined/setup in the pod/Agent as the default namespace.

花一些时间尝试在github中潜入kubectl代码,但是没有运气.

Spent some time try to dive in kubectl code in github, without luck..

我也尝试使用别名,但是它仅适用于bash/sh,不适用于pwsh,因为我不想更改命令名称kubectl,如果我使用别名kubectl ='kubectl -n default',则pwsh会陷入循环.

I also tried to use alias, but it only works for bash/sh, not for pwsh, since I don't want to change command name kubectl, if I do alias kubectl='kubectl -n default', pwsh would stuck into a loop.

任何答案都值得赞赏.

推荐答案

来自

最后,用于命名空间API的默认命名空间操作位于文件中/var/run/secrets/kubernetes.io/serviceaccount/namespace中的每个容器.

Finally, the default namespace to be used for namespaced API operations is placed in a file at /var/run/secrets/kubernetes.io/serviceaccount/namespace in each container.

从吊舱进行的简单测试:

Simple test from a pod:

root@ubuntu:/# strace -eopenat kubectl get pod 2>&1 | grep namespace
openat(AT_FDCWD, "/var/run/secrets/kubernetes.io/serviceaccount/namespace", O_RDONLY|O_CLOEXEC) = 6
Error from server (Forbidden): pods is forbidden: User "system:serviceaccount:default:default" cannot list resource "pods" in API group "" in the namespace "default"

默认情况下,

目录/run/secrets/kubernetes.io/serviceaccount 始终安装在pod上,并且包含用于访问Kube API的serviceaccount令牌.

Directory /run/secrets/kubernetes.io/serviceaccount is by default always mounted to pod and contains serviceaccount token to access Kube API.

这篇关于在pod内为命令kubectl定义/设置默认名称空间的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆