Skip to content

Kubernetes with Kubespray

Syed Sayem edited this page Apr 2, 2019 · 9 revisions

Table of contents

 

Prerequisite

  • macOS: 10.14 (Mojave)
  • Vagrant: 2.2.4
  • Ansible: 2.7.9
  • Kubespray

 

Install and configure Kubespray

git clone git@github.com:kubernetes-sigs/kubespray.git
cd kubespray
mv inventory/sample/{hosts.ini,hosts.ini.bak}
sed -i -e 's/"download_run_once": "True",/"download_run_once": "False",/' Vagrantfile

 

Start Vagrant

vagrant up

This will spin up 3 VMs and install kubernetes on them. Once they are completed you can connect to any of them by running

$ vagrant ssh k8s-1

 

Configure k8s cluster

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

 

Verify Kubernetes cluster

vagrant@k8s-1:~$ kubectl get nodes -o=wide

NAME    STATUS   ROLES         AGE   VERSION   INTERNAL-IP    EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
k8s-1   Ready    master,node   20h   v1.13.5   172.17.8.101   <none>        Ubuntu 18.04.2 LTS   4.15.0-46-generic   docker://18.6.2
k8s-2   Ready    master,node   20h   v1.13.5   172.17.8.102   <none>        Ubuntu 18.04.2 LTS   4.15.0-46-generic   docker://18.6.2
k8s-3   Ready    node          20h   v1.13.5   172.17.8.103   <none>        Ubuntu 18.04.2 LTS   4.15.0-46-generic   docker://18.6.2

 

Run Docker as a non-root user

Run the following command and then close the current terminal session by running exit command

vagrant@k8s-1:~$ sudo usermod -a -G docker $USER

Now close the current terminal session

vagrant@k8s-1:~$ exit

 

Troubleshooting

If you get an error like the one below,

The connection to the server localhost:8080 was refused - did you specify the right host or port?

Run the following command to solved this issue:

sudo cp /etc/kubernetes/admin.conf $HOME/ && sudo chown $(id -u):$(id -g) $HOME/admin.conf && export KUBECONFIG=$HOME/admin.conf

Now run the command again:

vagrant@k8s-1:~$ kubectl get nodes

top  

SSH Config

If you want to ssh into your vagrant box without switching into the project directory and typing vagrant ssh k8s-1. Simply copy vagrant ssh-config to your ~/.ssh/config

$ cd ~/Vagrant
$ vagrant ssh-config >> ~/.ssh/config

This will allow you to use ssh k8s-1 from anywhere.

top  

Using SSHFS to Mount Vagrant on Mac

Copy your public key to your Vagrant box

$ ssh-copy-id vagrant@k8s-1

Create a local directory

$ mkdir ~/Desktop/k8s

Mounting your remote directory

$ sshfs vagrant@k8s-1:/home/vagrant ~/Desktop/k8s

top  

Remote Kubernetes Cluster

To connect to a remote Kubernetes cluster from you Mac, you'll need copy the Kubernetes credentials to your home directory as shown below.

scp -r vagrant@k8s-1:/home/vagrant/.kube ~/.kube/

That’s all you have to do. Your local Kubectl should be able to connect with the remote Kubernetes cluster now.

top  

K8s Dashboard

You can access the Kubernetes Dashboard at the following URL
https://first-master:6443/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/#!/login

Clone this wiki locally