You will need SSH keys to connect to VM on GCP. This key will be passed to createvm
script which will include your public key during the machine creation.
ssh-keygen -t rsa -f <key-path> -C <your-username>
E.g:
ssh-keygen -t rsa -f ~/.ssh/vmkey -C devops
# 2. Configure the startup script
You can configure a script to perform tasks during the startup, such as updates or programs installation.
Inside the folder scripts you can reach the startup_script.sh. Wich is configured to install git and docker on VM.
scripts/startup.sh:
#! /bin/bash
sudo su
apt-get install git -y
curl https://releases.rancher.com/install-docker/20.10.sh | sh
usermod -aG docker ubuntu
The createvm is a wrapper for the gcloud command. This will automatically update a default VM model with new IP and name and will pass the startup script and the SSH key.
Inside the script, you must replace PROJECT and ZONE variables with yours.
$ cd scripts
$ ./createvm.sh --ip=34.95.134.36 --name=k8s0 --key=/home/maike/.ssh/vmkey.pub --script=./startup.sh --username=devops
$ ./createvm.sh --ip=35.198.57.145 --name=k8s1 --key=/home/maike/.ssh/vmkey.pub --script=./startup.sh --username=devops
$ ./createvm.sh --ip=35.247.212.89 --name=k8s2 --key=/home/maike/.ssh/vmkey.pub --script=./startup.sh --username=devops
$ ./createvm.sh --ip=34.151.241.74 --name=rancher-server --key=/home/maike/.ssh/vmkey.pub --script=./startup.sh --username=devops
$ ssh -i ~/.ssh/vmkey devops@34.95.134.36
$ exit
$ ssh -i ~/.ssh/vmkey devops@35.198.57.145
$ exit
$ ssh -i ~/.ssh/vmkey devops@35.247.212.89
$ exit
$ ssh -i ~/.ssh/vmkey devops@34.151.241.74
$ exit
Login on rancher VM:
$ ssh devops@34.151.241.74 'sudo mkdir /opt/rancher'
Install the rancher container
$ ssh devops@34.151.241.74 'sudo docker run -d -v /opt/rancher:/var/lib/rancher --restart=unless-stopped -p 80:80 -p 443:443 --privileged rancher/rancher:v2.4.3'
Access the GCP console, under VM instances edit the rancher-server VM and mark the option "Accept HTTP"
Wait until the rancher is up to work and access the rancher-server IP(34.151.241.74) in your browser.
If the rancher version is v2.6.2+:
Follow the instructions and login on rancher.
docker logs `docker ps --format {{.ID}}` 2>&1 | grep "Bootstrap Password:"
Run the script install_kubectl on the rancher-server
$ cd scripts
$ ssh devops@34.151.241.74 'bash -s' < install_kubectl.sh
Under rancher go to Cluster -> Click on "Kubeconfig File" -> Copy the content to clipboard
Paste the content on the file "~/.kube/config" under the rancher-server machine
$ sudo nano ~/.kube/config
Test if is working
$ kubectl get nodes
Login on rancher go to Cluster Managment -> Create (button)-> Custom -> Next -> Mark etcd and Control Plane (checkbox)
Copy the generated code and paste in a file called node_command.sh and execute for each node IP:
$ cd scripts
$ ssh devops@34.95.134.36 'bash -s' < node_command.sh #-k8s0
$ ssh devops@35.198.57.145 'bash -s' < node_command.sh #-k8s1
s ssh devops@35.247.212.89 'bash -s' < node_command.sh #-k8s2
Wait until the cluster is up to work. You can see the cluster status on rancher web page -> Cluster Management