Rancher + K8S RestApi使用


 

1前言

1.1使用的軟件及版本

軟件

版本號

Rancher

1.6stable

Kubernetes

1.8.3

Docker

1.12.6

 

 

 

 

 

 

1.2 RancherK8SRESTAPI差異

因為目前使用Rancher作為K8S的部署工具,Rancher封裝了K8SREST API(僅僅是做了一層代理),且K8S apiserver是作為內部服務(沒有開放對外端口),因此無法直接訪問K8Sapi。不過可以通過Rancher開放的端口訪問,其他內容還是沒有變化的。

K8SREST 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授權

要訪問K8SRESTAPI需要提供認證信息API Key

API Key使用Bearer認證,包含集群所有者的兩部分內容:Access KeySecret Key。這兩個信息都可以通過在RancherUI中獲得。

具體獲取方式如下:

①使用該集群所有者信息登錄RancherUI,點擊【API> 【密鑰】

 

 

②點擊【添加賬號API Key

這里Api Key的名稱和描述僅僅作為輔助作用,點創建會彈出真正需要記錄的內容。

 

 

③點擊【創建】

圖片中的警告信息說明的已經很清晰了,不再解釋。

 

 

④根據Access KeySecret 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主要管理三種資源:PodreplicationControllerservice

這里有一篇針對這三個資源的RESTAPI文檔:

http://cdn.rawgit.com/GoogleCloudPlatform/kubernetes/31a0daae3627c91bc96e1f02a6344cd76e294791/api/kubernetes.html

 

至於其他API,暫未找到好的較好的,如果想獲取某操作的API url,可以通過kubectl執行操作的時候添加-v=8來獲得。例如刪除pod

kubectl delete pods ubuntu1 -v=8

 

得到

 

 

 4. JavaClient 的使用

 

KubernetesAPIJavaClient

 

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM