{"id":1511,"date":"2019-03-16T22:40:06","date_gmt":"2019-03-16T22:40:06","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw93\/?p=1511"},"modified":"2019-03-22T18:18:16","modified_gmt":"2019-03-22T18:18:16","slug":"kubernetes-setup-using-ansible-and-vagrant","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw93\/index.php\/2019\/03\/16\/kubernetes-setup-using-ansible-and-vagrant\/","title":{"rendered":"Kubernetes Setup Using Ansible and Vagrant"},"content":{"rendered":"<h2 id=\"objective\">Objective<\/h2>\n<p>This blog post describes the steps required to setup a multi node Kubernetes cluster for development purposes. This setup provides a production-like cluster that can be setup on your local machine.<\/p>\n<h2 id=\"why-do-we-require-multi-node-cluster-setup\">Why do we require multi node cluster setup?<\/h2>\n<p>Multi node Kubernetes clusters offer a production-like environment which has various advantages. Even though Minikube provides an excellent platform for getting started, it doesn\u2019t provide the opportunity to work with multi node clusters which can help solve problems or bugs that are related to application design and architecture. For instance, Ops can reproduce an issue in a multi node cluster environment, Testers can deploy multiple versions of an application for executing test cases and verifying changes. These benefits enable teams to resolve issues faster which make the more agile.<\/p>\n<h2 id=\"why-use-vagrant-and-ansible\">Why use Vagrant and Ansible?<\/h2>\n<p>Vagrant is a tool that will allow us to create a virtual environment easily and it eliminates pitfalls that cause the works-on-my-machine phenomenon. It can be used with multiple providers such as Oracle VirtualBox, VMware, Docker, and so on. It allows us to create a disposable environment by making use of configuration files.<\/p>\n<p>Ansible is an infrastructure automation engine that automates software configuration management. It is agentless and allows us to use SSH keys for connecting to remote machines. Ansible playbooks are written in yaml and offer inventory management in simple text files.<\/p>\n<h3 id=\"prerequisites\">Prerequisites<\/h3>\n<ul>\n<li>Vagrant should be installed on your machine. Installation binaries can be found\u00a0<a href=\"https:\/\/www.vagrantup.com\/downloads.html\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/li>\n<li>Oracle VirtualBox can be used as a Vagrant provider or make use of similar providers as described in Vagrant\u2019s official\u00a0<a href=\"https:\/\/www.vagrantup.com\/docs\/providers\/\" target=\"_blank\" rel=\"noopener\">documentation<\/a>.<\/li>\n<li>Ansible should be installed in your machine. Refer to the\u00a0<a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/installation_guide\/intro_installation.html\" target=\"_blank\" rel=\"noopener\">Ansible installation guide<\/a>\u00a0for platform specific installation.<\/li>\n<\/ul>\n<h2 id=\"setup-overview\">Setup overview<\/h2>\n<p>We will be setting up a Kubernetes cluster that will consist of one master and two worker nodes. All the nodes will run Ubuntu Xenial 64-bit OS and Ansible playbooks will be used for provisioning.<\/p>\n<h4 id=\"step-1-creating-a-vagrantfile\">Step 1: Creating a Vagrantfile<\/h4>\n<p>Use the text editor of your choice and create a file with named\u00a0<code>Vagrantfile<\/code>, inserting the code below. The value of N denotes the number of nodes present in the cluster, it can be modified accordingly. In the below example, we are setting the value of N as 2.<\/p>\n<div class=\"highlight\">\n<pre><code class=\"language-ruby\" data-lang=\"ruby\">IMAGE_NAME = \"bento\/ubuntu-16.04\"\r\nN = 2\r\n\r\nVagrant.configure(\"2\") do |config|\r\n    config.ssh.insert_key = false\r\n\r\n    config.vm.provider \"virtualbox\" do |v|\r\n        v.memory = 1024\r\n        v.cpus = 2\r\n    end\r\n      \r\n    config.vm.define \"k8s-master\" do |master|\r\n        master.vm.box = IMAGE_NAME\r\n        master.vm.network \"private_network\", ip: \"192.168.50.10\"\r\n        master.vm.hostname = \"k8s-master\"\r\n        master.vm.provision \"ansible\" do |ansible|\r\n            ansible.playbook = \"kubernetes-setup\/master-playbook.yml\"\r\n        end\r\n    end\r\n\r\n    (1..N).each do |i|\r\n        config.vm.define \"node-#{i}\" do |node|\r\n            node.vm.box = IMAGE_NAME\r\n            node.vm.network \"private_network\", ip: \"192.168.50.#{i + 10}\"\r\n            node.vm.hostname = \"node-#{i}\"\r\n            node.vm.provision \"ansible\" do |ansible|\r\n                ansible.playbook = \"kubernetes-setup\/node-playbook.yml\"\r\n            end\r\n        end\r\n    end<\/code><\/pre>\n<\/div>\n<h3 id=\"step-2-create-an-ansible-playbook-for-kubernetes-master\">Step 2: Create an Ansible playbook for Kubernetes master.<\/h3>\n<p>Create a directory named\u00a0<code>kubernetes-setup<\/code>\u00a0in the same directory as the\u00a0<code>Vagrantfile<\/code>. Create two files named\u00a0<code>master-playbook.yml<\/code>\u00a0and\u00a0<code>node-playbook.yml<\/code>\u00a0in the directory\u00a0<code>kubernetes-setup<\/code>.<\/p>\n<p>In the file\u00a0<code>master-playbook.yml<\/code>, add the code below.<\/p>\n<h4 id=\"step-2-1-install-docker-and-its-dependent-components\">Step 2.1: Install Docker and its dependent components.<\/h4>\n<p>We will be installing the following packages, and then adding a user named \u201cvagrant\u201d to the \u201cdocker\u201d group. &#8211; docker-ce &#8211; docker-ce-cli &#8211; containerd.io<\/p>\n<div class=\"highlight\">\n<pre><code class=\"language-yaml\" data-lang=\"yaml\">---\r\n- hosts: all\r\n  become: true\r\n  tasks:\r\n  - name: Install packages that allow apt to be used over HTTPS\r\n    apt:\r\n      name: \"{{ packages }}\"\r\n      state: present\r\n      update_cache: yes\r\n    vars:\r\n      packages:\r\n      - apt-transport-https\r\n      - ca-certificates\r\n      - curl\r\n      - gnupg-agent\r\n      - software-properties-common\r\n\r\n  - name: Add an apt signing key for Docker\r\n    apt_key:\r\n      url: https:\/\/download.docker.com\/linux\/ubuntu\/gpg\r\n      state: present\r\n\r\n  - name: Add apt repository for stable version\r\n    apt_repository:\r\n      repo: deb [arch=amd64] https:\/\/download.docker.com\/linux\/ubuntu xenial stable\r\n      state: present\r\n\r\n  - name: Install docker and its dependecies\r\n    apt: \r\n      name: \"{{ packages }}\"\r\n      state: present\r\n      update_cache: yes\r\n    vars:\r\n      packages:\r\n      - docker-ce \r\n      - docker-ce-cli \r\n      - containerd.io\r\n    notify:\r\n      - docker status\r\n\r\n  - name: Add vagrant user to docker group\r\n    user:\r\n      name: vagrant\r\n      group: docker<\/code><\/pre>\n<\/div>\n<h4 id=\"step-2-2-kubelet-will-not-start-if-the-system-has-swap-enabled-so-we-are-disabling-swap-using-the-below-code\">Step 2.2: Kubelet will not start if the system has swap enabled, so we are disabling swap using the below code.<\/h4>\n<div class=\"highlight\">\n<pre><code class=\"language-yaml\" data-lang=\"yaml\">  - name: Remove swapfile from \/etc\/fstab\r\n    mount:\r\n      name: \"{{ item }}\"\r\n      fstype: swap\r\n      state: absent\r\n    with_items:\r\n      - swap\r\n      - none\r\n\r\n  - name: Disable swap\r\n    command: swapoff -a\r\n    when: ansible_swaptotal_mb &gt; 0<\/code><\/pre>\n<\/div>\n<h4 id=\"step-2-3-installing-kubelet-kubeadm-and-kubectl-using-the-below-code\">Step 2.3: Installing kubelet, kubeadm and kubectl using the below code.<\/h4>\n<div class=\"highlight\">\n<pre><code class=\"language-yaml\" data-lang=\"yaml\">  - name: Add an apt signing key for Kubernetes\r\n    apt_key:\r\n      url: https:\/\/packages.cloud.google.com\/apt\/doc\/apt-key.gpg\r\n      state: present\r\n\r\n  - name: Adding apt repository for Kubernetes\r\n    apt_repository:\r\n      repo: deb https:\/\/apt.kubernetes.io\/ kubernetes-xenial main\r\n      state: present\r\n      filename: kubernetes.list\r\n\r\n  - name: Install Kubernetes binaries\r\n    apt: \r\n      name: \"{{ packages }}\"\r\n      state: present\r\n      update_cache: yes\r\n    vars:\r\n      packages:\r\n        - kubelet \r\n        - kubeadm \r\n        - kubectl<\/code><\/pre>\n<\/div>\n<h4 id=\"step-2-3-initialize-the-kubernetes-cluster-with-kubeadm-using-the-below-code-applicable-only-on-master-node\">Step 2.3: Initialize the Kubernetes cluster with kubeadm using the below code (applicable only on master node).<\/h4>\n<div class=\"highlight\">\n<pre><code class=\"language-yaml\" data-lang=\"yaml\">  - name: Initialize the Kubernetes cluster using kubeadm\r\n    command: kubeadm init --apiserver-advertise-address=\"192.168.50.10\" --apiserver-cert-extra-sans=\"192.168.50.10\"  --node-name k8s-master --pod-network-cidr=192.168.0.0\/16<\/code><\/pre>\n<\/div>\n<h4 id=\"step-2-4-setup-the-kube-config-file-for-the-vagrant-user-to-access-the-kubernetes-cluster-using-the-below-code\">Step 2.4: Setup the kube config file for the vagrant user to access the Kubernetes cluster using the below code.<\/h4>\n<div class=\"highlight\">\n<pre><code class=\"language-yaml\" data-lang=\"yaml\">  - name: Setup kubeconfig for vagrant user\r\n    command: \"{{ item }}\"\r\n    with_items:\r\n     - mkdir -p \/home\/vagrant\/.kube\r\n     - cp -i \/etc\/kubernetes\/admin.conf \/home\/vagrant\/.kube\/config\r\n     - chown vagrant:vagrant \/home\/vagrant\/.kube\/config<\/code><\/pre>\n<\/div>\n<h4 id=\"step-2-5-setup-the-container-networking-provider-and-the-network-policy-engine-using-the-below-code\">Step 2.5: Setup the container networking provider and the network policy engine using the below code.<\/h4>\n<div class=\"highlight\">\n<pre><code class=\"language-yaml\" data-lang=\"yaml\">  - name: Install calico pod network\r\n    become: false\r\n    command: kubectl create -f https:\/\/docs.projectcalico.org\/v3.4\/getting-started\/kubernetes\/installation\/hosted\/calico.yaml<\/code><\/pre>\n<\/div>\n<h4 id=\"step-2-6-generate-kube-join-command-for-joining-the-node-to-the-kubernetes-cluster-and-store-the-command-in-the-file-named-join-command\">Step 2.6: Generate kube join command for joining the node to the Kubernetes cluster and store the command in the file named\u00a0<code>join-command<\/code>.<\/h4>\n<div class=\"highlight\">\n<pre><code class=\"language-yaml\" data-lang=\"yaml\">  - name: Generate join command\r\n    command: kubeadm token create --print-join-command\r\n    register: join_command\r\n\r\n  - name: Copy join command to local file\r\n    local_action: copy content=\"{{ join_command.stdout_lines[0] }}\" dest=\".\/join-command\"<\/code><\/pre>\n<\/div>\n<h4 id=\"step-2-7-setup-a-handler-for-checking-docker-daemon-using-the-below-code\">Step 2.7: Setup a handler for checking Docker daemon using the below code.<\/h4>\n<div class=\"highlight\">\n<pre><code class=\"language-yaml\" data-lang=\"yaml\">  handlers:\r\n    - name: docker status\r\n      service: name=docker state=started<\/code><\/pre>\n<\/div>\n<h4 id=\"step-3-create-the-ansible-playbook-for-kubernetes-node\">Step 3: Create the Ansible playbook for Kubernetes node.<\/h4>\n<p>Create a file named\u00a0<code>node-playbook.yml<\/code>\u00a0in the directory\u00a0<code>kubernetes-setup<\/code>.<\/p>\n<p>Add the code below into\u00a0<code>node-playbook.yml<\/code><\/p>\n<h4 id=\"step-3-1-start-adding-the-code-from-steps-2-1-till-2-3\">Step 3.1: Start adding the code from Steps 2.1 till 2.3.<\/h4>\n<h4 id=\"step-3-2-join-the-nodes-to-the-kubernetes-cluster-using-below-code\">Step 3.2: Join the nodes to the Kubernetes cluster using below code.<\/h4>\n<div class=\"highlight\">\n<pre><code class=\"language-yaml\" data-lang=\"yaml\">  - name: Copy the join command to server location\r\n    copy: src=join-command dest=\/tmp\/join-command.sh mode=0777\r\n\r\n  - name: Join the node to cluster\r\n    command: sh \/tmp\/join-command.sh<\/code><\/pre>\n<\/div>\n<h4 id=\"step-3-3-add-the-code-from-step-2-7-to-finish-this-playbook\">Step 3.3: Add the code from step 2.7 to finish this playbook.<\/h4>\n<h4 id=\"step-4-upon-completing-the-vagrantfile-and-playbooks-follow-the-below-steps\">Step 4: Upon completing the Vagrantfile and playbooks follow the below steps.<\/h4>\n<div class=\"highlight\">\n<pre><code class=\"language-shell\" data-lang=\"shell\">$ cd \/path\/to\/Vagrantfile\r\n$ vagrant up<\/code><\/pre>\n<\/div>\n<p>Upon completion of all the above steps, the Kubernetes cluster should be up and running. We can login to the master or worker nodes using Vagrant as follows:<\/p>\n<div class=\"highlight\">\n<pre><code class=\"language-shell\" data-lang=\"shell\">$ ## Accessing master\r\n$ vagrant ssh k8s-master\r\nvagrant@k8s-master:~$ kubectl get nodes\r\nNAME         STATUS   ROLES    AGE     VERSION\r\nk8s-master   Ready    master   18m     v1.13.3\r\nnode-1       Ready    &lt;none&gt;   12m     v1.13.3\r\nnode-2       Ready    &lt;none&gt;   6m22s   v1.13.3\r\n\r\n$ ## Accessing nodes\r\n$ vagrant ssh node-1\r\n$ vagrant ssh node-2<\/code><\/pre>\n<\/div>\n<p><a href=\"https:\/\/kubernetes.io\/blog\/2019\/03\/15\/kubernetes-setup-using-ansible-and-vagrant\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Objective This blog post describes the steps required to setup a multi node Kubernetes cluster for development purposes. This setup provides a production-like cluster that can be setup on your local machine. Why do we require multi node cluster setup? Multi node Kubernetes clusters offer a production-like environment which has various advantages. Even though Minikube &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw93\/index.php\/2019\/03\/16\/kubernetes-setup-using-ansible-and-vagrant\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Kubernetes Setup Using Ansible and Vagrant&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1511","post","type-post","status-publish","format-standard","hentry","category-kubernetes"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/1511","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/comments?post=1511"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/1511\/revisions"}],"predecessor-version":[{"id":1519,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/1511\/revisions\/1519"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/media?parent=1511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/categories?post=1511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/tags?post=1511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}