跳转至

18-k8s命名空间-单master

什么是命名空间

Kubernetes 支持多个虚拟集群,它们底层依赖于同一个物理集群。 这些虚拟集群被称为命名空间。 命名空间namespace是k8s集群级别的资源,可以给不同的用户、租户、环境或项目创建对应的命名空间,例如,可以为test、devlopment、production环境分别创建各自的命名空间。

命名空间应用场景

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

1、查看名称空间及其资源对象 k8s集群默认提供了几个名称空间用于特定目的,例如,kube-system主要用于运行系统级资源,存放k8s一些组件的。而default则为那些未指定名称空间的资源操作提供一个默认值。

使用kubectl get namespace可以查看namespace资源,使用kubectl describe namespace $NAME可以查看特定的名称空间的详细信息。

2、管理namespace资源 namespace资源属性较少,通常只需要指定名称即可创建,如“kubectl create namespace qa”。namespace资源的名称仅能由字母、数字、下划线、连接线等字符组成。删除namespace资源会级联删除其包含的所有其他资源对象。

命名空间日常管理

创建命名空间

创建一个test命名空间

[root@master1 ~]# kubectl create ns test
namespace/test created

查看哪些资源属于命名空间级别的

kubectl api-resources --namespaced=true

删除命名空间会将空间内所有的pod都删除,请谨慎删除。

kubectl delete ns test

切换命名空间

切换命名空间

kubectl  config set-context --current --namespace=kube-system

查看命名空间的pod

kubectl get pod

我们还可以切换到默认的命名空间

kubectl  config set-context --current --namespace=default

命名空间限额

namespace是命名空间,里面有很多资源,那么我们可以对命名空间资源做个限制,防止该命名空间部署的资源超过限制。

如何对namespace资源做限额呢?

cat >namespace-quota.yaml<<EOF
apiVersion: v1
kind: ResourceQuota
metadata:
  name: mem-cpu-quota
  namespace: test
spec:
  hard:
    requests.cpu: "1"
    requests.memory: 1Gi
    limits.cpu: "2"
    limits.memory: 2Gi
EOF

创建的ResourceQuota对象将在test名字空间中添加以下限制: 每个容器必须设置内存请求(memory request),内存限额(memory limit),cpu请求(cpu request)和cpu限额(cpu limit)。

[root@master1 ~]# kubectl apply -f namespace-quota.yaml
resourcequota/mem-cpu-quota created

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

查看命名空间的限制

[root@master1 ~]# kubectl describe ns test
Name:         test
Labels:       <none>
Annotations:  <none>
Status:       Active

Resource Quotas
 Name:            mem-cpu-quota
 Resource         Used  Hard
 --------         ---   ---
 limits.cpu       0     2
 limits.memory    0     2Gi
 requests.cpu     0     1
 requests.memory  0     1Gi

No LimitRange resource.

创建pod时候必须设置资源限额,否则创建失败,如下:

cat >pod-test.yaml<<EOF
apiVersion: v1
kind: Pod
metadata:
  name: pod-test
  namespace: test
  labels:
    app: tomcat-pod-test
spec:
  containers:
  - name:  tomcat-test
    ports:
    - containerPort: 80
    image: nginx:latest
    imagePullPolicy: IfNotPresent 
EOF

应用资源清单(这是失败的创建)

[root@master1 ~]# 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,limits.memory,requests.cpu,requests.memory

常见问题

kubernetes(k8s)命名空间一直Terminating

https://blog.csdn.net/qq_33921750/article/details/123911612

kubernetes(k8s)命名空间一直Terminating

root@hello:~# kubectl  get ns
NAME              STATUS        AGE
auth              Terminating   34m
default           Active        23h
kube-node-lease   Active        23h
kube-public       Active        23h
kube-system       Active        23h

新开命令行窗口打开proxy

root@hello:~# kubectl proxy
Starting to serve on 127.0.0.1:8001

回到刚才窗口 将 terminating 状态的命名空间信息导出到 json 文件:

root@hello:~# kubectl get namespace auth -o json >tmp.json 修改json文件中的 finalizers,将其设置为空

root@hello:~# vi tmp.json 
root@hello:~# cat tmp.json | grep finalizers
        "finalizers": []

在 temp.json 文件所在位置调下面的接口

root@hello:~# curl -k -H "Content-Type: application/json" -X PUT --data-binary @tmp.json http://127.0.0.1:8001/api/v1/namespaces/auth/finalize

*auth 改为需要删除的 terminating 状态的命名空间的名字

root@hello:~# kubectl  get ns
NAME              STATUS        AGE
default           Active        23h
kube-node-lease   Active        23h
kube-public       Active        23h
kube-system       Active        23h