Learn R Programming

AzureContainers (version 1.0.2)

kubernetes_cluster: Kubernetes cluster class

Description

Class representing a Kubernetes cluster. Note that this class can be used to interface with any Docker registry that supports the HTTP V2 API, not just those created via the Azure Container Registry service.

Usage

kubernetes_cluster

Arguments

Format

An object of class R6ClassGenerator of length 24.

Methods

The following methods are available, in addition to those provided by the AzureRMR::az_resource class:

  • new(...): Initialize a new registry object. See 'Initialization' below.

  • create_registry_secret(registry, secret_name, email): Provide authentication secret for a Docker registry. See 'Secrets' below.

  • delete_registry_secret(secret_name): Delete a registry authentication secret.

  • create(file): Creates a deployment or service from a file, using kubectl create -f.

  • get(type): Get information about resources, using kubectl get.

  • run(name, image): Run an image using kubectl run --image.

  • expose(name, type, file): Expose a service using kubectl expose. If the file argument is provided, read service information from there.

  • delete(type, name, file): Delete a resource (deployment or service) using kubectl delete. If the file argument is provided, read resource information from there.

  • apply(file): Apply a configuration file, using kubectl apply -f.

  • show_dashboard(port): Display the cluster dashboard. By default, use local port 30000.

  • kubectl(cmd): Run an arbitrary kubectl command on this cluster. Called by the other methods above.

  • helm(cmd): Run a helm command on this cluster.

Initialization

The new() method takes one argument: config, the name of the file containing the configuration details for the cluster. This should be a yaml or json file in the standard Kubernetes configuration format. Set this to NULL to use the default ~/.kube/config file.

Secrets

To allow a cluster to authenticate with a Docker registry, call the create_registry_secret method with the following arguments:

  • registry: An object of class either acr representing an Azure Container Registry service, or docker_registry representing the registry itself.

  • secret_name: The name to give the secret. Defaults to the name of the registry server.

  • email: The email address for the Docker registry.

Kubectl

The methods for this class call the kubectl commandline tool, passing it the --config option to specify the configuration information for the cluster. This allows all the features supported by Kubernetes to be available immediately and with a minimum of effort, although it does require that kubectl be installed. Any calls to kubectl will also contain the full commandline as the cmdline attribute of the (invisible) returned value; this allows scripts to be developed that can be run outside R.

See Also

aks, call_kubectl

Kubectl commandline reference

Examples

Run this code
# NOT RUN {
# recommended way of retrieving a cluster: via a resource group object
rg <- AzureRMR::az_rm$
    new(tenant="myaadtenant.onmicrosoft.com", app="app_id", password="password")$
    get_subscription("subscription_id")$
    get_resource_group("rgname")

# get the cluster endpoint
kubclus <- rg$get_aks("mycluster")$get_cluster()

# get registry authentication secret
kubclus$create_registry_secret(rg$get_acr("myregistry"))

# deploy a service
kubclus$create("deployment.yaml")

# can also supply the deployment parameters inline
kubclus$create("
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: model1
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: model1
    spec:
      containers:
      - name: model1
        image: myregistry.azurecr.io/model1
        ports:
        - containerPort: 8000
      imagePullSecrets:
      - name: myregistry.azurecr.io
---
apiVersion: v1
kind: Service
metadata:
  name: model1-svc
spec:
  selector:
    app: model1
  type: LoadBalancer
  ports:
  - protocol: TCP
    port: 8000")

# track status
kubclus$get("deployment")
kubclus$get("service")

# }

Run the code above in your browser using DataLab