Set the project name and the location
Set the project features. IMPORTANT: Enable Docker suppoort for automatically create the Dockerfile
Add the kubernetes manifest files to the project: deployment.yml and service.yml
Enable Kubernetes
Navigate to AWS ECR and create a public repo to store the .NET 6 WebAPI Docker image
Click on the created repo name and press the button View push commands
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
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
kubectl config get-contexts
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
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
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
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:
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
*service.yml
apiVersion: v1
kind: Service
metadata:
name: webapidotnet6-service
spec:
type: LoadBalancer
selector:
app: webapidotnet6
ports:
- protocol: TCP
port: 80
targetPort: 80
kubectl apply -f deployment.yml --namespace dev
kubectl apply -f service.yml --namespace dev