Namespace(命名空间)的使用
一、什么是命名空间
Kubernetes 支持多个虚拟集群,它们底层依赖于同一个物理集群。 这些虚拟集群被称为命名空间.
命名空间namespace是k8s集群级别的资源,可以给不同的用户、租户、环境或项目创建对应的命名空间。
Namespace(命名空间)是kubernetes系统中的一个非常重要的概念,Namespace在很多情况下用于实现多租户的资源隔离。Namespace通过将集群内部的资源对象“分配”到不同的Namespace中,形成逻辑上分组的不同项目、小组或用户组,便于不同的分组在共享使用整个集群的资源的同时还能被分别管理。
kubernetes集群在启动后,会默认创建一个名为“default”的Namespace。同一个namespace下面不允许出现两个service叫相同的名字。
二、 namespace应用场景
命名空间适用于存在很多跨多个团队或项目的用户的场景。对于只有几到几十个用户的集群,根本不需要创建或考虑命名空间。
1、查看名称空间及其资源对象
k8s集群默认提供了几个名称空间用于特定目的,例如,kube-system主要用于运行系统级资源,存放k8s一些组件的。而default则为那些未指定名称空间的资源操作提供一个默认值。
使用kubectl get namespace可以查看namespace资源,使用kubectl describe namespace $NAME可以查看特定的名称空间的详细信息。
2、管理namespace资源
namespace资源属性较少,通常只需要指定名称即可创建,如“kubectl create namespace qa”。
namespace资源的名称仅能由字母、数字、下划线、连接线等字符组成。
删除namespace资源会级联删除其包含的所有其他资源对象。
三、namespacs使用案例分享
1.查看命名空间
[root@kub_master ~]# kubectl get namespaces -o wide NAME STATUS AGE default Active 7d kube-system Active 7d
可以看到有两个个namespace
如果在创建过程中不指名Namespace,则用户创建的pod,RC,Service都将被系统创建到这个默认名为default的Namespace中。
2. 创建命名空间
[root@kub_master ~]# kubectl create namespace test namespace "test" created [root@kub_master ~]# kubectl get namespaces -o wide NAME STATUS AGE default Active 7d kube-system Active 7d test Active 3s
3. 切换命令空间
切换命名空间后,kubectl get pods 如果不指定-n,查看的就是kube-system命名空间的资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [root@kub-master ~] # kubectl config set-context --current --namespace=kube-system Context "kubernetes-admin@kubernetes" modified. [root@kub-master ~] # kubectl get pods NAME READY STATUS RESTARTS AGE calico-kube-controllers-6949477b58-82r4z 1 /1 Running 2 27d calico-node-kr8bt 1 /1 Running 7 27d calico-node-pzzlf 1 /1 Running 9 27d calico-node-wwrjq 1 /1 Running 20 27d coredns-7f89b7bc75-2cgxw 1 /1 Running 3 27d coredns-7f89b7bc75-gm6jp 1 /1 Running 2 27d etcd-k8s-master1 1 /1 Running 2 27d kube-apiserver-k8s-master1 1 /1 Running 2 27d kube-controller-manager-k8s-master1 1 /1 Running 42 27d kube-proxy-4tnzv 1 /1 Running 2 27d kube-proxy-mnnsg 1 /1 Running 2 27d kube-proxy-mxnhb 1 /1 Running 2 27d kube-scheduler-k8s-master1 1 /1 Running 41 27d |
4.查看哪些资源属于命名空间级别的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | [root@kub-master ~] # kubectl api-resources --namespaced=true NAME SHORTNAMES APIVERSION NAMESPACED KIND bindings v1 true Binding configmaps cm v1 true ConfigMap endpoints ep v1 true Endpoints events ev v1 true Event limitranges limits v1 true LimitRange persistentvolumeclaims pvc v1 true PersistentVolumeClaim pods po v1 true Pod podtemplates v1 true PodTemplate replicationcontrollers rc v1 true ReplicationController resourcequotas quota v1 true ResourceQuota secrets v1 true Secret serviceaccounts sa v1 true ServiceAccount services svc v1 true Service controllerrevisions apps /v1 true ControllerRevision daemonsets ds apps /v1 true DaemonSet deployments deploy apps /v1 true Deployment replicasets rs apps /v1 true ReplicaSet statefulsets sts apps /v1 true StatefulSet localsubjectaccessreviews authorization.k8s.io /v1 true LocalSubjectAccessReview horizontalpodautoscalers hpa autoscaling /v1 true HorizontalPodAutoscaler cronjobs cj batch /v1beta1 true CronJob jobs batch /v1 true Job leases coordination.k8s.io /v1 true Lease networkpolicies crd.projectcalico.org /v1 true NetworkPolicy networksets crd.projectcalico.org /v1 true NetworkSet endpointslices discovery.k8s.io /v1beta1 true EndpointSlice events ev events.k8s.io /v1 true Event ingresses ing extensions /v1beta1 true Ingress ingresses ing networking.k8s.io /v1 true Ingress networkpolicies netpol networking.k8s.io /v1 true NetworkPolicy poddisruptionbudgets pdb policy /v1beta1 true PodDisruptionBudget rolebindings rbac.authorization.k8s.io /v1 true RoleBinding roles rbac.authorization.k8s.io /v1 true Role |
5. 删除命名空间
注:特别危险!会删除namespace下所有的k8s资源
[root@kub_master ~]# kubectl delete namespace test namespace "test" deleted [root@kub_master ~]# kubectl get namespaces -o wide NAME STATUS AGE default Active 7d kube-system Active 7d test Terminating 1m [root@kub_master ~]# kubectl get namespaces -o wide NAME STATUS AGE default Active 7d kube-system Active 7d
6. Namespace的定义很简单,如下所示的yaml定义了名为develop的Namespace
[root@kub_master ~]# mkdir k8s/namespace [root@kub_master ~]# cd k8s/namespace/
[root@kub_master namespace]# vim develop-namespace.yaml [root@kub_master namespace]# cat develop-namespace.yaml apiVersion: v1 kind: Namespace metadata: name: develop
[root@kub_master namespace]# kubectl create -f develop-namespace.yaml namespace "develop" created [root@kub_master namespace]# kubectl get namespaces -o wide NAME STATUS AGE default Active 7d develop Active 15s kube-system Active 7d
7.创建属于develop命名空间的pod资源
一旦创建了Namespace,在创建资源对象时,就可以指定这个资源对象属于哪个Namespace。
[root@kub_master namespace]# vim pod-busybox.yaml [root@kub_master namespace]# cat pod-busybox.yaml apiVersion: v1 kind: Pod metadata: name: busybox namespace: develop spec: containers: - name: busybox image: docker.io/busybox:latest imagePullPolicy: IfNotPresent command: - sleep - "3600" [root@kub_master namespace]# kubectl create -f pod-busybox.yaml pod "busybox" created [root@kub_master namespace]# kubectl get pods --namespace=develop NAME READY STATUS RESTARTS AGE busybox 1/1 Running 0 21s
在kubectl命令中加入--namespace参数来查看某个命名空间中的对象。
8. 创建namespace=develop的RC资源
[root@kub_master namespace]# vim nginx-rc.yaml [root@kub_master namespace]# cat nginx-rc.yaml apiVersion: v1 kind: ReplicationController metadata: name: myweb namespace: develop spec: replicas: 2 selector: app: myweb template: metadata: labels: app: myweb spec: containers: - name: myweb image: 192.168.0.212:5000/nginx:1.13 ports: - containerPort: 80 [root@kub_master namespace]# kubectl create -f nginx-rc.yaml replicationcontroller "myweb" created [root@kub_master namespace]# kubectl get rc --namespace=develop NAME DESIRED CURRENT READY AGE myweb 2 2 2 20s [root@kub_master namespace]# kubectl get pods --namespace=develop NAME READY STATUS RESTARTS AGE busybox 1/1 Running 9 9h myweb-1fd25 1/1 Running 0 34s myweb-z1524 1/1 Running 0 34s
9. 创建namespace=develop的Service资源
[root@kub_master namespace]# vim nginx-svc.yaml [root@kub_master namespace]# cat nginx-svc.yaml apiVersion: v1 kind: Service metadata: name: myweb namespace: develop spec: type: NodePort ports: - port: 80 nodePort: 30008 targetPort: 80 selector: app: myweb [root@kub_master namespace]# kubectl create -f nginx-svc.yaml service "myweb" created [root@kub_master namespace]# kubectl get svc --namespace=develop NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE myweb 192.168.72.198 <nodes> 80:30008/TCP 14s
当给每个租户创建一个Namespace来实现多租户资源隔离时,还能结合Kubernetes的资源配额管理,限定不同租户能占用的资源。
四、namespace资源限额
namespace是命名空间,里面有很多资源,那么可以对命名空间资源做个限制,防止该命名空间部署的资源超过限制
如何对namespace资源做限额呢?
创建的ResourceQuota对象将在test名字空间中添加以下限制:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [root@kub-master ~] # cd pod/ [root@kub-master pod] # vim namespace-quota.yaml [root@kub-master pod] # cat namespace-quota.yaml apiVersion: v1 kind: ResourceQuota metadata: name: mem-cpu- quota namespace: test spec: hard: requests.cpu: "2" requests.memory: 2Gi limits.cpu: "4" limits.memory: 4Gi |
每个容器必须设置内存请求(memory request),内存限额(memory limit),cpu请求(cpu request)和cpu限额(cpu limit)。
所有容器的内存请求总额不得超过2GiB。
所有容器的内存限额总额不得超过4 GiB。
所有容器的CPU请求总额不得超过2 CPU。
所有容器的CPU限额总额不得超过4CPU。
#创建pod时候设置资源限额,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | [root@kub-master pod] # cat pod-test.yaml apiVersion: v1 kind: Pod metadata: name: pod- test namespace: test labels: app: nginx-pod- test spec: containers: - name: nginx- test ports: - containerPort: 80 image: nginx:latest imagePullPolicy: IfNotPresent resources: requests: memory: "100Mi" cpu: "500m" limits: memory: "2Gi" cpu: "2" [root@kub-master pod] # kubectl apply -f pod-test.yaml pod /pod-test created [root@kub-master pod] # kubectl get pod -n test NAME READY STATUS RESTARTS AGE pod- test 1 /1 Running 0 4s |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏