188 lines
5.2 KiB
Markdown
188 lines
5.2 KiB
Markdown
# Deploy Generic Kubernetes
|
|
The instructions outlined below assume you are deploying the environment using Ansible Playbooks either via Ansible's CLI or AWX.
|
|
|
|
### Deploy K8S User
|
|
```jsx title="01-deploy-k8s-user.yml"
|
|
- hosts: 'controller-nodes, worker-nodes'
|
|
become: yes
|
|
|
|
tasks:
|
|
- name: create the k8sadmin user account
|
|
user: name=k8sadmin append=yes state=present createhome=yes shell=/bin/bash
|
|
|
|
- name: allow 'k8sadmin' to use sudo without needing a password
|
|
lineinfile:
|
|
dest: /etc/sudoers
|
|
line: 'k8sadmin ALL=(ALL) NOPASSWD: ALL'
|
|
validate: 'visudo -cf %s'
|
|
|
|
- name: set up authorized keys for the k8sadmin user
|
|
authorized_key: user=k8sadmin key="{{item}}"
|
|
with_file:
|
|
- ~/.ssh/id_rsa.pub
|
|
```
|
|
|
|
### Install K8S
|
|
```jsx title="02-install-k8s.yml"
|
|
---
|
|
- hosts: "controller-nodes, worker-nodes"
|
|
remote_user: nicole
|
|
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.20.1-00 kubeadm=1.20.1-00 kubectl=1.20.1-00
|
|
sudo apt-mark hold kubelet kubeadm kubectl
|
|
```
|
|
|
|
### Configure ControlPlanes
|
|
```jsx title="03-configure-controllers.yml"
|
|
- hosts: controller-nodes
|
|
become: yes
|
|
|
|
tasks:
|
|
- name: Initialize the K8S Cluster
|
|
shell: kubeadm init --pod-network-cidr=10.244.0.0/16
|
|
args:
|
|
chdir: $HOME
|
|
creates: cluster_initialized.txt
|
|
|
|
- name: Create .kube directory
|
|
become: yes
|
|
become_user: k8sadmin
|
|
file:
|
|
path: /home/k8sadmin/.kube
|
|
state: directory
|
|
mode: 0755
|
|
|
|
- name: Copy admin.conf to user's kube config
|
|
copy:
|
|
src: /etc/kubernetes/admin.conf
|
|
dest: /home/k8sadmin/.kube/config
|
|
remote_src: yes
|
|
owner: k8sadmin
|
|
|
|
- name: Install the Pod Network
|
|
become: yes
|
|
become_user: k8sadmin
|
|
shell: 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: k8sadmin
|
|
shell: kubeadm token create --print-join-command
|
|
register: kubernetes_join_command
|
|
|
|
- name: Output Join Command to the Screen
|
|
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
|
|
```
|
|
|
|
### Join Worker Node(s)
|
|
```jsx title="04-join-worker-nodes.yml"
|
|
- hosts: worker-nodes
|
|
become: yes
|
|
gather_facts: yes
|
|
|
|
tasks:
|
|
- name: Copy join command from Ansible host 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
|
|
```
|
|
|
|
### Host Inventory File Template
|
|
```jsx title="hosts"
|
|
[controller-nodes]
|
|
k8s-ctrlr-01 ansible_host=192.168.3.6 ansible_user=nicole
|
|
|
|
[worker-nodes]
|
|
k8s-node-01 ansible_host=192.168.3.4 ansible_user=nicole
|
|
k8s-node-02 ansible_host=192.168.3.5 ansible_user=nicole
|
|
|
|
[all:vars]
|
|
ansible_become_user=root
|
|
ansible_become_method=sudo
|
|
```
|