kubernetes

package
v0.0.0-...-a355528 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2017 License: Apache-2.0 Imports: 20 Imported by: 0

README

InfraKit Flavor Plugin - Kubernetes

A reference implementation of a Flavor Plugin that creates a Kubernetes cluster.

Schema & Templates

This plugin has a schema that looks like this:

For manager

{
    "InitScriptTemplateURL": "file:///home/ubuntu/go/src/github.com/docker/infrakit/examples/flavor/kubernetes/manager-init.sh",
    "KubeJoinIP": "192.168.2.200",
    "KubeBindPort": 6443,
    "KubeAddOns": [
        {
            "Name" : "flannel",
            "Type" : "network",
            "Path" : ""
        }
    ]

}

For workers

{
    "InitScriptTemplateURL": "file:///home/ubuntu/go/src/github.com/docker/infrakit/examples/flavor/kubernetes/worker-init.sh",
    "KubeJoinIP": "192.168.2.200",
    "KubeBindPort": 6443,
}
 

Note KubeJoinIP, KubeBindPort that the Kubernetes connection information, as well as what IP in the Kubernetes managers and workers should use to advertise and join.

KubeAddOns is list of kubernetes addons. You can set Type as network or visualise. network Type addon should be set as your cluster will not be Ready status until network addon is applyed.

This plugin makes heavy use of Golang template to enable customization of instance behavior on startup. For example, the InitScriptTemplateURL field above is a URL where a init script template is served. The plugin will fetch this template from the URL and processe the template to render the final init script for the instance.

The plugin exposes a set of template functions that can be used, along with primitives already in [Golang template] (https://golang.org/pkg/text/template/) and functions from Sprig. This makes it possible to have complex templates for generating the user data / init script of the instances.

For example, this is a template for the init script of a manager node:

#!/bin/sh
set -o errexit
set -o nounset
set -o xtrace

{{/* Install Docker */}}
{{ include "install-docker.sh" }}

{{/* Install Kubeadm */}}
{{ include "install_kubeadam.sh" }}
kubeadm init --token {{ KUBEADM_JOIN_TOKEN }}
export KUBECONFIG=/etc/kubernetes/admin.conf
{{ if NETWORK_ADDON }}
    kubectl apply -f {{ NETWORK_ADDON }}
{{ else }}
{{ end }}

There are tags such as {{ KUBEADM_JOIN_TOKEN }} or {{ INSTANCE_LOGICAL_ID }}: these are made available by the plugin and they are evaluated / interpolated during the Prepare phase of the plugin. The plugin will substitute these 'placeholders' with actual values. The templating engine also supports inclusion of other templates / files, as seen in the {{ include "install-docker.sh" }} tag above. This makes it easy to embed actual shell scripts, and other texts, without painful and complicated escapes to meet the JSON syntax requirements. For example, the 'include' tag above will embed the install-docker.sh template/file:

# Tested on Ubuntu/trusty

apt-get update -y
wget -qO- https://get.docker.com/ | sh

A Word on Security

Since Kubeadm use Token to authorize nodes, initializing the Kubernetes requires:

Docken socke API server exposes the remote API, but it is protected by TLS. Infrakit intends to make access to kubernetes manager from the side, but we can not send commands such as get nodes yet. For installation, we use kubeadm and build a secure cluster.

Building & Running -- An Example

There are scripts in this directory to illustrate how to start up the InfraKit plugin ensemble and examples for creating a kubernetes via vagrant.

Building the binaries - do this from the top level project directory:

make binaries

Start required plugins. We use the infrakit plugin start utility and a plugins.json to start up all the plugins, along with the InfraKit manager:

infrakit-group-default
infrakit-instance-vagrant
infrakit-flavor-kubernetes

Now start up the cluster comprised of a manager and a worker group. In this case, see groups-master.json where we will create a manager group of one node and in group-worker.json create a worker group of 3 nodes. The topology in this is a single ensemble of infrakit running on your local machine that manages 4 vagrant vms running Kubernetes.
At Kubernetes flavor, you should run manager group first. Worker group will try to connect to manager before start. And as this flavor based on kubeadm, currently it support only one manager node.

infrakit group commit groups-manager.json

Wait for manager comes up. As it need to install docker and kubeadm, it take a little time...

infrakit group commit groups-worker.json

Now cluster will come up. Now check the kubernetes: You should log in to manager node. Then

export KUBECONFIG=/etc/kubernetes/admin.conf
kubectl get nodes
NAME               STATUS    AGE       VERSION
ip-192.168.2.200   Ready     4m        v1.6.3
ip-192.168.2.2     Ready     2m        v1.6.3
ip-192.168.2.3     Ready     2m        v1.6.3
ip-192.168.2.4     Ready     2m        v1.6.3

Documentation

Index

Constants

View Source
const (
	// DefaultManagerInitScriptTemplate is the default template for the init script which
	// the flavor injects into the user data of the instance to configure Docker Swarm Managers
	DefaultManagerInitScriptTemplate = `` /* 601-byte string literal not displayed */

	// DefaultWorkerInitScriptTemplate is the default template for the init script which
	// the flavor injects into the user data of the instance to configure Docker Swarm.
	DefaultWorkerInitScriptTemplate = `` /* 137-byte string literal not displayed */

)
View Source
const (
	// AllInstances as a special logical ID for use in the Attachments map
	AllInstances = instance.LogicalID("*")
)

Variables

View Source
var (

	// DefaultTemplateOptions contains the default values to use for templates
	DefaultTemplateOptions = template.Options{MultiPass: true}
)

Functions

This section is empty.

Types

type AddOnInfo

type AddOnInfo struct {
	Name string
	Type string
	Path string
}

AddOnInfo is info mation of kubernetes add on information. Type is add on type network and visualise. See https://kubernetes.io/docs/concepts/cluster-administration/addons/

type ManagerFlavor

type ManagerFlavor struct {
	// contains filtered or unexported fields
}

ManagerFlavor is the flavor for kube managers

func NewManagerFlavor

func NewManagerFlavor(plugins func() discovery.Plugins, options Options, stop <-chan struct{}) (*ManagerFlavor, error)

NewManagerFlavor creates a flavor.Plugin that creates manager and worker nodes connected in a kubernetes.

func (ManagerFlavor) Drain

func (s ManagerFlavor) Drain(flavorProperties *types.Any, inst instance.Description) error

TODO - call kubectl drain and then delete node

func (ManagerFlavor) Get

func (s ManagerFlavor) Get(path types.Path) (*types.Any, error)

Get implements the metadata.Plugin SPI's List method

func (ManagerFlavor) Healthy

func (s ManagerFlavor) Healthy(flavorProperties *types.Any, inst instance.Description) (flavor.Health, error)

Healthy determines whether an instance is healthy.

func (ManagerFlavor) List

func (s ManagerFlavor) List(path types.Path) ([]string, error)

List implements the metadata.Plugin SPI's List method

func (*ManagerFlavor) Prepare

func (s *ManagerFlavor) Prepare(flavorProperties *types.Any,
	instanceSpec instance.Spec, allocation group_types.AllocationMethod,
	index group_types.Index) (instance.Spec, error)

Prepare sets up the provisioner / instance plugin's spec based on information about the kubernetes to join.

func (*ManagerFlavor) Validate

func (s *ManagerFlavor) Validate(flavorProperties *types.Any, allocation group_types.AllocationMethod) error

Validate checks whether the helper can support a configuration.

type Options

type Options struct {

	// ConfigDir is the location where the plugin uses to store some state like join token
	ConfigDir string

	// DefaultManagerInitScriptTemplate is the URL for the default control plane template url.
	// It's overridden by the init script template url specified in the properties.
	DefaultManagerInitScriptTemplate types.URL

	// DefaultWorkerInitScriptTemplate is the URL for the default data plane (workers) template url.
	// This is overridden by the template specified in the properties
	DefaultWorkerInitScriptTemplate types.URL

	// MultiMaster specifies if the control plane supports multi master
	MultiMaster bool
}

Options capture static plugin-related settings

type Spec

type Spec struct {

	// Attachments indicate the devices that are to be attached to the instance.
	// If the logical ID is '*' (the AllInstances const) then the attachment applies to all instances.
	Attachments map[instance.LogicalID][]instance.Attachment

	// InitScriptTemplateURL overrides the template specified when the plugin started up.
	InitScriptTemplateURL string

	// KubeJoinIP is the IP for managers and workers to join
	KubeJoinIP string

	// KubeBindPort is the IP for managers and workers to join
	KubeBindPort int

	// KubeAddOns is the networking and network policy provider
	KubeAddOns []AddOnInfo

	// KubeClusterID is the ID of Kubernetes Cluster you will deploy.
	KubeClusterID string

	// SkipManagerValidation is skip to check manager for worker
	SkipManagerValidation bool

	// ControlPlane are the nodes that make up the Kube control plane.  This is a list of logical IDs
	// that correspond to the manager group of nodes.  For a single master setup, this slice should
	// contain a single element of the IP address or some identifier for the master node.
	ControlPlane []instance.LogicalID
}

Spec is the value passed in the `Properties` field of configs

type WorkerFlavor

type WorkerFlavor struct {
	// contains filtered or unexported fields
}

WorkerFlavor is the flavor for kubernetes workers

func NewWorkerFlavor

func NewWorkerFlavor(plugins func() discovery.Plugins, options Options, stop <-chan struct{}) (*WorkerFlavor, error)

NewWorkerFlavor creates a flavor.Plugin that creates manager and worker nodes connected in a kubernetes.

func (WorkerFlavor) Drain

func (s WorkerFlavor) Drain(flavorProperties *types.Any, inst instance.Description) error

TODO - call kubectl drain and then delete node

func (WorkerFlavor) Get

func (s WorkerFlavor) Get(path types.Path) (*types.Any, error)

Get implements the metadata.Plugin SPI's List method

func (WorkerFlavor) Healthy

func (s WorkerFlavor) Healthy(flavorProperties *types.Any, inst instance.Description) (flavor.Health, error)

Healthy determines whether an instance is healthy.

func (WorkerFlavor) List

func (s WorkerFlavor) List(path types.Path) ([]string, error)

List implements the metadata.Plugin SPI's List method

func (*WorkerFlavor) Prepare

func (s *WorkerFlavor) Prepare(flavorProperties *types.Any,
	instanceSpec instance.Spec, allocation group_types.AllocationMethod,
	index group_types.Index) (instance.Spec, error)

Prepare sets up the provisioner / instance plugin's spec based on information about the kubernetes to join.

func (*WorkerFlavor) Validate

func (s *WorkerFlavor) Validate(flavorProperties *types.Any, allocation group_types.AllocationMethod) error

Validate checks whether the helper can support a configuration.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL