当前位置:网站首页>【云原生 | 从零开始学Kubernetes】八、命名空间资源配额以及标签
【云原生 | 从零开始学Kubernetes】八、命名空间资源配额以及标签
2022-07-25 19:54:00 【是泡泡】
该篇文章已经被专栏《从零开始学k8s》收录

namespacs 使用案例分享
#创建一个 test 命名空间
[[email protected] ~]# kubectl create ns test
#删除命名空间
[[email protected] ~]# kubectl delete ns test
#切换命名空间
[[email protected] ~]# kubectl config set-context --current --namespace=kube-system
[[email protected] ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
coredns-7ff77c879f-272f4 1/1 Running 3 12d
coredns-7ff77c879f-flkxx 1/1 Running 3 12d
#切换命名空间后,kubectl get pods 如果不指定-n,查看的就是 kube-system 命名空间的资源了。
#查看哪些资源属于命名空间级别的 在创建资源的时候没有指定命名空间就会在默认的命名空间 pod是命名空间级别的
[[email protected] ~]# kubectl api-resources --namespaced=true
namespace 资源限额
namespace 是命名空间,里面有很多资源,那么我们可以对命名空间资源做个限制,防止该命名空间部署的资源超过限制。
如何对 namespace 资源做限额呢?
[[email protected] ~]# mkdir pp
[[email protected] ~]# cd pp
[[email protected] ~]# vim namespace-pp.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)。
所有容器的内存请求总额不得超过 2 GiB。
所有容器的内存限额总额不得超过 4 GiB。
所有容器的 CPU 请求总额不得超过 2 CPU。
所有容器的 CPU 限额总额不得超过 4 CPU。
[[email protected] pp]# kubectl apply -f namespace-pp.yaml
resourcequota/mem-cpu-quota created
[[email protected] pp]# kubectl describe ns test
Name: test
Labels: <none>
Annotations: <none>
Status: Active
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
No LimitRange resource.
#创建 pod 时候必须设置资源限额,否则创建失败,如下:
[[email protected] pp]# vim 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
imagePullPolicy: IfNotPresent
resources:
requests:
memory: "100Mi"
cpu: "500m"
limits:
memory: "2Gi"
cpu: "2"
[[email protected] pp]# kubectl get pods -n test
NAME READY STATUS RESTARTS AGE
pod-test 1/1 Running 0 8s
[[email protected] pp]# kubectl describe pods pod-test -n test
Name: pod-test
Namespace: test
Priority: 0
Node: k8snode/192.168.11.141
Start Time: Fri, 08 Jul 2022 21:31:01 -0700
Labels: app=tomcat-pod-test
Annotations: Status: Running
IP: 10.244.2.13
IPs:
IP: 10.244.2.13
Containers:
tomcat-test:
Container ID: docker://b8604abe672c701f1ea8d027568b375989e12061090e1c5d951dae637af9b726
Image: tomcat
Image ID: docker-pullable://[email protected]:9dee185c3b161cdfede1f5e35e8b56ebc9de88ed3a79526939701f3537a52324
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Fri, 08 Jul 2022 21:31:02 -0700
Ready: True
Restart Count: 0
Limits:
cpu: 2
memory: 2Gi
Requests:
cpu: 500m
memory: 100Mi
Environment: <none>
Mounts:
什么是标签?
标签其实就一对 key/value ,被关联到对象上,比如 Pod,标签的使用我们倾向于能够表示对象的特殊特点,就是一眼就看出了这个 Pod 是干什么的,标签可以用来划分特定的对象(比如版本,服务类型等),标签可以在创建一个对象的时候直接定义,也可以在后期随时修改,每一个对象可以拥有多个标签,但是,key 值必须是唯一的。创建标签之后也可以方便我们对资源进行分组管理。如果对 pod 打标签,之后就可以使用标签来查看、删除指定的 pod。
在 k8s 中,大部分资源都可以打标签。
给 pod 资源打标签
#对已经存在的 pod 打标签 表示这个pod版本是v1
[[email protected] pp]# kubectl label pods pod-first release=v1
#查看标签是否打成功 查看指定的pod
[[email protected] pp]# kubectl get pods pod-first --show-labels
显示如下,说明标签达成功了
NAME READY STATUS RESTARTS AGE LABELS
pod-first 1/1 Running 0 139m release=v1
查看资源标签
#查看默认名称空间下所有 pod 资源的标签
[[email protected] pp]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-f89759699-tc62k 1/1 Running 2 4d20h app=nginx,pod-template-hash=f89759699
nginx-test-84b997bfc5-6vccl 1/1 Running 0 16h app=nginx,pod-template-hash=84b997bfc5
nginx-test-84b997bfc5-z6lqm 1/1 Running 0 16h app=nginx,pod-template-hash=84b997bfc5
pod-first 1/1 Running 0 140m release=v1
#查看默认名称空间下指定 pod 具有的所有标签
[[email protected] pp]# kubectl get pods pod-first --show-labels
NAME READY STATUS RESTARTS AGE LABELS
pod-first 1/1 Running 0 141m release=v1
#列出默认名称空间下标签 key 是 release 的 pod,不显示标签
[[email protected] pp]# kubectl get pods -l release
NAME READY STATUS RESTARTS AGE
pod-first 1/1 Running 0 141m
#列出默认名称空间下标签 key 是 release、值是 v1 的 pod,不显示标签
[[email protected] pp]# kubectl get pods -l release=v1
NAME READY STATUS RESTARTS AGE
pod-first 1/1 Running 0 142m
#列出默认名称空间下标签 key 是 release 的所有 pod,并打印对应的标签值 key作为一列 value在这一列显示出来。
[[email protected] pp]# kubectl get pods -L release
NAME READY STATUS RESTARTS AGE RELEASE
nginx-f89759699-tc62k 1/1 Running 2 4d20h
nginx-test-84b997bfc5-6vccl 1/1 Running 0 16h
nginx-test-84b997bfc5-z6lqm 1/1 Running 0 16h
pod-first 1/1 Running 0 142m v1
#查看所有名称空间下的所有 pod 的标签
[[email protected] pp]# kubectl get pods --all-namespaces --show-labels
#把具有release标签的pod显示出来并且显示对应的key和value
[[email protected] pp]# kubectl get pods -l release=v1 -L release
NAME READY STATUS RESTARTS AGE RELEASE
pod-first 1/1 Running 0 145m v1
写在最后
创作不易,如果觉得内容对你有帮助,麻烦给个三连关注支持一下我!如果有错误,请在评论区指出,我会及时更改!
目前正在更新的系列:从零开始学k8s
感谢各位的观看,文章掺杂个人理解,如有错误请联系我指出~
边栏推荐
- NPM semantic version control, solution console prop being mutated: "placement" error
- A good way to generate interface documents efficiently
- YOLOv7论文部分解读【含自己的理解】
- VMware virtual machine download, installation and use tutorial
- RepVGG网络中重参化网络结构解读【附代码】
- [good book recommendation] - authoritative guide to Ethernet (2nd Edition)
- Global configuration and page configuration of wechat applet development
- Digital informatization (enumerate assumptions first, and then see whether the conditions are met) (1089 werewolf kill - simple version)
- 统信UOS下配置安装cocos2dx开发环境
- The use of new promise, async and await in the project, and the practical application of promise.all in the project
猜你喜欢

Deeplobv1 and V2
![Interpretation of repartitioned network structure in repvgg network [with code]](/img/0b/a2f3b312899043c9d5b9c7d6b22261.png)
Interpretation of repartitioned network structure in repvgg network [with code]

滑雪手机端H5小游戏源码下载

蓝桥杯基础练习——矩阵的回形取数(C语言)

随机梯度下降法、牛顿法、冲量法、AdaGrad、RMSprop以及Adam优化过程和理解

Concept of IP address

PMP采用最新考纲,这里有【敏捷项目管理】

北航等《深度学习事件抽取》文献综述论文,27页pdf阐述当前趋势
balanced binary tree

Common misunderstandings caused by a time reporting assistant of Blue Bridge Cup basic questions
随机推荐
4、Nacos 配置中心源码解析之 服务端启动
Recommendations on how to install plug-ins and baby plug-ins in idea
PreScan快速入门到精通第十九讲之PreScan执行器配置、轨迹同步及非配多个轨迹
Siemens-PLM-TeamCenter下载、安装、使用教程
微信小程序开发之全局配置与页面配置
When the V100 of mindpole 8 card is trained to 101 epochs, an error of reading data timeout is reported
An idea of solving div adapting to screen
Scala基础【集合01】
相机内参矩阵K和fov的相互转换
连接数据库警告 Establishing SSL connection without server‘s identity verification is not recommended.
Siemens PLM Teamcenter download, installation and use tutorial
High number_ Chapter 3 learning experience and summary of multiple integral
Skiing mobile H5 game source code download
How to get started quickly in software testing
How to set tiktok mobile network environment? How can tiktok break the playback volume?
Security foundation 6 - vulnerability recurrence
Dataloader reports an error "default_collate: batch must contain tensors, numpy arrays, numbers, dicts" when pytorch trains the model
What is idealism
谷歌Pixel 6a屏下指纹扫描仪存在重大安全漏洞
高数_第3章重积分 学习体会与总结