NEW! Mirantis Academy -   Learn confidently with expert guidance and On-demand content.   Learn More

< BLOG HOME

How to install Kubernetes with Kubeadm: A quick and dirty guide

image
Sometimes you just need a Kubernetes cluster, and you don't want to mess around with a full Kubernetes install procedure. Maybe you want to test out a small application, or create a development environment for yourself. Whatever your goal, you want it quick, and you want it simple. That's what we're going to do. This article is a quick and dirty guide to creating a single-node Kubernetes cluster using Kubeadm, the "best practices" tool the k8s community created to simplify the deployment process. (It's straightforward to create multi-node deployments, but we'll cover that in later articles.)

Create the VM

These instructions were tested using a Virtualbox VM, but in theory they should be the same for a bare metal deployment. (You can find instructions for creating the VM here.) A couple of things to note about the VM:
  • Allocate at least 2 vCPUs, even if you're overcommitting your resources; there are pieces of this example that won't start with just one.
  • Try to allocate at least 4096 MB of RAM and 20 GB of drive space.
  • You should have Ubuntu 16.04 or later installed.
  • Set the default network adapter to connect to the "Bridged adapter" to enable traffic between the VM and the host machine.

Prepare the VM

There are a few things you need to do to get the VM ready. Specifically, you need to turn off  swap, tweak some configuration settings, and make sure you have the prerequisites libraries installed.  To do that, follow these steps:
  1. Change to root:
    sudo su
  2. Turn off swap: To do this, you will first need to turn it off directly ...
    swapoff -a
    ... then comment out the reference to swap in /etc/fstab.  Start by editing the file:
    vi /etc/fstab
    Then comment out the appropriate line, as in:
    # /etc/fstab: static file system information.
    #
    # Use 'blkid' to print the universally unique identifier for a
    # device; this may be used with UUID= as a more robust way to name devices
    # that works even if disks are added and removed. See fstab(5).
    #
    # <file system> <mount point>   <type>  <options>    <dump>  <pass>
    # / was on /dev/sda1 during installation
    UUID=1d343a19-bd75-47a6-899d-7c8bc93e28ff /            ext4 errors=remount-ro 0    1
    # swap was on /dev/sda5 during installation
    #UUID=d0200036-b211-4e6e-a194-ac2e51dfb27d none         swap sw           0    0
  3. Now configure iptables to receive bridged network traffic. First edit the sysctl.conf file:
    vi /etc/ufw/sysctl.conf
    And add the following lines to the end:
    net/bridge/bridge-nf-call-ip6tables = 1
    net/bridge/bridge-nf-call-iptables = 1
    net/bridge/bridge-nf-call-arptables = 1
  4. Reboot so the changes take effect.
  5. Install ebtables and ethtool:
    sudo su
    apt-get install ebtables ethtool
  6. Reboot once more.

How to Install Kubeadm: The Kubeadm Install Process

OK, now we're ready to go ahead and do the install.  For the full details on this process, you can see the documentation, but here's the quick and dirty version:
  1. Install Docker:
    sudo su

    apt-get update
    apt-get install -y docker.io
  2. Install HTTPS support components (if necessary):
    apt-get update 
    apt-get install -y apt-transport-https
  3. Install Curl (if necessary):
    apt-get install curl
  4. Retrieve the key for the Kubernetes repo and add it to your key manager:
    curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
  5. Add the kubernetes repo to your system.
    cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
    deb http://apt.kubernetes.io/ kubernetes-xenial main
    EOF
  6. Actually install the three pieces you'll need, kubeadm, kubelet, and kubectl:
    apt-get update
    apt-get install -y kubelet kubeadm kubectl
At this point you should have all the tools you need, so you should be ready to go ahead and actually deploy a k8s cluster.

Create a cluster

Now that the Kubeadm installation is complete, we'll go ahead and create a new cluster using kubeadm init.  Part of this process is choosing a network provider, and there are several choices; we'll use Calico for this kubeadm init example example.
  1. Create the actual cluster.  For Calico, we need to add the --pod-network-cidr switch as command line arguments to kubeadm init, as in:
    kubeadm init --pod-network-cidr=192.168.0.0/16
    This will crank for a while, eventually giving you output something like this:
    To start using your cluster, you need to run (as a regular user):

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

    You should now deploy a pod network to the cluster.
    Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
     http://kubernetes.io/docs/admin/addons/

    You can now join any number of machines by running the following on each node
    as root:

     kubeadm join --token 354502.d6a9a425d5fa8f2e 192.168.0.9:6443 --discovery-token-ca-cert-hash sha256:ad7c5e8a0c909ed36a87452e65fa44b1c2a9729cef7285eb551e2f126a1d6a54
    Notice that last bit, about joining other machines to the cluster; we're not going to do that, but you do have that option.
  2. Prepare your system for adding workloads, including the network plugin.  Open a NEW terminal window and execute the commands kubeadm gave you:
    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
  3. Install the Calico network plugin:
    kubectl apply -f https://docs.projectcalico.org/v2.6/getting-started/kubernetes/installation/hosted/kubeadm/1.6/calico.yaml
  4. Check to see if the pods are running:
    kubectl get pods --all-namespaces
    The pods will start up over a short period of time.
  5. Untaint the master so that it will be available for scheduling workloads:
    kubectl taint nodes --all node-role.kubernetes.io/master-
At this point you should have a fully-functional kubernetes cluster on which you can run services and workloads.

Mirantis simplifies Kubernetes.

From the world’s most popular Kubernetes IDE to fully managed services and training, we can help you at every step of your K8s journey.

Connect with a Mirantis expert to learn how we can help you.

Contact Us

Test the cluster

Now let's make sure everything's working properly by installing the Sock Shop sample application.  Follow these steps:
    1. Create the namespace in which the Sock Shop will live:
      kubectl create namespace sock-shop
    2. Create the actual Sock Shop application:
      kubectl apply -n sock-shop -f "https://github.com/microservices-demo/microservices-demo/blob/master/deploy/kubernetes/complete-demo.yaml?raw=true"
      Use kubectl get pods --all-namespaces to make sure the pods are all running.
    3. We'll interact with it via the front-end service, so find the IP address for that service:
      kubectl -n sock-shop get svc front-end

      NAME        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
      front-end   10.110.250.153   <nodes>       80:30001/TCP   59s
      Visit http://<cluster-ip> (in this case, http://10.110.250.153) with your browser.  You should see the WeaveSocks interface:

      Kubeadm makes it easy to get a cluster on which to install the Sock Shop app.
      (NOTE:  If you are running VirtalBox on Windows, you may run into a bug that only enables you to view the application on port 30001.)
    4. You can also verify that you can reach the interface from the host machine.  You'll first need to find the proper IP address.  Your VM will have several, which you should be able to find using:
      ifconfig
      You're looking for an IP address in the same range as your host machine, most likely the Ethernet adapter.  For example, in my case, it was this section:
      enp0s3 Link encap:Ethernet  HWaddr 08:00:27:7c:14:fd  
            inet addr:192.168.2.50  Bcast:192.168.2.255  Mask:255.255.255.0
            inet6 addr: fe80::7638:2441:3326:b242/64 Scope:Link
            UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
            RX packets:415485 errors:0 dropped:0 overruns:0 frame:0
            TX packets:166889 errors:0 dropped:0 overruns:0 carrier:0
            collisions:0 txqueuelen:1000
            RX bytes:621031284 (621.0 MB)  TX bytes:12020009 (12.0 MB)
    5. Now take that IP address and point the browser on your host machine to that IP on port 30001:
      http://:30001
      (In my case that would be http://192.168.2.50:30001.)

      If all has gone well, you will see the Sock Shop, just as you did on the VM:

      Kubeadm makes it easy to get a cluster on which to install the Sock Shop app.

Cleaning up

We haven't done much, but if you want to start over, you can do the following:

To remove the Sock Shop:
kubectl delete namespace sock-shop
To remove the entire cluster:
sudo kubeadm reset
That's it!

Installing Kubernetes with kubeadm recap

So at this point you know how to:
      1. Prepare a VM for Kubeadm
      2. Install Kubeadm
      3. Deploy a Kubernetes cluster
      4. Deploy a sample application on a Kubernetes application
      5. Remove the sample application
      6. Remove the cluster
In future articles, we'll talk about other features such as creating custom applications and adding additional nodes to clusters.  You can also check out our previous tutorial on creating a Kubernetes deployment, or spend an hour learning the basics of Kubernetes with our free Kubernetes mini-boot camp. Do you anticipate this project growing, and you might need help managing Kubernetes? Check out our Kubernetes support options.

Choose your cloud native journey.

Whatever your role, we’re here to help with open source tools and world-class support.

GET STARTED
NEWSLETTER

Subscribe to our bi-weekly newsletter for exclusive interviews, expert commentary, and thought leadership on topics shaping the cloud native world.

JOIN NOW