当前位置:网站首页>【云原生 | 从零开始学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
感谢各位的观看,文章掺杂个人理解,如有错误请联系我指出~
边栏推荐
- Bash does not add single quotes to your string
- Oracle数据库下载、安装、使用教程及问题汇总
- 创意下拉多选js插件下载
- Selenium runs slowly - speed up by setting selenium load policy
- Global configuration and page configuration of wechat applet development
- Pytorch's transforms (numpy data type is converted to tensor, normalized and resized)
- Successfully solved typeerror: a bytes like object is required, not 'str‘
- Skiing mobile H5 game source code download
- sentinel简单限流和降级demo问题记录
- PMP practice once a day | don't get lost in the exam -7.25
猜你喜欢

微信小程序开发之网络数据请求

安全基础6 ---漏洞复现

919. Complete binary tree inserter
EZDML reverse engineering import database analysis practical operation tutorial

Day7: ordered binary tree (binary search tree)

Recommended system topic | Minet: cross domain CTR prediction

Rainbond插件扩展:基于Mysql-Exporter监控Mysql

C language learning diary 3 - realloc function

Distributed link logging minbox logging usage document

Shopping guide for high-end flagship projectors: dangbei X3 pro and dangbei F5 are more immersive!
随机推荐
软件设计师下午真题:2009-2022
Shopping guide for high-end flagship projectors: dangbei X3 pro and dangbei F5 are more immersive!
TFIDF examples and explanations
导电滑环在机械设备方面的应用
Selenium runs slowly - speed up by setting selenium load policy
03 isomorphism of tree 1
Siemens PLM Teamcenter download, installation and use tutorial
Code sharing of social chat platform developed by dating website (III)
Software designer afternoon real topic: 2009-2022
蓝桥杯基础练习——矩阵的回形取数(C语言)
重磅!《几何深度学习》新书发布,帝国理工/DeepMind等图ML大牛共同撰写,160页pdf阐述几何DL基础原理和统一框架
分享 25 个有用的 JS 单行代码
Day7:有序二叉树(二叉搜索树)
Binarysearch basic binary search
Configure and install cocos2dx development environment under Tongxin UOS
UNET and mask RCNN
什么是唯心主义
由一个蓝桥杯基础题报时助手而引出的常见误区
C语言学习日记3——realloc函数
六轴传感器使用学习记录