小言_互联网的博客

Kubernetes基础:namespace使用总结

419人阅读  评论(0)


namespace是Kubernetes中的一个重要概念,这篇文章总结和整理一些相关的一些主要操作方式和基本概念。

环境准备

快速环境搭建建议使用单机版Kubernetes的安装脚本,一键安装,详情可参看:

什么是namespace

根据Kubernetes的官方介绍,对于namespace有如下描述:

Kubernetes supports multiple virtual clusters backed by the same physical cluster. These virtual clusters are called namespaces.

在Kubernetes的物理集群之上创建的“虚拟集群”,这样的“虚拟集群”就被成为namespace。本质上属于抽象级别较容器跟深的隔离,这样在实际的PaaS平台中所需要的“租户”的能力,很多小型的需求下都是以namespace为中心进行封装和隔离的。

管理namespace

使用kubectl或者Restful API均可很简单地实现对于namespace的增删查询等操作,详细可参看如下内容:

  • namespace管理:https://liumiaocn.blog.csdn.net/article/details/100537187

初始的namespace

Kubernetes集群安装之后,初始状态下有default、kube-public和kube-system三个namespace。

[root@host132 ~]# kubectl get namespaces
NAME              STATUS   AGE
default           Active   4h14m
kube-public       Active   4h14m
kube-system       Active   4h14m
[root@host132 ~]# 

这三个namespace的说明如下所示:

namespace名称 作用说明
default 缺省的namespace,对于没有指定namespace的对象缺省会在此处进行管理
kube-system Kubernetes系统所创建的对象在此namespace中进行管理
kube-public 此namespace对所有用户均为可读(包括通过认证的用户)。此namespace的存在实施一种约定,并非为必选项。

使用方式

在kubectl中一般通过-n或者--namespace来指定所限定的范围

资源对象与namespace的关系

大多数Kubernetes资源诸如pod、service等都是在namespace中进行管理,但是也有一些资源诸如节点node或者持久卷persistentVolume不属于任何namespace,对于资源对象和namespace的关系如下所示

NAME SHORTNAMES APIGROUP NAMESPACED KIND
bindings true Binding
componentstatuses cs false ComponentStatus
configmaps cm true ConfigMap
endpoints ep true Endpoints
events ev true Event
limitranges limits true LimitRange
namespaces ns false Namespace
nodes no false Node
persistentvolumeclaims pvc true PersistentVolumeClaim
persistentvolumes pv false PersistentVolume
pods po true Pod
podtemplates true PodTemplate
replicationcontrollers rc true ReplicationController
resourcequotas quota true ResourceQuota
secrets true Secret
serviceaccounts sa true ServiceAccount
services svc true Service
mutatingwebhookconfigurations admissionregistration.k8s.io false MutatingWebhookConfiguration
validatingwebhookconfigurations admissionregistration.k8s.io false ValidatingWebhookConfiguration
customresourcedefinitions crd,crds apiextensions.k8s.io false CustomResourceDefinition
apiservices apiregistration.k8s.io false APIService
controllerrevisions apps true ControllerRevision
daemonsets ds apps true DaemonSet
deployments deploy apps true Deployment
replicasets rs apps true ReplicaSet
statefulsets sts apps true StatefulSet
auditsinks auditregistration.k8s.io false AuditSink
tokenreviews authentication.k8s.io false TokenReview
localsubjectaccessreviews authorization.k8s.io true LocalSubjectAccessReview
selfsubjectaccessreviews authorization.k8s.io false SelfSubjectAccessReview
selfsubjectrulesreviews authorization.k8s.io false SelfSubjectRulesReview
subjectaccessreviews authorization.k8s.io false SubjectAccessReview
horizontalpodautoscalers hpa autoscaling true HorizontalPodAutoscaler
cronjobs cj batch true CronJob
jobs batch true Job
certificatesigningrequests csr certificates.k8s.io false CertificateSigningRequest
leases coordination.k8s.io true Lease
events ev events.k8s.io true Event
daemonsets ds extensions true DaemonSet
deployments deploy extensions true Deployment
ingresses ing extensions true Ingress
networkpolicies netpol extensions true NetworkPolicy
podsecuritypolicies psp extensions false PodSecurityPolicy
replicasets rs extensions true ReplicaSet
ingresses ing networking.k8s.io true Ingress
networkpolicies netpol networking.k8s.io true NetworkPolicy
runtimeclasses node.k8s.io false RuntimeClass
poddisruptionbudgets pdb policy true PodDisruptionBudget
podsecuritypolicies psp policy false PodSecurityPolicy
clusterrolebindings rbac.authorization.k8s.io false ClusterRoleBinding
clusterroles rbac.authorization.k8s.io false ClusterRole
rolebindings rbac.authorization.k8s.io true RoleBinding
roles rbac.authorization.k8s.io true Role
priorityclasses pc scheduling.k8s.io false PriorityClass
podpresets settings.k8s.io true PodPreset
csidrivers storage.k8s.io false CSIDriver
csinodes storage.k8s.io false CSINode
storageclasses sc storage.k8s.io false StorageClass
volumeattachments storage.k8s.io false VolumeAttachment
  • 查询在namespace中的资源对象

执行命令:kubectl api-resources --namespaced=true

  • 查询不在namespace中的资源对象

执行命令:kubectl api-resources --namespaced=false

  • 查询资源对象与namespace的关系

执行命令:kubectl api-resources

常用使用方式

对namespace有很多种使用注意事项,结合官方的说明,简单整理如下:

注意事项1

namespace的引入是为了解决横跨多个团队或者项目并具有很多用户的情况,如果集群只有几个或者十几个用户的情况下,不建议使用创建新的namespace。

注意事项2

namespace提供了对名称范围的控制,同一个namespace中资源的名称需要保证唯一性,而在不同的namespace中则不受这一规则的限制。
另外,namespace不能嵌套,每一个Kubernetes的资源只能存在于一个namespace中。

注意事项3

namespace提供了为多用户提供集群资源分配的方法,通过资源配额来实现这一需求。在后续的Kubernetes的版本中,在相同的namespace中的对象缺省情况下将会具有相同的访问控制策略。

注意事项4

合理使用namespace,在只是为了分隔非常轻微的资源的不同的时候,namespace并不是一个必选项,比如只是为了区分相同软件的不同版本,并不需要使用namespace来进行进行隔离,而可以考虑使用label来进行资源的区分。

其他基础

参考内容

https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/


转载:https://blog.csdn.net/liumiaocn/article/details/100797371
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场