违法℃玩家 发表于 2018-1-5 22:01:45

Rancher + K8S RestApi使用



1前言
  1.1使用的软件及版本
  软件
  版本号
  Rancher
  1.6stable
  Kubernetes
  1.8.3
  Docker
  1.12.6
  1.2 Rancher与K8S的RESTAPI差异
  因为目前使用Rancher作为K8S的部署工具,Rancher封装了K8S的REST API(仅仅是做了一层代理),且K8S apiserver是作为内部服务(没有开放对外端口),因此无法直接访问K8S的api。不过可以通过Rancher开放的端口访问,其他内容还是没有变化的。
  原K8S的REST API访问链接
  https://k8s_apiserver_ip:6443/api/v1/namespaces/default/
  现Rancher提供的RESTAPI链接
  http://rancher_server_ip:8080/r/projects/1a7/kubernetes:6443/v1/namespaces/default/

2 REST API授权
  要访问K8S的RESTAPI需要提供认证信息API Key。
  API Key使用Bearer认证,包含集群所有者的两部分内容:Access Key和Secret Key。这两个信息都可以通过在RancherUI中获得。
  具体获取方式如下:
  ①使用该集群所有者信息登录RancherUI,点击【API】> 【密钥】
https://images2017.cnblogs.com/blog/390726/201712/390726-20171220093920396-1090637966.png
  ②点击【添加账号API Key】
  这里Api Key的名称和描述仅仅作为辅助作用,点创建会弹出真正需要记录的内容。
https://images2017.cnblogs.com/blog/390726/201712/390726-20171220093930693-421740319.png
  ③点击【创建】
  图片中的警告信息说明的已经很清晰了,不再解释。
https://images2017.cnblogs.com/blog/390726/201712/390726-20171220093939771-1824629770.png
  ④根据Access Key和Secret Key计算认证信息。
  先获取 <AccessKey>:<SecretKey> 的Base64编码,长度84,记做KeyCode84
  再获取 Basic <KeyCode84> 的Base64编码,长度120,记做KeyCode120
  ⑤使用
  发起REST API请求的时候,在请求头里加上以下认证信息即可:
  Authorization:Bearer <KeyCode120>
  例如:
  Authorization:Bearer
  QmFzaWMgTXpjNVFVSkRRalJCT1RRMFJUQTFPRVF5UlVZNldXbFJNbmM1UVcxUVIzaEZlbHBrTlc1NU56VnZRbmhCY1RreFIxRjBObU55ZVRsRVFuVnRhdz09
  QmFzaWMgTUVNME0wWXpPVU0wTmtNek5USXhNRVV4TWpRNmVHMDFhRlY2WkRnelMxazBjM2xTT0ZKa2MyTnJTRFJUYm1seGFXTktOelI2Tm5ONmFYUkJXQT09

3 REST API使用
  3.1 api功能分类
  官方api的使用文档,https://kubernetes.io/docs/api-reference/v1.9
  访问http://rancher_server_ip:8080/r/projects/1a7/kubernetes:6443可以获取到所有支持的接口:
  {
  "paths": [
  "/api",
  "/api/v1", //蓝色部分是核心API Group
  "/apis",
  "/apis/",
  "/apis/apiextensions.k8s.io",
  "/apis/apiextensions.k8s.io/v1beta1",
  "/apis/apiregistration.k8s.io",
  "/apis/apiregistration.k8s.io/v1beta1",
  "/apis/apps",
  "/apis/apps/v1beta1",
  "/apis/apps/v1beta2",
  "/apis/authentication.k8s.io",
  "/apis/authentication.k8s.io/v1",
  "/apis/authentication.k8s.io/v1beta1",
  "/apis/authorization.k8s.io",
  "/apis/authorization.k8s.io/v1",
  "/apis/authorization.k8s.io/v1beta1",
  "/apis/autoscaling",
  "/apis/autoscaling/v1",
  "/apis/autoscaling/v2beta1",
  "/apis/batch",
  "/apis/batch/v1",
  "/apis/batch/v1beta1",
  "/apis/batch/v2alpha1",
  "/apis/certificates.k8s.io",
  "/apis/certificates.k8s.io/v1beta1",
  "/apis/extensions",
  "/apis/extensions/v1beta1", //扩展组
  "/apis/networking.k8s.io",
  "/apis/networking.k8s.io/v1",
  "/apis/policy",
  "/apis/policy/v1beta1",
  "/apis/rbac.authorization.k8s.io",
  "/apis/rbac.authorization.k8s.io/v1",
  "/apis/rbac.authorization.k8s.io/v1beta1",
  "/apis/storage.k8s.io",
  "/apis/storage.k8s.io/v1",
  "/apis/storage.k8s.io/v1beta1",//其他APIGroup
  "/healthz",
  "/healthz/autoregister-completion",
  "/healthz/etcd",
  "/healthz/ping",
  "/healthz/poststarthook/apiservice-openapi-controller",
  "/healthz/poststarthook/apiservice-registration-controller",
  "/healthz/poststarthook/apiservice-status-available-controller",
  "/healthz/poststarthook/bootstrap-controller",
  "/healthz/poststarthook/ca-registration",
  "/healthz/poststarthook/generic-apiserver-start-informers",
  "/healthz/poststarthook/kube-apiserver-autoregistration",
  "/healthz/poststarthook/start-apiextensions-controllers",
  "/healthz/poststarthook/start-apiextensions-informers",
  "/healthz/poststarthook/start-kube-aggregator-informers",
  "/healthz/poststarthook/start-kube-apiserver-informers",
  "/logs",
  "/metrics",
  "/swagger-2.0.0.json",
  "/swagger-2.0.0.pb-v1",
  "/swagger-2.0.0.pb-v1.gz",
  "/swagger.json",
  "/swaggerapi",
  "/ui",
  "/ui/",
  "/version"
  ]
  }
  Kubernetes主要管理三种资源:Pod,replicationController,service
  这里有一篇针对这三个资源的RESTAPI文档:
  http://cdn.rawgit.com/GoogleCloudPlatform/kubernetes/31a0daae3627c91bc96e1f02a6344cd76e294791/api/kubernetes.html
  至于其他API,暂未找到好的较好的,如果想获取某操作的API url,可以通过kubectl执行操作的时候添加-v=8来获得。例如删除pod
  

kubectl delete pods ubuntu1 -v=8  

  得到
https://images2017.cnblogs.com/blog/390726/201712/390726-20171220093950287-1915494346.png
  4. JavaClient 的使用

KubernetesAPIJavaClient

xiaoxi2011 发表于 2024-7-8 01:26:46

强烈支持楼主ing……
页: [1]
查看完整版本: Rancher + K8S RestApi使用