Kubernetes Documentation

Note

This Kubernetes driver will be subject to change from community feedback. How to map the core assets (pods, clusters) to API entities will be subject to testing and further community feedback.

Kubernetes is an open source orchestration system for Docker containers. It handles scheduling onto nodes in a compute cluster and actively manages workloads to ensure that their state matches the users declared intentions. Using the concepts of “labels” and “pods”, it groups the containers which make up an application into logical units for easy management and discovery.

../../_images/kubernetes.png

Authentication

Authentication currently supported with the following methods:

Instantiating the driver

from libcloud.container.types import Provider
from libcloud.container.providers import get_driver

cls = get_driver(Provider.KUBERNETES)

# 1. Client side cert auth
conn = cls(
    host="192.168.99.103",
    port=8443,
    secure=True,
    key_file="/home/user/.minikube/client.key",
    cert_file="/home/user/.minikube/client.crt",
    ca_cert="/home/user/.minikube/ca.crt",
)

# 2. Bearer bootstrap token auth
conn = cls(key="my_token", host="126.32.21.4", ex_token_bearer_auth=True)

# 3. Basic auth
conn = cls(
    key="my_username", secret="THIS_IS)+_MY_SECRET_KEY+I6TVkv68o4H", host="126.32.21.4"
)

for container in conn.list_containers():
    print(container.name)

for cluster in conn.list_clusters():
    print(cluster.name)

Instantiating the driver (minikube installation - cert file auth)

This example shows how to connect to a local minikube Kubernetes cluster which utilizes certifcate based authentication.

from libcloud.container.types import Provider
from libcloud.container.providers import get_driver

import libcloud.security

# Disable cert vertification when running minikube locally using self signed
# cert
libcloud.security.VERIFY_SSL_CERT = False

cls = get_driver(Provider.KUBERNETES)

# You can retrieve cluster ip by running "minikube ip" command
conn = cls(
    host="192.168.99.103",
    port=8443,
    secure=True,
    key_file="/home/user/.minikube/client.key",
    cert_file="/home/user/.minikube/client.crt",
    ca_cert="/home/user/.minikube/ca.crt",
)

for container in conn.list_containers():
    print(container.name)

for cluster in conn.list_clusters():
    print(cluster.name)

Instantiating the driver (minikube installation - basic auth)

This example shows how to connect to a local minikube Kubernetes cluster which utilizes basic auth authentication.

When using basic auth, you need to start the minikube as shown below.

$ cat users.csv
pass123,user1,developers
# Mount a share with a local users file
minikube mount /home/libcloud/users.csv:/var/lib/docker/users.csv

# Start miniube
minikube --extra-config=apiserver.basic-auth-file=/var/lib/docker/users.csv start
from libcloud.container.types import Provider
from libcloud.container.providers import get_driver

import libcloud.security

# Disable cert vertification when running minikube locally using self signed
# cert
libcloud.security.VERIFY_SSL_CERT = False

cls = get_driver(Provider.KUBERNETES)

# You can retrieve cluster ip by running "minikube ip" command
conn = cls(key="user1", secret="pass123", host="192.168.99.100", port=8443, secure=True)

for container in conn.list_containers():
    print(container.name)

for cluster in conn.list_clusters():
    print(cluster.name)

Docker Hub Client HubClient is a shared utility class for interfacing to the public Docker Hub Service.

You can use this class for fetching images to deploy to services like ECS

from libcloud.container.types import Provider
from libcloud.container.providers import get_driver
from libcloud.container.utils.docker import HubClient

cls = get_driver(Provider.KUBERNETES)

conn = cls(
    key="my_username", secret="THIS_IS)+_MY_SECRET_KEY+I6TVkv68o4H", host="126.32.21.4"
)
hub = HubClient()

image = hub.get_image("ubuntu", "latest")

for cluster in conn.list_clusters():
    print(cluster.name)
    if cluster.name == "default":
        container = conn.deploy_container(
            cluster=cluster, name="my-simple-app", image=image
        )

API Docs

class libcloud.container.drivers.kubernetes.KubernetesContainerDriver(key=None, secret=None, secure=False, host='localhost', port=4243, key_file=None, cert_file=None, ca_cert=None, ex_token_bearer_auth=False)[source]
Parameters
  • key (str) – API key or username to be used (required)

  • secret (str) – Secret password to be used (required)

  • secure (bool) – Whether to use HTTPS or HTTP. Note: Some providers only support HTTPS, and it is on by default.

  • host (str) – Override hostname used for connections.

  • port (int) – Override port used for connections.

  • key_file (str) – Path to the key file used to authenticate (when using key file auth).

  • cert_file (str) – Path to the cert file used to authenticate (when using key file auth).

  • ex_token_bearer_auth (bool) – True to use token bearer auth.

Returns

None

connectionCls

alias of libcloud.common.kubernetes.KubernetesBasicAuthConnection

create_cluster(name, location=None)[source]

Create a container cluster (a namespace)

Parameters
  • name (str) – The name of the cluster

  • location (ClusterLocation) – The location to create the cluster in

Return type

ContainerCluster

deploy_container(name, image, cluster=None, parameters=None, start=True)[source]

Deploy an installed container image. In kubernetes this deploys a single container Pod. https://cloud.google.com/container-engine/docs/pods/single-container

Parameters
  • name (str) – The name of the new container

  • image (ContainerImage) – The container image to deploy

  • cluster (ContainerCluster) – The cluster to deploy to, None is default

  • parameters (str) – Container Image parameters

  • start (bool) – Start the container on deployment

Return type

Container

destroy_cluster(cluster)[source]

Delete a cluster (namespace)

Returns

True if the destroy was successful, otherwise False.

Return type

bool

destroy_container(container)[source]

Destroy a deployed container. Because the containers are single container pods, this will delete the pod.

Parameters

container (Container) – The container to destroy

Return type

bool

ex_destroy_pod(namespace, pod_name)[source]

Delete a pod and the containers within it.

ex_list_pods()[source]

List available Pods

Return type

list of KubernetesPod

get_cluster(id)[source]

Get a cluster by ID

Parameters

id (str) – The ID of the cluster to get

Return type

libcloud.container.base.ContainerCluster

get_container(id)[source]

Get a container by ID

Parameters

id (str) – The ID of the container to get

Return type

libcloud.container.base.Container

install_image(path)

Install a container image from a remote path.

Parameters

path (str) – Path to the container image

Return type

ContainerImage

list_clusters()[source]

Get a list of namespaces that pods can be deployed into

Parameters

location (libcloud.container.base.ClusterLocation) – The location to search in

Return type

list of libcloud.container.base.ContainerCluster

list_containers(image=None, all=True)[source]

List the deployed container images

Parameters
Return type

list of libcloud.container.base.Container

list_images()

List the installed container images

Return type

list of ContainerImage

list_locations()

Get a list of potential locations to deploy clusters into

Return type

list of ClusterLocation

restart_container(container)

Restart a deployed container

Parameters

container (Container) – The container to restart

Return type

Container

start_container(container)

Start a deployed container

Parameters

container (Container) – The container to start

Return type

Container

stop_container(container)

Stop a deployed container

Parameters

container (Container) – The container to stop

Return type

Container