Skip to content

VikashChauhan51/quotes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DotNet Core 7 Web API Sample

Sample HATEOS API with swagger OpenAPI documentation and Identity Server.

Pre-requisite

  • Engineering laptop or desktop computer with 16-64GB RAM and Windows 10 or later.
  • System administrator rights on laptop or desktop computer.

Steps to setup local development environment on Windows 11

Install Chocolatey

  • For existing choco, upgrade it

    # Execute on PowerShell in admin mode
        choco upgrade Chocolatey -y
  • For new installation with windows command prompt [CMD]

      #Execute on CMD in admin mode
    
      @"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "[System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
    
  • For new installation with PowerShell

      # Execute on PowerShell in admin mode
    
      Set-ExecutionPolicy Bypass -Scope Process -Force; 
      [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
    

Install Dotnet 7 SDK

  • For manual installation, download and install it.

  • For existing dotnet sdk, upgrade it

    # Execute on PowerShell in admin mode
    choco upgrade dotnet-7.0-sdk -y
  • With Chocolatey

    # Execute on PowerShell in admin mode
    choco install dotnet-7.0-sdk -y

Install Git

  • For manual installation, download and install it.

  • For new installation with Chocolatey, Reference.

    # Execute on PowerShell in admin mode
    choco install git -y

Install WSL

  • Install WSL2 on Windows 10

      # Execute on PowerShell in admin mode
      wsl --install
  • Restart your computer to finish the WSL installation on Windows 10 or later.

  • Set up your Linux username and password, Reference.

  • Confirm platform

      # Execute on PowerShell in admin mode
        wsl --list --verbose
        or
        wsl -l -v
  • Enable Windows Subsystem for Linux 2, Ignore if already set

      # Execute on PowerShell in admin mode
      wsl --set-default-version 2
      or
      wsl --set-version Ubuntu 2
  • Reference to install WSL2 on windows 10 or later.

Install Docker Desktop

  • For manual installation, download and install it.

  • With Chocolatey, Docker Desktop and Docker Engine,

    # Execute on PowerShell in admin mode
    choco install docker-desktop -y
  • After installation,restart your system and run the Docker desktop.

  • Enable Kubernetes(Setting->Kubernetes->Enable Kubernetes).

    • Enable kubernets
    • Docker with kubernets

    Note: After installation, keep docker desktop in running state.

Install Kubernetes cli (kubectl)

  • For existing kubectl, upgrade it

    # Execute on PowerShell in admin mode
     choco upgrade Kubernetes-cli -y 
    
    # Verify installation
     kubectl version --client
  • For new installation.

    # Execute on PowerShell in admin mode
     choco install kubernetes-cli -y
    
    # Verify installation
     kubectl version --client
  • Reference document for installation.

Install Helm cli (helm)

  • For new installation.

    # Execute on PowerShell in admin mode
     choco install kubernetes-helm -y
    
    # Verify installation
     helm version
  • For existing helm, upgrade it

    # Execute on PowerShell in admin mode
    choco upgrade Kubernetes-helm -y
    
    # Verify installation
     helm version
  • Reference document for installation.

Install Sql Server Management Studio(SSMS)

  • Manually download and install it.

  • With Chocolatey.

    # Execute on PowerShell in admin mode
     choco install ssms -y
  • Start docker desktop [Ignore if it's already running].

  • Start SSMS and try to connect with sql server:

    Server: localhost,1433 User: sa Password: Welcome@123

Install Visual Studio

  • For manual installation, download and install it [Recommended].

  • For new installation with Chocolatey, Reference.

    choco install visualstudio2022community
    # or
    choco install visualstudio2022community --package-parameters "--allWorkloads --includeRecommended --includeOptional --passive --locale en-US"

Install Windows Terminal

  • With Chocolatey.

    # Execute on PowerShell in admin mode
     choco install microsoft-windows-terminal -y
  • Reference document to set up it.

Install PowerShell 7

  • For manual installation, Download and install it.

  • With Chocolatey.

    # Execute on PowerShell in admin mode
     choco install powershell-core -y

Install OpenSsl

  • For manual installation, Download and install it.

  • With Chocolatey.

    # Execute on PowerShell in admin mode
     choco install openssl -y

Install Github Desktop

  • For manual installation, download and install it.

  • For new installation with Chocolatey, Reference.

    choco install github-desktop -y
  • Configure it with your github account.

    Note: Use any Git UI tool as per your choice.

Install Lens for Kubernetes

  • For manual installation, download and install it.

  • For new installation with Chocolatey, Reference.

    # Execute on PowerShell in admin mode
    choco install lens -y

Install the Dapr CLI

  • For new installation.

    # Execute on PowerShell in admin mode
     Set-ExecutionPolicy RemoteSigned -scope CurrentUser;
     powershell -Command "iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex"
    
    
    # Verify installation (run this in new terminal)
     dapr
  • For existing dapr, upgrade it

    # Execute on PowerShell in admin mode
     dapr upgrade

Dapr initialization in self-hosted mode

# Execute on PowerShell in admin mode
dapr init
# or
dapr init --slim
# Verify Dapr version
dapr --version

Here, dapr self-hosted mode initialization created dapr_redis,dapr_zipkin. You need to delete them for forever as we will deploy these separately later on.

Dapr initialization in local K8s

# Execute on PowerShell in admin mode
dapr init -k
# Verify Dapr version
dapr status -k

Create namespaces in local k8s

kubectl create ns vik
kubectl create ns monitoring
kubectl create ns ingress-nginx

Add helm repos

helm repo add bitnami https://charts.bitnami.com/bitnami 
helm repo add jetstack https://charts.jetstack.io
helm repo add kafka-ui https://provectus.github.io/kafka-ui
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
helm repo list

Install Cert Manager

helm install cert-manager jetstack/cert-manager --namespace vik --version v1.8.2 --set installCRDs=true

Install Ingress controller Ingress Controller

helm install ingress-nginx ingress-nginx/ingress-nginx --namespace ingress-nginx  --set service.loadBalancerIP='127.0.0.1' --set admissionWebhooks.enabled=false --set extraArgs.enable-ssl-passthrough=false

Install MsSqlServer

  • Start docker desktop [Ignore if it's already running].

    # Execute on PowerShell in admin mode
    docker pull mcr.microsoft.com/mssql/server:2019-latest
    cd C:\install
    
    # Git clone this:
    git clone https://github.com/microsoft/mssql-docker
    
    # Navigate to linux based chart.
    cd .\mssql-docker\linux\sample-helm-chart\
    
    # Helm install this:
    helm install sqlserver . --set sa_password=Welcome@123 --set pvc.StorageClass=hostpath -n vik
    # Installed in k8s vik namespace

Install Kafka

  • Zookeeper

    helm install zookeeper bitnami/zookeeper --set replicaCount=1 --set auth.enabled=false --set allowAnonymousLogin=true -n vik
    
    # ZooKeeper can be accessed via port 2181 on the following DNS name from within your cluster:
    # zookeeper.vik.svc.cluster.local
    
    # To connect to your ZooKeeper server from outside the cluster execute the following commands:
    
      kubectl port-forward --namespace vik svc/zookeeper 2181:2181 & zkCli.sh 127.0.0.1:2181
    
  • Kafka

    helm install kafka bitnami/kafka --set zookeeper.enabled=false --set replicaCount=1 --set externalZookeeper.servers=zookeeper.vik.svc.cluster.local --set externalAccess.enabled=true --set externalAccess.service.type=LoadBalancer --set externalAccess.autoDiscovery.enabled=true --set rbac.create=true --set autoCreateTopicsEnable=true --set deleteTopicEnable=true -n vik
    # Kafka can be accessed by consumers via port 9092 on the following DNS name from within your cluster:
    
     # kafka.vik.svc.cluster.local
    
    # Each Kafka broker can be accessed by producers via port 9092 on the following DNS name(s) from within your cluster:
    
     # kafka-0.kafka-headless.vik.svc.cluster.local:9092
     #  Kafka Brokers port: 9094
  • Create Kafka topic

    kubectl --namespace vik exec -it kafka-0 -- kafka-topics.sh --create --topic mytopic --replication-factor 1 --partitions 1 --bootstrap-server kafka.vik.svc.cluster.local:9092

Install Redis

helm install redis bitnami/redis --set auth.enabled=false -n vik
# create load balancer
kubectl expose service redis-master -n vik --port=6379 --target-port=6379 --name=redis-external --type=LoadBalancer

Install Zipkin

# Pull Zipkin image
docker pull openzipkin/zipkin
# Create Zipkin deployment in k8s
kubectl create deployment zipkin --image openzipkin/zipkin -n monitoring
# Create Zipkin service
kubectl expose deployment zipkin --type ClusterIP --port 9411 -n monitoring
# Create Zipkin load balancer
kubectl expose service zipkin -n monitoring --port=9411 --target-port=9411 --name=zipkin-external --type=LoadBalancer
# http://localhost:9411/zipkin/

Install Kafka UI

# Pull Kafka-UI image
docker pull provectuslabs/kafka-ui
# Create Kafka-UI deployment in k8s
kubectl run kafka-ui --image provectuslabs/kafka-ui --env="KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS=kafka-0.kafka-headless.vik.svc.cluster.local:9093" --port=8080 -n vik
# Create Kafka-UI service
kubectl expose pod kafka-ui --type ClusterIP --port 8080 -n vik
# Create Kafka-UI load balancer
kubectl expose service kafka-ui -n vik --port=8080 --target-port=8080 --name=kafka-ui-external --type=LoadBalancer
# http://localhost:8080

Clone repository into C:\ drive

git clone https://github.com/VikashChauhan51/quotes.git
# verify path
cd C:\quotes

Generate Self-signed certificate If Expired

# Execute on PowerShell in admin mode
openssl req  -nodes -new -x509  -keyout ca.key -out ca.crt

Import Issuer certificate to Trusted Root Certificate Authorities

# Execute on PowerShell in admin mode
Import-Certificate -FilePath C:\quotes\k8\certs\ca.crt -CertStoreLocation 'Cert:\LocalMachine\Root'

Setup shared storage for Quote

# Execute on PowerShell in admin mode
mkdir c:\mounted-k8
cd C:\quotes\k8\volume
kubectl apply -f .\local-persistent-volume.yaml -n vik

Create Secret internal-ca-key-pair and ConfigMap for Trusted CA

cd C:\quotes\k8\certs
kubectl create secret tls internal-ca-key-pair --cert=ca.crt --key=ca.key -n vik
kubectl apply -f .\issuer.yaml -n vik
kubectl create configmap trusted-root-ca-cert-configmap -- from-file=self-signed-ca.crt=ca.crt -n vik

# or
kubectl apply -f .\trusted-root-ca-cert-configmap.yaml -n vik

Create Secrets

# Execute on PowerShell in admin mode
cd C:\quotes\local-k8\secrets
.\secrets.ps1

Install Dapr Components

cd C:\quotes\charts
helm upgrade --install dapr-infra .\dapr-infra -n vik

Run Quote API with local Dapr sidecar

# Navigate to **Quotes.API** folder path in terminal.
cd C:\quotes\backend\api\src\Quotes.API

dapr run --app-id="quotes-api" --app-port=5000 --dapr-grpc-port=53000 --dapr-http-port=53001 --components-path="C:\quotes\local-k8\dapr\components"

# Now, run QuotesAPI project with following launch setting:
"profiles": {
    "QuotesAPI": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "DAPR_GRPC_PORT": "53000",
        "DAPR_HTTP_PORT": "53001"
      },
      "applicationUrl": "https://localhost:7149;http://localhost:5201",
      "dotnetRunMessages": true
    }
# Now, call API with swagger endpoint

# or
dapr run --app-id="quotes-api" --app-port=5000 --dapr-grpc-port=53000 --dapr-http-port=53001 --components-path="C:\quotes\local-k8\dapr\components" dotnet run

Run Quote API in local kubernets

cd C:\quotes\charts
helm upgrade --install quote-api .\quote-api -n vik
helm upgrade --install quote-identity .\quote-identity-app -n vik
helm upgrade --install quote-app .\quote-app -n vik

Update host file

127.0.0.1 quote-api.dev
127.0.0.1 quote-identity.dev
127.0.0.1 quote-app.dev

Rewrite DNS name in CoreDNS config CoreDNS

  • Add following entries into coredns COnfigMap of Kube-System namespace. (below ready text)
# rewrite name {url} {ingress-controller-service-name}.{ingress-service-namespace-name}.svc.cluster.local

 rewrite name quote-identity.dev ingress-nginx-controller.ingress-nginx.svc.cluster.local
 rewrite name quote-api.dev ingress-nginx-controller.ingress-nginx.svc.cluster.local
  • CoreDNS

Access API and Web Apps

# Access following get url with `Accept:application/json` header
https://quote-api.dev/api/v1/root

# or
https://quote-api.dev/swagger/index.html

# Access identity server
https://quote-identity.dev/

# Access quote web app
https://quote-app.dev/