Kubernetes之Namespace组件深入

Namespace

kubectl get namespace[获取所有的namespace]

[root@manager-node demo]# kubectl get namespace
NAME              STATUS   AGE
default           Active   4d22h
kube-node-lease   Active   4d22h
kube-public       Active   4d22h
kube-system       Active   4d22h

kubectl get ns[获取所有的namespace]

[root@manager-node demo]# kubectl get ns
NAME              STATUS   AGE
default           Active   4d23h
kube-node-lease   Active   4d23h
kube-public       Active   4d23h
kube-system       Active   4d23h
[root@manager-node demo]# 

kubectl get pods -n {namespace_name} [获取某个namespace下的pod]

[root@manager-node demo]# kubectl get pods -n kube-system
NAME                                       READY   STATUS    RESTARTS   AGE
calico-kube-controllers-594b6978c5-7lxqz   1/1     Running   9          4d22h
calico-node-82rx7                          1/1     Running   9          4d22h
calico-node-xrqf8                          1/1     Running   9          4d7h
calico-node-z86gh                          1/1     Running   7          4d7h
coredns-fb8b8dccf-6hvcz                    1/1     Running   9          4d22h
coredns-fb8b8dccf-x8xxc                    1/1     Running   9          4d22h
etcd-manager-node                          1/1     Running   9          4d22h
kube-apiserver-manager-node                1/1     Running   11         4d22h
kube-controller-manager-manager-node       1/1     Running   10         4d22h
kube-proxy-5q9b5                           1/1     Running   8          4d7h
kube-proxy-pw6r7                           1/1     Running   9          4d7h
kube-proxy-rvddm                           1/1     Running   9          4d22h
kube-scheduler-manager-node                1/1     Running   11         4d22h
[root@manager-node demo]# kubectl get pods -n default
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-784b7cc96d-czcgw   1/1     Running   0          6h43m
nginx-deployment-784b7cc96d-p549k   1/1     Running   0          6h44m
nginx-deployment-784b7cc96d-p8bs4   1/1     Running   0          6h43m
[root@manager-node demo]# 

kubectl apply -f {配置文件} [通过配置文件创建namespace]

创建配置文件 my-namespace.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: first-namespace                            

kubectl apply -f my-namespace.yaml 或 kubectl create -f my-namespace.yaml [启动]

[root@manager-node demo]# kubectl get ns
NAME              STATUS   AGE
default           Active   4d23h
kube-node-lease   Active   4d23h
kube-public       Active   4d23h
kube-system       Active   4d23h
[root@manager-node demo]# pwd
/root/demo
[root@manager-node demo]# vi my-namespace.yaml
[root@manager-node demo]# kubectl apply -f my-namespace.yaml 
namespace/first-namespace created
[root@manager-node demo]# kubectl get ns
NAME              STATUS   AGE
default           Active   4d23h
first-namespace   Active   7s
kube-node-lease   Active   4d23h
kube-public       Active   4d23h
kube-system       Active   4d23h
[root@manager-node demo]# 

kubectl create namespace third-namespace [创建namespace]

[root@manager-node demo]# kubectl create namespace third-namespace
namespace/third-namespace created
[root@manager-node demo]# kubectl get ns 
NAME               STATUS   AGE
default            Active   4d23h
first-namespace    Active   11m
kube-node-lease    Active   4d23h
kube-public        Active   4d23h
kube-system        Active   4d23h
second-namespace   Active   3m34s
third-namespace    Active   5s
[root@manager-node demo]# 

kubectl delete namespace third-namespace [删除某个namespace]

[root@manager-node demo]# kubectl get ns 
NAME               STATUS   AGE
default            Active   4d23h
first-namespace    Active   11m
kube-node-lease    Active   4d23h
kube-public        Active   4d23h
kube-system        Active   4d23h
second-namespace   Active   3m34s
third-namespace    Active   5s
[root@manager-node demo]# kubectl delete namespace third-namespace
namespace "third-namespace" deleted
[root@manager-node demo]# kubectl get ns
NAME               STATUS   AGE
default            Active   4d23h
first-namespace    Active   13m
kube-node-lease    Active   4d23h
kube-public        Active   4d23h
kube-system        Active   4d23h
second-namespace   Active   6m12s
[root@manager-node demo]# 

ymal文件中使用namespace

创建配置文件

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: first-namespace
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80

启动

[root@manager-node demo]# vi pod_nginx.yaml 
[root@manager-node demo]# kubectl apply -f pod_nginx.yaml
pod/nginx created
[root@manager-node demo]#

查看

[root@manager-node demo]# kubectl get ns
NAME               STATUS   AGE
default            Active   5d9h
first-namespace    Active   10h
kube-node-lease    Active   5d9h
kube-public        Active   5d9h
kube-system        Active   5d9h
second-namespace   Active   10h
[root@manager-node demo]# 
[root@manager-node demo]# kubectl get pods
No resources found.
[root@manager-node demo]# kubectl get pods -n first-namespace
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          16s
[root@manager-node demo]# kubectl get pods -n first-namespace -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP               NODE            NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          23s   192.168.101.28   worker01-node   <none>           <none>
[root@manager-node demo]# 

备注

kubectl get pods 命令,默认查看的是default 这个namespace下的pod;

kubectl get pods --all-namespaces [查看所有的namespace下的pod]

[root@manager-node demo]# kubectl get pods --all-namespaces
NAMESPACE         NAME                                       READY   STATUS    RESTARTS   AGE
first-namespace   nginx                                      1/1     Running   0          4m11s
kube-system       calico-kube-controllers-594b6978c5-7lxqz   1/1     Running   10         5d9h
kube-system       calico-node-82rx7                          1/1     Running   10         5d9h
kube-system       calico-node-xrqf8                          1/1     Running   10         4d18h
kube-system       calico-node-z86gh                          1/1     Running   8          4d18h
kube-system       coredns-fb8b8dccf-6hvcz                    1/1     Running   10         5d9h
kube-system       coredns-fb8b8dccf-x8xxc                    1/1     Running   10         5d9h
kube-system       etcd-manager-node                          1/1     Running   10         5d9h
kube-system       kube-apiserver-manager-node                1/1     Running   12         5d9h
kube-system       kube-controller-manager-manager-node       1/1     Running   11         5d9h
kube-system       kube-proxy-5q9b5                           1/1     Running   9          4d18h
kube-system       kube-proxy-pw6r7                           1/1     Running   10         4d18h
kube-system       kube-proxy-rvddm                           1/1     Running   10         5d9h
kube-system       kube-scheduler-manager-node                1/1     Running   12         5d9h
[root@manager-node demo]# 

你可能感兴趣的:(kubernetes)