When you think about Kubernetes, you probably think AWS or GCP, a nice managed service where you can easily spin up resources and build applications on top of them. This is great, and honestly the best way to experience Kubernetes. However, if all you need is a lab to mess around in and experiment, or learn new things in, this can be very cost inefficient. That is why we are going to look at setting up Kubernetes ourselves.
In this post, we are going to look at the initial deployment of Kubernetes, from creating our nodes (in this case CentOS 7 VMs) to getting a cluster up and running.
The first step is to create some VMs. I use a custom vCenter template in my lab, but if you do not have one of those, you can follow these simple steps.
You will need to complete these steps on at least 1 machine, however more is certainly better to get the full benefit of Kubernetes.
Your machine/VM should have at least 1 core and 3Gb of RAM.
- Install CentOS 7 from the USB ISO image, a basic install is fine
- Create a user for Ansible access. This user should be part of the sudo users group, and ideally have passwordless SSH authentication
- Assign static IP Addresses to your hosts. Optionally set a hostname.
- I hate to say it, but the official docs say to disable the firewall between the nodes, and I was unable to find documentation on which ports are needed.
- Optionally, add your hosts to DNS.
We are going to be using Kubespray for our cluster, as it makes creating and updating a Kubernetes cluster very simple and straightforward.
You can find the official docs here.
The quick guide is as follows:
Install the dependencies
sudo pip install -r requirements.txt
Copy the example Inventory
cp -rfp inventory/sample inventory/mycluster
Build the inventory, you can use the built in builder, or take a look here for an example. It is fine to have a single master, but the
etcdsections should be the same.
Deploy the cluster
ansible-playbook -i inventory/mycluster/hosts.yml --become --become-user=root cluster.yml
Then all you have to do is wait while Kubespray deploys your cluster automatically. On my 6 node cluster, it usually takes about 10–15 minutes for the cluster to be completely setup and running.
Note that in the Kubespray inventory there are a couple of options which are useful to enable. First, in the
addons.yaml file, it is a good idea to enable Helm and the Kubernetes Dashboard automatic deployments. It may also be beneficial to enable
kube_basic_auth in the
k8s-cluster.yaml file, if you are having issues with the default token based authentication. If you decide to do this later, you can simply make the change and then re-run the deployment with the command in step 4 above.
You can test that your cluster is up and running with the following commands:
kubectl cluster-infowhich should return something like:
Kubernetes master is running at https://10.0.40.245:6444
kubectl get nodeswhich displays the state of all of your nodes.
You can find more information about how I have setup Kubernetes at my Gitlab repo, which has helpful code snippets, full configuration files, as well as expanded documentation.