Kevin Ashcraft

Linux & Radio Tutorials

Setup Kubernetes on DigitalOcean

This is an example of how to setup a Kubernetes cluster on DigitalOcean with CoreOS.

Additional

Create Droplets

Create at least two CoreOS droplets in the same region: k8s-master, k8s-000, k8s-001, etc.

Save IP Addresses

For easy access throughout the rest of this guide, save the ip addresses of the new servers to the variables k8s_master and k8s_nodes.

k8s_master="PUBLIC_IP"
k8s_nodes="PUBLIC_IP_0 PUBLIC_IP_1"

Install Kubernetes

Install Kubernets with these steps from Kubernetes.io, by making a script to copy to each system.

Setup Commands

Create a file on your local machine (k8s-install.sh) with the setup commands.

k8s-install.sh

#!/bin/bash
# Start & Enable Docker
systemctl enable docker && systemctl start docker

# Install CNI Plugin
CNI_VERSION="v0.6.0"
mkdir -p /opt/cni/bin
curl -L "https://github.com/containernetworking/plugins/releases/download/${CNI_VERSION}/cni-plugins-amd64-${CNI_VERSION}.tgz" | tar -C /opt/cni/bin -xz

# Install kubeadm, kubelet, kubectl
RELEASE="$(curl -sSL https://dl.k8s.io/release/stable.txt)"
mkdir -p /opt/bin
cd /opt/bin
curl -L --remote-name-all https://storage.googleapis.com/kubernetes-release/release/${RELEASE}/bin/linux/amd64/{kubeadm,kubelet,kubectl}
chmod +x {kubeadm,kubelet,kubectl}

# Create K8s Services
curl -sSL "https://raw.githubusercontent.com/kubernetes/kubernetes/${RELEASE}/build/debs/kubelet.service" | sed "s:/usr/bin:/opt/bin:g" > /etc/systemd/system/kubelet.service
mkdir -p /etc/systemd/system/kubelet.service.d
curl -sSL "https://raw.githubusercontent.com/kubernetes/kubernetes/${RELEASE}/build/debs/10-kubeadm.conf" | sed "s:/usr/bin:/opt/bin:g" > /etc/systemd/system/kubelet.service.d/10-kubeadm.conf

# Start & Enable Kubelet
systemctl enable kubelet && systemctl start kubelet

# Networking
sysctl net.bridge.bridge-nf-call-iptables=1
ip=$(ip -f inet -o addr show eth0|cut -d\  -f 7 | cut -d/ -f 1)
hostname=`hostname`
echo "$ip $hostname" >> /etc/hosts

    

Copy the k8s-install.sh file to the servers and run it.

for node_ip in $k8s_master $k8s_nodes; do
  scp k8s-install.sh core@$node_ip:
  ssh core@$node_ip 'sudo chmod +x k8s-install.sh; sudo ./k8s-install.sh'
done

Initialize the Cluster

Kubeadm is a newer tool that initializes a Kubernetes cluster following best practices. Kubeadm is first ran on the master which produces another command to run on each additional node.

On the Master
ssh core@$k8s_master

Use kubeadm to initialize a cluster, and include an address range to use for the pod network (created with CNI).

sudo kubeadm init --pod-network-cidr=192.168.0.0/16

There will be a kubeadm command printed in the output. Save it to use on the nodes you want to join the cluster.

Next, copy the config file with the secrets and connection information for the cluster.

mkdir -p $HOME/.kube
sudo cp -f /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Finally, connect to the new Kubernetes cluster to install a CNI. Here's an example using Calico.

kubectl apply -f https://docs.projectcalico.org/v3.0/getting-started/kubernetes/installation/hosted/kubeadm/1.7/calico.yaml
Connect the Workers

Run the kubeadm command from the output above to join the cluster.

join_cmd="kubeadm join --token ..."
for node_ip in $k8s_nodes; do
  ssh core@$node_ip "sudo $join_cmd"
done

Useful Commands

List all of the nodes

kubectl get nodes

List all of the deployments

kubectl get deployments --all-namespaces

Describe a $service in a non-default $namespace

kubectl describe services $service -n $namespace

Apply a configuration file

kubectl apply -f $yaml_file

Stop Kubelet

sudo systemctl stop kubelet

Reset Kubeadm

sudo kubeadm reset

Describe the the $type of $thing

kubectl describe $get $thing --all-namespaces