How to setup kubernete

ngovu.dl@gmail.com | 416 day | 531


Note: At the moment, Micro-Services-Architect (MSA) is very popular, and if you don't know about it, please access this link to get information, and the image below is an MSA.

 c-sharp.vn

Like the image above, we have a lot of services, so how to deploy maintain and scale-out it. Lucky, Google has been developed an open-source to help solve it, it is Kubernetes.

What is Kubernetes?

Kubernetes (K8s) is an open-source system for automating deployment, scaling, and management of containerized applications.

How to do it?

First of all, we need 2 or 3 Linux-machines due to k8s only work with Linux. You can use VirtualBox or Hyper-V to create a virtual machine.

Linux for Hyper-v.

Linux for virtual-box.

At the moment, I will practice with Hyper-v.

  1. Setup a virtaul-network like below.

c-sharp.vn

2. Create a Linux-virtual-Machine.

c-sharp.vn

3. After done, you can access the machine, and make all steps like below.

First, login as ‘sudo’ user because the following set of commands need to be executed with ‘sudo’ permissions. Then, update your ‘apt-get’ repository.

$ sudo su
# apt-get update

Note: After logging-in as ‘sudo’ user, note that your shell symbol will change to ‘#’ from ‘$’.

Turn Off Swap Space:

Next, we have to turn off the swap space because Kubernetes will start throwing random errors otherwise. After that you need to open the ‘fstab’ file and comment out the line which has mention of swap partition.

# swapoff -a

Install Docker:

Now we have to install Docker because Docker images will be used for managing the containers in the cluster. Run the following commands

# sudo su
# apt-get update 
# apt-get install -y docker.io

Next we have to install these 3 essential components for setting up Kubernetes environment: kubeadm, kubectl, and kubelet.

Run the following commands before installing the Kubernetes environment.

# apt-get update && apt-get install -y apt-transport-https curl
# curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
# cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
# apt-get update

Install kubeadm, Kubelet And Kubectl

Now its time to install the 3 essential components. Kubelet is the lowest level component in Kubernetes. It’s responsible for what’s running on an individual machine. Kuebadm is used for administrating the Kubernetes cluster. Kubectl is used for controlling the configurations on various nodes inside the cluster.

# apt-get install -y kubelet kubeadm kubectl 

So now, we have been done with "Kubernetes", and now we will create node like the image below.

c-sharp.vn

Like the image above we have 4 nodes, 1 master nodes, and 3 workers nodes. So, I will create a master node first with a command like below.

# kubeadm init --apiserver-advertise-address=<ip-address-of-kmaster-vm> --pod-network-cidr=192.168.0.0/16

After executing the command we will get a result like below.

c-sharp.vn

  1. You will get the below output. The commands marked as (1), execute them as a non-root user. This will enable you to use kubectl from the CLI.
$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config

    c-sharp.vn

To verify, if kubectl is working or not, run the following command:

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

c-sharp.vn

You will notice from the previous command, that all the pods are running except one: ‘kube-dns’. For resolving this we will install a pod network. To install the CALICO pod network, run the following command:

kubectl apply -f https://docs.projectcalico.org/v3.0/getting-started/kubernetes/installation/hosted/kubeadm/1.7/calico.yaml 

At the moment, we are done with master node, next, we will join the worker node like below.

kubeadm token create --print-join-command

c-sharp.vn

 Then do like the image below.

c-sharp.vn

To verify, if kubectl is working or not, run the following command:

kubectl get nodes

c-sharp.vn

Now everything is done, next We will install dash-board like below

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
kubectl proxy

 Next, we will create the service account for the dashboard and get it’s credentials.

kubectl create serviceaccount dashboard -n default

kubectl create clusterrolebinding dashboard-admin -n default 
  --clusterrole=cluster-admin 
  --serviceaccount=default:dashboard

kubectl get secret $(kubectl get serviceaccount dashboard -o jsonpath="{.secrets[0].name}") -o jsonpath="{.data.token}" | base64 --decode

You should get the token like this:

c-sharp.vn

Copy this token and paste it in Dashboard Login Page, by selecting token option

c-sharp.vn

 You have successfully logged into your dashboard!

c-sharp.vn

So it's done now, next article I will guide you on how to deploy service on k8s.


Top Articles

Bất Đầu Với WebApi Và Dot Net Core (.Net Core)

1276 day
Butter Ngo
Views 8930
Comments 0

Dot Net Core Bearer Token With (JWT) (.Net Core)

1208 day
Butter Ngo
Views 5557
Comments 0

Repository Và Unit Of Work (Entity Framework)

1195 day
ndtung449@gmail.com
Views 5038
Comments 0

Bắt Đầu Với Dot NET Core (.Net Core)

1287 day
Butter Ngo
Views 4424
Comments 0

Top Question

Bi lỗi Invalid Column Name khi sử dụng LinQ (.Net)

1135 day
Bảo Dương
Views 1337
Answers 2

.NET CORE API JWT (.Net Core)

428 day
huynhminhnhut97@gmail.com
Views 939
Answers 2

Làm thế nào để lấy information từ token (.Net Core)

525 day
ngovu.dl@gmail.com
Views 850
Answers 1