Giter VIP home page Giter VIP logo

aws-eks-mongodb-replicaset's Introduction

How to deploy .NET 6 WebAPI to AWS EKS cluster

0. Create a .NET 6 WebAPI with Visual Studio Community edition

image

image

Set the project name and the location

image

Set the project features. IMPORTANT: Enable Docker suppoort for automatically create the Dockerfile

image

Add the kubernetes manifest files to the project: deployment.yml and service.yml

image

1. Run Docker Desktop

image

Enable Kubernetes

image

image

image

2. Create .NET 6 Web API Docker image and upload to AWS ECR

Navigate to AWS ECR and create a public repo to store the .NET 6 WebAPI Docker image

image

Click on the created repo name and press the button View push commands

image

aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/x6y4g2f4
docker build -t dotnet6webapi .
docker tag dotnet6webapi:latest public.ecr.aws/x6y4g2f4/dotnet6webapi:latest
docker push public.ecr.aws/x6y4g2f4/dotnet6webapi:latest

3. Create AWS EKS (Elastic Kubernetes Cluster)

eksctl create cluster ^
--name luiscocoenriquezdotnet6webapi-cluster ^
--version 1.25 ^
--region eu-west-3 ^
--nodegroup-name linux-nodes ^
--node-type t2.micro ^
--nodes 4

NOTE: if version 1.25 is not yet working in your laptop use version 1.24.

The AWS Kubernetes cluster creation takes around or more than 1 hour

NOTE: If you get an error during the cluster creation due to name is not unique, delete the cluster with the following command and input a new cluster name

eksctl delete cluster --region=eu-west-3 --name=luiscocoenriquezdotnet6webapi-cluster

4. Get and Set AWS Kubernetes Cluster context

kubectl config get-contexts

image

If you would like to delete a context

kubectl config delete-context luisnewuser@luiscocoenriquezdotnet6webapi-cluster.eu-west-3.eksctl.io

To select a cluster where to deploy applications, run the command:

kubectl config use-context luisnewuser@luiscocoenriquezdotnet6webapi-cluster.eu-west-3.eksctl.io

5. Verify the kubenetes parameters

We create a new namespace "dev"

kubectl create namespace dev

We verify the nodes

kubectl get nodes

We verify the namespaces

kubectl get ns

We verify the services, nodes, namespaces and pods

kubectl get all

We verify the pods

kubectl get pods
kubectl get all --namespace dev
kubectl get nodes --namespace dev
kubectl get ns --namespace dev
kubectl get pods --namespace dev

6. Write the deployment.yml file to deploy the Kubernetes cluster

This is the source code for the deployment.yml file:

IMPORTANT:

In this file do not forget to set the docker image in AWS ECR

image

Set this image URL in the deployment.yml file

spec:
      containers:
      - name: webapidotnet8
        image: public.ecr.aws/x6y4g2f4/dotnet6webapi:latest
        ports:
        - containerPort: 8080

Also it is very important to set the environmental variable.

Get the environmental variables values from the launchSettings.json file:

image

and set it in deployment.yml file

 env:
            - name: ASPNETCORE_ENVIRONMENT
              value: Development

This is the deployment.yml source code:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapidotnet6-deployment
  labels:
    app: webapidotnet6
spec:
  replicas: 1
  selector:
    matchLabels:
      app: webapidotnet6
  template:
    metadata:
      labels:
        app: webapidotnet6
    spec:
      containers:
        - name: webapidotnet6
          image: public.ecr.aws/x6y4g2f4/dotnet6webapi:latest
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80
          env:
            - name: ASPNETCORE_ENVIRONMENT
              value: Development
          resources:
            requests:
              memory: "64Mi"
              cpu: "250m"
            limits:
              memory: "128Mi"
              cpu: "500m"

IMPORTANT NOTE: take in consideration if you include in your application a connection string to a database the corresponding environmental variable also should be set (see this example)

 env:
            - name: ASPNETCORE_ENVIRONMENT
              value: Development
            - name: ConnectionStrings__FleetManagementDb
              valueFrom:
                  configMapKeyRef:
                    name: mongo-configmap
                    key: connection_string

mongo-configmap.yml

apiVersion: v1
kind: ConfigMap
metadata:
  name: mongo-configmap
data:
  #connection_string: mongodb://username:password@mongo-service:27017
  #connection_string: mongodb://mongodb-service:27017
  #connection_string: mongodb://mongod-0.mongodb-service.default.svc.cluster.local:27017,mongod-1.mongodb-service.default.svc.cluster.local:27017,mongod-2.mongodb-service.default.svc.cluster.local:27017
  connection_string: mongodb://mongod-0.mongodb-service.dev.svc.cluster.local:27017,mongod-1.mongodb-service.dev.svc.cluster.local:27017,mongod-2.mongodb-service.dev.svc.cluster.local:27017
  #connection_string: mongodb://mongod-0.mongodb-service.development.svc.cluster.local:27017,mongod-1.mongodb-service.development.svc.cluster.local:27017,mongod-2.mongodb-service.development.svc.cluster.local:27017

7. This is the service.yml file to deploy the Kubernetes cluster

*service.yml

apiVersion: v1
kind: Service
metadata:
  name: webapidotnet6-service
spec:
  type: LoadBalancer
  selector:
    app: webapidotnet6
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

8. Deploy the kubernetes manifest files (deployment.yml and service.yml)

kubectl apply -f deployment.yml --namespace dev
kubectl apply -f service.yml --namespace dev

9. Verify the Web API endpoint

image

aws-eks-mongodb-replicaset's People

Contributors

luiscoco avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.