Skip to content

Instantly share code, notes, and snippets.

@brianonn
Created July 8, 2022 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianonn/53cc7c3c7886188fdb1a374b7d34ad51 to your computer and use it in GitHub Desktop.
Save brianonn/53cc7c3c7886188fdb1a374b7d34ad51 to your computer and use it in GitHub Desktop.
stand up a kubernetes cluster on a host - shell or yaml for ansible - from
#!/bin/bash
# originally from: https://github.com/kubernetes/kubernetes/issues/106464#issuecomment-1142563656
# edited by Brian Onn https://github.com/brianonn
# see also
# https://kubernetes.io/docs/setup/production-environment/container-runtimes/#containerd-systemd
# works on debian 10, maybe 11 ? maybe 9 ?
#
## TODO test all the above
##
swapoff --all
cat <<EOF | tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
cat <<EOF | tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system
wget https://github.com/containerd/containerd/releases/download/v1.6.4/containerd-1.6.4-linux-amd64.tar.gz
tar Cxzvf /usr/local containerd-1.6.4-linux-amd64.tar.gz
mkdir /etc/containerd/
containerd config default > /etc/containerd/config.toml
sed -i 's|SystemdCgroup = false|SystemdCgroup = true|' /etc/containerd/config.toml
wget https://github.com/opencontainers/runc/releases/download/v1.1.2/runc.amd64
install -m 755 runc.amd64 /usr/local/sbin/runc
wget https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-amd64-v1.1.1.tgz
mkdir --parents /opt/cni/bin
tar Cxzvf /opt/cni/bin cni-plugins-linux-amd64-v1.1.1.tgz
wget https://raw.githubusercontent.com/containerd/containerd/main/containerd.service --output-document=/etc/systemd/system/containerd.service
systemctl daemon-reload
systemctl enable --now containerd
wget https://github.com/flannel-io/flannel/releases/download/v0.18.0/flannel-v0.18.0-linux-amd64.tar.gz
mkdir /opt/bin
tar --directory=/opt/bin --extract --gzip --file=flannel-v0.18.0-linux-amd64.tar.gz flanneld
apt-get update && apt-get install -y apt-transport-https ca-certificates curl
curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | tee /etc/apt/sources.list.d/kubernetes.list
apt-get update
apt-get install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl
kubeadm init --pod-network-cidr=10.244.0.0/16
echo 'KUBECONFIG=/etc/kubernetes/admin.conf' >> /etc/environment
export KUBECONFIG=/etc/kubernetes/admin.conf
kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
kubectl get pods --all-namespaces
---
# from: https://github.com/kubernetes/kubernetes/issues/106464#issuecomment-1143691262
#
# edited by Brian Onn https://github.com/brianonn
#
#
- hosts: all
become: true
tasks:
- name: disable swap
shell: |
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
- name: Add modules conf for k8s
blockinfile:
path: "/etc/modules-load.d/k8s.conf"
block: |
overlay
br_netfilter
create: yes
- name: Add modules
community.general.modprobe:
name: "{{ item }}"
state: present
with_items:
- overlay
- br_netfilter
- name: Set sysctl file and reload
ansible.posix.sysctl:
name: "{{ item }}"
value: '1'
state: present
reload: yes
with_items:
- net.ipv4.ip_forward
- net.bridge.bridge-nf-call-iptables
- net.bridge.bridge-nf-call-ip6tables
- name: Download containerd package
get_url:
url: https://github.com/containerd/containerd/releases/download/v1.6.4/containerd-1.6.4-linux-amd64.tar.gz
dest: /home/vagrant/containerd-1.6.4-linux-amd64.tar.gz
mode: '0777'
- name: Extract containerd
ansible.builtin.unarchive:
src: /home/vagrant/containerd-1.6.4-linux-amd64.tar.gz
dest: /usr/local
remote_src: yes
- name: Create containerd config.toml
lineinfile:
line: ""
path: "/etc/containerd/config.toml"
create: yes
- name: Populate containerd config
shell: containerd config default | tee /etc/containerd/config.toml
- name: Set SystemdCgroup to true in containerd config
replace:
path: /etc/containerd/config.toml
regexp: "SystemdCgroup = false"
replace: "SystemdCgroup = true"
- name: Download runc package
get_url:
url: https://github.com/opencontainers/runc/releases/download/v1.1.2/runc.amd64
dest: /home/vagrant/runc.amd64
mode: '0777'
- name: Install runc
shell: install -m 755 /home/vagrant/runc.amd64 /usr/local/sbin/runc
- name: Download cni plugin
get_url:
url: https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-amd64-v1.1.1.tgz
dest: /home/vagrant/cni-plugins-linux-amd64-v1.1.1.tgz
mode: '0777'
- name: Create /opt/cni/bin dir
file:
path: /opt/cni/bin
state: directory
- name: Extract cni plugin
ansible.builtin.unarchive:
src: /home/vagrant/cni-plugins-linux-amd64-v1.1.1.tgz
dest: /opt/cni/bin
remote_src: yes
- name: Download containerd service
get_url:
url: https://raw.githubusercontent.com/containerd/containerd/main/containerd.service
dest: /etc/systemd/system/containerd.service
mode: '0777'
- name: Issue daemon-reload to pick up config changes, restart containerd service
ansible.builtin.systemd:
state: restarted
daemon_reload: yes
name: containerd
- name: Download flannel
get_url:
url: https://github.com/flannel-io/flannel/releases/download/v0.18.0/flannel-v0.18.0-linux-amd64.tar.gz
dest: /home/vagrant/flannel-v0.18.0-linux-amd64.tar.gz
mode: '0777'
- name: Create /opt/bin dir
file:
path: /opt/bin
state: directory
- name: Extract flannel
ansible.builtin.unarchive:
src: /home/vagrant/flannel-v0.18.0-linux-amd64.tar.gz
dest: /opt/bin
remote_src: yes
- name: Install packages that allow apt to be used over HTTPS
apt:
name: "{{ packages }}"
state: present
update_cache: yes
vars:
packages:
- apt-transport-https
- ca-certificates
- curl
- name: Add an apt signing key for Kubernetes
apt_key:
url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
state: present
- name: Adding apt repository for Kubernetes
apt_repository:
repo: deb https://apt.kubernetes.io/ kubernetes-xenial main
state: present
filename: kubernetes.list
- name: Install Kubernetes binaries
apt:
name: "{{ packages }}"
state: present
update_cache: yes
vars:
packages:
- kubelet
- kubeadm
- kubectl
- name: Initialize the cluster
shell: kubeadm init --pod-network-cidr=10.244.0.0/16
- name: Create /home/vagrant/.kube dir
file:
path: /home/vagrant/.kube
state: directory
- name: Copies admin.conf to user's kube config
copy:
src: /etc/kubernetes/admin.conf
dest: /home/vagrant/.kube/config
remote_src: yes
owner: vagrant
- name: Install flannel for k8s
become: false
command: kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment