CloudFoundry命令列和Kubernetes命令列的Restful API消費方式

i042416發表於2018-10-12

先說CloudFoundry的命令列工具CLI。我們在CloudFoundry環境下工作,第一個使用的命令就是cf login。

如果在環境變數裡維護CF_TRACE的值為true:

CloudFoundry命令列和Kubernetes命令列的Restful API消費方式

則我們能發現,諸如cf login這種命令,實際上也是透過消費Restful API來完成的。

CloudFoundry命令列和Kubernetes命令列的Restful API消費方式

下圖是cf login這個命令的api endpoint請求細節,供大家參考:

API endpoint: 

REQUEST: [2018-09-21T14:50:57+08:00]

GET /v2/info HTTP/1.1

Host: api.cf.eu10.hana.ondemand.com

Accept: application/json

Content-Type: application/json

User-Agent: go-cli 6.36.1+e3799ad7e.2018-04-04 / windows

RESPONSE: [2018-09-21T14:50:59+08:00]

HTTP/1.1 200 OK

Connection: close

Content-Length: 550

Content-Type: application/json;charset=utf-8

Date: Fri, 21 Sep 2018 06:50:58 GMT

Server: nginx

X-Content-Type-Options: nosniff

X-Vcap-Request-Id: abf32f52-294a-41f5-5919-be948d78f0dd::a32b17bb-da82-4d45-930f-f0344c8a83b3

{"name":"","build":"","support":"","version":0,"description":"Cloud Foundry at SAP Cloud Platform","authorization_endpoint":" ","token_endpoint":"[PRIVATE DATA HIDDEN]","min_cli_version":null,"min_recommended_cli_version":null,"api_version":"2.115.0","app_ssh_endpoint":" ssh.cf.eu10.hana.ondemand.com:2222 ","app_ssh_host_key_fingerprint":"f3:12:47:b5:3a:19:6e:6c:4e:9d:90:2e:6f:8e:87:cc","app_ssh_oauth_client":"ssh-proxy","doppler_logging_endpoint":" wss://doppler.cf.eu10.hana.ondemand.com:443 "}

REQUEST: [2018-09-21T14:50:59+08:00]

GET /login HTTP/1.1

Host: login.cf.eu10.hana.ondemand.com

Accept: application/json

Content-Type: application/json

User-Agent: go-cli 6.36.1+e3799ad7e.2018-04-04 / windows

API響應結果:

RESPONSE: [2018-09-21T14:51:00+08:00]

HTTP/1.1 200 OK

Connection: close

Content-Length: 551

Cache-Control: no-store

Content-Language: en-US

Content-Type: application/json;charset=UTF-8

Date: Fri, 21 Sep 2018 06:50:59 GMT

Set-Cookie: X-Uaa-Csrf=8uoxBvyG8QCwo29efrrZNh; Max-Age=86400; Expires=Sat, 22-Sep-2018 06:51:00 GMT; Path=/; Secure; HttpOnly

Strict-Transport-Security: max-age=31536000 ; includeSubDomains

X-Content-Type-Options: nosniff

X-Frame-Options: DENY

X-Vcap-Request-Id: f6b29d8f-f78e-4c5e-61f3-5c9d906828ed

X-Xss-Protection: 1; mode=block

{"app":{"version":"4.19.0"},"links":{"uaa":" ","passwd":" ","login":" ","register":" "},"zone_name":"uaa","entityID":" login.cf.eu10.hana.ondemand.com ","commit_id":"7897100","idpDefinitions":{},"prompts":{"username":["text","Email"],"password":["password","Password"]},"timestamp":"2018-06-13T12:02:09-0700"}

Email>

再看Kubernetes。我們用的很多的命令:

kubectl get pods,返回pods列表。

CloudFoundry命令列和Kubernetes命令列的Restful API消費方式

CloudFoundry命令列和Kubernetes命令列的Restful API消費方式

而用命令列

kubectl --v=8 get pods

則發現,get pods這個命令實際上也是發請求發往Kubernetes的API server:

API請求明細如下:

CloudFoundry命令列和Kubernetes命令列的Restful API消費方式

CloudFoundry命令列和Kubernetes命令列的Restful API消費方式

API server是Kubernetes最重要的核心元件之一:

1. 提供叢集管理的REST API介面,包括認證授權、資料校驗以及叢集狀態變更等

2. 提供其他模組之間的資料互動和通訊的樞紐(其他模組透過API Server查詢或修改

資料,只有API Server才直接操作etcd)

kube-apiserver支援同時提供https(預設監聽在6443埠)和http API(預設監聽在

127.0.0.1的8080埠),其中http API是非安全介面,不做任何認證授權機制,不建議

生產環境啟用。兩個介面提供的REST API格式相同,參考Kubernetes API Reference查

看所有API的呼叫格式。

在實際使用中,通常透過kubectl來訪問apiserver,也可以透過Kubernetes各個語言的

client庫來訪問apiserver。

比如上面get pods命令對應的API的文件:

CloudFoundry命令列和Kubernetes命令列的Restful API消費方式

要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2216131/,如需轉載,請註明出處,否則將追究法律責任。

相關文章