Archivi categoria: c#

Kubernetes baby steps: deploying our first .Net App to a cluster

This blog post is the first of a new topic about Kubernetes.

Kubernetes is an open-source container-orchestration system for automating application deployment, scaling, and management. It was originally designed by Google, and is now maintained by the Cloud Native Computing Foundation. 

Wikipedia

This post introduces some basic concepts of Kubernetes with a working example based on .Net and Visual Studio.
We’ll develop a simple Web API service, package it as a Docker container, upload it to Docker Hub, and we’ll run it onto Kubernetes.

Our API is built from a project called IC6.RedSvc (Red Service) that when invoked returns a name of a red object. It’s a pretty simple business logic because the main purpose of this post is about learning Kubernetes. The source code for this working example is hosted on my GitHub.

Let’s get started!

Step-by-step

Create a new project ASP.NET Core Web Application.

Solution name: IC6.ColorSvcs, project name IC6.RedSvc.

Rename the DefaultController.cs file into RandomObjectController.cs and replace the code of the class with the following code. This is the controller that implements our simple business logic.

namespace IC6.RedSvc.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class RandomObjectController : ControllerBase
    {
        string[] redObjects = { "Ferrari", "Coca Cola", "Pendolino", "Cherry", "Strawberry" };

        // GET api/values
        [HttpGet]
        public ActionResult<string> Get()
        {
            return redObjects[DateTime.Now.Ticks % redObjects.Length];
        }
    }
}

Add Dockerfile

Right click on the Solution Explorer onto the Project, Add, Docker Support…

And we chose Linux.
This step creates the Dockerfile to build our Docker image.

Before we start the application edit launchSettings.json to change the default page. This step makes it easier to test in our browser the API when we launch the project with F5.

Docker build

Now we build our Docker image.

docker build -f "c:devic6.colorsvcsic6.redsvcdockerfile" -t phenixita/ic6redsvc:latest --name=IC6.RedSvc" "c:devic6.colorsvcs"

We have built the Docker image of our app!

Before we can move on with Kubernetes we need to upload this image to Docker Hub because Kubernetes will try to download from it the images for the containers that it will create. In order to do that we need an account (free for open projects) of Docker Hub.

Docker login

The first thing we need to do is a docker login from the command line to handle the authentication to our repository.

Push

Now we can push our image with:

docker push phenixita/ic6redsvc:latest

And it’s done!

Kubernetes time!

We can deploy our app onto Kubernetes!
I recommend to use a managed Kubernetes instance from your provider of choice (AWS, Azure, you name it) or run in your machine something like Minikube. Docker for Windows provides support for a single Kubernetes cluster with low hardware requirements.

This is a Kubernetes Cluster. It is composed by a master node and working nodes. The master node is the coordinator of all the activities that happens inside a cluster. The working nodes are the computer power (most of the time they are virtual machines) that will do the job of running our code.

Check that kubectl is configured to talk to your cluster, by running the kubectl version command.

To view the nodes in the cluster, run the kubectl get nodes command:

Here we see the available nodes (1 in our case). Kubernetes will choose where to deploy our application based on Node available resources. Let’s run our first app on Kubernetes with the kubectl run command.

Run!

The run command creates a new deployment. We need to provide the deployment name and app image location (include the full repository url for images hosted outside Docker hub). We want to run the app on a specific port so we add the –port parameter:

kubectl run ic6redsvc --image phenixita/ic6redsvc:latest --port=80

Testing

We need to get the pod name with:

kubectl get pods
Our app is running in a private network and we need to activate a proxy in another terminal to be able to test it. This is not the standard approach for production environments, we'll see in future blog post how to do it properly. Now we try to keep things as simple as possible.

Call

This is the final step where we call our API. We compose the command like the following example.

curl http://localhost:8001/api/v1/namespaces/default/pods/<podId>/proxy/api/RandomObject

If we did everything correctly we’ll se a repsonse message with one of the objects returned as implemented in the C# code.

TL;DR

With this blog post we learned how to do a very simple deployment of a Docker Image onto Kubernetes. Now we have the foundations to create more meaningful scenarios in future blog posts.

CQRS by Example – Simple ASP.Net CORE implementation

In this blog post we’re going to explore a simple ASP.Net Core CQRS WebApi project. The code for this tutorial exposes a demonstrative API to manage a blog.

Continua a leggere CQRS by Example – Simple ASP.Net CORE implementation

Import from JIRA to VSTS in 5 steps

In previous posts I documented some features of VSTS and a few scenarios of Application Lifecycle Management.

If you like VSTS it’s likely that you want to move your existing project from your current platform to VSTS. I’m currently planning a migration from JIRA to VSTS at work and here I am to share my findings.

This blog post has been inspired by the work of the ALM DevOps Rangers.

We’re going to build a simple tool to migrate and map Jira issues into VSTS items.

The source code is available on GitHub. Continua a leggere Import from JIRA to VSTS in 5 steps

VSTS for beginners: improve quality with continuous integration in 3 easy steps

In this blog post we’re going to configure a build process in VSTS to enable continuous integration for our ASP.Net Core example web-app.
Continuous integration is a powerful technique to prevent merge-hell and improve quality on the “left” stages of our software production process. In the fast-paced world of development we want to merge into the main line of development the new developed features as soon as possibile to avoid open branches that will cause painful merges. If we keep our unit of work small and focused we’ll have great benefits.

Continua a leggere VSTS for beginners: improve quality with continuous integration in 3 easy steps

How to upload images to an ASP NET Core REST service with Xamarin.Forms

With this blog post we continue the exploration of Xamarin.Forms. We’ll se how to upload a picture to a RESTful service developed with ASP.NET Core.

uwpScreenshot
UWP flavor of the upload with file picker

Continua a leggere How to upload images to an ASP NET Core REST service with Xamarin.Forms

Agile@School – settima lezione

Settimo appuntamento presso la scuola rodigina IIS Viola/Marchesini per il progetto Agile@School nella giornata di ieri 24 gennaio 2018. Si è giunti alla parte finale dove cerchiamo di mettere insieme tutte le parti finora studiate. Ci siamo dati come obiettivo quello di realizzare una semplice app per l’accesso a Twitter mettendo in gioco le conoscenze apprese.

1.png Continua a leggere Agile@School – settima lezione

Agile@School – sesta lezione

Sono ripresi oggi gli appuntamenti all’ITI F.Viola / Marchesini di Rovigo di Agile@School dopo le vacanze: siamo al sesto episodio su dieci. Come ripromesso negli episodi precedenti abbiamo ripreso in mano dei principi Agile/DevOps che negli ultimi incontri erano stati parcheggiati in favore di approfondimenti tecnologici su Xamarin Forms.

Continuous Delivery

Continua a leggere Agile@School – sesta lezione

Xamarin Forms & .NET Standard: basic navigation

In the last post about Xamarin Forms we’ve started to see the basic concepts on how to begin the development of a cross-Platform application. In this post we’re going to explore some basic navigation concepts and some tips to organize our solution. Also, we’ll develop a simple CrossPlatformApp that welcomes us with a main screen and a button to navigate to a new view where we can generate a random integer. Continua a leggere Xamarin Forms & .NET Standard: basic navigation

Xamarin Forms & .NET Standard – Getting started

In this blog post we’re going to lay the basis for future posts about Xamarin Forms. We’ll explore briefly what’s Xamarin Forms and why it’s so powerful. Then we’ll talk about .NET Standard and we’ll understand why it’s a very big achievement for the .NET ecosystem. Continua a leggere Xamarin Forms & .NET Standard – Getting started

Agile@School – Terza lezione

Eccoci giunti alla terza lezione su dieci del laboratorio Agile@School dell’IIS Viola/Marchesini di Rovigo

Screenshot_1

Stand-up meeting

Anche questa volta abbiamo fatto pratica con lo stand-up meeting in cui abbiamo fatto un piccolo riassunto della puntata precedente ed è stato introdotto a grandi linee il programma della giornata. La partecipazione al riassunto sugli argomenti passati è stata tiepida ma ugualmente ci ha fatto scontrare col fatto che per riassumere gli episodi precedenti bisogna scegliere l’adeguato livello di astrazione: né troppo elevato, né troppo dettagliato. A seguire ho introdotto gli argomenti della giornata e una previsione sulla lezione futura.

MVVM – Comandi

Abbiamo ripreso il filo della lezione precedente introducendo il concetto di comandi nel mondo MVVM. Con l’occasione abbiamo letto insieme la documentazione MSDN dell’interfaccia ICommand, abbiamo ripassato il concetto di delegati e studiato un’implementazione di ICommand. Ancora una volta il livello di preparazione delle quinte mi ha stupito perché la definizione di delegato era già stata spiegata dai prof. nelle ore “standard”, cose che ai miei tempi non si facevano.

Hands-on

Per impratichirsi coi comandi ai ragazzi è stata consegnata un’applicazione già iniziata ma con delle parti mancanti. Abbiamo preso questa occasione per riconciliare tutte le cose provate nelle puntate precedenti e quindi abbiamo:

  • Creato un nuovo progetto su VSTS;
  • Inizializzato il repository git con l’applicativo di base;
  • Ho fatto finta di essere un cliente che racconta un’esigenza che è stata trascritta dagli studenti in forma di user story in VSTS;
  • Sviluppato su feature-branch collegato alla user-story;
  • Creato pull-request e fatto merge del lavoro.

Quello che qui è riassunto in cinque punti di un elenco puntato è stato ricco di interazione tra me e la classe e tra gli studenti stessi. Confronti, dubbi, errori, chi finiva priva che spiegava agli altri…

A questo punto la classe mi ha nuovamente stupito perché hanno finito in anticipo rispetto alle tempistiche che avevo immaginato e quindi mi sono dovuto inventare al volo un’altra esigenza e abbiamo ripetuto di nuovo il processo di sviluppo. Già al secondo giro si vedeva una maggiore confidenza sia con VSTS che con il pattern MVVM.

Wrap-up

Anche questa volta una giornata positiva e sicuramente la più operativa in termini d’uso di VSTS e righe di codice.

Nella prossima lezione approderemo su Xamarin facendo una panoramica sui concetti base.

Alla prossima settimana!