Skip to content

Commit

Permalink
Merge pull request #501 from MangoInstantMessenger/k8s-deploy
Browse files Browse the repository at this point in the history
K8s deploy
  • Loading branch information
kolosovpetro authored Oct 5, 2023
2 parents 1e1071b + 88c8c95 commit 6ae5f85
Show file tree
Hide file tree
Showing 15 changed files with 312 additions and 17 deletions.
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ RUN npm ci
WORKDIR /angular
COPY ["MangoAPI.Client", "MangoAPI.Client/"]
WORKDIR "/angular/MangoAPI.Client/src/assets/config"
RUN sed -i 's/"baseUrl": "https:\/\/localhost:5001\/"/"baseUrl": "https:\/\/localhost:8002\/"/g' config.json
ARG FRONT_API_URL
RUN sed -i "s|https://localhost:5001|$FRONT_API_URL|" config.json
WORKDIR "/angular/MangoAPI.Client"
RUN ng build

Expand All @@ -28,10 +29,11 @@ COPY /img .
RUN dotnet restore "MangoAPI.Presentation/MangoAPI.Presentation.csproj"
COPY . .
WORKDIR "/src/MangoAPI.Presentation"
RUN dotnet build "MangoAPI.Presentation.csproj" -c Release -o /app/build --no-restore
ARG VERSION
RUN dotnet build "MangoAPI.Presentation.csproj" -c Release -p:Version=$VERSION -o /app/build --no-restore

FROM build AS publish
RUN dotnet publish "MangoAPI.Presentation.csproj" -c Release -o /app/publish --no-restore /p:UseAppHost=false
RUN dotnet publish "MangoAPI.Presentation.csproj" -c Release -p:Version=$VERSION -o /app/publish --no-restore /p:UseAppHost=false
COPY --from=angularBuild /angular/MangoAPI.Presentation/wwwroot/ /app/publish/wwwroot/

FROM base AS final
Expand Down
1 change: 1 addition & 0 deletions GitVersion.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mode: Mainline
16 changes: 8 additions & 8 deletions build/azure-pipelines-azcopy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ pool:
vmImage: 'windows-latest'

variables:
- group: AZURE_FOR_STUDENTS
- group: AzCopy_Settings
- name: serviceConnection
value: 'AzureForStudents'
value: 'AKS_Service_Connection'
- name: sourcePath
value: '$(Build.SourcesDirectory)/img/seed_images/*'

Expand All @@ -19,12 +19,12 @@ stages:
- job: 'AzCopy_Assets'
displayName: 'AzCopy Assets'
steps:
- task: AzureKeyVault@2
inputs:
azureSubscription: '$(serviceConnection)'
KeyVaultName: '$(KeyVaultName)'
SecretsFilter: "*"
RunAsPreJob: false
# - task: AzureKeyVault@2
# inputs:
# azureSubscription: '$(serviceConnection)'
# KeyVaultName: '$(KeyVaultName)'
# SecretsFilter: "*"
# RunAsPreJob: false

- task: AzureFileCopy@4
inputs:
Expand Down
22 changes: 22 additions & 0 deletions build/azure-pipelines-dockerhub-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
trigger: none

pr: none

variables:
- name: appName
value: 'MangoAPI'

pool:
vmImage: 'ubuntu-latest'

stages:
- stage: 'DockerHub_Push_${{ variables.appName }}'
displayName: 'DockerHub_Push_${{ variables.appName }}'
jobs:
- template: templates/publish-to-dockerhub-template.yml
parameters:
dockerUsername: 'petrokolosov'
imageRepository: 'mango-api'
dockerfilePath: 'Dockerfile'
dockerfileServiceConnection: 'MangoDockerHub'
baseUrl: 'mangomessenger.devtest.team' # service from kubernetes
68 changes: 68 additions & 0 deletions build/templates/publish-to-dockerhub-template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
parameters:
- name: dockerUsername
type: string

- name: imageRepository
type: string

- name: dockerfilePath
type: string

- name: dockerfileServiceConnection
type: string

- name: baseUrl
type: string

jobs:
- job: 'Publish_To_DockerHub'
displayName: 'Publish_To_DockerHub'
steps:
- checkout: self
fetchDepth: 0

- task: gitversion/setup@0
displayName: 'GitVersion Setup'
inputs:
versionSpec: '5.9.x'

- task: gitversion/execute@0
displayName: 'GitVersion Execute'
inputs:
updateAssemblyInfo: true

- bash: echo $Action$BuildVersion
displayName: 'Set Build Version'
env:
Action: '##vso[build.updatebuildnumber]'
BuildVersion: $(GitVersion.SemVer)

- task: Docker@2
displayName: 'Login to Docker Hub'
inputs:
command: login
containerRegistry: '${{ parameters.dockerfileServiceConnection }}'

- bash: |
IMAGE_FULL_NAME="$DOCKERUSERNAME/$REPOSITORY:$VERSION"
echo "Image full name: $IMAGE_FULL_NAME"
docker build --build-arg FRONT_API_URL="https://$BASEURL" --build-arg VERSION=$VERSION -t "$IMAGE_FULL_NAME" -f "$DOCKERFILEPATH" .
docker build --build-arg FRONT_API_URL="https://$BASEURL" --build-arg VERSION=$VERSION -t "$DOCKERUSERNAME/$REPOSITORY:latest" -f "$DOCKERFILEPATH" .
env:
REPOSITORY: ${{ parameters.imageRepository }}
DOCKERFILEPATH: ${{ parameters.dockerfilePath }}
VERSION: $(GitVersion.SemVer)
DOCKERUSERNAME: ${{ parameters.dockerUsername }}
BASEURL: ${{ parameters.baseUrl }}
workingDirectory: '$(Build.SourcesDirectory)'
displayName: 'Build Docker Image'
- task: Docker@2
displayName: 'Push to DockerHub'
inputs:
command: push
containerRegistry: '${{ parameters.dockerfileServiceConnection }}'
repository: '${{ parameters.dockerUsername }}/${{ parameters.imageRepository }}'
tags: |
$(GitVersion.SemVer)
latest
15 changes: 9 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ services:
api:
build:
context: .
args:
VERSION: '1.2.3.4'
FRONT_API_URL: 'http://localhost:8001'
dockerfile: Dockerfile
ports:
- "8001:80"
- "8002:443"
# - "8002:443"
environment:
- ASPNETCORE_ENVIRONMENT=Docker
- ASPNETCORE_URLS=https://+:443;http://+:80
- ASPNETCORE_Kestrel__Certificates__Default__Password=password
- ASPNETCORE_Kestrel__Certificates__Default__Path=/https/MangoAPI.Presentation.pfx
volumes:
- ~/.aspnet/https:/https:ro
# - ASPNETCORE_URLS=https://+:443;http://+:80
# - ASPNETCORE_Kestrel__Certificates__Default__Password=password
# - ASPNETCORE_Kestrel__Certificates__Default__Path=/https/MangoAPI.Presentation.pfx
# volumes:
# - ~/.aspnet/https:/https:ro
depends_on:
- db
- storage
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
helm upgrade my-ingress --install ingress-nginx --repo https://kubernetes.github.io/ingress-nginx `
--namespace ingress --set controller.service.externalTrafficPolicy=Local --create-namespace

kubectl --namespace ingress get services my-ingress-ingress-nginx-controller
12 changes: 12 additions & 0 deletions kubernetes/mango-api-cluster-ip/api-configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
apiVersion: v1
kind: ConfigMap
metadata:
name: mango-api-configmap
labels:
app: mango-api
data:
ASPNETCORE_ENVIRONMENT: 'Docker'
BlobAccess: 'https://mangostorage01.blob.core.windows.net/mangocontainer'
InitializeBlob: 'false'
DatabaseUrl: 'Server=mssql-service;Database=MANGO_AKS;User Id=sa;Password=x2yiJt!Fs;TrustServerCertificate=true;'
51 changes: 51 additions & 0 deletions kubernetes/mango-api-cluster-ip/api-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mango-api-deployment
spec:
selector:
matchLabels:
app: mango-api
replicas: 3
template:
metadata:
labels:
app: mango-api
spec:
containers:
- name: mango-api
image: petrokolosov/mango-api:latest
imagePullPolicy: Always
ports:
- containerPort: 80
env:
- name: BlobUrl
valueFrom:
secretKeyRef:
name: mango-connection-creds
key: BlobUrl

- name: ASPNETCORE_ENVIRONMENT
valueFrom:
configMapKeyRef:
name: mango-api-configmap
key: ASPNETCORE_ENVIRONMENT

- name: BlobAccess
valueFrom:
configMapKeyRef:
name: mango-api-configmap
key: BlobAccess

- name: InitializeBlob
valueFrom:
configMapKeyRef:
name: mango-api-configmap
key: InitializeBlob

- name: DatabaseUrl
valueFrom:
configMapKeyRef:
name: mango-api-configmap
key: DatabaseUrl
14 changes: 14 additions & 0 deletions kubernetes/mango-api-cluster-ip/api-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
kind: Service
apiVersion: v1
metadata:
name: mango-api-service
spec:
selector:
app: mango-api
ports:
- name: http
protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
32 changes: 32 additions & 0 deletions kubernetes/mango-https-nginx-ingress/https-ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ing-mango-api
annotations:
nginx.ingress.kubernetes.io/enable-cors: 'true'
nginx.ingress.kubernetes.io/cors-allow-headers: 'Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,Accept-Language'
nginx.ingress.kubernetes.io/cors-max-age: '600'
nginx.ingress.kubernetes.io/proxy-body-size: '12m'
nginx.ingress.kubernetes.io/rewrite-target: '/'
nginx.ingress.kubernetes.io/use-regex: 'true'
kubernetes.io/tls-acme: 'true'
nginx.ingress.kubernetes.io/ssl-redirect: 'true'
nginx.ingress.kubernetes.io/affinity: 'cookie'
nginx.org/websocket-services: 'mango-api-service'
spec:
ingressClassName: nginx
tls:
- hosts:
- mangomessenger.devtest.team
secretName: my-tls-secret
rules:
- host: mangomessenger.devtest.team
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: mango-api-service
port:
number: 80
39 changes: 39 additions & 0 deletions kubernetes/mssql-deployment/mssql-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mssql-deployment
spec:
selector:
matchLabels:
app: mssql
replicas: 1
template:
metadata:
labels:
app: mssql
spec:
securityContext:
fsGroup: 10001
terminationGracePeriodSeconds: 10
containers:
- name: mssql
image: mcr.microsoft.com/mssql/server:2022-latest
ports:
- containerPort: 1433
env:
- name: ACCEPT_EULA
value: "Y"
- name: SA_PASSWORD
value: "x2yiJt!Fs"
- name: MSSQL_ENABLE_HADR
value: "1"
- name: MSSQL_AGENT_ENABLED
value: "1"
volumeMounts:
- name: mssql-persistent-storage
mountPath: /var/opt/mssql
volumes:
- name: mssql-persistent-storage
persistentVolumeClaim:
claimName: mssql-pvc
23 changes: 23 additions & 0 deletions kubernetes/mssql-deployment/mssql-persistent-volume.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: azure-disk
provisioner: kubernetes.io/azure-disk
parameters:
storageaccounttype: Standard_LRS
kind: Managed
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mssql-pvc
annotations:
volume.beta.kubernetes.io/storage-class: azure-disk
spec:
storageClassName: default
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
14 changes: 14 additions & 0 deletions kubernetes/mssql-deployment/mssql-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
apiVersion: v1
kind: Service
metadata:
name: mssql-service
spec:
selector:
app: mssql
ports:
- protocol: TCP
port: 1433
targetPort: 1433
nodePort: 30201
type: NodePort
8 changes: 8 additions & 0 deletions kubernetes/secrets/secrets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: Secret
metadata:
name: mango-connection-creds
type: Opaque
#data: -- requires base 64 secrets
stringData: # takes plain text secrets
BlobUrl: "<YOUR_FULL_BLOB_URL>"

0 comments on commit 6ae5f85

Please sign in to comment.