-
Fish-pro authored
Signed-off-by:
Fish-pro <zechun.chen@daocloud.io>
Fish-pro authoredSigned-off-by:
Fish-pro <zechun.chen@daocloud.io>
Setting up your first cluster with Kubespray
This tutorial walks you through the detailed steps for setting up Kubernetes with Kubespray.
The guide is inspired on the tutorial Kubernetes The Hard Way, with the difference that here we want to showcase how to spin up a Kubernetes cluster in a more managed fashion with Kubespray.
Target Audience
The target audience for this tutorial is someone looking for a hands-on guide to get started with Kubespray.
Cluster Details
Prerequisites
- Google Cloud Platform: This tutorial leverages the Google Cloud Platform to streamline provisioning of the compute infrastructure required to bootstrap a Kubernetes cluster from the ground up. Sign up for $300 in free credits.
- Google Cloud Platform SDK: Follow the Google Cloud SDK documentation to install and configure the
gcloud
command line utility. Make sure to set a default compute region and compute zone. - The kubectl command line utility is used to interact with the Kubernetes API Server.
- Linux or Mac environment with Python 3
Provisioning Compute Resources
Kubernetes requires a set of machines to host the Kubernetes control plane and the worker nodes where containers are ultimately run. In this lab you will provision the compute resources required for running a secure and highly available Kubernetes cluster across a single compute zone.
Networking
The Kubernetes networking model assumes a flat network in which containers and nodes can communicate with each other. In cases where this is not desired network policies can limit how groups of containers are allowed to communicate with each other and external network endpoints.
Setting up network policies is out of scope for this tutorial.
Virtual Private Cloud Network
In this section a dedicated Virtual Private Cloud (VPC) network will be setup to host the Kubernetes cluster.
Create the kubernetes-the-kubespray-way
custom VPC network:
gcloud compute networks create kubernetes-the-kubespray-way --subnet-mode custom
A subnet must be provisioned with an IP address range large enough to assign a private IP address to each node in the Kubernetes cluster.
Create the kubernetes
subnet in the kubernetes-the-kubespray-way
VPC network:
gcloud compute networks subnets create kubernetes \
--network kubernetes-the-kubespray-way \
--range 10.240.0.0/24
The
10.240.0.0/24
IP address range can host up to 254 compute instances.
Firewall Rules
Create a firewall rule that allows internal communication across all protocols. It is important to note that the vxlan protocol has to be allowed in order for the calico (see later) networking plugin to work.
gcloud compute firewall-rules create kubernetes-the-kubespray-way-allow-internal \
--allow tcp,udp,icmp,vxlan \
--network kubernetes-the-kubespray-way \
--source-ranges 10.240.0.0/24
Create a firewall rule that allows external SSH, ICMP, and HTTPS:
gcloud compute firewall-rules create kubernetes-the-kubespray-way-allow-external \
--allow tcp:80,tcp:6443,tcp:443,tcp:22,icmp \
--network kubernetes-the-kubespray-way \
--source-ranges 0.0.0.0/0
It is not feasible to restrict the firewall to a specific IP address from where you are accessing the cluster as the nodes also communicate over the public internet and would otherwise run into this firewall. Technically you could limit the firewall to the (fixed) IP addresses of the cluster nodes and the remote IP addresses for accessing the cluster.
Compute Instances
The compute instances in this lab will be provisioned using Ubuntu Server 18.04. Each compute instance will be provisioned with a fixed private IP address and a public IP address (that can be fixed - see guide). Using fixed public IP addresses has the advantage that our cluster node configuration does not need to be updated with new public IP addresses every time the machines are shut down and later on restarted.
Create three compute instances which will host the Kubernetes control plane:
for i in 0 1 2; do
gcloud compute instances create controller-${i} \
--async \
--boot-disk-size 200GB \
--can-ip-forward \
--image-family ubuntu-1804-lts \
--image-project ubuntu-os-cloud \
--machine-type e2-standard-2 \
--private-network-ip 10.240.0.1${i} \
--scopes compute-rw,storage-ro,service-management,service-control,logging-write,monitoring \
--subnet kubernetes \
--tags kubernetes-the-kubespray-way,controller
done
Do not forget to fix the IP addresses if you plan on re-using the cluster after temporarily shutting down the VMs - see guide
Create three compute instances which will host the Kubernetes worker nodes:
for i in 0 1 2; do
gcloud compute instances create worker-${i} \
--async \
--boot-disk-size 200GB \
--can-ip-forward \
--image-family ubuntu-1804-lts \
--image-project ubuntu-os-cloud \
--machine-type e2-standard-2 \
--private-network-ip 10.240.0.2${i} \
--scopes compute-rw,storage-ro,service-management,service-control,logging-write,monitoring \
--subnet kubernetes \
--tags kubernetes-the-kubespray-way,worker
done
Do not forget to fix the IP addresses if you plan on re-using the cluster after temporarily shutting down the VMs - see guide
List the compute instances in your default compute zone:
gcloud compute instances list --filter="tags.items=kubernetes-the-kubespray-way"
Output
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
controller-0 us-west1-c e2-standard-2 10.240.0.10 XX.XX.XX.XXX RUNNING
controller-1 us-west1-c e2-standard-2 10.240.0.11 XX.XXX.XXX.XX RUNNING
controller-2 us-west1-c e2-standard-2 10.240.0.12 XX.XXX.XX.XXX RUNNING
worker-0 us-west1-c e2-standard-2 10.240.0.20 XX.XX.XXX.XXX RUNNING
worker-1 us-west1-c e2-standard-2 10.240.0.21 XX.XX.XX.XXX RUNNING
worker-2 us-west1-c e2-standard-2 10.240.0.22 XX.XXX.XX.XX RUNNING
Configuring SSH Access
Kubespray is relying on SSH to configure the controller and worker instances.
Test SSH access to the controller-0
compute instance:
IP_CONTROLLER_0=$(gcloud compute instances list --filter="tags.items=kubernetes-the-kubespray-way AND name:controller-0" --format="value(EXTERNAL_IP)")
USERNAME=$(whoami)
ssh $USERNAME@$IP_CONTROLLER_0
If this is your first time connecting to a compute instance SSH keys will be generated for you. In this case you will need to enter a passphrase at the prompt to continue.
If you get a 'Remote host identification changed!' warning, you probably already connected to that IP address in the past with another host key. You can remove the old host key by running
ssh-keygen -R $IP_CONTROLLER_0
Please repeat this procedure for all the controller and worker nodes, to ensure that SSH access is properly functioning for all nodes.
Set-up Kubespray
The following set of instruction is based on the Quick Start but slightly altered for our set-up.
As Ansible is a python application, we will create a fresh virtual environment to install the dependencies for the Kubespray playbook:
python3 -m venv venv
source venv/bin/activate
Next, we will git clone the Kubespray code into our working directory:
git clone https://github.com/kubernetes-sigs/kubespray.git
cd kubespray
git checkout release-2.17
Now we need to install the dependencies for Ansible to run the Kubespray playbook:
pip install -r requirements.txt
Copy inventory/sample
as inventory/mycluster
:
cp -rfp inventory/sample inventory/mycluster
Update Ansible inventory file with inventory builder:
declare -a IPS=($(gcloud compute instances list --filter="tags.items=kubernetes-the-kubespray-way" --format="value(EXTERNAL_IP)" | tr '\n' ' '))
CONFIG_FILE=inventory/mycluster/hosts.yaml python3 contrib/inventory_builder/inventory.py ${IPS[@]}
Open the generated inventory/mycluster/hosts.yaml
file and adjust it so
that controller-0, controller-1 and controller-2 are control plane nodes and
worker-0, worker-1 and worker-2 are worker nodes. Also update the ip
to the respective local VPC IP and
remove the access_ip
.
The main configuration for the cluster is stored in
inventory/mycluster/group_vars/k8s_cluster/k8s_cluster.yml
. In this file we
will update the supplementary_addresses_in_ssl_keys
with a list of the IP
addresses of the controller nodes. In that way we can access the
kubernetes API server as an administrator from outside the VPC network. You
can also see that the kube_network_plugin
is by default set to 'calico'.
If you set this to 'cloud', it did not work on GCP at the time of testing.
Kubespray also offers to easily enable popular kubernetes add-ons. You can
modify the
list of add-ons in inventory/mycluster/group_vars/k8s_cluster/addons.yml
.
Let's enable the metrics server as this is a crucial monitoring element for
the kubernetes cluster, just change the 'false' to 'true' for
metrics_server_enabled
.
Now we will deploy the configuration:
ansible-playbook -i inventory/mycluster/hosts.yaml -u $USERNAME -b -v --private-key=~/.ssh/id_rsa cluster.yml
Ansible will now execute the playbook, this can take up to 20 minutes.
Access the kubernetes cluster
We will leverage a kubeconfig file from one of the controller nodes to access the cluster as administrator from our local workstation.
In this simplified set-up, we did not include a load balancer that usually sits on top of the three controller nodes for a high available API server endpoint. In this simplified tutorial we connect directly to one of the three controllers.
First, we need to edit the permission of the kubeconfig file on one of the controller nodes:
ssh $USERNAME@$IP_CONTROLLER_0
USERNAME=$(whoami)
sudo chown -R $USERNAME:$USERNAME /etc/kubernetes/admin.conf
exit
Now we will copy over the kubeconfig file:
scp $USERNAME@$IP_CONTROLLER_0:/etc/kubernetes/admin.conf kubespray-do.conf
This kubeconfig file uses the internal IP address of the controller node to access the API server. This kubeconfig file will thus not work of from outside the VPC network. We will need to change the API server IP address to the controller node his external IP address. The external IP address will be accepted in the TLS negotiation as we added the controllers external IP addresses in the SSL certificate configuration. Open the file and modify the server IP address from the local IP to the external IP address of controller-0, as stored in $IP_CONTROLLER_0.
Example
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: XXX
server: https://35.205.205.80:6443
name: cluster.local
...
Now, we load the configuration for kubectl
:
export KUBECONFIG=$PWD/kubespray-do.conf
We should be all set to communicate with our cluster from our local workstation:
kubectl get nodes
Output
NAME STATUS ROLES AGE VERSION
controller-0 Ready master 47m v1.17.9
controller-1 Ready master 46m v1.17.9
controller-2 Ready master 46m v1.17.9
worker-0 Ready <none> 45m v1.17.9
worker-1 Ready <none> 45m v1.17.9
worker-2 Ready <none> 45m v1.17.9
Smoke tests
Metrics
Verify if the metrics server addon was correctly installed and works:
kubectl top nodes
Output
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
controller-0 191m 10% 1956Mi 26%
controller-1 190m 10% 1828Mi 24%
controller-2 182m 10% 1839Mi 24%
worker-0 87m 4% 1265Mi 16%
worker-1 102m 5% 1268Mi 16%
worker-2 108m 5% 1299Mi 17%
Please note that metrics might not be available at first and need a couple of minutes before you can actually retrieve them.
Network
Let's verify if the network layer is properly functioning and pods can reach each other: