Deploy the ASPNET Core App on Linux and Capture Perfview traces
My previous blog was specifically to capture the perfview traces for aspnet core MVC application on a Windows box.
The current blog targets capturing the perfview traces for aspnet core MVC application on a LINUX box.
Pre-requisites:
1. On the Windows Development Box ensure that you have the below components:
- putty.exe: This is used to connect to your Guest LINUX box
- pscp.exe: This is a command line application to securely transfer the files.
2. Have a LINUX Operating system with most recent release. I am making use of Ubuntu 17.04 release for my demo.
3. The ASPNET Core application with the logging enabled. More Info on enabling logging can be found here in my previous blog.
Let's get started.
Step 1: Installing the Dotnet Core SDK on Linux
1. Install the Dotnet Core SDK for Linux from this article
Then run the commands mentioned in the above article. Note that you need to run the command as per the product version. If you are not sure of the version, you can simply run the below command:
navba@CoreLinuxDemo:~$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 17.04 Release: 17.04 Codename: zesty |
2. Post adding dotnet product feed and installing dotnet SDK, you can test the installation by simply creating a sample .NET MVC application using the dotnet CLI. We are creating the app within the myapp directory.
navba@CoreLinuxDemo:~$ dotnet new mvc -o myapp
To see the contents / files the above command gave you, run the below commands:
navba@CoreLinuxDemo:~$ cd myapp navba@CoreLinuxDemo:~/myapp$ ls appsettings.Development.json appsettings.json bower.json bundleconfig.json Controllers Models myapp.csproj obj Program.cs Startup.cs Views wwwroot |
3. To restore the dependencies of your application, you can run the below command:
navba@CoreLinuxDemo:~/myapp$dotnet restore Restore completed in 39.36 ms for /home/navba/myapp/myapp.csproj. Restore completed in 27.37 ms for /home/navba/myapp/myapp.csproj. |
To run the application, you can run the below command:
navba@CoreLinuxDemo:~/myapp$ dotnet run warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35] No XML encryptor configured. Key {79a1ec79-a634-4707-936e-a91a85576f75} may be persisted to storage in unencrypted form. Hosting environment: Production Content root path: /home/navba/myapp Now listening on: https://localhost:5000 Application started. Press Ctrl+C to shut down. |
Note: dotnet run command will internally run the dotnet restore / dotnet build commands.
4. To test the application, you can launch another instance of the putty and connect to your LINUX box and try accessing the https://localhost:5000 using curl as shown below:
navba@CoreLinuxDemo:~$ curl https://localhost:5000
You should be getting the html response of the application.
Alternatively, you can also try accessing the app using wget as shown below:
navba@CoreLinuxDemo:~$ wget https://localhost:5000 --2017-12-21 12:11:50-- https://localhost:5000/ Resolving localhost (localhost)... 127.0.0.1 Connecting to localhost (localhost)|127.0.0.1|:5000... connected. HTTP request sent, awaiting response... 200 OK Length: unspecified [text/html] Saving to: 'index.html' index.html [ <=> ] 8.62K --.-KB/s in 0s 2017-12-21 12:11:50 (162 MB/s) - 'index.html' saved [8827] |
Once we confirm that we get the expected response from the application we can confirm that the .NET Core SDK is installed fine, and our application is functioning right.
Step 2: Install Nginx server
1. We install the nginx server using the below command:
navba@CoreLinuxDemo:~/myapp$ sudo apt-get install nginx
2. Once it is installed, try to access your nginx server using your external IP address of the LINUX box. If you see the nginx HOME page then your nginx server installation did succeed and its functioning fine.
Welcome to nginx!If you see this page, the nginx web server is successfully installed and working. Further configuration is required.For online documentation and support please refer to nginx.org.Commercial support is available at nginx.com.Thank you for using nginx. |
Note: Remember to open PORT 80 in the external firewall (if any). See my configurations below:
Step 3: Configuring Nginx as reverse proxy to dotnet core application:
1. We will clear the default configuration using the below command:
navba@CoreLinuxDemo:~/myapp$ sudo truncate -s 0 /etc/nginx/sites-available/default
2. Modify the nginx config using the below command:
navba@CoreLinuxDemo:~/myapp$ sudo nano /etc/nginx/sites-available/default
The editor opens, and you need to manually enter the below to avoid any syntactical errors:
server { listen 80; location / { proxy_pass https://localhost:5000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; }} |
Note: The port which dotnet.exe process will be listening to can be different, ensure that you place a correct one in the nginx config file.
3. To ensure that you have the syntax placed right in the configuration file you can run the below command:
navba@CoreLinuxDemo:~/myapp$ sudo nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful |
4. If the above command throws any syntactical errors, you need to verify the nginx configuration and manually type the above entry again.
5. The below command should reload the new configuration settings.
navba@CoreLinuxDemo:~/myapp$ sudo nginx -s reload
6. Then you run the dotnet run command again to spawn up the dotnet.exe process.
navba@CoreLinuxDemo:~/myapp$ dotnet run
7. Now try to access the application externally. You should see that the Home/Index page of your MVC application. Our nginx server successfully proxied the request to dotnet application.
Note: If you are having trouble in getting this page, please follow the above steps again.
Now that the application is up and running on Linux via nginx you can try capturing perfview traces.
Note that you can do custom logging so that it gets logged within the perfview using this article.
Step 4: Deploying your concerned Core application to Linux:
If you need help in moving your own application from windows environment to Linux before capturing the Perfview traces, you can use pscp.exe1. On your Dev box, Open the CMD prompt in admin mode.
2. Navigate to the location where you placed your application.
3. From this folder place the path to your pscp.exe and run the below command to place your application contents within the myapp folder within your profile.
E:\dotnet\CoreDemo> C:\temp\pscp.exe -r * navba@52.170.94.20:/home/navba/myapp
4. Post this go back to you linux files and you can run the ls command to ensure that your files have been placed right.
5. Run the dotnet run command to launch the dotnet.exe and note the port number it is listening on. Then you modify the nginx configuration file by following the steps from Step 3 section mentioned above.
Step 5: Capturing Perfview traces for your application
1. Launch a different instance of PUTTY and connect to your LINUX box.
2. The below command will download the perfview tool:
navba@CoreLinuxDemo:~$curl -OL https://aka.ms/perfcollect
3. You can confirm it again by using ls command.
navba@CoreLinuxDemo:~$ ls |
4. You can try giving it JUST executable permissions. I simply gave it FULL Control for all groups to avoid any issues.
navba@CoreLinuxDemo:~$chmod 777 perfcollect
5. Install the perfview using the below command:
navba@CoreLinuxDemo:~$ sudo ./perfcollect install
6. Add the below environment variable before you start collecting the trace:
navba@CoreLinuxDemo:~$ export COMPlus_PerfMapEnabled=1
7. Then you run the below command to start collecting it. Ensure that your dotnet.exe process is running and try accessing your application.
8. Once you are sure that you have captured the issue, you can stop the perfview by clicking Ctrl + C as shown below:
navba@CoreLinuxDemo:~$ sudo ./perfcollect collect MyPerfviewFile
Collection started. Press CTRL+C to stop.
^C
...STOPPED.
Starting post-processing. This may take some time.
Generating native image symbol files
...SKIPPED
Crossgen not found. Framework symbols will be unavailable.
See https://github.com/dotnet/coreclr/blob/master/Documentation/project-docs/lin ux-performance-tracing.md#resolving-framework-symbols for details.
Saving native symbols
...FINISHED
Exporting perf.data file
...FINISHED
Compressing trace files
...FINISHED
Cleaning up artifacts
...FINISHED
Trace saved to MyPerfviewFile.trace.zip
8. You can run the ls command to see the collected trace:
navba@CoreLinuxDemo:~$ lsindex.html lttng-traces myapp MyPerfviewFile.trace.zip perfcollect |
More Info about the analysis of perfview trace on Linux you can see this:
https://blogs.msdn.microsoft.com/vancem/2016/02/20/analyzing-cpu-traces-from-linux-with-perfview/
Hope this helps :)