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
shakir@cloudshell:~ gcloud config set compute/zone us-central1-a
shakir@cloudshell:~ gcloud compute instances create controller --image-family ubuntu-1904 --image-project ubuntu-os-cloud

Install gcloud on the controller instance

shakir@controller:~ cat > install-gcloud.sh <<EOF
# Add the Cloud SDK distribution URI as a package source
echo "deb http://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
# Import the Google Cloud Platform public key
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
# Update the package list and install the Cloud SDK
sudo apt-get update && sudo apt-get install google-cloud-sdk -y

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
shakir@controller:~ gcloud config set compute/region us-central1
shakir@controller:~ gcloud config set compute/zone us-central1-a

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
shakir@controller:~ gcloud compute instances create k8s-node1 --image-family ubuntu-1904 --image-project ubuntu-os-cloud
shakir@controller:~ gcloud compute instances create k8s-node2 --image-family ubuntu-1904 --image-project ubuntu-os-cloud
shakir@controller:~ gcloud compute instances create k8s-node3 --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
shakir@controller:~ echo "sudo usermod -aG docker $USER" | gcloud compute ssh k8s-node1
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
sudo apt-get update && sudo apt-get install -y apt-transport-https curl
sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo sh -c "cat > /etc/apt/sources.list.d/kubernetes.list" <<EOF
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
sudo apt-get update
sudo apt-get install -y kubeadm kubectl
sudo apt-mark hold kubeadm kubectl

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
sudo apt-get update && sudo apt-get install -y apt-transport-https curl
sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo sh -c "cat > /etc/apt/sources.list.d/kubernetes.list" <<EOF
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
sudo apt-get update
sudo apt-get install -y kubeadm kubelet
sudo apt-mark hold kubeadm kubelet

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
shakir@controller:~ gcloud compute instances set-machine-type k8s-master --custom-cpu=2 --custom-memory=4
shakir@controller:~ gcloud compute instances stop k8s-master
shakir@controller:~ gcloud compute instances start k8s-master

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 \
--discovery-token-ca-cert-hash sha256:bebca8ab84f7f8545e30b3334d0d77793afbf032b05a9e6a8c4fb80fb367b9a1

Setup kubeconfig on the master

shakir@controller:~ cat setup-kubeconfig.sh
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

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
kubectl apply -f https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/rbac-kdd.yaml
kubectl apply -f https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml

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
sudo kubeadm join 10.128.0.4:6443 --token wznnx1.zkjl7tjgvc7tj5xw \
--discovery-token-ca-cert-hash sha256:bebca8ab84f7f8545e30b3334d0d77793afbf032b05a9e6a8c4fb80fb367b9a1

shakir@controller:~ cat join-nodes-to-cluster.sh | gcloud compute ssh k8s-node1
shakir@controller:~ cat join-nodes-to-cluster.sh | gcloud compute ssh k8s-node2
shakir@controller:~ cat join-nodes-to-cluster.sh | gcloud compute ssh k8s-node3

SSH into the master and start working

shakir@controller:~ gcloud compute ssh k8s-master
shakir@k8s-master:~ kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready master 140m v1.15.2
k8s-node1 Ready 119m v1.15.2
k8s-node2 Ready 119m v1.15.2
k8s-node3 Ready 119m v1.15.2

--end-of-post--