Skip to content

Shihab's Blog Posts

Why I like golang? – 1

Multiple Variable Return in Function

A function can return any number of results in Golang. Consider the following program as an example.

package main

import "fmt"

func swap(x string, y int) (int, string) {
	return y, x
}

func main() {
	a, b := swap("Hello", 1)
	fmt.Println(a, b)
}
Output: 1 Hello

In the above program you are passing one string and one int from main function to swap() function. And swap function will return two different datatype’s variable at a time. This is so amazing. Even JAVA can not do it that simple. If you want to return multiple values in JAVA, then you have to encapsulate them into a class and then return an object of that class.

Reference

https://tour.golang.org

Leave a Comment

Node.js – Beginner to Pro (Basic Ideas of Node.js)

What is Node.js?

Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js is not a programming language. It is only a runtime for JavaScript. Previously, JavaScript only worked in client side. But by using Node.js, JavaScript also can handle server side requests now.

What is Chromes V8 JavaScript Engine?

V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++. It is used in Chrome and in Node.js, among others. It implements ECMAScript and WebAssembly, and runs on Windows 7 or later, macOS 10.12+, and Linux systems that use x64, IA-32, ARM, or MIPS processors. V8 can run standalone, or can be embedded into any C++ application.

How Node.js works in server side?

Node.js pass the JavaScript code to V8 engine and V8 engine process the code and send the result to Node.js. Node.js and V8 JavaScript Engine are both are written in C++. So V8 Engine can directly interact with machine and the operating system. Google Chrome also use V8 engine to render JavaScript code.

How Node.js uses non-blocking I/O?

Non-blocking I/O defines, when a browser or client sends the JavaScript code to V8 engine, the browser won’t block any I/O operation by waiting for the response.

Here is an example of Blocking I/O.

const getUserSync = require('./src/getUserSync')

const userOne = getUserSync(1)
console.log(userOne)

const userTwo = getUserSync(2)
console.log(userTwo)

const sum = 1 + 33
console.log(sum)

For the above code, it will first fetch the first user. So second user will wait for fetching the first user. After fetching first user, it will fetch the second user. At last it will calculate the sum.

Here is an example of Non-Blocking I/O.

const getUserSync = require('./src/getUserSync')

getUser(1, (user) => {
    console.log(user)
})

getUser(2, (user) => {
    console.log(user)
})

const sum = 1 + 33
console.log(sum)

For the above code, calculating sum will not wait for any of the user fetching. So if the data fetching is getting late, it will print the sum first and after that it will print the user data in a order they are fetched.

That’s all for today. I hope you will enjoy this series. If you have any question please do not hesitate to write in comment.

References

https://nodejs.org/en/

https://v8.dev/

2 Comments

Configure Docker, Kubernetes with Dashboard and Nginx on Ubuntu 18.04 LTS

Kubernetes is an open source platform for managing containerized applications. It allows you to manage, scale, and automatically deploy your containerized applications in the clustered environment. Kubernetes is developed by Google.

With Kubernetes, you can orchestrate containers across multiple hosts, scale the containerized applications with all resources on the fly, and have a centralized container management environment.

In this tutorial, I will show you step-by-step how to install and configure Kubernetes on Ubuntu 18.04. We will be using 1 server ‘k8s-master’ as the Kubernetes Host Master, and 2 servers as Kubernetes workers, ‘worker01’ and ‘worker02’.

Prerequisites

  • 3 Ubuntu Servers
  • 10.0.15.10  k8s-master
  • 10.0.15.21  worker01
  • 10.0.15.22  worker02
  • Root privileges

Step 1 – Kubeadm Installation

In this first step, we will prepare those 3 servers for Kubernetes installation, so run all commands on the master and worker nodes.

We will prepare all servers for Kubernetes installation by changing the existing configuration on servers, and also installing some packages, including docker and kubernetes itself.

Setup Hosts

Setup Hostname of each PC.

nano /etc/hostname

Put the name of the host for each PC. Then save and exit.

Now Setup static IP for each PC.

nano /etc/network/interfaces

Paste the code given bellow. Just change the IP according to the PC.

auto lo
iface lo inet loopback

auto enp0s8
iface enp0s8 inet static
address 10.0.15.10

Edit host file of each node including k8s-master

sudo nano /etc/hosts

Paste hosts configuration below.

10.0.15.10  k8s-master
10.0.15.21  worker01
10.0.15.22  worker02

Save and exit.

Now test ping all servers hostname.

ping -c 3 k8s-master
ping -c 3 worker01
ping -c 3 worker02

Make sure all IP address get resolved as a hostname and you get successful ping response.

Install Docker

In this tutorial, we will install Docker from the Ubuntu repository.

Install Docker using the apt command below.

apt-get install -y docker.io

After the installation is complete, start the docker service and enable it to launch everytime at system boot.

sudo systemctl start docker
sudo systemctl enable docker

Docker installation has been completed.

Disable SWAP

In order to set up the Kubernetes Linux servers, we need to disable the SWAP.Check the swap list and disable it.

sudo swapon -s
sudo swapoff -a

To disable the SWAP permanently, we need to edit the ‘/etc/fstab’ file.

sudo nano /etc/fstab

Make a comment on the SWAP partition type.

#/dev/mapper/hakase--labs--vg-swap_1 none            swap    sw              0       0

Save and exit, then reboot the system.

sudo reboot

Install Kubeadm Packages

In this tutorial, we will be using Kubeadm packages to set up the Kubernetes Cluster. We will install the Kubeadm packages from the official Kubernetes repository.

Install apt-transport-https.

sudo apt install -y apt-transport-https

Add the Kubernetes Key.

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

And add the Kubernetes Repository by creating a new repo.list file on the ‘/etc/apt/sources.list.d’ directory.

cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF

paste kubernetes repository below.

deb http://apt.kubernetes.io/ kubernetes-xenial main

Note:

We’re still using the Xenial Ubuntu 16.04 repository for our Kubeadm Installation.

Now update the repository and install kubeadm packages using apt commands below.

sudo apt update
sudo apt install -y kubeadm kubelet kubectl

Wait for kubeadm packages installations.

Edit 10-kubeadm.conf

nano /etc/systemd/system/kubelet.service.d/10-kubeadm.conf 

Add the line given bellow.

Environment="cgroup-driver=systemd/cgroup-driver=cgroupfs"

Reboot all machines.

Step 2 – Kubernetes Cluster Initialization

In this step, we will initialize Kubernetes on the ‘k8s-master’ node. Run all commands in this stage only on the ‘k8s-master’ server. Initialize the Kubernetes cluster using the kubeadm command below.

sudo kubeadm init --pod-network-cidr=10.244.10.0/16 --apiserver-advertise-address=10.0.15.10 --kubernetes-version=v1.14.2

Note:

  • –apiserver-advertise-address = determines which IP address Kubernetes should advertise its API server on.
  • –pod-network-cidr = specify the range of IP addresses for the pod network. We’re using the ‘flannel’ virtual network. If you want to use another pod network such as weave-net or calico, change the range IP address.

When the Kubernetes initialization is complete, you will get a kubeadm join … … …‘ command . Copy the ‘kubeadm join … … …‘ command to your text editor. The command will be used to register new worker nodes to the kubernetes cluster.

Now in order to use Kubernetes, we need to run some commands as shown in the result.

Create new ‘.kube’ configuration directory and copy the configuration ‘admin.conf’ from ‘/etc/kubernetes’ directory.

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

Next, deploy the flannel network to the kubernetes cluster using the kubectl command.

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

The flannel network has been deployed to the Kubernetes cluster.

Wait for a minute and then check kubernetes node and pods using commands below.

kubectl get nodes
kubectl get pods -o wide --all-namespaces

And you will get the ‘k8s-master’ node is running as a ‘master’ cluster with status ‘ready’, and all ‘kube-system’ pods that are needed for the cluster is up and running.

Kubernetes cluster master initialization and configuration has been completed.

Step 3 – Adding Worker Nodes to the Kubernetes Cluster

In this step, we will add two node workers ‘worker01’ and ‘worker02’ to the Kubernetes Cluster.

Connect to the ‘worker01’ server and run the kubeadm join command that you get from the cluster initialization.

kubeadm join 10.0.15.10:6443 --token daync8.5dcgj6c6xc7l8hay --discovery-token-ca-cert-hash sha256:65a3e69531d323c335613dea1e498656236bba22e6cf3d5c54b21d744ef97dcd

Connect to the ‘worker02’ server and run the kubeadm join command that you get from the cluster intialization..

kubeadm join 10.0.15.10:6443 --token daync8.5dcgj6c6xc7l8hay --discovery-token-ca-cert-hash sha256:65a3e69531d323c335613dea1e498656236bba22e6cf3d5c54b21d744ef97dcd

Wait for some minutes and back to the ‘k8s-master’ node master and check node status.

kubectl get nodes

You will see those worker nodes ‘worker01’ and ‘worker02’ are part of the Kubernetes Cluster.

Create Dashboard

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/master/aio/deploy/recommended/kubernetes-dashboard.yaml

Start Proxy

kubectl proxy

Now open this link with your browser.

http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/

Create Service Account Dashboard

kubectl create serviceaccount dashboard -n default

cluster role binding

kubectl create clusterrolebinding kubernetes-dashboard --clusterrole=cluster-admin --serviceaccount=kube-system:kubernetes-dashboard

Generate token

kubectl -n kube-system get secret

kubectl -n kube-system describe secret kubernetes-dashboard-token-p8bsj

Now you will get a token. Use this token to login http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/

Configure NGINX

kubectl create deployment nginx --image=nginx

kubectl get deployments

kubectl describe deployment nginx

kubectl create service nodeport nginx --tcp=80:80

Now you will get a port number. Use that port to access the nginx webserver.

http://k8s-master:31555/

The Kubernetes Cluster installation and configuration on Ubuntu 18.04 has been completed successfully.

Reference

Leave a Comment