How to Install and Set Up Kubernetes on a VPS with AnonVM

Kubernetes is a powerful open-source platform for automating deployment, scaling, and management of containerized applications. Installing Kubernetes on a VPS allows you to manage and scale applications seamlessly. In this guide, we’ll cover the steps for setting up a Kubernetes single-node cluster on a VPS with AnonVM.


System Requirements

Before starting, ensure your server meets the following prerequisites:

  • OS: Ubuntu 20.04+ or CentOS 7+
  • CPU: Minimum 2 vCPUs (recommended 4)
  • RAM: 2GB+ (recommended 4GB)
  • Access: Root or sudo privileges

Step 1: Update and Configure Your System

Begin by updating your package list and upgrading any outdated packages:

 
sudo apt update && sudo apt upgrade -y # Ubuntu/Debian sudo yum update -y # CentOS

Ensure the swap memory is turned off, as Kubernetes does not support it:

 
sudo swapoff -a

To make this change permanent, edit the /etc/fstab file and comment out any lines related to swap:

 
sudo nano /etc/fstab

Step 2: Install Docker

Kubernetes requires a container runtime, and Docker is one of the most commonly used options.

Install Docker on Ubuntu

  1. Install necessary packages:

     
    sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
  2. Add Docker’s official GPG key:

     
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  3. Add the Docker repository:

     
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
  4. Install Docker:

     
    sudo apt update sudo apt install -y docker-ce

Install Docker on CentOS

  1. Install Docker:

     
    sudo yum install -y yum-utils sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo sudo yum install -y docker-ce docker-ce-cli containerd.io
  2. Start Docker:

     
    sudo systemctl start docker sudo systemctl enable docker

Step 3: Install Kubernetes Components

You will need three key Kubernetes components: kubeadm, kubelet, and kubectl.

  1. Add Kubernetes Repository:

     
    sudo apt install -y apt-transport-https curl # Ubuntu curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list sudo apt update

    For CentOS:

     
    sudo cat <<EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg EOF
  2. Install Kubernetes Components:

     
    sudo apt install -y kubelet kubeadm kubectl # Ubuntu sudo yum install -y kubelet kubeadm kubectl # CentOS
  3. Start and Enable kubelet:

     
    sudo systemctl enable kubelet

Step 4: Initialize the Kubernetes Cluster

Now that Docker and Kubernetes components are installed, you can initialize the Kubernetes cluster. Run the following command as a root or sudo user:

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

Take note of the kubeadm join command output, as you’ll need it to add more nodes to the cluster in the future.


Step 5: Set Up kubeconfig for Rootless Access

To use kubectl as a non-root user, you need to set up a kubeconfig file.

  1. Create the .kube directory:

     
    mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
  2. Verify the Cluster Status:

     
    kubectl get nodes

    You should see the master node listed as Ready.


Step 6: Install a Pod Network Add-On

To allow communication between Kubernetes nodes, you need to set up a network add-on. A common choice is Flannel.

  1. Deploy Flannel:

     
    kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
  2. Verify Network Pods:

     
    kubectl get pods --all-namespaces

You should see Flannel-related pods running in the kube-system namespace.


Step 7: Deploy a Test Application

To verify that Kubernetes is working correctly, deploy a sample application.

  1. Create a Nginx Deployment:

     
    kubectl create deployment nginx --image=nginx
  2. Expose the Deployment:

     
    kubectl expose deployment nginx --port=80 --type=NodePort
  3. Retrieve the Node Port:

     
    kubectl get svc nginx

The output will show the port Kubernetes assigned. Access the Nginx server by navigating to <your-server-ip>:<NodePort> in your browser.


Step 8: Enable Kubernetes Dashboard (Optional)

The Kubernetes Dashboard is a web-based user interface to manage Kubernetes resources.

  1. Apply Dashboard YAML:

     
    kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.2.0/aio/deploy/recommended.yaml
  2. Create an Admin User:

    Create an admin user to log into the Dashboard by defining an admin service account and cluster role binding.

    yaml
     
    apiVersion: v1 kind: ServiceAccount metadata: name: admin-user namespace: kubernetes-dashboard --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: admin-user roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-admin subjects: - kind: ServiceAccount name: admin-user namespace: kubernetes-dashboard
  3. Retrieve Dashboard Access Token:

     
    kubectl -n kubernetes-dashboard create token admin-user

Copy the token, then access the Dashboard by navigating to https://<your-server-ip>:6443/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/.


Conclusion

You’ve now set up a Kubernetes single-node cluster on your VPS with AnonVM! This environment is ideal for development, testing, and learning Kubernetes concepts. As your project grows, you can expand by adding more nodes. Remember to keep your Kubernetes installation updated and practice secure cluster management.

Was this answer helpful? 0 Users Found This Useful (0 Votes)

Powered by WHMCompleteSolution