How to Access KubeSphere API

Edit

ks-apigateway is KubeSphere’s API gateway. After deploying KubeSphere or its backend, you can refer to the follow instruction to access the APIs.

Step 1: Expose ks-apigatway Service

ks-apigatway’s service port can be exposed via NodePort. You can do it either through console or through command line:

  1. Log in to KubeSphere using the admin account, open Web Kubectl in the「Toolbox」in the bottom right corner, and execute the following command.
  1. $ kubectl -n kubesphere-system patch svc ks-apigateway -p '{"spec":{"type":"NodePort"}}'
  2. service/ks-apigateway patched
  1. Use the following command to view the generated port number. Here is 31078.
  1. $ kubectl -n kubesphere-system get svc ks-apigateway -o jsonpath='{.spec.ports[0].nodePort}'
  2. 31078

Step 2: Get Token

All the KubeSphere’s APIs should pass the JWT Bearer token authentication. Before invoking API, you need to get access_token from /kapis/iam.kubesphere.io/v1alpha2/login port. Then add the Authorization: Bearer <access_token> into the immediate next requests.

Execute the following commands where 192.168.0.20 is the cluster node IP and 31078 is the ks-apigatway service NodePort exposed in the previous step.

Note: Please replace the IP and NodePort with yours.

Request example

  1. curl -X POST \
  2. http://192.168.0.20:31078/kapis/iam.kubesphere.io/v1alpha2/login \
  3. -H 'Content-Type: application/json' \
  4. -d '{
  5. "username":"admin",
  6. "password":"P@88w0rd"
  7. }'

Response

  1. {
  2. "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImFkbWluQGt1YmVzcGhlcmUuaW8iLCJpYXQiOjE1NzM3Mjg4MDMsInVzZXJuYW1lIjoiYWRtaW4ifQ.uK1KoK1c8MFkm8KnyORFTju31OsZ1ajtGNZQnUS1qk8"
  3. }

Step 3: Access KubeSphere API

After the access token retrieved, the KubeSphere API can be invoked in a user-defined request function, please note attach the request header Authorization: Bearer <access_token> in each API request. For further details, please refer to API Guide.

For instance, the following request is to get all components status.

  1. curl -X GET \
  2. http://192.168.0.20:31078/api/v1/componentstatuses \
  3. -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImFkbWluQGt1YmVzcGhlcmUuaW8iLCJpYXQiOjE1NzM3Mjg4MDMsInVzZXJuYW1lIjoiYWRtaW4ifQ.uK1KoK1c8MFkm8KnyORFTju31OsZ1ajtGNZQnUS1qk8'

It responses as follows:

  1. {
  2. "kind": "ComponentStatusList",
  3. "apiVersion": "v1",
  4. "metadata": {
  5. "selfLink": "/api/v1/componentstatuses"
  6. },
  7. "items": [
  8. {
  9. "metadata": {
  10. "name": "scheduler",
  11. "selfLink": "/api/v1/componentstatuses/scheduler",
  12. "creationTimestamp": null
  13. },
  14. "conditions": [
  15. {
  16. "type": "Healthy",
  17. "status": "True",
  18. "message": "ok"
  19. }
  20. ]
  21. },
  22. {
  23. "metadata": {
  24. "name": "controller-manager",
  25. "selfLink": "/api/v1/componentstatuses/controller-manager",
  26. "creationTimestamp": null
  27. },
  28. "conditions": [
  29. {
  30. "type": "Healthy",
  31. "status": "True",
  32. "message": "ok"
  33. }
  34. ]
  35. },
  36. {
  37. "metadata": {
  38. "name": "etcd-1",
  39. "selfLink": "/api/v1/componentstatuses/etcd-1",
  40. "creationTimestamp": null
  41. },
  42. "conditions": [
  43. {
  44. "type": "Healthy",
  45. "status": "True",
  46. "message": "{\"health\": \"true\"}"
  47. }
  48. ]
  49. },
  50. {
  51. "metadata": {
  52. "name": "etcd-0",
  53. "selfLink": "/api/v1/componentstatuses/etcd-0",
  54. "creationTimestamp": null
  55. },
  56. "conditions": [
  57. {
  58. "type": "Healthy",
  59. "status": "True",
  60. "message": "{\"health\": \"true\"}"
  61. }
  62. ]
  63. },
  64. {
  65. "metadata": {
  66. "name": "etcd-2",
  67. "selfLink": "/api/v1/componentstatuses/etcd-2",
  68. "creationTimestamp": null
  69. },
  70. "conditions": [
  71. {
  72. "type": "Healthy",
  73. "status": "True",
  74. "message": "{\"health\": \"true\"}"
  75. }
  76. ]
  77. }
  78. ]
  79. }

How to Access Swagger UI

KubeSphere’s API can be previewed by accessing the URL http://IP:NodePort/swagger-ui to visit Swagger UI, for instance, this example is http://192.168.0.20:31078/swagger-ui/.

Swagger