8-Overview-Namespaces
concepts/overview/working-with-objects/namespaces/
Namespaces
kubernetes支持由同一物理集群支持的多个虚拟集群。这些虚拟集群称为名称空间(Namespaces, NS)。
- When to Use Multiple Namespaces
- Working with Namespaces
- Namespaces and DNS
- Not All Objects are in a Namespace
- What's next
When to Use Multiple Namespaces
名称空间旨在用于具有分布在多个团队或项目中的许多用户的环境中。对于拥有几个到几十个用户的集群,您根本不需要创建或考虑名称空间。在需要名称空间提供的功能时开始使用名称空间。(简而言之当团队人数很多时,使用不同的名称空间更有意义,否则额外增加维护成本)
Namespaces provide a scope for names. Names of resources need to be unique within a namespace, but not across namespaces. Namespaces can not be nested inside one another and each Kubernetes resource can only be in one namespace.名称空间提供名称的作用域。资源
的名称在命名空间中必须是唯一
的,但不能跨命名空间。名称空间不能相互嵌套
,每个kubernetes资源只能位于一个名称空间中。
Namespaces are a way to divide cluster resources between multiple users 名称空间是在多个用户之间划分集群资源的一种方法。(via resource quota).
在kubernetes的未来版本中,默认情况下,相同命名空间中的对象将具有相同的访问控制策略。
不必使用多个名称空间来分隔稍有不同的资源,例如同一软件的不同版本:使用标签来区分同一名称空间中的资源。
Working with Namespaces
名称空间的创建和删除在[名称空间管理指南文档] Admin Guide documentation for namespaces.
查看命名空间
可以使用以下命令列出群集中的当前命名空间:
kubectl get namespace
NAME STATUS AGE
default Active 1d
kube-system Active 1d
kube-public Active 1d
Kubernetes 安装后默认有三个名称空间:
default
没有其他命名空间的对象的默认命名空间kube-system
由kubernetes系统创建的对象的命名空间(集群关键组件)kube-public
此命名空间是自动创建的,所有用户(包括未经身份验证的用户)都可以读取。这个名称空间主要是为集群使用而保留的,以防某些资源在整个集群中公开可见和可读。这个名称空间的公共方面只是一个约定,而不是一个要求。
设置请求的命名空间
要为当前请求设置命名空间,请使用--namespace
参数.
例如:
kubectl run nginx --image=nginx --namespace=<insert-namespace-name-here>
kubectl get pods --namespace=<insert-namespace-name-here>
设置命名空间首选项(设置默认NS)
可以永久保存该上下文中所有后续kubectl命令的命名空间。
kubectl config set-context --current --namespace=<insert-namespace-name-here>
# Validate it
kubectl config view | grep namespace:
命名空间和DNS
When you create a Service, it creates a corresponding DNS entry. This entry is of the form <service-name>.<namespace-name>.svc.cluster.local
, which means that if a container just uses <service-name>
, it will resolve to the service which is local to a namespace. This is useful for using the same configuration across multiple namespaces such as Development, Staging and Production. If you want to reach across namespaces, you need to use the fully qualified domain name (FQDN).创建服务时,它会创建相应的dns条目。此条目的格式为..svc.cluster.local,这意味着如果容器仅使用,它将解析为命名空间的本地服务。这对于跨多个命名空间(如开发、登台和生产)使用相同的配置非常有用。如果要跨命名空间访问,则需要使用完全限定的域名(fqdn)。
Not All Objects are in a Namespace 并非所有对象都在命名空间中
Most Kubernetes resources (e.g. pods, services, replication controllers, and others) are in some namespaces. However namespace resources are not themselves in a namespace. And low-level resources, such as nodes and persistentVolumes, are not in any namespace.
大多数kubernetes资源(例如pods、服务、复制控制器和其他)位于某些名称空间中。但是命名空间资源本身不在命名空间中。而低级别资源(如节点和persistentVolumes)不在任何命名空间中。
要查看哪些kubernetes资源在命名空间中,哪些不在命名空间中,请执行以下操作:
# In a namespace
kubectl api-resources --namespaced=true
# Not in a namespace
kubectl api-resources --namespaced=false
What's next
- Learn more about creating a new namespace.
- Learn more about deleting a namespace.
本作品采用《CC 协议》,转载必须注明作者和本文链接