Install kubectl and rke in server k8s-manager

Step 1: Install kubectl

(Original link: https://kubernetes.io/docs/tasks/tools/install-kubectl/#install-kubectl)

curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl

Step2: Install rke

(Original link: https://rancher.com/docs/rke/latest/en/installation/)

wget https://github.com/rancher/rke/releases/download/v1.4.0/rke_linux-amd64    
mv rke_linux-amd64 /usr/local/bin/rke
chmod +x /usr/local/bin/rke

Step 3: Create the RSA Key Pair on k8s-manager

ssh-keygen -t rsa

Step 4: Add public key of k8s-manager to all k8s node ( all vm)

ssh-copy-id root@VM1
ssh-copy-id root@VM2

Step 5: Create file setup docker by scripts (script.bash)

cat rancher-cluster.yml | grep internal_address | awk '{print $2}' |
    while read IP
do
    ssh - n - oStrictHostKeyChecking = no root @$IP 'docker --version'
if [$ ? -ne 0];
then
echo "Docker is not installed on $IP"
echo "Start install Docker"
ssh - n - oStrictHostKeyChecking = no root @$IP 'apt update; apt install -y apt-transport-https ca-certificates curl software-properties-common; curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -; add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"; apt update; apt install -y docker-ce=5:19.03.15~3-0~ubuntu-bionic docker-ce-cli=5:19.03.15~3-0~ubuntu-bionic containerd.io'
printf "\n"
else
    echo "Docker has been installed on $IP"
printf "\n"
fi
done

Step 6: Create file rancher-cluster.yml

cluster_name: k8s-staging
nodes:
    - address: IP master #k8s - master01.staging
      internal_address: IP master #k8s - master01.staging
      user: root
role: [controlplane, etcd]
    - address: IP Worker 1 #k8s - worker01.staging
      internal_address: IP Worker 1 #k8s - worker01.staging
      user: root
      role: [worker]
      labels:
          app: app - common
    - address: IP worker 2 #k8s - worker02.staging
      internal_address: IP worker 2 #k8s - worker02.staging
      user: root
      role: [worker]
      labels:
          app: app-common
network:
    plugin: canal
services:
    etcd:
      snapshot: true
      creation: 6h
      retention: 24h
    kubelet:
      extra_binds:
        "/lib/modules:/lib/modules"
    ingress:
      provider: nginx
    options:
      use-forwarded-headers: "true"

Step 7: Run scripts

bash ./script.bash

Step 8: Install k8s cluster. Run command bellow on k8s-manager

rke up --config ./rancher-cluster.yml
export KUBECONFIG=/k8s/kube_config_rancher-cluster.yml
### Note:
## Decode secret
kubectl get secret $SECRET_NAME -n $NAMESPACE -o go-template='{{range $k,$v := .data}}{{printf "%s: " $k}}{{if not $v}}{{$v}}{{else}}{{$v | base64decode}}{{end}}{{"\n"}}{{end}}'