Installing Kubernetes Using Docker

Kubernetes is software for managing a cluster of Docker containers. Kubernetes orchestration includes scheduling, distributing workload, and scaling. Kubernetes takes the software encapsulation provided by
Docker further by introducing Pods. A Pod is a collection of one or more Docker containers with single interface features such as providing networking and filesystem at the Pod level rather than at the container
level. Kubernetes also introduces “labels” using which services and replication controllers (replication controller is used to scale a cluster) identify or select the containers or pods they manage. Kubernetes is
lightweight, portable (suited for the cloud architecture), and modular. Kubernetes may be run on almost any platform. Local machine solutions include local Docker based, Vagrant, and no-VM local cluster.

Hosted solutions include Google Container Engine. Some of the other platforms supported by Kubernetes are Fedora (Ansible and Manual), Amazon Web Services, Mesos,
vSphere, and CoreOS. Kubernetes is an orchestration software for Docker containers; the recommended solution for installation is to use the Docker Engine. In this blog we shall install Kubernetes on Docker,
which runs on Ubuntu. We shall use an Amazon EC2 instance hosting Ubuntu as the operating system.

This blog has the following sections.

Setting the Environment
Installing Docker
Installing Kubernetes
Starting etcd
Starting Kubernetes Master
Starting Service Proxy
Listing the Kubernetes Docker Containers
Installing kubectl
Listing Services
Listing Nodes
Testing the Kubernetes Installation

Setting the Environment

The following software is required for this task.
– Docker Engine (latest version)
– Kubernetes (version 1.01)

Linux is required to support 64-bit software. We have used an Amazon EC2 instance created from AMI Ubuntu Server 14.04 LTS (HVM), SSD Volume Type – ami-d05e75b8.

A different Ubuntu version may be used if the requirement of a 64-bit architecture is met. The minimum kernel version requirement is 3.10. The kernel version may be verified with the following command.

uname –r

The Public IP would be different for different users. Multiple Amazon EC2 instances and therefore multiple Public IP addresses have been used in the blog as a different Public IP is assigned each time an
Amazon EC2 instance is started. The Private IP Address of an Amazon EC2 instance is the same across restarts. SSH into an Ubuntu instance on Amazon EC2 (Public IP is 52.91.90.173 in following command).

ssh -i “docker.pem” ubuntu@52.91.90.183

The command prompt becomes “ubuntu@ip-172-40-2-190” instead of root@localhost. Ip 172.40.2.190 is the Private IP of the Amazon EC2 instance and would also be different for different users.

In the next section we shall install Docker on Ubuntu hosted on an Amazon EC2 instance .

Installing Docker

Ubuntu uses apt for package management; apt stores a list of repositories in the /etc/apt/sources.list list.
Docker’s apt repository is kept in the /etc/apt/sources.list.d/docker.list file. First, add the new repository key
(gpg key) for the Docker repository with the following command.

sudo apt-key adv –keyserver hkp://pgp.mit.edu:80 –recv-keys
58118E89F3A912897C070ADBF76221572C52609D

Next, update the apt sources for the Docker repository in the /etc/apt/sources.list.d/docker.list file
based on the Ubuntu distribution, which may be found with the following command.

lsb_release –a

For Ubuntu Trusty, add the following line to the /etc/apt/sources.list.d/docker.list file; the docker.list file may be opened with sudo vi /etc/apt/sources.list.d/docker.list.

deb https://apt.dockerproject.org/repo ubuntu-trusty main

Create the /etc/apt/sources.list.d/docker.list file if the file does not already exist. Save the file with the :wq command if opened in the vi editor.

Table 1-1. The docker.list file Entry Based on Ubuntu Distribution
Ubuntu Distribution Entry
Ubuntu Precise 12.04 (LTS) deb https://apt.dockerproject.org/repo ubuntu-precise main
Ubuntu Trusty 14.04 (LTS) deb https://apt.dockerproject.org/repo ubuntu-trusty main
Ubuntu Vivid 15.04 deb https://apt.dockerproject.org/repo ubuntu-vivid main

Run the following commands after updating the /etc/apt/sources.list.d/docker.list file to update the apt
package index.

sudo apt-get update

 

Purge the old repository if it exists with the following command.

sudo apt-get purge lxc-docker*

Run the following command to verify that apt is pulling from the updated repository for Docker.

sudo apt-cache policy docker-engine

Next, install the prerequisites for Ubuntu, but first update the package manager with the following
command.

sudo apt-get update

Install the prerequisite linux-image-extra package with the following command.
sudo apt-get install linux-image-generic-lts-trusty
When the preceding command is run, select Y if prompted with the following message.
After this operation, 281 MB of additional disk space will be used.
Do you want to continue? [Y/n]

Subsequently, before the command completes, a Package Configuration dialog might prompt with the
following message:
A new version of /boot/grub/menu.lst is available, but the version installed currently has
been locally modified. What would you like to do about menu.lst?

Reboot the system with the following command.
sudo reboot
When the sudo reboot command is run the AmazonEC2 instance is exited. Reconnect with the Amazon
EC2 Ubuntu instance with the same ssh command as before.

ssh -i “docker.pem” ubuntu@52.91.90.183

After the host system reboots, update the package manager again with the following command.
sudo apt-get update

install Docker with the following command.

sudo apt-get install docker-engine

Start the Docker service with the following command.
sudo service docker start
To verify the status of the Docker service, run the following command.
sudo service docker status

 

Installing Kubernetes
Kubernetes is an open source container cluster manager. The main components of Kubernetes are the following:

1. etcd
2. Kubernetes master
3. Service proxy
4. kubelet

etcd is a simple, secure, fast and reliable distributed key-value store.
Kubernetes master exposes the Kubernetes API using which containers are run on nodes to handle tasks. kubelet is an agent that runs on each node to monitor the containers running on the node, restarting
them if required to keep the replication level.

A service proxy runs on each node to provide the Kubernetes service interface for clients. A service is an abstraction for the logical set of pods represented by the service, and a service selector is used to select the
pods represented by the service. The service proxy routes the client traffic to a matching pod. Labels are used to match a service with a pod.

Optionally create a directory (/kubernetes) to install Kubernetes and set its permissions to global (777).

sudo mkdir /kubernetes
sudo chmod -R 777 /kubernetes

Change directory to the /kubernetes directory and start the Docker engine.

cd /kubernetes
sudo service docker start

If the Docker Engine is not running, it gets started.

As a prerequisite we need to set some Linux kernel parameters if not already set. Add support for
memory and swap accounting. The following configs should be turned on in the kernel.

CONFIG_RESOURCE_COUNTERS=y
CONFIG_MEMCG=y
CONFIG_MEMCG_SWAP=y
CONFIG_MEMCG_SWAP_ENABLED=y
CONFIG_MEMCG_KMEM=y

The kernel configs are enabled when the Ubuntu system boots and the kernel configuration file is in the
/boot directory. Change directory (cd) to the /boot directory and list the files/directories.

cd /boot
ls –l

The files in the /boot directory get listed . The kernel configs are configured in the config-3.13.0-48-generic file. The kernel version could be different for different users; for example, the kernel config file could /boot/config-3.13.0-66-generic.

Open the config-3.13.0-48-generic file in a vi editor.

sudo vi /boot/config-3.13.0-48-generic

Most of the configs listed earlier are already turned on . The CONFIG_MEMCG_SWAP_ENABLED config is not set.

Set CONFIG_MEMCG_SWAP_ENABLED = y and save the kernel configuration file

Next, we need to add support for memory and swap accounting to the kernel. The command-line parameters provided to the kernel may be listed with the following command.

cat /proc/cmdline

Grub 2 is the default boot loader for Ubuntu. To turn on memory and swap accounting, open the /etc/default/grub file in the vi editor. The GRUB_CMDLINE_LINUX is set to an empty string.

Set the GRUB_CMDLINE_LINU as follows, which enables memory and swap accounting in the kernel at boot.

GRUB_CMDLINE_LINUX=”cgroup_enable=memory swapaccount=1″

The modified /etc/default/grub file. Save the file with the :wq command.

Update the grub.cfg file with the following command.

sudo update-grub

Reboot the system.
sudo reboot

SSH log in back into the Ubuntu instance. Rerun the command to list the command-line kernel
parameters.
cat /proc/cmdline
The cgroup_enable = memory swapaccount = 1 settings get output a

Having set the prerequisite kernel parameters, in the next post  we shall start the Kubernetes components etcd, master, and service proxy.

Leave a Reply

Your email address will not be published. Required fields are marked *