Saturday, September 23, 2017

Bash Script to Build Kubernetes on LXD for Development Environment

LXD is cool
Docker is cool
Kubernetes is cool

Getting them to all play nice, not so easy.  Here's what I've done to make it happen all in one script run on the latest/greatest Ubuntu 16.04. You'll have to get interactive with the lxd init piece because --auto doesn't seem to do the trick for whatever reason.

This assumes you've configured LXD to your liking on the parent host.

Don't ask about the sleeps and the double installation of conjure-up, because I don't know why, it just works on my server. Feel free to suggest improvements.

2 Scripts

#1 echo_hosts.sh - A quick add of a self-referencing hosts entry for the parent container
-------------------
#!/bin/bash

echo `ip addr show |grep eth0 |grep inet|grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b\/24"|cut -d \/ -f1` "kubernetes" >> /etc/hosts
--------------------

#2 build_kubernetes_lxd.sh
---------------

#!/bin/bash
sudo sysctl fs.inotify.max_user_instances=1048576  
sudo sysctl fs.inotify.max_queued_events=1048576  
sudo sysctl fs.inotify.max_user_watches=1048576  
sudo sysctl vm.max_map_count=262144
lxc launch ubuntu:16.04 kubernetes -c security.privileged=true -c security.nesting=true -c linux.kernel_modules=ip_tables,ip6_tables,netlink_diag,nf_nat,overlay -c raw.lxc=lxc.aa_profile=unconfined
lxc config device add kubernetes mem unix-char path=/dev/mem
sleep 5
lxc exec kubernetes -- apt update
sleep 5
lxc exec kubernetes -- apt dist-upgrade -y
sleep 5
lxc exec kubernetes -- apt install squashfuse -y
sleep 20 
lxc exec kubernetes -- snap install conjure-up --classic 
sleep 5
lxc exec kubernetes -- snap install conjure-up --classic 
sleep 5
lxc exec kubernetes -- reboot
sleep 20
lxc exec kubernetes -- apt -y remove lxd
sleep 5
lxc exec kubernetes -- snap install lxd
sleep 10
lxc exec kubernetes -- /snap/bin/lxd init 
sleep 5
lxc file push echo_hosts.sh kubernetes/root/
lxc exec kubernetes -- chmod 770 ~/echo_hosts.sh
lxc exec kubernetes -- ~/echo_hosts.sh
lxc exec kubernetes -- sudo -u ubuntu -i /snap/bin/conjure-up kubernetes

--------------

Note: This script gets you through to the interactive conjure-up console. From there you choose the options of your choice and aim it at localhost (which should not be greyed out if all went well with the installer.) It's important that you use /snap/bin/lxc to see the nested kubernetes containers.  It's easy to forget, so might add a symlink to /usr/bin/ if it suites you.

Reference Sources:
https://insights.ubuntu.com/2017/02/20/running-kubernetes-inside-lxd/
https://insights.ubuntu.com/2016/11/21/conjure-up-canonical-kubernetes-under-lxd-today/

No comments :

Post a Comment