Kubernetes Cluster On Hetzner: A Quick Setup Guide

by Team 51 views
Kubernetes Cluster on Hetzner: A Quick Setup Guide

Are you looking to deploy a Kubernetes cluster on Hetzner Cloud? You've come to the right place! In this comprehensive guide, we'll walk you through the process step-by-step, making it easy even if you're relatively new to Kubernetes. We'll cover everything from setting up your Hetzner Cloud account to deploying your first application. So, let's dive in and get your Kubernetes cluster up and running!

Why Choose Hetzner for Kubernetes?

Before we get started, let's briefly discuss why Hetzner Cloud is a great choice for hosting your Kubernetes cluster. Hetzner offers several advantages, including:

  • Affordable Pricing: Hetzner Cloud provides highly competitive pricing, making it an excellent option for cost-conscious users.
  • High-Performance Hardware: You get access to powerful servers with fast processors and ample RAM, ensuring your applications run smoothly.
  • Reliable Infrastructure: Hetzner's data centers are known for their reliability and uptime, giving you peace of mind.
  • Simple Interface: Hetzner Cloud's interface is clean and intuitive, making it easy to manage your servers and resources.
  • Scalability: Easily scale your cluster as your needs grow, adding or removing nodes as required.

Prerequisites

Before we begin, make sure you have the following prerequisites in place:

  • Hetzner Cloud Account: You'll need an active Hetzner Cloud account. If you don't have one, you can sign up on their website. They often have promotional offers for new users.
  • kubectl: The Kubernetes command-line tool, kubectl, must be installed on your local machine. You can find installation instructions on the Kubernetes website.
  • hcloud CLI (Optional): While not strictly required, the Hetzner Cloud command-line interface (hcloud) can be helpful for managing your Hetzner Cloud resources from the command line. You can install it following the instructions on the Hetzner Cloud website.
  • SSH Key Pair: You'll need an SSH key pair to securely access your servers. If you don't have one, you can generate one using ssh-keygen.

Step-by-Step Guide to Deploying a Kubernetes Cluster on Hetzner

Now, let's get to the fun part: deploying your Kubernetes cluster! We'll use kubeadm to bootstrap the cluster. kubeadm is a tool provided by Kubernetes to simplify cluster creation.

Step 1: Create Hetzner Cloud Servers

First, you'll need to create the servers that will form your Kubernetes cluster. You'll need at least one master node and one or more worker nodes. A master node manages the cluster, while worker nodes run your applications.

  1. Log in to your Hetzner Cloud account.
  2. Create a new project (optional but recommended). This helps you organize your resources.
  3. Create servers for your master and worker nodes. When creating the servers, consider the following:
    • Operating System: Choose a supported operating system, such as Ubuntu 20.04 or Debian 11. Ensure the OS is compatible with Kubernetes.
    • Server Type: Select an appropriate server type based on your resource requirements. For a small cluster, a CX21 or CX31 instance might be sufficient. For larger clusters, consider more powerful instances.
    • SSH Key: Add your SSH key to the servers so you can access them securely.
    • Firewall: Create a firewall to allow necessary traffic to your servers. At a minimum, allow SSH (port 22) and traffic between the nodes. Kubernetes requires specific ports to be open for communication.
  4. Note the IP addresses of your servers. You'll need these later.

Important Note: When creating servers, ensure each has a unique hostname to prevent potential conflicts during cluster initialization. Using a firewall is crucial for security. Make sure to restrict access to the necessary ports only. If you are using the hcloud CLI, you can automate server creation using scripts.

Step 2: Install Container Runtime and kubeadm on All Nodes

Next, you need to install a container runtime and kubeadm on all your nodes (master and workers). We'll use Containerd as our container runtime. Perform the following steps on each server:

  1. SSH into the server. Use the IP address and the SSH key you configured earlier.

  2. Update the package index:

    sudo apt update
    
  3. Install required packages:

    sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
    
  4. Add the Kubernetes apt repository:

    curl -fsSL https://pkgs.k8s.io/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
    echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/apt/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list
    
  5. Update the package index again:

    sudo apt update
    
  6. Install kubelet, kubeadm, and kubectl:

    sudo apt install -y kubelet kubeadm kubectl
    sudo apt-mark hold kubelet kubeadm kubectl
    
  7. Install Containerd:

    sudo apt install -y containerd
    
  8. Configure Containerd:

    sudo mkdir -p /etc/containerd
    containerd config default | sudo tee /etc/containerd/config.toml
    sudo systemctl restart containerd
    

Important Note: It's crucial to hold the versions of kubelet, kubeadm, and kubectl to prevent accidental upgrades that might break your cluster. Always double-check that the Kubernetes apt repository is correctly configured before installing packages.

Step 3: Initialize the Kubernetes Master Node

Now, it's time to initialize the Kubernetes master node. Perform these steps only on the server you designated as the master node:

  1. Initialize the Kubernetes cluster:

    sudo kubeadm init --pod-network-cidr=10.244.0.0/16
    

    Note: The --pod-network-cidr flag specifies the network range for Pod IPs. You can choose a different range if needed, but ensure it doesn't conflict with your existing network.

  2. Configure kubectl to connect to the cluster:

    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
    
  3. Deploy a Pod network: We'll use Calico as our Pod network. Other options include Flannel and Weave Net.

    kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
    

    Important Note: The kubeadm init command will output a kubeadm join command. Save this command, as you'll need it to join the worker nodes to the cluster. Ensure the Pod network is deployed correctly before proceeding to the next step. A misconfigured Pod network can cause connectivity issues within your cluster.

Step 4: Join the Worker Nodes to the Cluster

Next, join the worker nodes to the cluster. Perform these steps on each worker node:

  1. Run the kubeadm join command that was output by the kubeadm init command on the master node. It should look something like this:

    sudo kubeadm join <master-ip>:<port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
    

    Note: If you've lost the kubeadm join command, you can regenerate it on the master node using the following commands:

    kubeadm token create --print-join-command
    

Important Note: Ensure the worker nodes can communicate with the master node on the specified port. Firewall rules might need adjustment to allow this communication. Double-check that the token and discovery token CA cert hash are correct.

Step 5: Verify the Cluster

After joining the worker nodes, verify that the cluster is working correctly. On the master node, run the following command:

kubectl get nodes

You should see all your nodes listed, with their status as Ready.

You can also check the status of the Pods:

kubectl get pods --all-namespaces

This will show you all the Pods running in your cluster, including the system Pods. Make sure all essential Pods are running without errors.

Step 6: Deploy Your First Application

Now that your Kubernetes cluster is up and running, you can deploy your first application! Let's deploy a simple Nginx deployment.

  1. Create a deployment:

    kubectl create deployment nginx --image=nginx
    
  2. Expose the deployment as a service:

    kubectl expose deployment nginx --port=80 --type=LoadBalancer
    

    Note: If you're using Hetzner Cloud's load balancer service, you'll need to configure it separately. Alternatively, you can use a NodePort service if you don't have a load balancer. LoadBalancer type service might take some time to get an external IP assigned from the cloud provider. Monitor the service status using kubectl get service nginx.

  3. Get the service's external IP:

    kubectl get service nginx
    

    Look for the EXTERNAL-IP column. This is the IP address you can use to access your Nginx server.

  4. Access your application in a web browser. Open a web browser and enter the external IP address. You should see the default Nginx welcome page.

Important Note: When exposing your application, consider security implications. Use appropriate authentication and authorization mechanisms to protect your application. Regularly update your application images to patch security vulnerabilities. Consider using Kubernetes namespaces to isolate different applications within your cluster.

Conclusion

Congratulations! You've successfully deployed a Kubernetes cluster on Hetzner Cloud. This guide provided you with a solid foundation for building and deploying your applications on Kubernetes. Remember to explore the vast ecosystem of Kubernetes tools and resources to further enhance your cluster's capabilities. With Hetzner's affordable pricing and reliable infrastructure, combined with the power of Kubernetes, you're well-equipped to tackle your container orchestration needs. Don't be afraid to experiment and explore the endless possibilities that Kubernetes offers. Happy clustering!