在保证k8s集群成功运行的情况下,可以在任何一台联网的主机上向上文指定的IP+port发起HTTP请求,来查看k8s集群的各种信息情况。
由于k8s的Api是基于REST的设计思想,因此,不同种类的HTTP请求也就对应了不同的操作。比较常用的对应关系是:
具体关于RESTful Api的设计思想,详见阮老师的博客:http://www.ruanyifeng.com/blog/2014/05/restful_api.html
# 首先,需要在K8s的master节点上开启K8s API 代理。这里选择8080端口
kubectl proxy --port=8080 &
# 其次,设置变量KUBERNETES_MASTER
KUBERNETES_MASTER=localhost:8080
1.根据给定的nginx-pod.json文件创建一个nginx :
curl $KUBERNETES_MASTER/api/v1/namespaces/default/pods \
-XPOST -H'Content-Type: application/json' [email protected]
ps : 也可以利用REST API的格式传入长串json文本
curl --header "Content-Type: application/json" \
--request POST \
--data '{"kind": "Pod","apiVersion": "v1","metadata":{"name": "nginx","namespace": "default","labels": {"name": "nginx"}},"spec": {"containers": [{"name": "nginx","image": "nginx","ports": [{"containerPort": 80}],"resources": {"limits": {"memory": "128Mi","cpu": "500m"}}}]}}' \
http://$KUBERNETES_MASTER/api/v1/namespaces/default/pods
2.获取Pods的列表
curl $KUBERNETES_MASTER/api/v1/pods
3.根据给定的文件创建nginx的service :
curl $KUBERNETES_MASTER/api/v1/namespaces/default/services \
-XPOST -H'Content-Type: application/json' [email protected]
ps : 也可以利用REST API的格式传入长串json文本
curl --header "Content-Type: application/json" \
--request POST \
--data '{"kind": "Service","apiVersion": "v1","metadata": {"name": "nginx-service","namespace": "default","labels": {"name": "nginx"}},"spec": {"type":"NodePort","ports": [{"protocol":"TCP","port": 80}],"selector": {"name": "nginx"}}}' \
http://$KUBERNETES_MASTER/api/v1/namespaces/default/services
4.获取service的列表
curl $KUBERNETES_MASTER/api/v1/services
5.给定service中的详细信息
curl $KUBERNETES_MASTER/api/v1/namespaces/default/services/nginx-service/
6.删除nginx-service service
curl $KUBERNETES_MASTER/api/v1/namespaces/default/services/nginx-service -XDELETE
7.删除nginx pod
curl $KUBERNETES_MASTER/api/v1/namespaces/default/pods/nginx -XDELETE