Ensure overcommit_memory=0
[iec.git] / src / foundation / scripts / k8s_common.sh
1 #!/bin/bash -ex
2
3 if grep -q -e rhel /etc/*-release; then
4   OS_ID_LIKE=rhel
5 elif grep -q -e debian /etc/*-release; then
6   OS_ID_LIKE=debian
7 fi
8
9 case ${OS_ID_LIKE:-} in
10 debian)
11   DOCKER_VERSION=18.06.1~ce~3-0~ubuntu
12   KUBE_VERSION=${1:-1.13.0}-00
13   K8S_CNI_VERSION=${2:-0.6.0}-00
14   KUBELET_CFG=/etc/default/kubelet
15   ;;
16 rhel)
17   DOCKER_VERSION=18.06.1.ce-3.el7
18   KUBE_VERSION=${1:-1.13.0}-0
19   K8S_CNI_VERSION=${2:-0.6.0}-0
20   KUBELET_CFG=/etc/sysconfig/kubelet
21   ;;
22 *)
23   echo 'Unsupported distribution detected!'
24   exit 1
25   ;;
26 esac
27
28
29 case ${OS_ID_LIKE:-} in
30 debian)
31   # Install basic software
32   echo "Acquire::ForceIPv4 \"true\";" | sudo tee -a /etc/apt/apt.conf.d/99force-ipv4 > /dev/null
33   sudo apt update
34   sudo apt install -y software-properties-common apt-transport-https curl
35
36   # Install Docker as Prerequisite
37   curl -4fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
38   sudo apt-key fingerprint 0EBFCD88
39   sudo add-apt-repository \
40     "deb https://download.docker.com/linux/ubuntu \
41     $(lsb_release -cs) \
42     stable"
43   sudo apt update
44   sudo apt install -y docker-ce=${DOCKER_VERSION}
45   ;;
46 rhel)
47   sudo yum install -y yum-utils device-mapper-persistent-data lvm2
48   sudo yum-config-manager --add-repo \
49     https://download.docker.com/linux/centos/docker-ce.repo
50   sudo yum install -y \
51     docker-ce-$DOCKER_VERSION \
52     docker-ce-cli-$DOCKER_VERSION \
53     containerd.io
54   ;;
55 esac
56
57 # Disable swap on your machine
58 sudo swapoff -a
59
60 case ${OS_ID_LIKE:-} in
61 debian)
62   # Install Kubernetes with Kubeadm
63   curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
64
65   cat <<-EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
66 deb https://apt.kubernetes.io/ kubernetes-xenial main
67 EOF
68   sudo apt update
69   # Minor fix for broken kubernetes-cni dependency in upstream xenial repo
70   sudo apt install -y \
71     kubernetes-cni=${K8S_CNI_VERSION} kubelet=${KUBE_VERSION} \
72     kubeadm=${KUBE_VERSION} kubectl=${KUBE_VERSION}
73   sudo apt-mark hold kubernetes-cni kubelet kubeadm kubectl
74   ;;
75 rhel)
76   cat <<-EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
77 [kubernetes]
78 name=Kubernetes
79 baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-$(uname -m)
80 enabled=1
81 gpgcheck=1
82 repo_gpgcheck=1
83 gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
84        https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
85 EOF
86   sudo yum install -y kubelet-$KUBE_VERSION kubeadm-$KUBE_VERSION \
87                       kubectl-$KUBE_VERSION kubernetes-cni-$K8S_CNI_VERSION
88   ;;
89 esac
90
91 # Add extra flags to Kubelet
92 if [ ! -f "$KUBELET_CFG" ]; then
93   echo 'KUBELET_EXTRA_ARGS=--fail-swap-on=false' | sudo tee $KUBELET_CFG > /dev/null
94 elif ! grep -q -e 'fail-swap-on' $KUBELET_CFG; then
95   sudo sed 's/KUBELET_EXTRA_ARGS=/KUBELET_EXTRA_ARGS=--fail-swap-on=false/' -i $KUBELET_CFG
96 fi
97
98 sudo systemctl enable docker kubelet
99 sudo systemctl restart docker kubelet
100
101 sudo modprobe br_netfilter
102 _conf='/etc/sysctl.d/99-akraino-iec.conf'
103 echo 'net.bridge.bridge-nf-call-iptables = 1' |& sudo tee "${_conf}"
104 # Set memory overcommit to 0 for extra checks during memory allocation
105 echo 'vm.overcommit_memory = 0' |& sudo tee -a "${_conf}"
106 sudo sysctl -q -p "${_conf}"