Kubernetes > Sample tasks > Launch a cluster using CLI on Google Cloud
OS on all the instances: Ubuntu 1904
Task: Create a Kubernetes cluster using VMs, not GKE
We would execute commands from a controller instance, this can be your own machine or a VM on your machine or on Google cloud or some other cloud, however I have this on the same project in GCP, where k8s instances would be launched too
If you prefer to launch the controller instance on gcloud, the procedure is as follows
Click on at the top right corner of the Google cloud dashboard
shakir@cloudshell:~ gcloud config set compute/region us-central1 |
Install gcloud on the controller instance
shakir@controller:~ cat > install-gcloud.sh <<EOF EOF shakir@controller:~ chmod +x install-gcloud.sh shakir@controller:~ ./install-gcloud.sh |
To login to google cloud, and to set region and zone
shakir@controller:~ gcloud auth login |
To create a kubernetes master instance and 3 node instances, all with ubuntu 1904
shakir@controller:~ gcloud compute instances create k8s-master --image-family ubuntu-1904 --image-project ubuntu-os-cloud |
Install Docker on Master and Nodes
shakir@controller:~ nano install-docker.sh sudo su groupadd docker usermod -aG docker $USER # Install Docker CE ## Set up the repository: ### Install packages to allow apt to use a repository over HTTPS apt-get update && apt-get install apt-transport-https ca-certificates curl software-properties-common -y ### Add Docker’s official GPG key curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - ### Add Docker apt repository. add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable" ## Install Docker CE. apt-get update && apt-get install docker-ce=18.06.2~ce~3-0~ubuntu -y # Setup daemon. cat > /etc/docker/daemon.json <<EOF { "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" }, "storage-driver": "overlay2" } EOF mkdir -p /etc/systemd/system/docker.service.d # Restart docker. systemctl daemon-reload systemctl restart docker shakir@controller:~ cat install-docker.sh | gcloud compute ssh k8s-master
shakir@controller:~ cat install-docker.sh | gcloud compute ssh k8s-node1 shakir@controller:~ cat install-docker.sh | gcloud compute ssh k8s-node2 shakir@controller:~ cat install-docker.sh | gcloud compute ssh k8s-node3 |
Add the user to docker group, so as to execute docker commands without sudo
shakir@controller:~ echo "sudo usermod -aG docker $USER" | gcloud compute ssh k8s-node1 |
Install kubeadm, kubectl on master
shakir@controller:~ cat install-kubeadm-ctl.sh shakir@controller:~ cat install-kubeadm-ctl.sh | gcloud compute ssh k8s-master |
Install kubeadm and kubelet on nodes
shakir@controller:~ cat install-kubeadm-let.sh shakir@controller:~ cat install-kubeadm-let.sh | gcloud compute ssh k8s-node1 shakir@controller:~ cat install-kubeadm-let.sh | gcloud compute ssh k8s-node2 shakir@controller:~ cat install-kubeadm-let.sh | gcloud compute ssh k8s-node3 |
We need at least 2 CPUs on master, let’s change it
shakir@controller:~ gcloud compute instances k8s-master stop |
Initialize the control plane a.k.a master
shakir@controller:~ echo "sudo kubeadm init --pod-network-cidr=192.168.0.0/16" | gcloud compute ssh k8s-master |
Note down the final two lines of the output, that has the token information
kubeadm join 10.128.0.4:6443 --token wznnx1.zkjl7tjgvc7tj5xw \ |
Setup kubeconfig on the master
shakir@controller:~ cat setup-kubeconfig.sh shakir@controller:~ cat setup-kubeconfig.sh | gcloud compute ssh k8s-master |
Setup Pod networking, here we have chosen Calico
shakir@controller:~ cat setup-pod-networking.sh shakir@controller:~ cat setup-pod-networking.sh | gcloud compute ssh k8s-master |
Setup the cluster, this step refers to the token copied few steps back
shakir@controller:~ cat join-nodes-to-cluster.sh shakir@controller:~ cat join-nodes-to-cluster.sh | gcloud compute ssh k8s-node1 |
SSH into the master and start working
shakir@controller:~ gcloud compute ssh k8s-master |
--end-of-post--