小言_互联网的博客

Kubernetes基础:CLI与Restful API:namespace管理

272人阅读  评论(0)


使用CLI方式可以通过kubectl对Kubernetes进行操作,同时也使用Restful API直接进行操作。这篇文章介绍一下两种方式下进行namespace的管理的方法。

环境准备

快速环境搭建建议使用单机版Kubernetes的安装脚本,一键安装,详情可参看:

kubectl 方式

获取所有namespace列表信息

使用kubectl可以直接使用如下命令获取namespace列表信息

kubectl命令:kubectl get namespaces

执行示例信息

[root@host132 ~]# kubectl get namespaces
NAME              STATUS   AGE
default           Active   6h25m
kube-node-lease   Active   6h25m
kube-public       Active   6h25m
kube-system       Active   6h25m
[root@host132 ~]# 

获取指定namespace信息

使用kubectl可以直接使用如下命令获取指定namespace信息(名为default的namespace)

kubectl命令:kubectl get namespaces default

执行示例信息

[root@host132 ~]# kubectl get namespaces default
NAME      STATUS   AGE
default   Active   6h29m
[root@host132 ~]# 

创建新的namespace

使用kubectl可以直接使用如下命令创建新的namespace(名为nskubectltest1的namespace)

kubectl命令:kubectl create namespace nskubectltest1

执行示例信息

[root@host132 ~]# kubectl create namespace nskubectltest1
namespace/nskubectltest1 created
[root@host132 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   6h41m
kube-node-lease   Active   6h41m
kube-public       Active   6h41m
kube-system       Active   6h41m
nskubectltest1    Active   6s
[root@host132 ~]#

删除指定的namespace

使用kubectl可以直接使用如下命令删除指定的namespace(名为nskubectltest1的namespace)

kubectl命令:kubectl delete namespace nskubectltest1

执行示例信息

[root@host132 ~]# kubectl delete namespace nskubectltest1
namespace "nskubectltest1" deleted
[root@host132 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   6h41m
kube-node-lease   Active   6h41m
kube-public       Active   6h41m
kube-system       Active   6h41m
[root@host132 ~]# 

curl方式

获取所有namespace列表信息

使用curl可以直接使用如下命令获取namespace列表信息

curl命令:curl --header “Authorization: bearer token信息” -k https://192.168.163.132:6443/api/v1/namespaces

执行示例信息

[root@host132 ~]# bearer_token=`cat /etc/k8s/token.csv |awk -F, '{print $1}'`
[root@host132 ~]# header_option="Authorization: bearer $bearer_token"
[root@host132 ~]# curl --header "$header_option" -k https://192.168.163.132:6443/api/v1/namespaces 2>/dev/null |grep name
    "selfLink": "/api/v1/namespaces",
        "name": "default",
        "selfLink": "/api/v1/namespaces/default",
        "name": "kube-node-lease",
        "selfLink": "/api/v1/namespaces/kube-node-lease",
        "name": "kube-public",
        "selfLink": "/api/v1/namespaces/kube-public",
        "name": "kube-system",
        "selfLink": "/api/v1/namespaces/kube-system",
[root@host132 ~]# 

获取指定namespace信息

使用curl可以直接使用如下命令获取指定namespace信息(名为default的namespace)

kubectl命令:curl --header “Authorization: bearer token信息” -k https://192.168.163.132:6443/api/v1/namespaces/default
执行示例信息

[root@host132 ~]# bearer_token=`cat /etc/k8s/token.csv |awk -F, '{print $1}'`
[root@host132 ~]# header_option="Authorization: bearer $bearer_token"
[root@host132 ~]# curl --header "$header_option" -k https://192.168.163.132:6443/api/v1/namespaces/default
{
  "kind": "Namespace",
  "apiVersion": "v1",
  "metadata": {
    "name": "default",
    "selfLink": "/api/v1/namespaces/default",
    "uid": "615afada-d48c-4e54-a638-8dbb6e1aa139",
    "resourceVersion": "154",
    "creationTimestamp": "2019-08-30T19:02:35Z"
  },
  "spec": {
    "finalizers": [
      "kubernetes"
    ]
  },
  "status": {
    "phase": "Active"
  }
}[root@host132 ~]#

创建新的namespace

使用curl可以直接使用如下命令创建新的namespace(名为nscurltest1的namespace)

kubectl命令:curl -k -X POST --header “Content-Type: application/json”
–header “Authorization: bearer $bearer_token”
https://192.168.163.132:6443/api/v1/namespaces/ -d ’
{
“apiVersion”: “v1”,
“kind”: “Namespace”,
“metadata”: {
“name”: “nscurltest1”
}
}’

执行示例信息

[root@host132 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   7h6m
kube-node-lease   Active   7h6m
kube-public       Active   7h6m
kube-system       Active   7h6m
[root@host132 ~]# bearer_token=`cat /etc/k8s/token.csv |awk -F, '{print $1}'`
[root@host132 ~]# curl -k -X POST --header "Content-Type: application/json" \
>                      --header "Authorization: bearer $bearer_token" \
>                      https://192.168.163.132:6443/api/v1/namespaces/ -d '
> {
>     "apiVersion": "v1",
>     "kind": "Namespace",
>     "metadata": {
>         "name": "nscurltest1"
>     }
> }'
{
  "kind": "Namespace",
  "apiVersion": "v1",
  "metadata": {
    "name": "nscurltest1",
    "selfLink": "/api/v1/namespaces/nscurltest1",
    "uid": "53e0b697-3520-4950-92a4-9aafa606898f",
    "resourceVersion": "31448",
    "creationTimestamp": "2019-08-31T02:08:50Z"
  },
  "spec": {
    "finalizers": [
      "kubernetes"
    ]
  },
  "status": {
    "phase": "Active"
  }
}[root@host132 ~]# 
[root@host132 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   7h6m
kube-node-lease   Active   7h6m
kube-public       Active   7h6m
kube-system       Active   7h6m
nscurltest1       Active   5s
[root@host132 ~]#

删除指定的namespace

使用kubectl可以直接使用如下命令删除指定的namespace(名为nscurltest1的namespace)

curl命令:curl -k -X DELETE --header “Content-Type: application/json”
–header “Authorization: bearer $bearer_token”
https://192.168.163.132:6443/api/v1/namespaces/nscurltest1

执行示例信息

[root@host132 ~]# kubectl delete namespace nscurltest1
[root@host132 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   7h7m
kube-node-lease   Active   7h7m
kube-public       Active   7h7m
kube-system       Active   7h7m
nscurltest1       Active   79s
[root@host132 ~]# bearer_token=`cat /etc/k8s/token.csv |awk -F, '{print $1}'`
[root@host132 ~]# curl -k -X DELETE --header "Content-Type: application/json" \
>                      --header "Authorization: bearer $bearer_token" \
>                      https://192.168.163.132:6443/api/v1/namespaces/nscurltest1
{
  "kind": "Namespace",
  "apiVersion": "v1",
  "metadata": {
    "name": "nscurltest1",
    "selfLink": "/api/v1/namespaces/nscurltest1",
    "uid": "53e0b697-3520-4950-92a4-9aafa606898f",
    "resourceVersion": "31561",
    "creationTimestamp": "2019-08-31T02:08:50Z",
    "deletionTimestamp": "2019-08-31T02:10:22Z"
  },
  "spec": {
    "finalizers": [
      "kubernetes"
    ]
  },
  "status": {
    "phase": "Terminating"
  }
}[root@host132 ~]# 
[root@host132 ~]# 
[root@host132 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   7h7m
kube-node-lease   Active   7h8m
kube-public       Active   7h8m
kube-system       Active   7h8m
[root@host132 ~]#

Restful API方式常见问题

  • https://liumiaocn.blog.csdn.net/article/details/100518110

其他基础


转载:https://blog.csdn.net/liumiaocn/article/details/100537187
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场