Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
vagrant_kubernetes_install [2020/03/09 19:03] – created andonovj | vagrant_kubernetes_install [2020/03/09 19:42] (current) – andonovj | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | TODO | + | =====Overview===== |
+ | Kubernetes is the next Evolution of Docker swarm, so in order to configure it, we have to firstly configure the Docker (just without the swarm this time) | ||
+ | We will configure 3 servers again: | ||
+ | |||
+ | * One master | ||
+ | * Two Workers | ||
+ | |||
+ | Since I hate seeing only Debian based, I will do it in both: Ubuntu and Centos. Firstly we will be the Ubuntu. | ||
+ | So let's get going: | ||
+ | |||
+ | |||
+ | =====Provision the VMs===== | ||
+ | You can return use the first section of the Vagrant advanced configurations or you can continue here, where we will do pretty much the same. I like wasting space :D | ||
+ | |||
+ | So let's get going with Vagrant again :) | ||
+ | |||
+ | |||
+ | < | ||
+ | IMAGE_NAME = " | ||
+ | N = 2 | ||
+ | |||
+ | Vagrant.configure(" | ||
+ | config.ssh.insert_key = false | ||
+ | |||
+ | config.vm.provider " | ||
+ | v.memory = 1024 | ||
+ | v.cpus = 2 | ||
+ | end | ||
+ | |||
+ | config.vm.define " | ||
+ | master.vm.box = IMAGE_NAME | ||
+ | master.vm.network " | ||
+ | master.vm.hostname = " | ||
+ | master.vm.provision " | ||
+ | ansible.playbook = " | ||
+ | ansible.extra_vars = { | ||
+ | node_ip: " | ||
+ | } | ||
+ | end | ||
+ | end | ||
+ | |||
+ | (1..N).each do |i| | ||
+ | config.vm.define " | ||
+ | node.vm.box = IMAGE_NAME | ||
+ | node.vm.network " | ||
+ | node.vm.hostname = " | ||
+ | node.vm.provision " | ||
+ | ansible.playbook = " | ||
+ | ansible.extra_vars = { | ||
+ | node_ip: " | ||
+ | } | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | |||
+ | As you can see, we are refering to two files here: | ||
+ | |||
+ | * master-playbook.yml - Ansible Playbook for the Master node | ||
+ | * node-playbook.yml - Ansible Playbook for the other Nodes. | ||
+ | |||
+ | Please create the following files as follows: | ||
+ | |||
+ | ====Configure the Master Playbook==== | ||
+ | Let's start building the master playbook: | ||
+ | |||
+ | < | ||
+ | --- | ||
+ | - hosts: all | ||
+ | become: true | ||
+ | tasks: | ||
+ | - name: Install packages that allow apt to be used over HTTPS | ||
+ | apt: | ||
+ | name: "{{ packages }}" | ||
+ | state: present | ||
+ | update_cache: | ||
+ | vars: | ||
+ | packages: | ||
+ | - apt-transport-https | ||
+ | - ca-certificates | ||
+ | - curl | ||
+ | - gnupg-agent | ||
+ | - software-properties-common | ||
+ | |||
+ | - name: Add an apt signing key for Docker | ||
+ | apt_key: | ||
+ | url: https:// | ||
+ | state: present | ||
+ | |||
+ | - name: Add apt repository for stable version | ||
+ | apt_repository: | ||
+ | repo: deb [arch=amd64] https:// | ||
+ | state: present | ||
+ | |||
+ | - name: Install docker and its dependecies | ||
+ | apt: | ||
+ | name: "{{ packages }}" | ||
+ | state: present | ||
+ | update_cache: | ||
+ | vars: | ||
+ | packages: | ||
+ | - docker-ce | ||
+ | - docker-ce-cli | ||
+ | - containerd.io | ||
+ | notify: | ||
+ | - docker status | ||
+ | |||
+ | - name: Add vagrant user to docker group | ||
+ | user: | ||
+ | name: vagrant | ||
+ | group: docker | ||
+ | - name: Remove swapfile from / | ||
+ | mount: | ||
+ | name: "{{ item }}" | ||
+ | fstype: swap | ||
+ | state: absent | ||
+ | with_items: | ||
+ | - swap | ||
+ | - none | ||
+ | </ | ||
+ | |||
+ | ===Remove the swap=== | ||
+ | Please bare in mind that the kubelet won't install if the swap is enabled, so we have to add the following: | ||
+ | |||
+ | < | ||
+ | - name: Disable swap | ||
+ | command: swapoff -a | ||
+ | when: ansible_swaptotal_mb > 0 | ||
+ | </ | ||
+ | |||
+ | ===Install Kubelet, kubeadm, kubectl=== | ||
+ | After that, we can add a task to install the: kubelet, kubeadm and kubectl using the below code. | ||
+ | |||
+ | < | ||
+ | - name: Add an apt signing key for Kubernetes | ||
+ | apt_key: | ||
+ | url: https:// | ||
+ | state: present | ||
+ | |||
+ | - name: Adding apt repository for Kubernetes | ||
+ | apt_repository: | ||
+ | repo: deb https:// | ||
+ | state: present | ||
+ | filename: kubernetes.list | ||
+ | |||
+ | - name: Install Kubernetes binaries | ||
+ | apt: | ||
+ | name: "{{ packages }}" | ||
+ | state: present | ||
+ | update_cache: | ||
+ | vars: | ||
+ | packages: | ||
+ | - kubelet | ||
+ | - kubeadm | ||
+ | - kubectl | ||
+ | |||
+ | - name: Configure node ip | ||
+ | lineinfile: | ||
+ | path: / | ||
+ | line: KUBELET_EXTRA_ARGS=--node-ip={{ node_ip }} | ||
+ | |||
+ | - name: Restart kubelet | ||
+ | service: | ||
+ | name: kubelet | ||
+ | daemon_reload: | ||
+ | state: restarted | ||
+ | </ | ||
+ | |||
+ | ===Initialize the Kubernetes=== | ||
+ | Finally, we can add the Tasks for the Initialization as follows: | ||
+ | |||
+ | < | ||
+ | - name: Initialize the Kubernetes cluster using kubeadm | ||
+ | command: kubeadm init --apiserver-advertise-address=" | ||
+ | </ | ||
+ | |||
+ | ===Configure Vagrant User for the Cluster=== | ||
+ | Since we are using vagrant, we can setup vagrant to access the Kubernetes Cluster using the following Task: | ||
+ | |||
+ | < | ||
+ | - name: Setup kubeconfig for vagrant user | ||
+ | command: "{{ item }}" | ||
+ | with_items: | ||
+ | - mkdir -p / | ||
+ | - cp -i / | ||
+ | - chown vagrant: | ||
+ | </ | ||
+ | |||
+ | ===Configure the Network provider and policy engine=== | ||
+ | < | ||
+ | - name: Setup kubeconfig for vagrant user | ||
+ | command: "{{ item }}" | ||
+ | with_items: | ||
+ | - mkdir -p / | ||
+ | - cp -i / | ||
+ | - chown vagrant: | ||
+ | </ | ||
+ | |||
+ | ====Configure the Node Playbook==== | ||
+ | We will setup a join file which will be used in the playbook for the other nodes: | ||
+ | |||
+ | < | ||
+ | - name: Generate join command | ||
+ | command: kubeadm token create --print-join-command | ||
+ | register: join_command | ||
+ | |||
+ | - name: Copy join command to local file | ||
+ | local_action: | ||
+ | </ | ||
+ | |||
+ | The generated, from Kubernetes, join command will be saved in file called: " | ||
+ | |||
+ | ===Configure handlers=== | ||
+ | We have to also setup the handlers for checking the Docker daemon: | ||
+ | |||
+ | < | ||
+ | handlers: | ||
+ | - name: docker status | ||
+ | service: name=docker state=started | ||
+ | </ | ||
+ | |||
+ | Finally we can configure the node-playbook.yml | ||
+ | |||
+ | < | ||
+ | - name: Copy the join command to server location | ||
+ | copy: src=join-command dest=/ | ||
+ | |||
+ | - name: Join the node to cluster | ||
+ | command: sh / | ||
+ | </ | ||
+ |