当前位置: 首页 > news >正文

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
http://www.cnnetsun.cn/news/117772.html

相关文章:

  • K8S-Service资源对象
  • 郭嘉队动手了?刺激消费扩大内需!
  • 记力扣2105.给植物浇水 练习有感
  • 突破性智能容器管理:自托管服务器的革命性演进
  • 超越Borel:论非Borel集的存在性、构造及其在实分析中的核心作用
  • 百度网盘提取码智能查询工具:告别繁琐搜索的终极方案
  • Launcher3深度定制指南:打造个性化Android桌面体验
  • DuckDB Java集成实战指南:3分钟配置嵌入式OLAP数据库
  • MaxScript 实现多边形层级切换按钮
  • NideShop电商系统:打造高效在线商城的终极Node.js解决方案
  • Selenium 自动化 | 案例实战篇
  • 开源RAW图像处理工具darktable:5大核心模块构建专业摄影工作流
  • Wan2.1-I2V-14B-480P:如何在消费级GPU上实现实时图像到视频生成
  • 百度贴吧终极体验优化:baidu-tieba-userscript完整使用指南
  • HFT-Orderbook:突破传统的高性能C语言订单簿引擎
  • Stable-Dreamfusion实战指南:5步掌握文本到3D模型生成核心技术
  • 浅析NCE0130KA在功率开关设计中的应用特性
  • 学习Java27天
  • ThingsBoard物联网平台消息队列实战:3大核心技术架构深度解析
  • Free Sidecar终极指南:5分钟解锁macOS多屏扩展功能
  • Universe性能优化终极指南:cProfile与火焰图实战分析
  • DeeplxFile:免费跨平台文件翻译工具的完整使用指南
  • Qwen3-4B-FP8模型实战手册:从零开始构建智能对话应用
  • IPCA改进主成分分析法 主元分析在处理数据过程中会平等的对待每一维特征,即认为每一维特征的权...
  • Carsim+Simulink联合仿真实现换道超车及弯道道路处理演示
  • 测试代码如何成为团队通用语言:从技术债到沟通桥梁的蜕变之路
  • 低代码、RPA融合、云边协同……盘点五大AI Agent平台为开发者带来的机遇与挑战。
  • 智能体(Agent)全景解析:技术路线、落地实践与产业生态
  • 3步搞定:这款智能LLM微调工具让数据准备如此简单
  • 百度网盘下载加速神器:免费解析工具完整使用指南