RK-API Quick Start Guide

You can access Rancher’s resources through the Kubernetes API. This guide helps you get started on using this API as a Rancher user.

  1. In the upper left corner, click ☰ > Global Settings.

  2. Find and copy the address in the server-url field.

  3. Create a Rancher API key with no scope.

    RK-API Quick Start Guide - 图1danger

    A Rancher API key with no scope grants unrestricted access to all resources that the user can access. To prevent unauthorized use, this key should be stored securely and rotated frequently.

  4. Create a kubeconfig.yaml file. Replace $SERVER_URL with the server url and $API_KEY with your Rancher API key:

    1. apiVersion: v1
    2. kind: Config
    3. clusters:
    4. - name: "rancher"
    5. cluster:
    6. server: "$SERVER_URL"
    7. users:
    8. - name: "rancher"
    9. user:
    10. token: "$API_KEY"
    11. contexts:
    12. - name: "rancher"
    13. context:
    14. user: "rancher"
    15. cluster: "rancher"
    16. current-context: "rancher"

You can use this file with any compatible tool, such as kubectl or client-go. For a quick demo, see the kubectl example.

For more information on handling more complex certificate setups, see Specifying CA Certs.

For more information on available kubeconfig options, see the upstream documentation.

API kubectl Example

In this example, we’ll show how to use kubectl to create a project, followed by deleting it. For a list of other Rancher resources available, refer to the API Reference page.

RK-API Quick Start Guide - 图2note

At this time, not all Rancher resources are available through the Rancher Kubernetes API.

  1. Set your KUBECONFIG environment variable to the kubeconfig file you just created:

    1. export KUBECONFIG=$(pwd)/kubeconfig.yaml
  2. Use kubectl explain to view the available fields for projects, or complex sub-fields of resources:

    1. kubectl explain projects
    2. kubectl explain projects.spec

Not all resources may have detailed output.

  1. Add the following content to a file named project.yaml:

    1. apiVersion: management.cattle.io/v3
    2. kind: Project
    3. metadata:
    4. # name should be unique across all projects in every cluster
    5. name: p-abc123
    6. # generateName can be used instead of `name` to randomly generate a name.
    7. # generateName: p-
    8. # namespace should match spec.ClusterName.
    9. namespace: local
    10. spec:
    11. # clusterName should match `metadata.Name` of the target cluster.
    12. clusterName: local
    13. description: Example Project
    14. # displayName is the human-readable name and is visible from the UI.
    15. displayName: Example
  2. Create the project:

    1. kubectl create -f project.yaml
  3. Delete the project:

    How you delete the project depends on how you created the project name.

    A. If you used name when creating the project:

    1. kubectl delete -f project.yaml

    B. If you used generateName:

    Replace $PROJECT_NAME with the randomly generated name of the project displayed by Kubectl after you created the project.

    1. kubectl delete project $PROJECT_NAME -n local

Specifying CA Certs

To ensure that your tools can recognize Rancher’s CA certificates, most setups require additional modifications to the above template.

  1. In the upper left corner, click ☰ > Global Settings.

  2. Find and copy the value in the ca-certs field.

  3. Save the value in a file named rancher.crt.

    RK-API Quick Start Guide - 图3note

    If your Rancher instance is proxied by another service, you must extract the certificate that the service is using, and add it to the kubeconfig file, as demonstrated in step 5.

  4. The following commands convert rancher.crt to base64 output, trim all new-lines, and update the cluster in the kubeconfig with the certificate, then finish by removing the rancher.crt file:

    1. export KUBECONFIG=$PATH_TO_RANCHER_KUBECONFIG
    2. kubectl config set clusters.rancher.certificate-authority-data $(cat rancher.crt | base64 -i - | tr -d '\n')
    3. rm rancher.crt
  5. (Optional) If you use self-signed certificatess that aren’t trusted by your system, you can set the insecure option in your kubeconfig with kubectl:

    RK-API Quick Start Guide - 图4danger

    This option shouldn’t be used in production as it is a security risk.

    1. export KUBECONFIG=$PATH_TO_RANCHER_KUBECONFIG
    2. kubectl config set clusters.rancher.insecure-skip-tls-verify true

    If your Rancher instance is proxied by another service, you must extract the certificate that the service is using, and add it to the kubeconfig file, as demonstrated above.