Skip to content

Commit

Permalink
Adding kubernetes setup files
Browse files Browse the repository at this point in the history
  • Loading branch information
GowthamChowta committed Mar 29, 2022
1 parent 4bae568 commit 58a84d8
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 7 deletions.
78 changes: 78 additions & 0 deletions install-k8s.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
- hosts: 'terraclu-k8s-node-1, terraclu-k8s-master-1 '
remote_user: ubuntu
become: yes
become_method: sudo
become_user: root
gather_facts: yes
connection: ssh

tasks:
- name: Create containerd config file
file:
path: "/etc/modules-load.d/containerd.conf"
state: "touch"

- name: Add conf for containerd
blockinfile:
path: "/etc/modules-load.d/containerd.conf"
block: |
overlay
br_netfilter
- name: modprobe
shell: |
sudo modprobe overlay
sudo modprobe br_netfilter
- name: Set system configurations for Kubernetes networking
file:
path: "/etc/sysctl.d/99-kubernetes-cri.conf"
state: "touch"

- name: Add conf for containerd
blockinfile:
path: "/etc/sysctl.d/99-kubernetes-cri.conf"
block: |
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
- name: Apply new settings
command: sudo sysctl --system

- name: install containerd
shell: |
sudo apt-get update && sudo apt-get install -y containerd
sudo mkdir -p /etc/containerd
sudo containerd config default | sudo tee /etc/containerd/config.toml
sudo systemctl restart containerd
- name: disable swap
shell: |
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
- name: install and configure dependencies
shell: |
sudo apt-get update && sudo apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
- name: Create kubernetes repo file
file:
path: "/etc/apt/sources.list.d/kubernetes.list"
state: "touch"

- name: Add K8s Source
blockinfile:
path: "/etc/apt/sources.list.d/kubernetes.list"
block: |
deb https://apt.kubernetes.io/ kubernetes-xenial main
- name: install kubernetes
shell: |
sudo apt-get update
sudo apt-get install -y kubelet=1.22.0-00 kubeadm=1.22.0-00 kubectl=1.22.0-00
sudo apt-mark hold kubelet kubeadm kubectl
17 changes: 10 additions & 7 deletions instance-creation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ sudo chmod +x terraform &&
sudo cp terraform /usr/local/bin/ &&
sudo mv terraform /usr/bin/ &&
rm -rf terraform_0.14.4_linux_amd64.zip &&
# Change the directory
cd jetstream_kubespray &&
# Export cluser name
export CLUSTER=terraclu &&
cp -r inventory/kubejetstream inventory/$CLUSTER &&
cd inventory/$CLUSTER &&
# Update the IP address you have provided
sed -i '/k8s_master_fips/c\k8s_master_fips =["'$IP'"]' cluster.tfvars &&

export CLUSTER=terraclu
cp -r inventory/kubejetstream inventory/$CLUSTER
cd inventory/$CLUSTER
sed -i '/k8s_master_fips/c\k8s_master_fips "=['$IP']"' cluster.tfvars

bash terraform_init.sh
bash terraform_apply.sh
bash terraform_init.sh &&
bash terraform_apply.sh &&

48 changes: 48 additions & 0 deletions master.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
- hosts: 'terraclu-k8s-master-1 '
become: yes
tasks:
- name: change docker driver
shell: |
cat <<EOF | sudo tee /etc/docker/daemon.json
{"exec-opts": ["native.cgroupdriver=systemd"],"log-driver": "json-file","log-opts": {"max-size": "100m"},"storage-driver": "overlay2"}
EOF
- name: update and reset the changes
shell: |
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo kubeadm reset -f
- name: initialize the cluster
shell: sudo kubeadm init --pod-network-cidr=192.168.0.0/16 --kubernetes-version 1.22.0
args:
chdir: $HOME
creates: cluster_initialized.txt

- name: for accessing kubectl
shell: |
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
- name: install Pod network
become: yes
become_user: kube
shell: sudo kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
args:
chdir: $HOME

- name: Get the token for joining the worker nodes
become: yes
become_user: kube
shell: sudo kubeadm token create --print-join-command
register: kubernetes_join_command

- debug:
msg: "{{ kubernetes_join_command.stdout }}"

- name: Copy join command to local file.
become: yes
local_action: copy content="{{ kubernetes_join_command.stdout_lines[0] }}" dest="/tmp/kubernetes_join_command" mode=0777
17 changes: 17 additions & 0 deletions users.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
- hosts: 'terraclu-k8s-node-1, terraclu-k8s-master-1 '
become: yes

tasks:
- name: create the kube user account
user: name=kube append=yes state=present createhome=yes shell=/bin/bash

- name: allow 'kube' to use sudo without needing a password
lineinfile:
dest: /etc/sudoers
line: 'kube ALL=(ALL) NOPASSWD: ALL'
validate: 'visudo -cf %s'

- name: set up authorized keys for the kube user
authorized_key: user=kube key="{{item}}"
with_file:
- ~/.ssh/id_rsa.pub
27 changes: 27 additions & 0 deletions workers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
- hosts: 'terraclu-k8s-node-1 '
become: yes
gather_facts: yes

tasks:
- name: change docker driver
shell: |
cat <<EOF | sudo tee /etc/docker/daemon.json
{"exec-opts": ["native.cgroupdriver=systemd"],"log-driver": "json-file","log-opts": {"max-size": "100m"},"storage-driver": "overlay2"} EOF
- name: update and reset the changes
shell: |
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo kubeadm reset -f
- name: Copy join command from Ansiblehost to the worker nodes.
become: yes
copy:
src: /tmp/kubernetes_join_command
dest: /tmp/kubernetes_join_command
mode: 0777

- name: Join the Worker nodes to the cluster.
become: yes
command: sh /tmp/kubernetes_join_command
register: joined_or_not

0 comments on commit 58a84d8

Please sign in to comment.