asp.net 5 auf raspberry pi & docker

44
ASP.NET 5 auf RPI und Docker ausführen ASP.NET 5 Core

Upload: juergen-gutsch

Post on 15-Apr-2017

2.310 views

Category:

Internet


3 download

TRANSCRIPT

Page 1: ASP.NET 5 auf Raspberry PI & docker

ASP.NET 5 auf RPI und Docker ausführen

ASP.NET 5 Core

Page 2: ASP.NET 5 auf Raspberry PI & docker

Jürgen Gutsch

• Microsoft MVP (ASP.NET/IIS)• Blogger auf aspnetzone.de• Fachautor• Leiter der .NET User-Groups in Basel und Konstanz/Kreuzlingen• Software-Entwickler, Berater und Trainer bei der YooApplications AG

in Basel

Page 3: ASP.NET 5 auf Raspberry PI & docker

Agenda• .NET Core• ASP.NET 5• Raspberry PI • Prepare• Deploy• Run

• Docker• Prepare• Deploy• Run

Page 4: ASP.NET 5 auf Raspberry PI & docker

.NET Core• Cross Plattform Runtime• Runs on Windows, Linux, Mac• Open Source • NuGet Based• Lightweight• Agile and Flexible

Page 5: ASP.NET 5 auf Raspberry PI & docker

ASP.NET 5• Completely rewritten• Lightweight• Fast• Cross Plattform• NuGet based

Page 6: ASP.NET 5 auf Raspberry PI & docker

ASP.NET 5 on the Raspberry PI 2

Page 7: ASP.NET 5 auf Raspberry PI & docker

Raspberry PI 2• 4 Core ARM processor• 1 GB RAM• 40 Pins• 4 USB• 1 LAN• 1 HDMI• 1 Sound out

Page 8: ASP.NET 5 auf Raspberry PI & docker

Installing the PI• Downloading the latest Raspian image from raspberry.org• Use Win32 Disk Imager to prepare a SD Card• Install Raspian on the PI

Page 9: ASP.NET 5 auf Raspberry PI & docker

Preparing the PI

• Add and configure a Network• Connect to the PI via SSH e. g. via Putty

Page 10: ASP.NET 5 auf Raspberry PI & docker

Update the System

$ sudo apt-get update$ sudo apt-get upgrade

Page 11: ASP.NET 5 auf Raspberry PI & docker

Install the latest Mono

$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF

$ echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list

$ sudo apt-get update$ sudo apt-get install mono-complete

Page 12: ASP.NET 5 auf Raspberry PI & docker

Test the Mono installation

$ mono -V

Page 13: ASP.NET 5 auf Raspberry PI & docker

Getting ASP.NET 5 samples (optional)

$ mkdir ~/sources/aspnet5 & cd ~/sources/aspnet5$ git clone git://github.com/aspnet/home.git$ sh ~/sources/aspnet5/dnvminstall.sh$ source ~/.dnx/dnvm/dnvm.sh$ dnvm upgrade

Page 14: ASP.NET 5 auf Raspberry PI & docker

Import needed certificates

$ sudo certmgr -ssl -m https://go.microsoft.com$ sudo certmgr -ssl -m https://nugetgallery.blob.core.windows.net$ sudo certmgr -ssl -m https://nuget.org$ sudo certmgr -ssl -m https://www.myget.org$ mozroots --import --sync

Page 15: ASP.NET 5 auf Raspberry PI & docker

Build and install Libuv for Kestrel$ sudo apt-get install gyp$ wget http://dist.libuv.org/dist/v1.4.2/libuv-v1.4.2.tar.gz$ tar -xvf libuv-v1.4.2.tar.gz$ cd libuv-v1.4.2/$ ./gyp_uv.py -f make -Duv_library=shared_library$ make -C out$ sudo cp out/Debug/lib.target/libuv.so $ usr/lib/libuv.so. 1.4.2$ sudo ln -s libuv.so. 1.4.2 /usr/lib/libuv.so.1

Page 16: ASP.NET 5 auf Raspberry PI & docker

Install the latest DNVM

$ curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh && source ~/.dnx/dnvm/dnvm.sh

$ dnvm upgrade

Test the DNVM$ dnvm

Page 17: ASP.NET 5 auf Raspberry PI & docker

Install node.js

$ wget http://node-arm.herokuapp.com/node_latest_armhf.deb $ sudo dpkg -i node_latest_armhf.deb

Page 18: ASP.NET 5 auf Raspberry PI & docker

Install grunt & bower

$ sudo npm install -g grunt$ sudo npm install -g bower

Page 19: ASP.NET 5 auf Raspberry PI & docker

Possible IDEs• Working on the PI• vi Startup.cs• Nano Startup.cs

• Working on Windows• Visual Studio• Visual Studio Code

Page 20: ASP.NET 5 auf Raspberry PI & docker

Using Git

• Transfer files with Git• Git is easy to use• Git is integrated in Raspian• Git is integrated in Visual Studio 2015

Page 21: ASP.NET 5 auf Raspberry PI & docker

Setup a Git workspace on Windows$ git init$ git add –all$ git commit -m „initial commit“

Push initially:$ git add origin https://github.org/juergengutsch/...$ git push -u origin master

Push the latest changes:$ git push

Page 22: ASP.NET 5 auf Raspberry PI & docker

Setup a Git workspace on the PISetup a the IDE on the PI

$ mkdir ~/projects/dnc15 & cd ~/projects/dnc15

Initial clone$ git clone https://github.org/juergengutsch/...

Get the latest changes$ git pull

Page 23: ASP.NET 5 auf Raspberry PI & docker

Setup the first ASP.NET 5 App for Kestrel• Add Dependencies:• "Microsoft.AspNet.Hosting": "1.0.0-beta7"• "Microsoft.AspNet.Server.Kestrel": "1.0.0-beta7”

• Add Command:• "kestrel": "Microsoft.AspNet.Hosting --server crosoft.AspNet.Server.Kestrel --

server.urls http://localhost:5001"

Page 24: ASP.NET 5 auf Raspberry PI & docker

Start the sample webPrepare the Start

$ cd ~/projects/dnc15/Sensors/src/Sensors$ dnvm install latest$ dnu restore

Starting the web server$ dnx kestrel

Page 25: ASP.NET 5 auf Raspberry PI & docker

About the GPIOsGeneral purpose input/output• GPIO numbering vs. physical/pin numbering• 2 x output 5V• 2 x output 3.3V• 8 x ground• 26 x GPIO• (2x ID EEPROM)

Page 26: ASP.NET 5 auf Raspberry PI & docker

About the GPIOs

Page 27: ASP.NET 5 auf Raspberry PI & docker

Working with the GPIOs

• Calling the Raspian native API to connect the GPIOs• Using existing C# libraries to access the pins:• Raspberry.System

• https://github.com/raspberry-sharp/raspberry-sharp• Raspberry.IO.GeneralPurpose

• https://github.com/raspberry-sharp/raspberry-sharp-io• RPI.GPIO

• https://github.com/fatihboy/RPI.GPIO

Page 28: ASP.NET 5 auf Raspberry PI & docker

Good to know: Be carefully

• Read carefully the GPIO specification• Wrong connected pins can kill the sensor or the PI

• Read carefully the sensor specification• Be carefully with external power

Page 29: ASP.NET 5 auf Raspberry PI & docker

Raspberry.SystemGetting common information about the PI:

Page 30: ASP.NET 5 auf Raspberry PI & docker

Raspberry.IO.GeneralPurpose• Includes drivers to access the GPIO Pins• Includes Pin connections• Includes conversions between pin numberings• ConnectorPin: Pin using physical pin numbers• ProcessorPin: Pin using GPIO pin numbering

• Includes methods to work with the pins

Page 31: ASP.NET 5 auf Raspberry PI & docker

Simple blinking LED

Page 32: ASP.NET 5 auf Raspberry PI & docker

Good to know: Cheat with node.js• node.js is pretty cool on the PI • It‘s easy to write• It runs fast• many GPIO examples written in Javascript

• node.js is a pretty awesome tool to tryout things, you want to implement with C#

Page 33: ASP.NET 5 auf Raspberry PI & docker

ASP.NET 5 on Docker

Page 34: ASP.NET 5 auf Raspberry PI & docker

Docker• Lightweight Virtualization• Based on Linux Hosts (Windows is coming soon)• Use of VirtualBox to run the Linux Host on Windows

• Machine = the Host• Image = Configuration• Container = a running image

Page 35: ASP.NET 5 auf Raspberry PI & docker

Docker on Windows

• Installing boot2docker (deprecated) or Docker Toolbox

• Preparing a bash to work with Docker• Setup the environment variables (HOME and PATH)• Configure the bash using git-bash

Page 36: ASP.NET 5 auf Raspberry PI & docker

Docker MachinesList all available machines:

$ docker-machine ls

Create a new machine$ docker-mashine create testmachine

Get the IP address or the config of the machine$ docker-mashine ip testmachine$ docker-machine config testmachine

Start a machine$ docker-machine start testmachine

Page 37: ASP.NET 5 auf Raspberry PI & docker

Docker ImagesList all available images

$ docker images

Get an image from the Docker Hub$ docker pull hello-world

Starts and run an image (pulls the image if needed)$ docker run hello-world

Page 38: ASP.NET 5 auf Raspberry PI & docker

Prepare ASP.NET for docker• Create a new ASP.NET Project• Add the web server Kestrel to the commands:

NuGet Packages:"Microsoft.AspNet.Hosting": "1.0.0-beta7", "Microsoft.AspNet.Server.Kestrel": "1.0.0-beta7“

Command:"kestrel": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.Kestrel --server.urls http://localhost:5001"

Page 39: ASP.NET 5 auf Raspberry PI & docker

Define the Image for ASP.NETAdd a „Dockerfile“ to the project next to the project.json:

FROM microsoft/aspnet:latest

COPY . /app WORKDIR /app

RUN ["dnu", "restore"]

EXPOSE 5001

ENTRYPOINT ["dnx", "kestrel"]

Page 40: ASP.NET 5 auf Raspberry PI & docker

Build and run the Image• Move to the project folder using the bash

• Building the image:$ docker build -t guj/aspnet5 .

• Check whether the image is registerd:$ Docker images

• Running the image:$ docker run -i -t -p 5001:5001 guj/aspnet5

Page 41: ASP.NET 5 auf Raspberry PI & docker

Calling the ASP.NET Web• Call http://<machines ip>:5001/

Page 42: ASP.NET 5 auf Raspberry PI & docker

Passing environment variables • Pass any argument as environment variable with docker run:

$ docker run VARNAME1=OneValue VARNAME2=AnotherValue

• Running the web in production mode:$ docker run -i -t -p 5001:5001 guj/aspnet5 ASPNET_ENV=Production

• Running the web in staging mode:$ docker run -i -t -p 5001:5001 guj/aspnet5 ASPNET_ENV=Staging

• Running the web in development mode:$ docker run -i -t -p 5001:5001 guj/aspnet5 ASPNET_ENV=Development

Page 43: ASP.NET 5 auf Raspberry PI & docker

Why using Docker?• Fast and lightweight virtualisation• Composing infrastructure for microservice applications• Fast created on the dev machines• Easily distributed images to other devs• Fast and easy deployed to production machines

Page 44: ASP.NET 5 auf Raspberry PI & docker

Many Thanks• Feel free to contact me:• Twitter.com/sharpcms• facebook.com/juergen.gutsch• github.com/juergengutsch• bitucket.org/juergengutsch• aspnetzone.de/blogs/juergengutsch