Setting up the API
You can use the IBM Cloud® Kubernetes Service API to create and manage your community Kubernetes or Red Hat OpenShift clusters. To use the CLI, see Setting up the CLI.
About the API
The IBM Cloud Kubernetes Service API automates the provisioning and management of IBM Cloud infrastructure resources for your clusters so that your apps have the compute, networking, and storage resources that they need to serve your users.
The API supports the different infrastructure providers that are available for you to create clusters. For more information, see the infrastructure provider overview.
You can use the version two (v2) API to manage both classic and VPC clusters. The v2 API is designed to avoid breaking existing functionality when possible. However, make sure that you review the following differences
between the v1 and v2 API.
- API endpoint prefix
- v1 API:
https://containers.cloud.ibm.com/global/v1 - v2 API:
https://containers.cloud.ibm.com/global/v2 - v3 API:
https://containers.cloud.ibm.com/global/v3 - API reference docs
- v1 and v2 API
- v3 API.
- API architectural style
- v1 API: Representational state transfer (REST) that focuses on resources that you interact with through HTTP methods such as
GET,POST,PUT,PATCH, andDELETE. - v2 API: Remote procedure calls (RPC) that focus on actions through only
GETandPOSTHTTP methods. - Supported container platforms
- v1 API: Use the IBM Cloud Kubernetes Service API to manage your IBM Cloud infrastructure resources, such as worker nodes, for both community Kubernetes and Red Hat OpenShift clusters.
- v2 API: Use the IBM Cloud Kubernetes Service
v2API to manage your IBM Cloud infrastructure resources, such as worker nodes, for both community Kubernetes and Red Hat OpenShift VPC clusters. - Kubernetes API
- v1 API: To use the Kubernetes API to manage Kubernetes resources within the cluster, such as pods or namespaces, see Working with your cluster by using the Kubernetes API.
- v2 API: Same as
v1; see Working with your cluster by using the Kubernetes API. - Supported APIs by infrastructure type
- v1 API:
classic - v2 API:
vpcandclassic- The
vpcprovider is designed to support multiple VPC subproviders. The supported VPC subprovider isvpc-gen2, which corresponds to a VPC cluster for Generation 2 compute resources. - Provider-specific requests have a path parameter in the URL, such as
v2/vpc/createCluster. Some APIs are only available to a particular provider, such asGET vlanfor classic orGET vpcsfor VPC. - Provider-neutral requests can include a provider-specific body parameter that you specify, usually in JSON, such as
{"provider": "vpc"}, if you want to return responses for only the specified provider.
- The
GETresponses- v1 API: The
GETmethod for a collection of resources (such asGET v1/clusters) returns the same details for each resource in the list as aGETmethod for an individual resource (such asGET v1/clusters/{idOrName}). - v2 API: To return responses faster, the v2
GETmethod for a collection of resources (such asGET v2/clusters) returns only a subset of information that is detailed in aGETmethod for an individual resource (such asGET v2/clusters/{idOrName}). Some list responses include a providers property to identify whether the returned item applies to classic or VPC infrastructure. For example, theGET zoneslist returns some results such asmon01that are available only in the classic infrastructure provider, while other results such asus-south-01are available only in the VPC infrastructure provider. - Cluster, worker node, and worker-pool responses
- v1 API: Responses include only information that is specific to the classic infrastructure provider, such as the VLANs in
GETcluster and worker responses. - v2 API: The information that is returned varies depending on the infrastructure provider. For such provider-specific responses, you can specify the provider in your request. For example, VPC clusters don't return VLAN information since they don't have VLANs. Instead, they return subnet and CIDR network information.
Automating cluster deployments with the API
You can use the IBM Cloud Kubernetes Service API to automate the creation, deployment, and management of your Kubernetes clusters.
The IBM Cloud Kubernetes Service API requires header information that you must provide in your API request and that can vary depending on the API that you want to use. To determine what header information is needed for your API, see the IBM Cloud Kubernetes Service API documentation.
To authenticate with IBM Cloud Kubernetes Service, you must provide an IBM Cloud Identity and Access Management (IAM) token that is generated with your IBM Cloud credentials and that includes the IBM Cloud account ID where the cluster was created. Depending on the way you authenticate with IBM Cloud, you can choose between the following options to automate the creation of your IBM Cloud IAM token.
- Unfederated ID
-
- Generate an IBM Cloud API key: As an alternative to using the IBM Cloud username and password, you can use IBM Cloud API keys. IBM Cloud API keys are dependent on the IBM Cloud account they are generated for. You can't combine your IBM Cloud API key with a different account ID in the same IBM Cloud IAM token. To access clusters that were created with an account other than the one your IBM Cloud API key is based on, you must log in to the account to generate a new API key.
- IBM Cloud username and password: You can follow the steps in this topic to fully automate the creation of your IBM Cloud IAM access token.
- Federated ID
-
- Generate an IBM Cloud API key: IBM Cloud API keys are dependent on the IBM Cloud account they are generated for. You can't combine your IBM Cloud API key with a different account ID in the same IBM Cloud IAM token. To access clusters that were created with an account other than the one your IBM Cloud API key is based on, you must log in to the account to generate a new API key.
- Use a one-time passcode: If you authenticate with IBM Cloud by using a one-time passcode, you can't fully automate the creation of your IBM Cloud IAM token because the retrieval of your one-time passcode requires a manual interaction with your web browser. To fully automate the creation of your IBM Cloud IAM token, you must create an IBM Cloud API key instead.
- API key: To generate your IBM Cloud API key do as follows.
- From the menu bar, click Manage > Access (IAM).
- Click the Users page and then select yourself.
- In the API keys pane, click Create an IBM Cloud API key.
- Enter a Name and Description for your API key and click Create.
- Click Show to see the API key that was generated for you.
- Copy the API key so that you can use it to retrieve your new IBM Cloud IAM access token.
-
Create your IBM Cloud IAM access token. The body information that is included in your request varies based on the IBM Cloud authentication method that you use.
POST https://iam.cloud.ibm.com/identity/token- Header
-
Content-Type: application/x-www-form-urlencodedAuthorization: Basic Yng6Yng=whereYng6Yng=equals the URL-encoded authorization for the username bx and the password bx.
- Body for IBM Cloud username and password
-
grant_type: passwordusername: Your IBM Cloud username.password: Your IBM Cloud password.
- Body for IBM Cloud API keys
-
grant_type: urn:ibm:params:oauth:grant-type:apikeyapikey: Your IBM Cloud API key
- Body for IBM Cloud one-time passcode
-
grant_type: urn:ibm:params:oauth:grant-type:passcodepasscode: Your IBM Cloud one-time passcode. Runibmcloud login --ssoand follow the instructions in your CLI output to retrieve your one-time passcode by using your web browser.
The following example shows output for the previous request.
{ "access_token": "<iam_access_token>", "refresh_token": "<iam_refresh_token>", "token_type": "Bearer", "expires_in": 3600, "expiration": 1493747503 "scope": "ibm openid" }You can find the IBM Cloud IAM token in the
access_tokenfield of your API output. Note the IBM Cloud IAM token to retrieve additional header information in the next steps. -
Retrieve the ID of the IBM Cloud account that you want to work with. Replace
TOKENwith the IBM Cloud IAM token that you retrieved from theaccess_tokenfield of your API output in the previous step. In your API output, you can find the ID of your IBM Cloud account in the resources.metadata.guid field.GET https://accounts.cloud.ibm.com/coe/v2/accounts- Header
-
Content-Type: application/jsonAuthorization: bearer TOKENAccept: application/json
The following example shows the output of the previous request.
{ "next_url": null, "total_results": 5, "resources": [ { "metadata": { "guid": "<account_ID>", "url": "/coe/v2/accounts/<account_ID>", "created_at": "2016-09-29T02:49:41.842Z", "updated_at": "2018-08-16T18:56:00.442Z", "anonymousId": "1111a1aa1a1111a1aa11aa11111a1111" }, "entity": { "name": "<account_name>", -
Generate a new IBM Cloud IAM token that includes your IBM Cloud credentials and the account ID that you want to work with.
If you use an IBM Cloud API key, you must use the IBM Cloud account ID the API key was created for. To access clusters in other accounts, log in to this account and create an IBM Cloud API key that is based on this account.
POST https://iam.cloud.ibm.com/identity/token- Header
-
Content-Type: application/x-www-form-urlencodedAuthorization: Basic Yng6Yng=whereYng6Yng=equals the URL-encoded authorization for the username bx and the password bx.
- Body for IBM Cloud username and password
-
grant_type: passwordusername: Your IBM Cloud username.password: Your IBM Cloud password.bss_account: The IBM Cloud account ID that you retrieved in the previous step.
- Body for IBM Cloud API keys
-
grant_type: urn:ibm:params:oauth:grant-type:apikeyapikey: Your IBM Cloud API key.bss_account: The IBM Cloud account ID that you retrieved in the previous step.
- Body for IBM Cloud one-time passcode
-
grant_type: urn:ibm:params:oauth:grant-type:passcodepasscode: Your IBM Cloud passcode.bss_account: The IBM Cloud account ID that you retrieved in the previous step.
The following example shows output for the API request.
{ "access_token": "<iam_token>", "refresh_token": "<iam_refresh_token>", "token_type": "Bearer", "expires_in": 3600, "expiration": 1493747503 }You can find the IBM Cloud IAM token in the
access_tokenand the refresh token in therefresh_tokenfield of your API output. -
List all classic or VPC clusters in your account. If you want to run Kubernetes API requests against a cluster, make sure to note the name or ID of the cluster that you want to work with. Example request to list Classic clusters.
GET https://containers.cloud.ibm.com/global/v2/classic/getClusters- Header
Authorization: bearer <iam_token>
Example command to list VPC clusters.
GET https://containers.cloud.ibm.com/global/v2/vpc/getClusters?provider=vpc-gen2- Header
Authorization: Your IBM Cloud IAM access token (bearer <iam_token>).
-
Review the IBM Cloud Kubernetes Service API documentation to find a list of supported APIs.
When you use the API for automation, be sure to rely on the responses from the API, not files within those responses. For example, the Kubernetes configuration file for your cluster context is subject to change, so don't build automation based
on specific contents of this file when you use the GET /v1/clusters/{idOrName}/config call.
Working with your cluster by using the Kubernetes API
You can use the Kubernetes API to interact with your cluster in IBM Cloud Kubernetes Service.
The following instructions require public network access in your cluster to connect to the public cloud service endpoint of your Kubernetes master.
-
Follow the steps in Automating cluster deployments with the API to retrieve your IBM Cloud IAM access token, your IBM Cloud API key, the ID of the cluster where you want to run Kubernetes API requests, and the IBM Cloud Kubernetes Service region where your cluster is located.
-
Retrieve an IBM Cloud IAM ID, IAM access, and IAM refresh token using the IBM Cloud API key. In your API output, you can find the IAM ID token in the
id_tokenfield, the IAM access token in theaccess_tokenfield, and the IAM refresh token in therefresh_tokenfield.POST https://iam.cloud.ibm.com/identity/token- Header
-
Content-Type: application/x-www-form-urlencodedAuthorization: Basic a3ViZTprdWJla3ViZTprdWJlequals the URL-encoded authorization for the usernamekubeand the passwordkube.
- Body
-
grant_type: urn:ibm:params:oauth:grant-type:apikeyapikey:Your IBM Cloud API key.
The following example shows output from the previous API request.
{ "access_token": "<iam_access_token>", "id_token": "<iam_id_token>", "refresh_token": "<iam_refresh_token>", "token_type": "Bearer", "expires_in": 3600, "expiration": 1553629664, "refresh_token_expiration": 1761334993, "scope": "ibm openid containers-kubernetes" }Alternatively, using the
ibmcloud ks cluster config --cluster <cluster_name> --output jsonCLI command will display theid_tokenand therefresh_token. -
Before you can access your cluster with your current identity, you must run the following request.
POST https://containers.cloud.ibm.com/global/v2/applyRBAC- Header
Authorization: bearer <TOKEN>Your IBM Cloud IAM access token- Body
cluster: <cluster_name_or_ID>
-
Note that RBAC sync is asynchronous, so run the following request until it has synced.
GET https://containers.cloud.ibm.com/global/v2/getRBACStatus?cluster=<cluster_name_or_ID>
-H "Authorization: ${BEARER2}" ``` Header : Authorization: bearer <TOKEN> Your IBM Cloud IAM access token
Example response. Ensure the output shows `synchronized:true`.
```json {: screen}
{"synchronized":true,"error":false}
```
-
Retrieve the URL of the default service endpoint for your Kubernetes master by using the IAM access token and the name or ID of your cluster. You can find the URL in the
masterURLof your API output.If only the public cloud service endpoint or only the private cloud service endpoint is enabled for your cluster, that endpoint is listed for the
masterURL. If both the public and private cloud service endpoints are enabled for your cluster, the public cloud service endpoint is listed by default for themasterURL. To use the private cloud service endpoint instead, find the URL in theprivateServiceEndpointURLfield of the output.GET https://containers.cloud.ibm.com/global/v2/getCluster?cluster=<cluster_name_or_ID>- Header
-
Authorization: Your IBM Cloud IAM access token.
- Path
-
<cluster_name_or_ID>: The name or ID of your cluster that you retrieved with theGET https://containers.cloud.ibm.com/global/v2/classic/getClustersorGET https://containers.cloud.ibm.com/global/v2/vpc/getClusters?provider=vpc-gen2API in Automating cluster deployments with the API.
The following example shows output for a public cloud service endpoint request.
... "etcdPort": "31593", "masterURL": "https://c2.us-south.containers.cloud.ibm.com:30422", "ingress": { ...}The following example shows output for a private cloud service endpoint request.
... "etcdPort": "31593", "masterURL": "https://c2.private.us-south.containers.cloud.ibm.com:30422", "ingress": { ...} -
To use a private cloud service endpoint, you must first expose the private cloud service endpoint by using a load balancer IP that is routable from your VPN connection into the private network.
-
Run Kubernetes API requests against your cluster by using the IAM ID token that you retrieved earlier. For example, list the Kubernetes version that runs in your cluster.
If you enabled SSL certificate verification in your API test framework, make sure to disable this feature.
GET <masterURL>/api- Header
-
Authorization: bearer <id_token>
- Path
-
<masterURL>: The service endpoint of your Kubernetes master that you retrieved in the previous step.
The following example shows the output of the previous API request.
{ "kind": "APIVersions", "versions": [ "v1" ], "serverAddressByClientCIDRs": [ { "clientCIDR": "0.0.0.0/0", "serverAddress": "xxx.xx.x.x:xxxx" } ] } -
Review the Kubernetes API documentation to find a list of supported APIs for the latest Kubernetes version. Make sure to use the API documentation that matches the Kubernetes version of your cluster. If you don't use the latest Kubernetes version, append your version at the end of the URL. For example, to access the API documentation for version 1.12, add
v1.12.
Refreshing IAM access tokens with the API
Every IBM Cloud Identity and Access Management (IAM) access token that is issued via the API expires after one hour. You must refresh your access token regularly to assure access to the IBM Cloud API.
Before you begin, make sure that you have an IBM Cloud API key that you can use to request a new access token.
Use the following steps if you want to obtain a new IBM Cloud IAM token.
-
Generate a new IBM Cloud IAM access token by using the IBM Cloud API key.
POST https://iam.cloud.ibm.com/identity/token- Header
-
Content-Type: application/x-www-form-urlencoded
- Body
-
grant_type: urn:ibm:params:oauth:grant-type:apikeyapikey:Your IBM Cloud API key.
The following example shows the output of the previous API request.
{ "access_token": "<iam_token>", "refresh_token": "<iam_refresh_token>", "token_type": "Bearer", "expires_in": 3600, "expiration": 1493747503, "scope": "ibm openid" }You can find your new IBM Cloud IAM token in the
access_tokenfield of your API output. -
Continue working with the IBM Cloud Kubernetes Service API documentation by using the token from the previous step.