news 2026/6/10 1:42:46

K8S-namespace资源对象

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
K8S-namespace资源对象

一、概述
Kubernetes 支持多个虚拟集群,它们底层依赖于同一个物理集群。 这些虚拟集群被称为命名空间。

命名空间namespace是k8s集群级别的资源,可以给不同的用户、租户、环境或项目创建对应的命名空间,例如,可以为test、devlopment、production、deployment环境分别创建各自的命名空间。

namespace应用场景
命名空间适用于存在很多跨多个团队或项目的用户的场景。对于只有几到几十个用户的集群,根本不需要创建或考虑命名空间。

二、namespace资源管理
2.1 查看名称空间及其资源对象
k8s集群默认提供了几个名称空间用于特定目的,例如,kube-system主要用于运行系统级资源,存放k8s一些组件的。而default则为那些未指定名称空间的资源操作提供一个默认值。

##查看名称空间 kubectl get namespace ##查看特定的名称空间的详细信息 kubectl describe namespace NAME

2.2 管理namespace资源

namespace资源的名称仅能由字母、数字、下划线、连接线等字符组成。删除namespace资源会级联删除其包含的所有其他资源对象。

##创建名称空间 kubectl create namespace 名称 ##删除名称空间 kubectl delete namespace 名称

2.3 使用yaml文件创建namespace

apiVersion: v1 kind: Pod metadata: name: myns2

三、namespace使用案例

3.1 创建一个test命名空间

[root@k8s-master01 ~]# kubectl create ns test

3.2 切换默认查看命名空间

[root@k8s-master01 ~]# kubectl config set-context --current --namespace=kube-system #注意:切换命名空间后,kubectl get pods 如果不指定-n,查看的就是kube-system命名空间的资源了。

3.3 查看哪些资源属于命名空间级别的

[root@k8s-master01 ~]# 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/v2 true HorizontalPodAutoscaler cronjobs cj batch/v1 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/v1 true EndpointSlice events ev events.k8s.io/v1 true Event pods metrics.k8s.io/v1beta1 true PodMetrics ingresses ing networking.k8s.io/v1 true Ingress networkpolicies netpol networking.k8s.io/v1 true NetworkPolicy poddisruptionbudgets pdb policy/v1 true PodDisruptionBudget rolebindings rbac.authorization.k8s.io/v1 true RoleBinding roles rbac.authorization.k8s.io/v1 true Role csistoragecapacities storage.k8s.io/v1 true CSIStorageCapacity

3.4 namespace资源限额

namespace是命名空间,里面有很多资源,那么我们可以对命名空间资源做个限制,防止该命名空间部署的资源超过限制。

如何对namespace资源做限额呢?

[[root@k8s-master01 ~]# vim 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 ####解析#### #创建的ResourceQuota对象将在test名字空间中添加以下限制: #每个容器必须设置内存请求(memory request),内存限额(memory limit),cpu请求(cpu request)和cpu限额(cpu limit)。 ##所有容器的内存请求总额不得超过2GiB。 ##所有容器的内存限额总额不得超过4GiB。 ##所有容器的CPU请求总额不得超过2CPU。 ##所有容器的CPU限额总额不得超过4CPU。 ##应用配额文件并查看命名空间是否收到了限制
[root@k8s-master01 ~]# kubectl apply -f namespace-quota.yaml [root@k8s-master01 ~]# kubectl describe ns test Name: test Resource Quotas Name: mem-cpu-quota Resource Used Hard -------- --- --- limits.cpu 0 4 limits.memory 0 4Gi requests.cpu 0 2 requests.memory 0 2Gi

3.5 创建资源限制Pod

创建pod时候可以不用设置资源限额,如下:

[root@k8s-master01 ~]# vim pod-test.yaml
apiVersion: v1 kind: Pod metadata: name: pod-test namespace: myns1 labels: app: nginx129 spec: containers: - name: nginx129 ports: - containerPort: 80 image: nginx:latest imagePullPolicy: IfNotPresent
[root@k8s-master01 ~]# kubectl apply -f pod-test.yaml Error from server (Forbidden): error when creating "pod-test.yaml": pods "pod-test" is forbidden: failed quota: mem-cpu-quota: must specify limits.cpu for: nginx1; limits.memory for: nginx1; requests.cpu for: nginx1; requests.memory for: nginx1 ##发现无法正常启动

修改一下资源配置清单

[root@k8s-master01 ~]# cat pod-test.yaml
apiVersion: v1 kind: Pod metadata: name: pod-test namespace: test labels: app: tomcat-pod-test spec: containers: - name: tomcat-test ports: - containerPort: 8080 image: tomcat:8.5-jre8-alpine imagePullPolicy: IfNotPresent resources: requests: cpu: 0.5 memory: 1024Mi limits: cpu: 0.5 memory: 1024Mi

再次执行

[root@k8s-master01 ~]# kubectl apply -f pod-test.yaml ##查看,pod正常运行了 [root@k8s-master01 ~]# kubectl get pod -n test NAME READY STATUS RESTARTS AGE pod-test 1/1 Running 0 15s
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 9:53:29

基于Qoder实现AI漫剧生成Agent搭建

项目背景 行业痛点 漫剧/短视频内容生产成本高、周期长(脚本→分镜→美术→动画→配音)初创漫剧企业/教育机构缺乏专业动漫制作能力,但有高频轻量级视频需求(如营销广告、儿童英语启蒙)现有AIGC工具链割裂&#xff0…

作者头像 李华
网站建设 2026/6/10 1:08:30

STM323:ERXI和NVIC

1.外部中断EXTIEXTI:External interrupt/event controller外部中断/事件控制器1.外部中断基础知识1.STM32外部中断框架中断的概念:在主程序运行过程中,出现了特定的中断触发条件,使得CPU暂停当前正在运行的程序,转而去…

作者头像 李华
网站建设 2026/6/10 10:48:52

LSPosed框架升级指南:从传统Xposed到现代化模块开发的完美过渡

LSPosed框架升级指南:从传统Xposed到现代化模块开发的完美过渡 【免费下载链接】LSPosed LSPosed Framework 项目地址: https://gitcode.com/gh_mirrors/ls/LSPosed 你是否曾经遇到过这样的困扰?精心开发的Xposed模块在新版Android系统上突然失效…

作者头像 李华
网站建设 2026/6/10 14:33:09

企业级构建系统性能优化实战:从Bazel分布式架构到高效团队协作

你是否正在为大型项目的构建速度而烦恼?面对代码库日益膨胀、多团队协作混乱、跨平台兼容性差的现实困境,传统的构建工具往往力不从心。Bazel作为Google开源的多语言构建系统,凭借其精确的增量构建和分布式缓存能力,正在成为解决企…

作者头像 李华
网站建设 2026/6/9 17:25:53

Framework7与Ionic深度解析:2025年移动开发实战指南

Framework7与Ionic深度解析:2025年移动开发实战指南 【免费下载链接】framework7 Full featured HTML framework for building iOS & Android apps 项目地址: https://gitcode.com/gh_mirrors/fra/Framework7 你是否曾经在项目启动时纠结于选择哪个移动开…

作者头像 李华
网站建设 2026/6/10 10:55:54

CosyVoice2流式语音合成音色混合问题快速解决指南

CosyVoice2流式语音合成音色混合问题快速解决指南 【免费下载链接】CosyVoice Multi-lingual large voice generation model, providing inference, training and deployment full-stack ability. 项目地址: https://gitcode.com/gh_mirrors/cos/CosyVoice 在FunAudioLL…

作者头像 李华