大多数k8s资源(例如pod、services、replication controllers或其他)都在某些Namespace中,但Namespace资源本身并不在Namespace中。而低级别资源(如Node和persistentVolumes)不在任何Namespace中。Events是一个例外:它们可能有也可能没有Namespace,具体取决于Events的对象。
kubectl create namespace new-namespace # 创建namespace
kubectl create -f ./my-namespace.yaml # 通过文件创建
# file: my-namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: new-namespace
<aside> 💡 命名空间名称满足正则表达式a-z0-9?,最大长度为63位
</aside>
kubectl delete namespaces new-namespace
kubectl get namespaces
kubectl --namespace=<insert-namespace-name-here> run nginx --image=nginx
kubectl --namespace=<insert-namespace-name-here> get pods
# 将namespace永久保存在context中
kubectl config set-context $(kubectl config current-context) --namespace=<insert-namespace-name-here>