A day with .Net

My day to day experince in .net

Archive for June, 2018

Serverless meet Containerization – Running Azure functions in Docker and then to Kubernetes.

Posted by vivekcek on June 23, 2018

Microservices, Containerization and Serverless are the current hot architecture patterns in our industry.Each have their pros and cons. I will disuss about the selection criteria for these in a later post.

In this post i am trying to bring the Microsoft serverless platform Azure functions inside the Docker Container.

1. Make sure you installed Docker for windows Edge.
2. Switch to Linux containers. Azure functions now support only linux images.
3. Install Azure Functions CLI Version 2.0 from this link https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local
4. After installing the CLI open command prompt and issue below command.

func init MyFunctionProject --docker

5. Select dotnet

6. Now switch to MyFunctionProject folder and issue below command. Select HTTP Triger as template.

func new

7.Now issue below command to test whether your function work fine locally.

func host start

8. Your function will run at http://localhost:7071/api/myfunction?name=”vivek”
9. Now add a Docker file.

FROM microsoft/azure-functions-base:dev-nightly
ENV AzureWebJobsScriptRoot=/home/site/wwwroot
COPY . /home/site/wwwroot

10. Now build the Docker Image.

docker build -t functionapp .

11. Now run the image.

docker run -p 8080:80 functionapp

12. Your function will be running at http://localhost:8080/api/myfunction?name=”vivek”

4. Once the image is running successfully we can deploy the image to kubernetes.
5. First create a deployment.

kubectl run hello --image=functionapp:latest --image-pull-policy=IfNotPresent

6. Now create a service to expose the deployment aka pod.

kubectl expose deployment hello --type="LoadBalancer" --port=8080 --target-port=80

7.Now browse to http://localhost:8080/api/myfunction?name=”vivek”

Advertisements

Posted in Azure, Docker, Kubernetes | Leave a Comment »

Deploying a local ASP.NET Core docker image to kubernetes from your local machine (No docker registry)

Posted by vivekcek on June 23, 2018

I faced some issues in deploying a local asp.net core image to my local kubernetes cluster.So in this post I will describe the steps to deploy a local docker image to kubernetes cluster.Hope you installed Docker For Windows Edge version and Enabled Kubernetes support, Also switch to Linux containers.

1. First build a local asp.net core docker image from below docker file. Use linux container.

FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app
EXPOSE 80

WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore 

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=build-env /app/out .

ENTRYPOINT ["dotnet", "KubeTest.dll"]

2. Now build your image.

docker build -t mycoreapp .

3. Now run your image. This step is optional, just verify your image is working fine.

docker run -p 8080:80 mycoreapp

4. Once the image is running successfully we can deploy the image to kubernetes.
5. First create a deployment.

kubectl run hello --image=mycoreapp:latest --image-pull-policy=IfNotPresent

6. Now create a service to expose the deployment aka pod.

kubectl expose deployment hello --type="LoadBalancer" --port=8080 --target-port=80

7.Now browse to http://localhost:8080/

Posted in Docker, Kubernetes | Tagged: | Leave a Comment »

Installing and Uninstalling of Kubernetes Dashboard – Docker for windows

Posted by vivekcek on June 17, 2018

I installed the Docker for windows edge with Kubernetes enabled(Switch to linux containers to use kubernetes with docker for windows edge).
After deploying a pod to kubernetes, I found that the kubernetes dashboard is missing. So i tried to install it from various sources.
End of the day my dashboard installation got corrupted and I had to re-install it again.

In this post, I will show you how to install kubernetes dashboard first, then how to uninstall and reinstall.

1. Download and install Docker for windows edge.
2. Enable kubernetes.
3. Docker for windows is not coming with kubernetes dashboard. So we need to install it manually.
4. Run Docker as administrator.
5. Open command prompt and enter below command.

kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml

6. After installation run below command.

kubectl proxy

7. Now open the browser and paste below url.

http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/.

How to uninstall dashboard.

Execute below commands, one by one. Then follow the above installation steps 5 to 7.

kubectl delete deployment kubernetes-dashboard --namespace=kube-system 
kubectl delete service kubernetes-dashboard  --namespace=kube-system 
kubectl delete role kubernetes-dashboard-minimal --namespace=kube-system 
kubectl delete rolebinding kubernetes-dashboard-minimal --namespace=kube-system
kubectl delete sa kubernetes-dashboard --namespace=kube-system 
kubectl delete secret kubernetes-dashboard-certs --namespace=kube-system
kubectl delete secret kubernetes-dashboard-key-holder --namespace=kube-system

Posted in Docker, Kubernetes, kubernetes dashboard instllation | Tagged: , , , | Leave a Comment »

Connecting to local or remote SQL Server from Docker Container

Posted by vivekcek on June 10, 2018

In this post, I will show you how to connect from a docker container to local sql express running in your machine.
Please note that the SQL Server is not running inside a docker container. SQL is installed in my machine.

My docker container host an asp.net core application with EF Core. During the local debugging the app was perfectly connecting to my sql database. But after deploying the app to docker container I got exception like unable to connect to sql server.

So why this happen?. Here the docker container is running as a separate machine inside your host computer. So to connect to the sql database in your host machine, you need to enable remote connections to sql server.

So follow below steps to enable remote connections to sql server.

1. You must configure SQL Server with Mixed Mode Authentication. For remote connection you need to supply user name and password.
2. Open SQL Server Configuration Manager as Administrator.

3.Now select Protocols for SQL and then select TCP/IP now open the Properties by right click.
4.In the TCP/IP properties window enable TCP/IP for SQL Server (Enabled=Yes, Listen All=Yes).

5.Now select IP Addresses tab in properties window. And under IPAll give port number as 49172.

6. Now restrat SQL Server service.

7.Make sure SQL Server Browser is running, if not start it.

7. Now setup your firewall to accept inbound connection to 49172.

8.To connect to sql server from docker, you can’t use the host computer name. You need to find the right ip address of you host.

9. Open command prompt and issue “ipconfig” command, Then you can see a nat ip address of Docker NAT ip address copy it.

10. Now we need to make sure, we can connect to this ip from our docker container.
11.User “docker ps” command to find the id of your running container.
12. Now use the “docker exec -i cmd” to get command prompt from your container. And use the ping command to chek the ip address connectivity.

13. If the connectivity is fine you can use below connection string.

Data Source=<IPAddress>\\SQLEXPRESS,49172;Database=<Database name>;User ID=sa;Password=<Your password>;MultipleActiveResultSets=true;

Posted in Docker | Tagged: , , , | Leave a Comment »