0 of 15 done
Provision the Ground

Provision the machines

Where you are

This is the last setup step before you do real Kubernetes work. You have a jumpbox. Now you will make the three cluster machines reachable and give them names. Once SSH and hostnames work, everything after this is run from the jumpbox. You will not need to log in to each machine by hand again.

Build the machine database

You will keep the details of your machines in a plain text file. This file acts as a machine database. It stores the attributes you need when you set up the Kubernetes control plane and the worker nodes. Each line in the file is one machine, and the columns follow this schema:

IPV4_ADDRESS FQDN HOSTNAME POD_SUBNET

Each line records a machine’s IP address IPV4_ADDRESS, its fully qualified domain name FQDN, its host name HOSTNAME, and its pod subnet POD_SUBNET. Kubernetes assigns one IP address per pod, and POD_SUBNET is the unique IP address range each machine uses to hand out those addresses.

Here is an example machine database similar to the one used when writing this course. The IP addresses are masked. Your machines can use any IP address as long as each machine can reach the others and the jumpbox.

cat machines.txt
XXX.XXX.XXX.XXX server.kubernetes.local server
XXX.XXX.XXX.XXX node-0.kubernetes.local node-0 10.200.0.0/24
XXX.XXX.XXX.XXX node-1.kubernetes.local node-1 10.200.1.0/24

Now create your own machines.txt file with the details for the three machines in your cluster. Use the example above as a template and fill in the details for your machines.

If you are running locally with Multipass, find each machine’s IP address by running multipass list, then copy those addresses into machines.txt.

Configure SSH access

You will use SSH to configure the machines in the cluster. Verify that you have root SSH access to each machine listed in your machine database. You may need to enable root SSH access on each node by updating the sshd_config file and restarting the SSH server.

Enable root SSH access

If root SSH access is already enabled for each of your machines, you can skip this section.

By default, a new debian install disables SSH access for the root user. This is done for security reasons, because the root user has total administrative control of unix-like systems. If a weak password is used on a machine connected to the internet, it is only a matter of time before that machine belongs to someone else. You are enabling root access over SSH here to keep the steps in this course short. This is a tradeoff, and you are choosing convenience. Log on to each machine via SSH using your user account, then switch to the root user using the su command:

su - root

Edit the /etc/ssh/sshd_config SSH daemon configuration file and set the PermitRootLogin option to yes:

sed -i \
  's/^#*PermitRootLogin.*/PermitRootLogin yes/' \
  /etc/ssh/sshd_config

Restart the sshd SSH server to pick up the updated configuration file:

systemctl restart sshd

Generate and distribute SSH keys

In this section you will generate an SSH keypair and distribute it to the server, node-0, and node-1 machines. You will use this keypair to run commands on those machines throughout the course. Run the following commands from the jumpbox machine.

Generate a new SSH key:

ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub

Copy the SSH public key to each machine:

while read IP FQDN HOST SUBNET; do
  ssh-copy-id root@${IP}
done < machines.txt

Once each key is added, verify SSH public key access is working:

while read IP FQDN HOST SUBNET; do
  ssh -n root@${IP} hostname
done < machines.txt
server
node-0
node-1

Set the hostnames

In this section you will assign hostnames to the server, node-0, and node-1 machines. You will use the hostname when you run commands from the jumpbox to each machine. The hostname also matters inside the cluster. Instead of issuing commands to the Kubernetes API server by IP address, clients will use the server hostname. The worker machines node-0 and node-1 also use their hostnames when they register with a cluster.

To configure the hostname for each machine, run the following commands on the jumpbox.

Set the hostname on each machine listed in the machines.txt file:

while read IP FQDN HOST SUBNET; do
    CMD="sed -i 's/^127.0.1.1.*/127.0.1.1\t${FQDN} ${HOST}/' /etc/hosts"
    ssh -n root@${IP} "$CMD"
    ssh -n root@${IP} hostnamectl set-hostname ${HOST}
    ssh -n root@${IP} systemctl restart systemd-hostnamed
done < machines.txt

Verify the hostname is set on each machine:

while read IP FQDN HOST SUBNET; do
  ssh -n root@${IP} hostname --fqdn
done < machines.txt
server.kubernetes.local
node-0.kubernetes.local
node-1.kubernetes.local

Build the host lookup table

In this section you will generate a hosts file. You will append it to the /etc/hosts file on the jumpbox and to the /etc/hosts files on all three cluster members. This lets each machine be reached by a hostname such as server, node-0, or node-1.

Create a new hosts file and add a header to identify the machines being added:

echo "" > hosts
echo "# Kubernetes The Hard Way" >> hosts

Generate a host entry for each machine in the machines.txt file and append it to the hosts file:

while read IP FQDN HOST SUBNET; do
    ENTRY="${IP} ${FQDN} ${HOST}"
    echo $ENTRY >> hosts
done < machines.txt

Review the host entries in the hosts file:

cat hosts

# Kubernetes The Hard Way
XXX.XXX.XXX.XXX server.kubernetes.local server
XXX.XXX.XXX.XXX node-0.kubernetes.local node-0
XXX.XXX.XXX.XXX node-1.kubernetes.local node-1

Add /etc/hosts entries on the jumpbox

In this section you will append the DNS entries from the hosts file to the local /etc/hosts file on your jumpbox machine.

Append the DNS entries from hosts to /etc/hosts:

cat hosts >> /etc/hosts

Verify that the /etc/hosts file has been updated:

cat /etc/hosts
127.0.0.1       localhost
127.0.1.1       jumpbox

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

# Kubernetes The Hard Way
XXX.XXX.XXX.XXX server.kubernetes.local server
XXX.XXX.XXX.XXX node-0.kubernetes.local node-0
XXX.XXX.XXX.XXX node-1.kubernetes.local node-1

At this point you should be able to SSH to each machine listed in the machines.txt file using a hostname.

for host in server node-0 node-1
   do ssh root@${host} hostname
done
server
node-0
node-1

Add /etc/hosts entries on the remote machines

In this section you will append the host entries from hosts to /etc/hosts on each machine listed in the machines.txt text file.

Copy the hosts file to each machine and append the contents to /etc/hosts:

while read IP FQDN HOST SUBNET; do
  scp hosts root@${HOST}:~/
  ssh -n \
    root@${HOST} "cat hosts >> /etc/hosts"
done < machines.txt

At this point you can use hostnames when connecting to machines from your jumpbox, or from any of the three machines in the cluster. Instead of IP addresses you can now connect using a hostname such as server, node-0, or node-1.

Key terms

Machine database (machines.txt) is the plain text file that lists your machines, one per line, with each machine’s IP address, fully qualified domain name, hostname, and pod subnet. You read from it in later steps to run the same command across every machine.

Pod subnet is the unique range of IP addresses a single machine uses to assign addresses to its pods. Each machine in the cluster gets its own range so the addresses never overlap.

FQDN stands for fully qualified domain name. It is the complete name of a machine, such as server.kubernetes.local, that includes both the host name and the domain.

Jumpbox-driven SSH means you run every command from the jumpbox and reach the other machines over SSH. After this step you do not log in to each machine by hand.

Reflect

Each command in this step reads from machines.txt and acts on every machine in turn. If you later add a fourth machine to your cluster, what would you need to change to bring it to the same state as the others?

Next: Provision a certificate authority