当前位置:网站首页>【云原生 | 从零开始学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
感谢各位的观看,文章掺杂个人理解,如有错误请联系我指出~
边栏推荐
- 安全基础6 ---漏洞复现
- 03 isomorphism of tree 1
- University of California | feasible confrontation robust reinforcement learning for unspecified environments
- 手机端触摸图片slider轮播插件photoswipe.js
- Error when creating dataset with mindscore
- Wxss template style and WXS scripting language for wechat applet development
- Bash does not add single quotes to your string
- Bash does not add single quotes to your string
- VMware 虚拟机下载、安装和使用教程
- Distributed link logging minbox logging usage document
猜你喜欢

wallys//wifi6 wifi5 router IPQ6018 IPQ4019 IPQ4029 802.11ax 802.11ac

Beihang and other "deep learning event extraction" literature review paper, 27 page PDF describes the current trend
![[wp]ctfshow-web getting started - Explosion](/img/4b/6d8f4c044578382b9353d4d1c69c8f.png)
[wp]ctfshow-web getting started - Explosion

Creative drop-down multi choice JS plug-in download

蓝桥杯基础练习——矩阵的回形取数(C语言)
![[wp]ctfshow-web introductory information collection](/img/22/c2e5cca918800dda9df27272eb9871.png)
[wp]ctfshow-web introductory information collection

Common misunderstandings caused by a time reporting assistant of Blue Bridge Cup basic questions

How to set tiktok mobile network environment? How can tiktok break the playback volume?

推荐系统专题 | MiNet:跨域CTR预测

PyTorch 模型 onnx 文件的导出和调用
随机推荐
Wxss template style and WXS scripting language for wechat applet development
YOLOv7论文部分解读【含自己的理解】
谷歌Pixel 6a屏下指纹扫描仪存在重大安全漏洞
On interface encryption
Network data request for wechat applet development
Security Basics 4 - regular expressions
随机梯度下降法、牛顿法、冲量法、AdaGrad、RMSprop以及Adam优化过程和理解
Hongke shares | how to solve blackmail software security vulnerabilities
Shopping guide for high-end flagship projectors: dangbei X3 pro and dangbei F5 are more immersive!
Recommended system topic | Minet: cross domain CTR prediction
Stochastic gradient descent method, Newton method, impulse method, adagrad, rmsprop and Adam optimization process and understanding
wallys//wifi6 wifi5 router IPQ6018 IPQ4019 IPQ4029 802.11ax 802.11ac
PreScan快速入门到精通第十八讲之PreScan轨迹编辑的特殊功能
分享 25 个有用的 JS 单行代码
【好书推荐】-- 《以太网权威指南》(第2版)
UNET and mask RCNN
Detailed explanation of three methods of selenium setting element waiting
从瞳代到“瞳代”再到品牌,暴利的美瞳的变与未变
北航等《深度学习事件抽取》文献综述论文,27页pdf阐述当前趋势
基于海思3559 高效率的 0延时 0拷贝 qt播放器方案