欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

云原生与 Kubernetes 实践]04、k8s 命名空间和资源配额

最编程 2024-04-06 20:18:47
...

目录

一、什么是命名空间?

二、namespace 应用场景

三、namespacs 使用案例

四、namespace 资源限额


一、什么是命名空间?

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

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

二、namespace 应用场景

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

三、namespacs 使用案例

# 创建一个test命名空间。namespace 可以简写为 ns
[root@k8s-master01 pod-yaml]# kubectl create ns test
namespace/test created

# 查看命名空间
[root@k8s-master01 pod-yaml]# kubectl get ns
NAME                   STATUS   AGE
default                Active   36d
kube-node-lease        Active   36d
kube-public            Active   36d
kube-system            Active   36d
kubernetes-dashboard   Active   36d
test                   Active   16s

四、namespace 资源限额

给 test 这个名称空间做资源限额,即限制 test 名称空间下pod的资源。

#1. 查看帮助命令
[root@k8s-master01 pod-yaml]# kubectl explain resourcequota
[root@k8s-master01 pod-yaml]# kubectl explain resourcequota.metadata
[root@k8s-master01 pod-yaml]# kubectl explain resourcequota.spec

#2. 编写 yaml 文件
[root@k8s-master01 pod-yaml]# vi 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

#3. 创建resourcequotas资源
[root@k8s-master01 pod-yaml]# kubectl get resourcequotas -n test
NAME            AGE   REQUEST                                     LIMIT
mem-cpu-quota   56s   requests.cpu: 0/2, requests.memory: 0/2Gi   limits.cpu: 0/4, limits.memory: 0/4Gi
  • spec.hard字段官网参考文档:资源配额 | Kubernetes

上面创建的 ResourceQuota 对象将在test名字空间中添加以下限制:

        每个容器必须设置内存请求(memory request),内存限额(memory limit),cpu请求(cpu request)和cpu限额(cpu limit)。

  • 所有容器的内存请求总额不得超过 2 GiB。
  • 所有容器的内存限额总额不得超过 4 GiB。
  • 所有容器的CPU请求总额不得超过 2 CPU。
  • 所有容器的CPU限额总额不得超过 4 CPU。 

        ResouceQuota 对象是在我们的名称空间中创建的,并准备好控制该名称空间中的所有容器的总请求和限制。

接下来我们创建pod的时候也必须设置资源限额(requests、limits 字段),否则创建pod资源失败:

[root@k8s-master01 pod-yaml]# vi 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:latest
    imagePullPolicy: IfNotPresent
    resources:
      requests:
        memory: "100Mi"
        cpu: "500m"
      limits:
        memory: "2Gi"
        cpu: "2"

# 创建pod资源
[root@k8s-master01 pod-yaml]# kubectl apply -f pod-test.yaml 
pod/pod-test created
您在 /var/spool/mail/root 中有新邮件

[root@k8s-master01 pod-yaml]# kubectl get pods -n test 
NAME       READY   STATUS    RESTARTS   AGE
pod-test   1/1     Running   0          14s

# 查看 test 名称空间的资源情况
[root@k8s-master01 pod-yaml]# kubectl get resourcequotas -n test 
NAME            AGE   REQUEST                                            LIMIT
mem-cpu-quota   45m   requests.cpu: 500m/2, requests.memory: 100Mi/2Gi   limits.cpu: 2/4, limits.memory: 2Gi/4Gi
  • pods.spec.containers.resources 字段官网参考文档:为 Pod 和容器管理资源 | Kubernetes

        上述 Pod 里的 tomcat-test 容器的请求为 0.5 CPU 和 100 MiB 内存, 该容器的资源限制为 2 CPU 和 2 GB 内存。 你也可以认为该 Pod 的资源请求为 0.5 CPU 和 100 MiB 内存,资源限制为 2 CPU 和 2 GB 内存。 

如果我们再更改下yaml文件,使其超出设置的资源额度:

[root@k8s-master01 pod-yaml]# vi 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:latest
    imagePullPolicy: IfNotPresent
    resources:
      requests:
        memory: "100Mi"
        cpu: "500m"
      limits:
        memory: "5Gi"
        cpu: "5"

[root@k8s-master01 pod-yaml]# kubectl delete pods -n test pod-test 
pod "pod-test" deleted

[root@k8s-master01 pod-yaml]# kubectl get pods -n test 
No resources found in test namespace.
您在 /var/spool/mail/root 中有新邮件

[root@k8s-master01 pod-yaml]# kubectl apply -f pod-test.yaml 
Error from server (Forbidden): error when creating "pod-test.yaml": pods "pod-test" is forbidden: exceeded quota: mem-cpu-quota, requested: limits.cpu=5,limits.memory=5Gi, used: limits.cpu=0,limits.memory=0, limited: limits.cpu=4,limits.memory=4Gi

 报错,超出了原来的资源限制(4 cpu,4 GB内存):

 上一篇文章:【云原生 | Kubernetes 实战】03、手把手教你基于YAML文件运行pod应用_Stars.Sky的博客-****博客

下一篇文章:【云原生 | Kubernetes 实战】05、Pod高级实战:基于污点、容忍度、亲和性的多种调度策略(上)_Stars.Sky的博客-****博客