Thursday, December 26, 2024
Google search engine
HomeGuest BlogsBlockchain and Deploying Applications on Docker and Kubernetes

Blockchain and Deploying Applications on Docker and Kubernetes

This article was published as a part of the Data Science Blogathon.

Introduction

Niti Ayog, one of the transforming national institutions, has published an article on Blockchain use cases in India. Few questions about Blockchain, why Blockchain, and how we can deploy our applications through the docker and Kubernetes we should know.

Objectives

We will discuss in this article Blockchain and deployment of applications on the Docker Hub and Kubernetes. I hope you all will be interested to know about this in detail.

What is Blockchain?

Blockchain, the word suggests each data of ours is stored in Blocks by a technique called cryptography or encryption. It changes our digital data into codes that are not readable by everyone. The digital data gets added in the form of a Block. When one block of the data is stored, and no space is left, another block gets ready for storing the data.

Why do we need it?

To stop fraud and secure our data, we need Blockchain technology.

Some of the applications of Blockchain in our day-to-day life

1. Cybersecurity

Blockchain can play a vital role in security systems with its decentralized solutions means not a single person is the leader but many authorities. For the centralized system, only one is a head leader.

2. Medical Healthcare

It is a hassle for patients to keep medical records, but they can upload them to Blockchain and update it. It is beneficial in both ways for patients and doctors to have a look at the medical record.

3. In the Lands registry

We have seen many fraud cases in lands and properties, and with the help of Blockchain technology, we can save it to some extent.

4. In Financial Security

When we have to transfer money online overseas, banks use one of the cryptocurrencies (Digital currency not controlled by the so-called government) called a ripple. If you are involved in any charity work and a donation online, you must also want to know where your hard-earned money went. In this case, blockchain technology can give insight as it provides transparency.

Now the question arises in our mind what cryptocurrencies are in actuality?

Cryptocurrencies

Cryptocurrencies are digital currencies used to buy or sell goods online or transfer money and have no connection with the government. It is also referred to as a decentralized currency because of the role of government.

Bitcoin and Ethereum are old Cryptocurrencies, and almost more than a thousand cryptocurrencies occur in the world.

What is Bitcoin?

It is one of the oldest digital currencies, can be used globally without the involvement of any govt or financial institutions, and uses a decentralized protocol. In other words, it is a peer-peer exchange of values.

What is Ethereum?

Ethereum is blockchain technology after Bitcoin in the market that uses Ether digital currency based on a decentralized protocol.

 

Blockchain

Now the question arises how is it created?

How are cryptocurrencies created?

So how do cryptocurrencies are created? It is software that runs on Blockchain. It is decentralized and distributed on various servers of one company where data in the form of code exists.

Most new cryptocurrencies occur when the transaction is confirmed, and the process is known as mining. It’s not necessary that cryptocurrencies by mining only exist; they can use other technology.

Docker and Blockchain

To make our applications or websites run, we used to need a LAMP, but if more traffic is, how we make our website or application run?

Docker provides the sources for running our application.

We will now use the docker applications to deploy our application which consists of HTML, CSS, and JavaScript files.

We have made the Employer Applications form Employment Applications (codepen.io)

How to install Docker Desktop on windows?

We can install the Docker application on window10 pro by going to the website Docker Desktop for Windows by Docker | Docker Hub.

docker

We can uninstall on windows by going to the setting, selecting Apps and features, and uninstalling by right clicking the docker app. However, the new version will be there only if the previous version from windows doesn’t exist. The docker engine can be removed by going to Task Manager and ending the task of the docker engine. All the files will be easily removed by going to Program Files and deleting the folder by going to the app and features.

In the terminal type docker run -d -p 80:80 docker/getting-started.

It will download the newer images.

C:>docker run -d -p 80:80 docker/getting-started
Unable to find image 'docker/getting-started: latest' locally
latest: Pulling from docker/getting-started
97518928ae5f: Pull complete
a4e156412037: Pull complete
e0bae2ade5ec: Pull complete
3f3577460f48: Pull complete
e362c27513c3: Pull complete
a2402c2da473: Pull complete
eb65930377cd: Pull complete
69465e074227: Pull complete
Digest: sha256:86093b75a06bf74e3d2125edb77689c8eecf8ed0cb3946573a24a6f71e88cf80
Status: Downloaded newer image for docker/getting-started:latest
2dca1872873605d54ba811c57838c0dff183220c9a1acd5fc6bea114cea897e2
kubernets

 

Blockchain

 

After it opens the window shell, type docker ps, and docker version.

We will make a folder with the name docker test where the dockerfile will be and then another folder called HTML where we will keep our HTML files.

To create a Docker file in Windows echo. > Dockerfile
and type
>>>FROM httpd:2.4
COPY ./public-HTML/ /usr/local/apache2/htdocs/
On command-line write
>>>docker build -t my-apache2.
After it types in the command line
>>>docker run -dit -p 8080:80 --name my-running app my-apache2
Now we will open our Docker Desktop.
We can see the my-running-app running. One can give another name of choice.
We can see that our application is running on localhost:8080.
Blockchain

After the docker hub, we will go to Kubernetes in detail.

What is Kubernetes?

Kubernetes is the most widely used open-source container engine in the world. It allows you to schedule and manage containers. It can run on a desktop, on-premises, or in the cloud because it is open-source. It makes it simple to move your workload across these distinct contexts.

Why Blockchain on Kubernetes (BoK)?

Azure, Evernote, and Shopify use Kubernetes.

Kubernetes provides easy deployment for Blockchain applications to run the easy deployment, and services are necessary.

It provides interoperability, and blockchain applications that run on Kubernetes can run on different architecture styles hosted by the host. IBM and AWS are some examples that offer templates through Kubernetes for creating blockchain networks.

Difference between Kubernetes and Docker

Deployment of applications occurs in pods, deployment forms, and services, whereas on Docker hub occurs in the form of only services.

Setup and installation are not much easy, whereas, on the docker hub, it’s easy.

Azure and Shopify are examples of Kubernetes, whereas Google and Amazon are some examples of Docker hub.

Kubernetes works with the docker for smooth running and better control of containerized applications.

Code Work

We will first learn to use Minikube to get a Kubernetes environment up and running on Windows. Then, using the Kubernetes dashboard, launch an example Kubernetes application and manage it.

Importance of Kubectl

The Kubernetes Cluster gets controlled through Kubectl. When the setup is complete, it is one of the fundamental components of Kubernetes, and it operates on any operating workstation. It manages the nodes. Kubernetes objects, the cluster is managed by using the commands of Kubectl.

Installing Kubectl

You can download the latest release from the recent release version v1.23.0.

If you have curl installed, you can go to the command line.

kubectl checksum file

compare CertUtil’s output to the checksum file

CertUtil -hashfile kubectl.exe SHA256
type kubectl.exe.sha256

Checking the version

kubectl version --client

The tutorial can be found on Install and Set Up kubectl on Windows | Kubernetes.

Kubectl Configuration  
kubectl cluster-info

It will throw an error if kuberctl configuration is not correct.

We have to install minikube before going further.

Importance of Minikube

Minikube enables Kubernetes run locally. minikube creates a single-node Kubernetes cluster on your computer (including operating systems Microsoft Windows and macOS so you may experiment with Kubernetes or do daily development work.

Installing Minikube

Minikube we have installed from the website Release v1.2.0 · kubernetes/minikube (github.com).

Blockchain

 

We will copy the Kubectl and Minikube on C drive to have the same path. Hyper-V is required. Once installed, open the Hyper-V Manager and click Virtual Switch Manager.

kubernets

 

Let’s make the new name minikube. After it chooses an internal network, apply and hit the button OK.

kubernets

 

Afterward, open the control panel, click on Network and Internet, Network and Sharing Center, and then click on New Virtual Switch and Properties. Then goes to tab sharing and allows it to connect by selecting and choosing minikube and hitting the button OK.

Finally, on the Command line, type minikube start

Before typing, make sure Docker Daemon is running. Otherwise, it will show an error.

C:>minikube start

After configuring type

C:>kubectl get nodes
NAME       STATUS   ROLES                  AGE    VERSION 
minikube   Ready    control-plane,master   2m3s   v1.22.3 
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE 
service/kubernetes   ClusterIP   10.96.0.1            443/TCP   4m43s
C:>kubectl get all

Running First Application Hello World

C:>git clone https://github.com/skynet86/hello-world-k8s.git
C:>cd hello-world-k8s/
helloworld.yaml file contents 
apiVersion: apps/v1 
kind: Deployment 
metadata: 
  name: hello-world-deployment 
  labels: 
    app: hello-world 
spec: 
  selector: 
    matchLabels: 
      app: hello-world 
  replicas: 2

Where the “name” parameter indicates that a Deployment named “hello-world-deployment” is created.

The replicas column means two replicated Pods.

The “selector” column indicates how the Deployment determines which Pods it should manage.

apiVersion: v1 
kind: Service 
metadata: 
  name: hello-world 
spec: 
  selector: 
    app: hello-world 
  ports: 
    - protocol: TCP 
      port: 80 
      targetPort: 80 
      nodePort: 30081    
 type: NodePort 
kubectl create -f hello-world.yaml

It creates the “hello-world” service. We are exposing our application at a node using this service. The “type” field specifies the service type. Node 30081 -> Service 80 -> Pod/container 80 are the ports. You can visit the application from your local machine by going to http://localhost:30081.

You can also create your file example we created happybirthday.yaml

apiVersion: apps/v1 
kind: Deployment 
metadata: 
  name: happybirthday 
spec: 
  selector: 
    matchLabels: 
      app: happybirthday 
    metadata: 
      labels: 
        app: happybirthday 
    spec: 
      containers: 
      - name: happybirthday 
        image: karthequian/happybirthday:latest 
        ports: 
        - containerPort: 80 
--- 
apiVersion: v1 
kind: Service 
metadata: 
  name: happybirthday 
spec: 
  selector: 
    app: happybirthday 
  ports: 
    - protocol: TCP 
      port: 80 
      targetPort: 80 
      nodePort: 32700    
 type: NodePort 
Note: NodePort can be selected between 30000- 32727. 
C:hello-world-k8s>kubectl create -f happybirthday.yaml 
deployment.apps/happybirthday created
C:>kubectl get all 
NAME                                         READY   STATUS    RESTARTS   AGE 
pod/happybirthday-584b65cd4-fvhjh            1/1     Running   0          20m 
pod/hello-world-deployment-b9558ff44-24hn7   1/1     Running   0          35m 
pod/hello-world-deployment-b9558ff44-5jbqs   1/1     Running   0          35m 
NAME                    TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE 
service/happybirthday   NodePort    10.111.3.87            80:32700/TCP   30s 
service/hello-world     NodePort    10.108.12.73           80:30081/TCP   35m 
service/kubernetes      ClusterIP   10.96.0.1              443/TCP        79m 
NAME                                     READY   UP-TO-DATE   AVAILABLE   AGE 
deployment.apps/happybirthday            1/1     1            1           20m 
deployment.apps/hello-world-deployment   2/2     2            2           35m 
NAME                                               DESIRED   CURRENT   READY   AGE 
replicaset.apps/happybirthday-584b65cd4            1         1         1       20m 
replicaset.apps/hello-world-deployment-b9558ff44   2         2         2       35m 
C:>minikube service list 
C:>minikube service happybirthday
Blockchain

Application of Kubernetes

Kubernetes allows several containers to run at the same time. A developer constructs a Deployment for these containers, defines the number of replicas, then presents a set of instructions humans can understand.

Supported by multiple cloud infrastructures and can run anywhere. It saves money since it optimizes infrastructural resources by using hardware for companies that use K8s.

It monitors the health of the nodes and containers at all times. According to the Container survey, half of the respondents use Kubernetes in their production environment.

Kubernetes and Amazon EC2

Kubernetes uses deployment, maintenance, and scaling processes to manage clusters for Amazon EC2, compute instances, and run containers on these instances.

It allows you to execute any containerized application using the same toolset. Amazon Elastic Kubernetes Services (EKS) manage the Kubernetes control plane.

Kubernetes and Apache Spark

Apache Spark is a distributed computing framework that is free and open-source. With the help of a cluster of machines, Spark can manage large data sets.

Real-world example

We will use kubectl apply minus f to deploy some example infrastructure from LinkedIn learning exercise files, and we have a file called sample-infrastructure .yaml in Exercise Files. Let us put that to the test. It is generating namespaces and deploying a slew of pods.

kubectl apply -f sample-infrastructure.yaml

So, when you run kubectl get pods after this has deployed, it looks a little strange since we just produced pods, but we don’t see any pods over here. It’s because we’ve deployed pods in various namespaces in this case. Let’s take a look at the namespaces we’ve made so far.

C:hello-world-k8s>kubectl get pods 
NAME                                     READY   STATUS    RESTARTS      AGE 
happybirthday-584b65cd4-fvhjh            1/1     Running   1 (13m ago)   3d7h 
hello-world-deployment-b9558ff44-24hn7   1/1     Running   1 (13m ago)   3d8h 
hello-world-deployment-b9558ff44-5jbqs   1/1     Running   1 (13m ago)   3d8h
We will execute this on a new screen with Kubectl to obtain namespaces. As a result, various namespaces, such as auth, cart, and catalog, were formed 40 seconds ago. Kube-public, Kube-system, and others have only recently existed. Some things to note here are that the default namespace will always pull data from the default namespace when you execute commands like kubectl get pods, etc. Kubernetes or Minikube on your machine gets installed by default.
C:hello-world-k8s>kubectl get namespaces 
NAME              STATUS   AGE 
auth              Active   6m41s 
cart              Active   6m41s 
catalog           Active   6m40s 
default           Active   3d8h 
infra             Active   6m38s 
kube-node-lease   Active   3d8h 
kube-public       Active   3d8h 
kube-system       Active   3d8h 
purchasing        Active   6m38s 
quote             Active   6m39s 
social            Active   6m41s 
web               Active   6m42s

If you run Kubectl, get pods and choose the cart namespace, you will see a minus n for the namespace, and the namespace will be called cart. We can also use kubectl to get pods and minus all dash namespaces to see it for all namespaces. It returns all infrastructures, including heapster, influxdb, and other services operating in the Kube-system namespace, other namespaces, quotation, social, and so on.

kubectl get pods -n cart
NAME           READY   STATUS    RESTARTS   AGE
cart-dev       1/1     Running   0          13m
cart-prod      1/1     Running   0          13m
cart-staging   1/1     Running   0          13m
C:hello-world-k8s>kubectl get pods --all-namespaces

We can use kubectl to get pods without O. It gives us more information, specifically IP and node information. Other output formats are also available.

C:hello-world-k8s>kubectl get pods -o wide --all-namespaces

Conclusion

Blockchain is an emerging technology that can prevent fraud. We can run the application on the docker and Kubernetes machines. In this article, our objective was to deploy the application through Docker and Kubernetes.

Key Takeaways

1. We discuss in detail the meaning of Blockchain and why we need Blockchain. You must now know how to define the Blockchain, where our data gets stored in the form of Blocks.

2. Uses of Blockchain in various Industries. We get to see in the Medical, Bank sector where we can in maximum amount prevent fraud from happening.

3. We saw how to deploy our application on the docker hub in windows. It is easier once the docker gets installed on windows.

4. We mentioned the definition of Cryptocurrencies, how some have formed, and a few examples of cryptocurrencies, Bitcoin and Ethereum.

5. We mentioned Kubernetes and how to install it step by step. It is not easy to install, but going step by step will not be difficult either.

6. We discuss the difference between Kubernetes and the docker hub.

7. We took some terms named Minikube and Kubectl. After going through the article, you must now be able to define them, why we need to install and about Kubetcl.

8. Why do we need Kubernetes? We can see the uses of Kubernetes along with the docker engine.

9. We used some present real-world examples.

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

Sonia Singla

04 Nov 2022

RELATED ARTICLES

Most Popular

Recent Comments