Difference between revisions of "Installing Kubernetes with Ansible on AWS"

From PKC
Jump to navigation Jump to search
 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This article explains how to install Kubernetes using Ansible on AWS's Ubuntu 20.04 computing clusters.
This article explains how to install Kubernetes using Ansible on AWS's Ubuntu 20.04 computing clusters.  
 
A relevant article can be found here: [https://levelup.gitconnected.com/how-to-create-multi-node-kubernetes-cluster-using-ansible-roles-automation-inside-ec2-instance-c539a1b1f9a1 Create Multi-Node Kubernetes using Ansible inside AWS EC2 Instance].
 
For installing Kuberenetes onto [[Vagrant]] configurations, please refer to this page: [https://kubernetes.io/blog/2019/03/15/kubernetes-setup-using-ansible-and-vagrant/ Kubernetes setup using Ansible and Vagrant].
 
__TOC__
__TOC__
=Set up at least two machines on AWS=
=Set up at least two machines on AWS=
The first task is to create two EC2 instances that matches [[Kubernetes Cluster minimal configuration]]:
The first task is to create two EC2 instances that matches [[Kubernetes Cluster minimal configuration]]:
Line 19: Line 23:
   pip3 install ansible
   pip3 install ansible


==Set up Ansible Hosts and Inventory file==
==Set up Ansible Inventory file==
It is necessary to set up the <code>hosts</code> and <code>inventory</code> files for [[Ansible]].
It is necessary to set up the <code>inventory</code> files for [[Ansible]].
See the following examples:
See the following examples:


For <code>hosts</code> file:
<syntaxhighlight lang="CSS">
m1.xlp.pub
w1.xlp.pub
</syntaxhighlight>


For <code>inventory</code> file, one should provide a list of machines, and a list of variables (<code>vars</code>) that applies to <code>all</code> these machines. In our case, we want to access all these machines using the same <code>ansible_user</code>: <code>ubuntu</code>.
For <code>inventory</code> file, one should provide a list of machines, and a list of variables (<code>vars</code>) that applies to <code>all</code> these machines. In our case, we want to access all these machines using the same <code>ansible_user</code>: <code>ubuntu</code>.
Line 35: Line 34:
[all:vars]
[all:vars]
ansible_user=ubuntu
ansible_user=ubuntu
</syntaxhighlight>
==Set up Ansible Configuration file==
One must also create the <code>ansible.cfg</code> files in the current working directory.
See the following examples:
<syntaxhighlight lang="CSS">
[defaults]
inventory = inventory
private_key_file = ~/.ssh/ansible.pem
</syntaxhighlight>
</syntaxhighlight>


==Test to see if Ansible can access the machines==
==Test to see if Ansible can access the machines==
One must supply the following data points to enable Ansible to access the machines:
In the following instruction, <code>all</code> indicates running the instruction set on all machines, where <code>-m ping</code>, indicates that this instruction will run the module, <code>ping</code> on all the machines.
#<code>all</code> for all the machines listed in the <code>inventory</code> file in the current working directory.
ansible all -m ping
#<code>-i</code> for input file, in this case, the <code>inventory</code> file in the current working directory.
For a more elaborate instruction, see [[Ansible/First Instruction]].
#<code>--key-file</code> for the secret key file, in this case, the <code>~/.ssh/ansible.pem</code> is the one that is shared with all the nodes.
Other useful instructions to test if [[Ansible]] is running properly: [[Ansible/Useful Instructions]].
#<code>-u</code> for user name, in this case, <code>ubuntu</code> is the user associated with the key file.
 
#<code>-m</code> for module name, in this case, <code>ping</code> is the module to be executed.
==Make changes to machines using Ansible==
  ansible all -i inventory --key-file ~/.ssh/ansible.pem -u ubuntu -m ping
If you have certain password that you can key-in during an interactive session, you may use the following instruction. Note that if the user already is in the administrator group, you can ignore the <code>--ask-become-pass</code> part of the statement. By supplying the <code>--ask-become-pass</code> option, you will be asked to enter a password.
  ansible all -m apt -a update_cache=true --become --ask-become-pass

Latest revision as of 16:56, 3 August 2021

This article explains how to install Kubernetes using Ansible on AWS's Ubuntu 20.04 computing clusters.

A relevant article can be found here: Create Multi-Node Kubernetes using Ansible inside AWS EC2 Instance.

For installing Kuberenetes onto Vagrant configurations, please refer to this page: Kubernetes setup using Ansible and Vagrant.

Set up at least two machines on AWS

The first task is to create two EC2 instances that matches Kubernetes Cluster minimal configuration:

The master node must have at least 2 CPU cores, 2 Gb of memory.
The worker node (slave) should have at least 1 CPU core and 2 Gb of memory.

Set the right Security Groups

{{#lst:Input/K8s Installation/Security Groups|Security Groups}}

Ideally, one could first create a Security Group, and name it K8s Sec Grp, then, just create two or more instances of EC2, and select the said security group during the creation cycle.

Use the same Signature File

When creating these instances, make sure that you create or select the same Identity file (.pem file) for all instances of the same cluster. This will significantly reduce the complexity of installation.

Make sure Ansible is installed

Install Ansible on the machine that will run Ansible to install the cluster. You may follow this procedure: Installing Ansible. If you already installed Python 3.0 and pip3, launch the following instruction in your terminal should work:

 pip3 install ansible

Set up Ansible Inventory file

It is necessary to set up the inventory files for Ansible. See the following examples:


For inventory file, one should provide a list of machines, and a list of variables (vars) that applies to all these machines. In our case, we want to access all these machines using the same ansible_user: ubuntu.

m1.xlp.pub
w1.xlp.pub
[all:vars]
ansible_user=ubuntu

Set up Ansible Configuration file

One must also create the ansible.cfg files in the current working directory. See the following examples:

[defaults]
inventory = inventory
private_key_file = ~/.ssh/ansible.pem

Test to see if Ansible can access the machines

In the following instruction, all indicates running the instruction set on all machines, where -m ping, indicates that this instruction will run the module, ping on all the machines.

ansible all -m ping

For a more elaborate instruction, see Ansible/First Instruction. Other useful instructions to test if Ansible is running properly: Ansible/Useful Instructions.

Make changes to machines using Ansible

If you have certain password that you can key-in during an interactive session, you may use the following instruction. Note that if the user already is in the administrator group, you can ignore the --ask-become-pass part of the statement. By supplying the --ask-become-pass option, you will be asked to enter a password.

ansible all -m apt -a update_cache=true --become --ask-become-pass