Kevin Ashcraft

Linux & Radio Tutorials

Setup Kubernetes on DigitalOcean

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


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_nodes="PUBLIC_IP_0 PUBLIC_IP_1"

Install Kubernetes

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

Setup Commands

Create a file on your local machine ( with the setup commands.

# Start & Enable Docker
systemctl enable docker && systemctl start docker

# Install CNI Plugin
mkdir -p /opt/cni/bin
curl -L "${CNI_VERSION}/cni-plugins-amd64-${CNI_VERSION}.tgz" | tar -C /opt/cni/bin -xz

# Install kubeadm, kubelet, kubectl
RELEASE="$(curl -sSL"
mkdir -p /opt/bin
cd /opt/bin
curl -L --remote-name-all${RELEASE}/bin/linux/amd64/{kubeadm,kubelet,kubectl}
chmod +x {kubeadm,kubelet,kubectl}

# Create K8s Services
curl -sSL "${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 "${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)
echo "$ip $hostname" >> /etc/hosts


Copy the file to the servers and run it.

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

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=

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
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"

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