Installing Kubernetes via MicroK8s and configuring the deployment of NestJS and Angular applications

WHAT TO KNOW - Sep 14 - - Dev Community

Kubernetes, NestJS, and Angular: Building Scalable Web Applications with MicroK8s

Introduction

The modern web development landscape demands scalability, reliability, and rapid deployment. This trifecta is increasingly met by containerized applications and orchestration platforms like Kubernetes. Kubernetes, an open-source container orchestration platform, provides the tools to manage and automate the deployment, scaling, and operations of containerized applications.

This article dives into the world of Kubernetes, NestJS, and Angular, showcasing how to build and deploy robust web applications. We'll use MicroK8s, a lightweight and easy-to-install Kubernetes distribution, to set up our development environment. This setup allows us to leverage the power of Kubernetes without the complexities of managing a full-blown Kubernetes cluster.

Understanding the Key Players

Kubernetes:

At its core, Kubernetes is a powerful platform that automates the deployment, scaling, and management of containerized applications. It offers:

  • Container Orchestration: Automatically handles deployment, scaling, and lifecycle management of containerized applications.
  • Service Discovery and Load Balancing: Enables services to communicate with each other within the cluster and provides load balancing across multiple instances of an application.
  • Self-Healing: Monitors application health and automatically restarts or replaces unhealthy containers.
  • Automated Rollouts and Rollbacks: Allows for seamless updates and rollbacks, ensuring minimal downtime.

MicroK8s:

MicroK8s is a lightweight and easy-to-install Kubernetes distribution designed for single-node deployments. It's perfect for:

  • Development: Provides a local Kubernetes environment for testing and development purposes.
  • Personal Projects: Offers a quick and easy way to get started with Kubernetes for small projects.
  • Educational Purposes: Serves as a great tool for learning Kubernetes concepts and practices.

NestJS:

NestJS is a progressive Node.js framework built on top of TypeScript. It offers a powerful, scalable, and testable architecture for building efficient back-end applications.

  • TypeScript: Provides type safety and improved code organization.
  • Modularity: Encourages well-structured code with clear separation of concerns.
  • Dependency Injection: Simplifies code organization and promotes maintainability.

Angular:

Angular is a powerful framework for building client-side web applications. It offers:

  • Component-Based Architecture: Structures applications into reusable components, promoting code reusability and maintainability.
  • Data Binding: Simplifies data management and updates between components and the view.
  • Routing: Enables navigation within the application with clear and organized URLs.

Setting Up Your Development Environment with MicroK8s

1. Install MicroK8s:

  • Ubuntu/Debian:

    sudo snap install microk8s --classic
    
  • macOS (using Homebrew):

    brew install microk8s
    

2. Start MicroK8s:

```bash
sudo microk8s start
```
Enter fullscreen mode Exit fullscreen mode

3. Verify Installation:

```bash
kubectl version
```
Enter fullscreen mode Exit fullscreen mode

This will show you the Kubernetes client version and server version.

4. Configure kubectl for local access:

```bash
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
```
Enter fullscreen mode Exit fullscreen mode

5. Test your Kubernetes installation:

```bash
kubectl get nodes
```
Enter fullscreen mode Exit fullscreen mode

This should list the nodes (in this case, your local machine) in your Kubernetes cluster.

Building a NestJS Application

1. Create a NestJS Project:

```bash
nest new nestjs-app
```
Enter fullscreen mode Exit fullscreen mode

2. Develop your API endpoints:

NestJS provides a clear and well-structured framework for building REST APIs. Let's create a simple API endpoint to return "Hello World":

src/app.controller.ts:

import { Controller, Get } from '@nestjs/common';

@Controller()
export class AppController {
  @Get()
  getHello(): string {
    return 'Hello World!';
  }
}
Enter fullscreen mode Exit fullscreen mode

3. Start your NestJS server:

```bash
npm run start:dev
```
Enter fullscreen mode Exit fullscreen mode

Deploying the NestJS Application to Kubernetes

1. Create a Dockerfile:

```dockerfile
FROM node:16-alpine

WORKDIR /app

COPY package.json ./
COPY yarn.lock ./

RUN yarn install

COPY . .

EXPOSE 3000

CMD ["yarn", "start:dev"]
```
Enter fullscreen mode Exit fullscreen mode

2. Build the Docker image:

```bash
docker build -t nestjs-app .
```
Enter fullscreen mode Exit fullscreen mode

3. Create a Kubernetes deployment file (deployment.yaml):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nestjs-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nestjs-app
  template:
    metadata:
      labels:
        app: nestjs-app
    spec:
      containers:
      - name: nestjs-app
        image: nestjs-app
        ports:
        - containerPort: 3000
Enter fullscreen mode Exit fullscreen mode

4. Apply the deployment to your Kubernetes cluster:

```bash
kubectl apply -f deployment.yaml
```
Enter fullscreen mode Exit fullscreen mode

5. Expose the NestJS API as a service:

Create a service file (service.yaml):

apiVersion: v1
kind: Service
metadata:
  name: nestjs-app-service
spec:
  selector:
    app: nestjs-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 3000
  type: LoadBalancer
Enter fullscreen mode Exit fullscreen mode

6. Apply the service definition:

```bash
kubectl apply -f service.yaml
```
Enter fullscreen mode Exit fullscreen mode

7. Access your NestJS API:

You can now access your deployed NestJS API through the IP address and port assigned by the LoadBalancer service. This will be displayed by kubectl get services.

Building an Angular Application

1. Create an Angular project:

```bash
ng new angular-app
```
Enter fullscreen mode Exit fullscreen mode

2. Develop your Angular application:

Angular's component-based architecture makes it easy to build feature-rich applications. Let's create a simple component that displays a welcome message:

src/app/app.component.html:

<h1>
 Welcome to the Angular App
</h1>
Enter fullscreen mode Exit fullscreen mode

3. Run your Angular application:

```bash
ng serve
```
Enter fullscreen mode Exit fullscreen mode

Deploying the Angular Application to Kubernetes

1. Build the Angular application:

```bash
ng build --prod
```
Enter fullscreen mode Exit fullscreen mode

2. Create a Dockerfile for the Angular application:

```dockerfile
FROM nginx:latest

COPY dist/angular-app /usr/share/nginx/html

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
```
Enter fullscreen mode Exit fullscreen mode

3. Build the Docker image:

```bash
docker build -t angular-app .
```
Enter fullscreen mode Exit fullscreen mode

4. Create a Kubernetes deployment file (deployment.yaml):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: angular-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: angular-app
  template:
    metadata:
      labels:
        app: angular-app
    spec:
      containers:
      - name: angular-app
        image: angular-app
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

5. Apply the deployment to your Kubernetes cluster:

```bash
kubectl apply -f deployment.yaml
```
Enter fullscreen mode Exit fullscreen mode

6. Expose the Angular application as a service:

Create a service file (service.yaml):

apiVersion: v1
kind: Service
metadata:
  name: angular-app-service
spec:
  selector:
    app: angular-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  type: LoadBalancer
Enter fullscreen mode Exit fullscreen mode

7. Apply the service definition:

```bash
kubectl apply -f service.yaml
```
Enter fullscreen mode Exit fullscreen mode

8. Access your Angular application:

You can now access your deployed Angular application through the IP address and port assigned by the LoadBalancer service. This will be displayed by kubectl get services.

Conclusion

This article has provided a comprehensive guide to deploying NestJS and Angular applications using MicroK8s, a lightweight and user-friendly Kubernetes distribution. By leveraging the power of containerization and orchestration, we can build robust, scalable, and highly available web applications.

Key takeaways:

  • MicroK8s provides an excellent environment for development, testing, and deploying applications using Kubernetes.
  • NestJS offers a powerful framework for building scalable and maintainable Node.js back-end applications.
  • Angular is a robust framework for building client-side web applications with a focus on component-based architecture and data binding.
  • Kubernetes offers a range of features for automated deployment, scaling, and management of containerized applications.

By mastering these tools and concepts, you can build sophisticated and scalable web applications while embracing the benefits of containerization and Kubernetes orchestration.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player