An HelloWorld Kubernetes example

In this tutorial I will show how I have configured an HelloWorld Kubernetes applications using Vagrant and Fedora

Pre-requisite

The Prerequisite to this tutorial is that you have installed Vagrant on your machine. See this tutorial for help on installing Vagrant: Quick introduction to Vagrant

We will now provision two Fedora 23 Virtual machines named fedora1 and fedora2 assigning a static IP address to both of them.

Here is the Vagrantfile I have used:

Vagrant.configure("2") do |config|
  config.vm.provision :shell, path: "boot.sh"
  config.vm.network :public_network, :bridge => 'enp0s25', :dev => 'enp0s25'

  config.vm.define "fedora1" do |fedora1|
    fedora1.vm.box = "fedora/23-cloud-base"
    fedora1.vm.network "private_network", ip: "192.168.122.10"
  end

  config.vm.define "fedora2" do |fedora2|
    fedora2.vm.box = "fedora/23-cloud-base"
    fedora2.vm.network "private_network", ip: "192.168.122.20"
  end
end

 Start the Virtual Machines with:

vagrant up

 Check the status of your machines with:

[root@localhost fedora]# vagrant status
Current machine states:

fedora1                   running (libvirt)
fedora2                   running (libvirt)

 Great, now open two shells: you need to ssh to both machines to install and configure kubernetes.

$ vagrant ssh fedora1

$ vagrant ssh fedora2

 Install Kubernetes and etcd on both machines with:

sudo yum -y install --enablerepo=updates-testing kubernetes

sudo yum -y install etcd iptables

 Next, on both machines, you need to specify the host mapping on /etc/hosts

192.168.122.10  fed-master
192.168.122.20  fed-node

 Next, on both machines you need to specify on /etc/kubernetes/config the address of the master node (fed-master) and logging properties:

# Comma separated list of nodes in the etcd cluster
KUBE_MASTER="--master=http://fed-master:8080"

# logging to stderr means we get it in the systemd journal
KUBE_LOGTOSTDERR="--logtostderr=true"

# journal message level, 0 is debug
KUBE_LOG_LEVEL="--v=0"

# Should this cluster be allowed to run privileged docker containers
KUBE_ALLOW_PRIV="--allow-privileged=false"

 Next, on the master node (fed-master), configure the Services Addresses in /etc/kubernetes/apiserver

# default admission control policies
#KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota"

# The address on the local server to listen to.
KUBE_API_ADDRESS="--address=0.0.0.0"

# Comma separated list of nodes in the etcd cluster
KUBE_ETCD_SERVERS="--etcd-servers=http://127.0.0.1:2379"

# Address range to use for services
KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16"

# Add your own!
KUBE_API_ARGS=""

Still on the master node (fed-master) specify the listen address of etcd server in /etc/etcd/etcd.conf:

ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"

Still on the master node, create the following directories and set these permissions:

$ mkdir /var/run/kubernetes
$ chown kube:kube /var/run/kubernetes
$ chmod 750 /var/run/kubernetes

 Next, on the fed-node specify the location of the master node in /etc/kubernetes/kubelet:

KUBELET_ARGn=" kubelet (node) config

# The address for the info server to serve on (set to 0.0.0.0 or "" for all interfaces)
KUBELET_ADDRESS="--address=0.0.0.0"

# You may leave this blank to use the actual hostname
KUBELET_HOSTNAME="--hostname-override=fed-node"

# location of the api-server
KUBELET_API_SERVER="--api-servers=http://fed-master:8080"

# Add your own!
#KUBELET_ARGS=""

 Now it's time to start Kubernetes and etcd on the master node!

for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler; do
    sudo systemctl restart $SERVICES
    sudo systemctl enable $SERVICES
    sudo systemctl status $SERVICES
done

With all services up and running, we will now create our first node!

What is a Node ?

A Node (previously known as Minion) is a worker machine in Kubernetes. A Node may be a VM or physical machine, depending on the cluster. Each node has the services necessary to run Pods and is managed by the master components. The services on a node include docker, kubelet and network proxy.

Define a node with a file named node.json file on Kubernetes master node. The node will be bound to the fed-node running on the other Virtual Machine:

{
    "apiVersion": "v1",
    "kind": "Node",
    "metadata": {
        "name": "fed-node",
        "labels":{ "name": "fed-node-label"}
    },
    "spec": {
        "externalID": "fed-node"
    }
}

You can create the node with:

[vagrant@fed-master ~]$ kubectl create -f ./node.json

Now verify if the node is reached from Kubernetes. Start the services on the fed-node:

for SERVICES in kube-proxy kubelet docker; do
    sudo systemctl restart $SERVICES
    sudo systemctl enable $SERVICES
    sudo systemctl status $SERVICES
done

 Check the available nodes from the master node:

[vagrant@fed-master ~]$ kubectl get nodes
NAME       LABELS                STATUS    AGE
fed-node   name=fed-node-label   Ready     2h

 Awesome. We will now deploy our first Pod on Kubernetes. Create the following busybox.yaml file referencing the image busybox:

apiVersion: v1
kind: Pod
metadata:
  name: busybox
  namespace: default
spec:
  containers:
  - image: busybox
    command:
      - sleep
      - "3600"
    imagePullPolicy: IfNotPresent
    name: busybox
  restartPolicy: Always

Create the Pod as follows:

[vagrant@fed-master ~]$ kubectl create -f ./busybox.yaml
pod "busybox" created

Wait until the Pod is Running:

[vagrant@fed-master ~]$ kubectl get pods
NAME               READY     STATUS    RESTARTS   AGE
busybox            1/1       Running   0          2m

Now run kubectl exec to execute the busybox image, passing a command to it:

[vagrant@fed-master ~]$ kubectl  exec busybox -- ifconfig

 You should be able to see the output of the ifconfig command, executed on the Container:

eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:04  
          inet addr:172.17.0.4  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe11:4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:10 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:816 (816.0 B)  TX bytes:258 (258.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

 Congratulations! You have just created your first Kubernetes application!

 


Advertisement

Cookie Alert