diff --git a/README.md b/README.md index 8d39d4b..cba4039 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,59 @@ -# Documentation +# Vagrant Kubernetes + +Table of Content + * [Requirements](#requirements) + * [Setup](#setup) + * [Kubernetes version](#kubernetes-version) + * [Starting](#starting) + * [Credits](#credits) + +A small playground to experiment or play with Kubernetes on multiple Vagrant Ubuntu `ubuntu/bionic64` instances. So do not use this as a base for production like deployments (Kubespray for example). + +## Requirements + +Please make sure the following is installed before using this repo: + +* Ansible; +* Vagrant; +* VirtualBox; + +## Setup + +Default the following is started/installed: + +* 1 Control node; +* 2 Worker nodes; + +Each node has 2 CPU's configured with each 2GB of RAM. You can change this to your needs by updating the `Vagrantfile`. + +## Kubernetes version + +Kubernetes version 1.19.3 which can be changed in the `Vagrantfile` by looking for the `k8s_version` in the `ansible.extra_vars` property. + +```ruby + ansible.extra_vars = { + ... + k8s_version: "1.19.3", + ansible_python_interpreter: "/usr/bin/python3", + } +``` + +## Starting + +Once you are ready, run the following to start everything: + +```sh +vagrant up +``` + +Once booted, use the following command to logon to the control node: + +```sh +vagrant ssh control +``` + +You should be able to run `kubectl get nodes` now. + +## Credits Combination of code https://graspingtech.com/create-kubernetes-cluster/ and some custom things. diff --git a/Vagrantfile b/Vagrantfile index 29cd69d..eeb48c5 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -9,29 +9,32 @@ Vagrant.configure("2") do |config| v.cpus = 2 end - config.vm.define "master" do |master| - master.vm.box = IMAGE_NAME - master.vm.network "private_network", ip: "10.10.1.10" - master.vm.hostname = "master" + config.vm.define "control" do |control| + control.vm.box = IMAGE_NAME + control.vm.network "private_network", ip: "10.10.1.10" + control.vm.hostname = "control" - master.vm.provision "ansible" do |ansible| - ansible.playbook = "master-playbook.yml" + control.vm.provision "ansible" do |ansible| + ansible.playbook = "control-playbook.yml" ansible.extra_vars = { node_ip: "10.10.1.10", + k8s_version: "1.19.3", ansible_python_interpreter: "/usr/bin/python3", } end end (1..N).each do |i| - config.vm.define "node-#{i}" do |node| - node.vm.box = IMAGE_NAME - node.vm.network "private_network", ip: "10.10.1.#{i + 10}" - node.vm.hostname = "node-#{i}" - node.vm.provision "ansible" do |ansible| - ansible.playbook = "node-playbook.yml" + config.vm.define "worker-#{i}" do |worker| + worker.vm.box = IMAGE_NAME + worker.vm.network "private_network", ip: "10.10.1.#{i + 10}" + worker.vm.hostname = "worker-#{i}" + worker.vm.provision "ansible" do |ansible| + ansible.playbook = "worker-playbook.yml" ansible.extra_vars = { + control_node_ip: "10.10.1.10", node_ip: "10.10.1.#{i + 10}", + k8s_version: "1.19.3", ansible_python_interpreter: "/usr/bin/python3", } end diff --git a/master-playbook.yml b/control-playbook.yml similarity index 85% rename from master-playbook.yml rename to control-playbook.yml index 9f0903a..44f1ece 100644 --- a/master-playbook.yml +++ b/control-playbook.yml @@ -1,17 +1,17 @@ --- - hosts: all become: true - tasks: + tasks: - name: "Update hosts file" lineinfile: path: /etc/hosts line: '{{ item }}' with_items: - - '10.10.1.10 master' - - '10.10.1.11 node-1' - - '10.10.1.12 node-2' - - '10.10.1.13 node-3' + - '{{ node_ip }} control' + - '10.10.1.11 worker-1' + - '10.10.1.12 worker-2' + - '10.10.1.13 worker-3' - name: Make sure group wheel is not in the sudoers configuration lineinfile: @@ -83,9 +83,9 @@ - name: Install Kubernetes binaries apt: name: - - kubelet - - kubeadm - - kubectl + - kubelet={{ k8s_version }}-00 + - kubeadm={{ k8s_version }}-00 + - kubectl={{ k8s_version }}-00 - helm - etcd-client state: present @@ -105,7 +105,7 @@ state: restarted - name: Initialize the Kubernetes cluster using kubeadm - command: kubeadm init --apiserver-advertise-address="10.10.1.10" --apiserver-cert-extra-sans="10.10.1.10" --node-name master --pod-network-cidr=192.168.0.0/16 + command: kubeadm init --apiserver-advertise-address="{{ node_ip }}" --apiserver-cert-extra-sans="{{ node_ip }}" --node-name control --pod-network-cidr=192.168.0.0/16 when: installed is changed - name: Create .kube folder @@ -131,6 +131,10 @@ become: false command: kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml + - name: Bash autocompletion + become: false + shell: kubectl completion bash >> ~/.bashrc + - name: Generate join command shell: kubeadm token create --print-join-command 2>/dev/null > /tmp/join register: join_command @@ -145,4 +149,4 @@ - name: docker status service: name: docker - state: started \ No newline at end of file + state: started diff --git a/node-playbook.yml b/worker-playbook.yml similarity index 89% rename from node-playbook.yml rename to worker-playbook.yml index 9f67c6e..3c8697d 100644 --- a/node-playbook.yml +++ b/worker-playbook.yml @@ -1,17 +1,17 @@ --- - hosts: all become: true - tasks: + tasks: - name: "Update hosts file" lineinfile: path: /etc/hosts line: '{{ item }}' with_items: - - '10.10.1.10 master' - - '10.10.1.11 node-1' - - '10.10.1.12 node-2' - - '10.10.1.13 node-3' + - '{{ control_node_ip }} control' + - '10.10.1.11 worker-1' + - '10.10.1.12 worker-2' + - '10.10.1.13 worker-3' - name: Make sure group wheel is not in the sudoers configuration lineinfile: @@ -73,9 +73,9 @@ - name: Install Kubernetes binaries apt: name: - - kubelet - - kubeadm - - kubectl + - kubelet={{ k8s_version }}-00 + - kubeadm={{ k8s_version }}-00 + - kubectl={{ k8s_version }}-00 state: present update_cache: yes @@ -105,4 +105,4 @@ - name: docker status service: name: docker - state: started \ No newline at end of file + state: started