Skip to content

Commit 60262cd

Browse files
author
Anurag Guda
committed
Kubernetes on AWS
0 parents  commit 60262cd

17 files changed

+1269
-0
lines changed

‎Readmd.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<h1> Kubernetes on AWS with Terraform </h1>
2+
3+
This repository helps to spin up AWS environment and create kubernetes cluster on top of that.
4+
5+
- Prerequisites
6+
- AWS account details
7+
- Ansible on your local machine
8+
- Terraform on your local machine
9+
10+
### Usage
11+
12+
Update the aws account details in terrform varaiable file, then run the below command to install kubernetes cluster on AWS
13+
14+
```
15+
bash k8scluster.sh
16+
```
17+
18+
To clean up the AWS environment with kubernetes, run the below command
19+
20+
```
21+
cd terrform
22+
terraform destroy -auto-approve
23+
```
24+

‎ansible/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<h1> Install multi-node kubernetes cluster with Ansible Playbooks </h1>
2+
3+
- Prerequisites
4+
- SSH trust setup from your local vm to remote hosts or use remote host private_key
5+
- ssh-keygen -b 2048 -t rsa -f /root/.ssh/id_rsa -q -N ""
6+
ssh anguda@$host | sudo -S mkdir /root/.ssh
7+
ssh anguda@$host | sudo -S touch /root/.ssh/authorized_keys
8+
ssh anguda@$host | sudo -S apt install git ansible vim sshpass openssh-server -y
9+
cat /root/.ssh/id_rsa.pub | sshpass -p k8s123 ssh root@$host "cat >> /root/.ssh/authorized_keys"
10+
11+
12+
13+
This directory helps you to install kubernetes cluster with ansible playbooks. Please make sure to pass inventory file for each playbooks
14+
15+
- inventory example
16+
17+
```
18+
[k8s-masters]
19+
54.219.223.243 ansible_ssh_host=54.219.223.243 ansible_ssh_port=22 ansible_ssh_user=ubuntu ansible_ssh_extra_args='-o StrictHostKeyChecking=no'
20+
21+
[k8s-workers]
22+
52.52.238.67 ansible_ssh_host=52.52.238.67 ansible_ssh_port=22 ansible_ssh_user=ubuntu ansible_ssh_extra_args='-o StrictHostKeyChecking=no'
23+
52.8.50.178 ansible_ssh_host=52.8.50.178 ansible_ssh_port=22 ansible_ssh_user=ubuntu ansible_ssh_extra_args='-o StrictHostKeyChecking=no'
24+
```
25+
26+
### Usage
27+
28+
First make sure to install the prerequisites.yaml to install all componenets
29+
30+
```
31+
ansible-playbook prerequisites.yaml -i inventory
32+
```
33+
34+
Then run k8s.yaml to install kubernetes cluster with kubeadm
35+
36+
```
37+
ansible-playbook k8s.yaml -i inventory
38+
```
39+

‎ansible/inventory

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
[k8s-masters]
3+
ec2-52-52-180-22.us-west-1.compute.amazonaws.com ansible_ssh_host=52.52.180.22 ansible_ssh_port=22 ansible_ssh_user=ubuntu ansible_ssh_private_key_file=/Users/aguda/Downloads/AWS/awstest.pem ansible_ssh_extra_args='-o StrictHostKeyChecking=no'
4+
5+
[k8s-workers]
6+
ec2-13-57-111-53.us-west-1.compute.amazonaws.com ansible_ssh_host=13.57.111.53 ansible_ssh_port=22 ansible_ssh_user=ubuntu ansible_ssh_private_key_file=/Users/aguda/Downloads/AWS/awstest.pem ansible_ssh_extra_args='-o StrictHostKeyChecking=no'
7+
ec2-13-57-45-138.us-west-1.compute.amazonaws.com ansible_ssh_host=13.57.45.138 ansible_ssh_port=22 ansible_ssh_user=ubuntu ansible_ssh_private_key_file=/Users/aguda/Downloads/AWS/awstest.pem ansible_ssh_extra_args='-o StrictHostKeyChecking=no'

‎ansible/k8s.yaml

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
- hosts: k8s-masters
2+
become: True
3+
tasks:
4+
5+
- name: Reset Kubernetes component
6+
shell: "kubeadm reset --force"
7+
register: reset_cluster
8+
9+
- name: remove etcd directory
10+
ignore_errors: yes
11+
shell: "{{ item }}"
12+
with_items:
13+
- rm -rf /var/lib/etcd
14+
- rm -rf $HOME/.kube
15+
16+
- name: Initialize the Kubernetes cluster using kubeadm
17+
command: kubeadm init --pod-network-cidr=10.244.0.0/16 --v 9
18+
register: kubeadm
19+
20+
- debug: msg={{ kubeadm.stdout_lines }}
21+
22+
- name: Create kube directory
23+
file:
24+
path: $HOME/.kube
25+
state: directory
26+
27+
- name: Copy kubeconfig to home
28+
shell: |
29+
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
30+
sudo chown $(id -u):$(id -g) $HOME/.kube/config
31+
32+
- name: Install networking plugin to kubernetes cluster
33+
command: "kubectl apply -f {{ item }}"
34+
with_items:
35+
- https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
36+
- https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
37+
- https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/influxdb/heapster.yaml
38+
- https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/influxdb/influxdb.yaml
39+
- https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/rbac/heapster-rbac.yaml
40+
41+
- name: Change permissions of the service account(kubernetes-dashboard) for the dashboard
42+
command: kubectl create clusterrolebinding kubernetes-dashboard -n kube-system --clusterrole=cluster-admin --serviceaccount=kube-system:kubernetes-dashboard
43+
44+
- name: Run KubeProxy
45+
shell: nohup kubectl proxy --address='0.0.0.0' --accept-hosts='^*$' </dev/null >/dev/null 2>&1 &
46+
47+
- name: Generate join token
48+
shell: kubeadm token create --print-join-command
49+
register: kubeadm_join_cmd
50+
51+
- set_fact:
52+
kubeadm_join: "{{ kubeadm_join_cmd.stdout }}"
53+
54+
- debug: var=kubeadm_join
55+
56+
- name: Store join command
57+
action: copy content="{{ kubeadm_join }}" dest="/etc/kubernetes/kubeadm-join.command"
58+
59+
- name: ansible copy file from remote to local.
60+
fetch:
61+
src: /etc/kubernetes/kubeadm-join.command
62+
dest: /tmp/kubeadm-join.command
63+
flat: yes
64+
65+
- hosts: k8s-workers
66+
become: true
67+
vars:
68+
kubeadm_join: "{{ lookup('file', '/tmp/kubeadm-join.command') }}"
69+
tasks:
70+
71+
- name: Copy Kubeadm join
72+
copy:
73+
src: /tmp/kubeadm-join.command
74+
dest: /tmp/kubeadm-join.command
75+
76+
- name: Reset Kubernetes component
77+
shell: "kubeadm reset --force"
78+
ignore_errors: yes
79+
80+
- name: remove kubernetes directory
81+
shell: "/bin/rm -rf /etc/kubernetes"
82+
ignore_errors: yes
83+
84+
- name: Run kubeadm join
85+
shell: "{{ kubeadm_join }} --ignore-preflight-errors=swap"
86+
87+
- hosts: k8s-masters
88+
become: true
89+
tasks:
90+
- name: Get Node name
91+
shell: "kubectl get nodes | grep -v master | awk '{print $1}' | grep -v NAME"
92+
register: node_name
93+
94+
- debug: var=node_name
95+
96+
- name: Lable the node
97+
shell: "kubectl label node {{ item }} node-role.kubernetes.io/node="
98+
with_items: "{{ node_name.stdout_lines }}"
99+
100+
- name: "Check if Helm is installed"
101+
shell: command -v helm >/dev/null 2>&1
102+
register: helm_exists
103+
ignore_errors: yes
104+
105+
- name: "Install Helm"
106+
command: "{{ item }}"
107+
args:
108+
warn: false
109+
with_items:
110+
- curl -O https://get.helm.sh/helm-v3.1.1-linux-amd64.tar.gz
111+
- tar -xvzf helm-v3.1.1-linux-amd64.tar.gz
112+
- cp linux-amd64/helm /usr/local/bin/
113+
- cp linux-amd64/helm /usr/bin/
114+
- rm -rf helm-v3.1.1-linux-amd64.tar.gz linux-amd64
115+
116+
when: helm_exists.rc > 0

‎ansible/prerequisites.yaml

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
- name: Define hosts
2+
hosts: all
3+
become: true
4+
tasks:
5+
- name: upgrade a server
6+
become: true
7+
become_user: root
8+
apt: update_cache=yes only_upgrade=yes
9+
ignore_errors: yes
10+
11+
- name: Add an Kubernetes apt signing key for Ubuntu
12+
when: "ansible_distribution == 'Ubuntu'"
13+
apt_key:
14+
url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
15+
state: present
16+
17+
- name: Adding Kubernetes apt repository for Ubuntu
18+
when: "ansible_distribution == 'Ubuntu'"
19+
apt_repository:
20+
repo: deb https://apt.kubernetes.io/ kubernetes-xenial main
21+
state: present
22+
filename: kubernetes
23+
24+
- name: install kubernetes components for Ubuntu
25+
when: "ansible_distribution == 'Ubuntu'"
26+
apt:
27+
name: ['apt-transport-https', 'curl', 'ca-certificates', 'gnupg-agent' ,'software-properties-common', 'kubelet=1.15.3-00', 'kubeadm=1.15.3-00', 'kubectl=1.15.3-00']
28+
state: present
29+
30+
- name: Validate whether Kubernetes cluster installed
31+
shell: kubectl cluster-info
32+
register: k8sup
33+
ignore_errors: yes
34+
35+
- name: Add Docker GPG key for Ubuntu
36+
when: "ansible_distribution == 'Ubuntu' and 'running' not in k8sup.stdout"
37+
apt_key: url=https://download.docker.com/linux/ubuntu/gpg
38+
39+
- name: Add Docker APT repository for Ubuntu
40+
when: "ansible_distribution == 'Ubuntu' and 'running' not in k8sup.stdout"
41+
apt_repository:
42+
repo: deb [arch=amd64] https://download.docker.com/linux/{{ansible_distribution|lower}} {{ansible_distribution_release}} stable
43+
44+
- name: Install Docker-CE Engine on Ubuntu
45+
when: " ansible_distribution == 'Ubuntu' and 'running' not in k8sup.stdout"
46+
apt:
47+
name: [ 'docker-ce=5:19.03.1~3-0~ubuntu-bionic' ]
48+
state: present
49+
update_cache: yes
50+
51+
- name: Creating a Kubernetes repository file for RHEL/CentOS
52+
when: "ansible_distribution in ['RedHat', 'CentOS']"
53+
file:
54+
path: /etc/yum.repos.d/kubernetes.repo
55+
state: touch
56+
57+
- name: Adding repository details in Kubernetes repo file for RHEL/CentOS
58+
when: "ansible_distribution in ['RedHat', 'CentOS']"
59+
blockinfile:
60+
path: /etc/yum.repos.d/kubernetes.repo
61+
block: |
62+
[kubernetes]
63+
name=Kubernetes
64+
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
65+
enabled=1
66+
gpgcheck=0
67+
repo_gpgcheck=0
68+
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
69+
https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
70+
71+
- name: Installing required packages for RHEL/CentOS
72+
when: "ansible_distribution in ['RedHat', 'CentOS']"
73+
yum:
74+
name: ['bind-utils', 'yum-utils', 'device-mapper-persistent-data', 'lvm2', 'telnet', 'kubelet-1.15.5', 'kubeadm-1.15.5', 'kubectl-1.15.5', 'firewalld', 'curl']
75+
state: present
76+
77+
78+
- name: "Configuring Docker-CE repo for RHEL/CentOS"
79+
when: "ansible_distribution in ['RedHat', 'CentOS'] and 'running' not in k8sup.stdout"
80+
get_url:
81+
url: https://download.docker.com/linux/centos/docker-ce.repo
82+
dest: /etc/yum.repos.d/docker-ce.repo
83+
mode: 0644
84+
85+
- name: Install Docker-CE Engine on RHEL/CentOS
86+
when: "ansible_distribution in ['RedHat', 'CentOS'] and 'running' not in k8sup.stdout"
87+
args:
88+
warn: false
89+
shell: yum install docker -y
90+
91+
- name: SetEnforce for RHEL/CentOS
92+
when: "ansible_distribution in ['RedHat', 'CentOS'] and 'running' not in k8sup.stdout"
93+
ignore_errors: yes
94+
command: "setenforce 0"
95+
96+
- name: SELinux for RHEL/CentOS
97+
when: "ansible_distribution in ['RedHat', 'CentOS'] and 'running' not in k8sup.stdout"
98+
args:
99+
warn: false
100+
command: sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
101+
102+
- name: Enable Firewall Service for RHEL/CentOS
103+
when: "ansible_distribution in ['RedHat', 'CentOS'] and 'running' not in k8sup.stdout"
104+
service:
105+
name: firewalld
106+
state: started
107+
enabled: yes
108+
ignore_errors: yes
109+
110+
- name: Allow Network Ports in Firewalld for RHEL/CentOS
111+
when: "ansible_distribution in ['RedHat', 'CentOS'] and 'running' not in k8sup.stdout"
112+
firewalld:
113+
port: "{{ item }}"
114+
state: enabled
115+
permanent: yes
116+
immediate: yes
117+
with_items:
118+
- "6443/tcp"
119+
- "10250/tcp"
120+
121+
122+
- name: Remove swapfile from /etc/fstab
123+
when: "'running' not in k8sup.stdout"
124+
mount:
125+
name: "{{ item }}"
126+
fstype: swap
127+
state: absent
128+
with_items:
129+
- swap
130+
- none
131+
132+
- name: Disable swap
133+
when: "'running' not in k8sup.stdout"
134+
command: swapoff -a
135+
136+
- name: Starting and enabling the required services
137+
when: "'running' not in k8sup.stdout"
138+
service:
139+
name: "{{ item }}"
140+
state: started
141+
enabled: yes
142+
ignore_errors: yes
143+
with_items:
144+
- docker
145+
- kubelet
146+

‎k8scluster.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
cd terraform
3+
terraform init
4+
terraform plan
5+
terraform apply -auto-approve
6+
terraform output inventory > ../ansible/inventory
7+
8+
echo "Please wait for a while to bring aws instances up"
9+
10+
sleep 60
11+
cd ../ansible
12+
ansible -m ping -i inventory all
13+
ansible-playbook -i inventory prerequisites.yaml
14+
ansible-playbook -i inventory k8s.yaml
15+

‎terraform/Readmd.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<h1> Kubernetes on AWS with Terraform </h1>
2+
3+
This repository helps to spin up AWS environment and create kubernetes cluster on top of that.
4+
5+
- Prerequisites
6+
- AWS account details
7+
- Ansible on your local machine
8+
- Terraform on your local machine
9+
10+
### Usage
11+
12+
Update the aws account details in terrform varaiable file, then run the below command to install kubernetes cluster on AWS
13+
14+
```
15+
bash k8scluster.sh
16+
```
17+
18+
To clean up the AWS environment with kubernetes, run the below command
19+
20+
```
21+
cd terrform
22+
terraform destroy -auto-approve
23+
```
24+

0 commit comments

Comments
 (0)