.net container on kubernetes

Dec 2, 2020 09:00 · 5950 words · 28 minute read manage demo syntax 41 container

so hey thanks for joining everybody uh my name’s elton uh i work for docker this session is all about uh net applications in kubernetes so i’ve been using before i joined docker i was a uh i was a uh a a consultant working on big.net projects i’ve been an mvp for 10 years now i was working with.net from the very first version back in 2002 one of the things i really like about the framework is that it’s it’s longevity you know you can take those apps from 2002 and you can run them on all the newest platforms and this session is all about doing that with kubernetes so there’s a couple things i’m going to cover uh start with a few slides i’m going to talk about well i’m going to give you some 101 because i know this is this is a new topic for a lot of people i’ve been working with docker since the earliest versions so like and if you if you if you’re with me on there then the first few slides will be stuff you already know but i want to get get everyone on the same level talking about what docker is how net fits in uh what windows containers mean in in docker and kubernetes and then most of the sessions gonna be demos so i’m gonna do a bunch of demos uh running some brand new dot net core 3.0 apps uh building them in docker running them locally on my machine and then running that exact same app in kubernetes uh with the issue with kubernetes service so i’ll do that with a brand new.net core 3 app running linux containers and then i’ll use the exact same process the same tools and the same frameworks to do it with a very old.

net app running on windows so that’s that that’s the session today 01:23 - okay so the very first thing so if you’re not familiar with docker like primarily it is a packaging distribution and runtime for applications so what you do with docker is you have your windows server or your linux server you install docker on it and you don’t install anything else because everything else that’s going that your application needs is going to come through the container the way you make that happen is you write this script which which is basically an installation script that says all the bits and pieces that your app needs that’s called a docker file and what that might look like for an old let’s say a dotnet web forms app it’s basically a list of all the components that you need so uh i’m a windows app so i need to start on a version of windows windows server core i’m a web app so i need iis i’m a dot net app so i need net and i’m asp.net so i need asp.net installed and configured and then right at the top there i package my own web application which would be the binaries or the content the html and javascript and any configuration that i need so i write this thing called a docker file which describes all those steps i run this command docker image build and that produces this thing called an image which is just a static package that contains everything in my application and i can share that now that docker file that i’ve talked about that does all those steps uh could be something as simple as this so this docker file uh it actually is the previous the previous diagram so this requires iis.net and asp.net but that first line that from line says use somebody else’s docker image this happens to be a microsoft image and that’s already got everything i need so it’s got iis it’s got net it’s got asp.net already configured so everything my application needs is already there right in that first line so and then the next stage imagine this is an old application i’ve already got a build process that gives me an installer a windows msi i want to just see how that looks in docker this is literally all i need to do i copy in my msi and that run command this all happens within docker it’s going to run msi exec to deploy my application from my msi you’re not necessarily going to do this with your new applications but it’s a good way to see how they how your old applications your old windows apps are going to look in containers for a new app it’s going to look something more like this so it’s the same syntax it’s the same few commands that you need to understand the words in blue there that they’re the docker syntax most of what you’re going to do though is is just run script inside that inside this docker file this is a bit more complicated this is my brand new.net core application and this does a bit more than the previous example firstly there are two parts to this so the first part is going to compile my application from source code so you see here the from line is saying use an image that microsoft owned so there’s a public images microsoft maintain them every time there’s a windows update or a net update they push out new versions this is using your version this is using an image that’s got net core installed and it’s the sdk so it’s got everything you need to build and package your applications the rest of the lines here copy in the source code from whichever machine that’s running docker and then run.

net publish which is exactly what you would do if 04:18 - you were building it in the ci system or locally on your machine this all happens within docker and then the second stage starts from the net runtime so this has got asp.net this doesn’t have the sdk so i can’t build an app because when i’m running my container in production there’s no need for me to have the sdk i just need the runtime so this starts from the asp.net runtime it specifies what to do when the application starts so that entry point line says run.net with my dll and then it copies everything that was built in the previous stage so the the the previous um the previous example that i showed you you need to have a bunch of stuff first so you need to have something a process and a machine that can build your msi but it’s very quick to get up and running this version you don’t need anything at all because all you need is docker and your source code and everything you need to build your app and compile your app and run your app is going to come from docker so it’s a really nice way to make everything super portable okay so when i built my image and i’ve run through that that docker file so what docker will do is effectively execute each line in that docker file and it will build me this thing called called a docker image that lives on the machine where i did the docker build and i can push it to a thing called a registry so that’s docker hub or as your container registry or a local registry or whatever you’re doing or github packages which is just a store for all these images so i can share that now and then anyone who has access to that image because i could make it public or private anyone who has access can do a docker run and that will bring that container image onto their machine which brings everything with it so everything that you’ve packaged comes down with that container image and then the container will start so your application will start the actual process for your app is going to run on the server directly because that’s the way containers work but everything the application needs comes packaged with that container image so my server doesn’t need iis or net and if i’m running several containers with different versions of net they’re all completely isolated so that’s the kind of basics of containers and of course it’s the same with linux 2. the commands are the same the dockerfile syntax is the same when you run your container it’s in its container it needs to run on a linux server but the syntax is the same i bring that down this may be based on debian or a lightweight linux version it’s your.net core installed and asp.

net core and my web app and the application that’s running is the net runtime but it’s exactly 06:36 - the same set of tools same set of artifacts to run your your old windows apps on.net framework or your new apps running on on linux okay so that’s your introduction to docker now what you can then end up with is uh because your linux containers have to run on the next servers and your windows containers have to run on windows servers uh you end up needing to kind of join these things together that’s where kubernetes comes in so what kubernetes can do is that you can take a bunch of servers bunch of linux machines a bunch of windows machines you install docker on all of them and then what you end up with is a bunch of isolated machines that can all run containers individually but then you deploy kubernetes and you turn the whole thing into a cluster you can treat it like one big uh one big platform where you’re just gonna throw your apps and it’s gonna decide what to do with them now i haven’t got time to go interview we’re not using any depth but basically what it is is it’s a container orchestrator so it manages where the containers run so you you manage your your kubernetes cluster remotely so i can use my laptop i can connect to my community’s cluster when i want to deploy an application i send kubernetes a description of what the pieces that the app needs uh in a yaml file it’s called your application manifest that says i need this contact i need this this container and i want five of them i need this container i want 10 of them this one has to run on windows this one has to run on linux you throw that to kubernetes and it makes all the choices for you about what’s going to run where so it might run for someone one server and someone another server and then it monitors the whole thing and guarantees you your the service levels that you’ve specified so if i need 10 containers running my web app to get to get scale and failover uh if three of those are running on one of my linux servers and that server dies then kubernetes will see that server’s gone it’s taking three containers with it so i need to start up another three somewhere else on the cluster to maintain the service level so the reason why kubernetes is so exciting and why people are really interested in it is if you if you build your app and package your app with docker to be uh to be self-healing then you can get these kind of autonomous systems where you throw them the application manifest they work out what to do with it they keep it up and running they keep it healthy for you and so they’re kind of self-operating okay and this is what that application manifest looks like in kubernetes this is a simplified version because the kubernetes the kubernetes manifest spec is quite involved but there’s two parts to this there’s that the top left the top element here is a service which is basically just um something that will let network traffic from the outside world into your containers so this is saying i’m listening on port 8081 so any traffic that comes into kubernetes from the outside world on that port is going to go into a container on port 80. and in this case the container is going to be this dotnet conf to do list which is my net core container so the second part is the deployment that says this is the container that i want to run that’s going to deal with the traffic that comes into the service there’s way more to humanities than that but this is just to give you a bit of a a bit of a grounding so that the demos make sense and then all the rest of this session is going to be demos until we come back and recap at the end okay so i’m going to move on to my demos now let’s open vs code so first demo is i’m going to take a dotnet core 3.0 app uh and i’m going to run it locally and then i’m going to push it up to aks my azure kubernetes cluster and see how it looks in azure so uh all the code i’ve got is on github uh the final slide i’ve got is a whole bunch of links that will show you where all this stuff is you don’t really need any prereqs in order to do this yourself you just need docker desktop which runs on mac or windows you can get the source code you can try it all yourself and then there’s actually in the uh in the readme for today’s session there’s instructions how to spin up an aks cluster so you can try on aks2 okay so this is the dockerfile for my.net core app there are two parts to it like in the example i showed you it starts from the.

net core sdk microsoft owned and maintained that it’s got all the tools 10:22 - i copy in my source code i do a restore to get my packages and then i do a net publish there’s this thing here which is about the runtime id and i’m using advantage of a new.net core 3 feature which is in here that i can publish a single file and i can publish it trimmed and those two things together mean i don’t need the.net core framework deployed in my container at all i’m just gonna get a single binary which has got my whole application this happens to be an asp.net core application but i don’t need asp.net core deployed to my container because everything is in my application so this gives me a super super lightweight container on time okay so the way i do this is i’m gonna i’m connected to uh a linux machine so i’m running on windows here but i’m connected to docker on linux which means i can build this to run on linux i clear this down and just do a build so that’s going to run through all the steps in my docker file and you’ll see it was it was like ridiculously suspiciously fast because i’ve already run it today and if nothing changes if none of the source code changes if none of the docker file changes then all that stuff comes from the cache so you’ll see all these lines here each of these lines from the docker file says using cache now if i run this without the cache so i can say no cache you’ll see a bit more familiar output so each of these steps are going to be executed now this is running on my linux machine you’ll see i’ve come into the dot net restore part so i copy in the cs proj file into my docker image that’s all i copied to start with then i run the restore because that might be a time consuming process and then i copy in the rest uh and then like i go and do the build and you’ll see now the the build engine starts it’s doing the net publish which is gonna actually do the the single file packing and the trimming and all that stuff so that that takes a little while so you’ll see this is just and the output here all these lines of output are just what you would normally see in visual studio or vs code if you’re doing this as part of your ci cd pipeline then you will see all this in the output too so it’s nothing different it’s just a different way of actually running these things okay i’m going to kill this because the the compression of all the bits and pieces takes a little while and i already have a version that’s built so i’m going to run that container from the version that actually i’m going to show you i’ll show you the difference between some of these these new options that you’ve got with dot net core 3. so i’ve built a few of these images already i’ve built them with different variations of those single file and trimmed stuff so uh this is the the biggest version if i run that without the single file and without the trim then it ends up being 200 megabytes if i include the single file and the trimmed flags then it comes down to 170 megabytes what that means is that entire package which has got uh debian it’s got the linux operating system it’s got my binary for my application it’s got enough bits of net core to be able to run the website it’s got all my all my dependencies all my configuration the whole thing comes in at 170 megabytes which it is pretty much as small as you’re going to get a full asp.

net core app okay so that’s the difference those new flags mean so if i run this 13:25 - so i’m going to do a docker container run so imagine i could put i could share this now with with a docker hub actually already have so if you want to try this yourself you can these flags here just mean it’s a background container so detach it put it in the background it’s going to keep running and this flag means i want to send traffic into my host on port 1898 and that’s going to go into the container on port 80. so again i’m zipping through this it’s going to get a feel for how this stuff works okay so i’m going to run this container and then i’m going to open firefox and browse to that app this is the machine that i’m connected to my linux machine and this i’m afraid to say it’s another to-do list application they’re really easy to write so they’re good to they’re good for demos i’ve got nothing in here right now and this is connect this is running on a local sql lite database that’s inside the container uh i’ve got a to-do list here it’s all empty i can put some new items in but i’ve got a diagnostics page which is really handy for me to see what’s going on this is the name of the container this is the version of net that’s running so i can see it’s uh it’s running on preview 9 actually i’ve got the operating system architecture and the description so i know it’s linux i know it’s running on intel i know it’s running on.net core okay so that’s looking pretty good if i want to deploy this to kubernetes let’s go back here close this down here’s my application manifest so what this is saying is again it’s a slightly more elaborate version of the one i showed you in the slides but it’s the same principle i’ve got a service here which is the entry point to my app so kubernetes will listen on port 8081 it’ll send traffic into my to-do list container on port 80. when i scroll down here this is describing how kubernetes should run that container so this is the version of my container that i’ve already pushed to docker hub so i’ve done the docker image push this is all ready to go i’ve got my ports that i’m exposing there are a few other bits and pieces here so i’m taking uh i’m taking configuration from the cluster so i’ve created some configuration settings in kubernetes now this is one of the great things about these platforms is that you can separate your application from the runtime config so the app has enough logic inside it to look in this particular path and if it finds a bunch of config files it’ll use them and those config files aren’t there in the docker image they are provided by the runtime so kubernetes has this has this con configuration deployed when the container runs that configuration gets injected from kubernetes into the application it sees them there and it configures itself so i’ve got one docker image that i can run in dev in test in uat in production and just apply the configuration from the platform so this thing is really portable so that’s that’s an important thing to have okay so i’m going to go over to azure here and see if my cloud shell is still running yeah okay cool so i’ve got a resource group here with a bunch of things for today’s demos i’ve got a kubernetes service that has some windows and some linux nodes i’ve got a sql server database that i’m going to use later and i’ve also got a postgres database which i’m using for my to-do list application so there’s to do app that i’m running locally i’ve just run it in a container using sql lite it also has enough logic using ef core to go and talk to postgres and that’s what i’m using as ur so in my cloud shell here i’m going to kill this sleep i’m ready to switch between linux and windows i’m ready to deploy this because i’ve already got everything set up so if i do a cube custom cube title is the command line to deal with kubernetes cube other people pronounce it different ways it’s officially pronounced cubecuttle don’t let anyone tell you different so cubecastle’s get nodes will tell me what machines are running in my cluster i’ve got two linux machines two windows machines so i can run a variety of different applications but the way i deploy them and manage them is all exactly the same so if i look at the secrets that i’ve got these are the configuration items that i’ve deployed so i’ve already got ones set up i’ve got my in here in my to-do list config i’ve got a um all the information to connect to postgres so if i describe that particular secret which is called list config inside here there are two files json files and inside the secrets.json is the connection string to postgres so as a kubernetes administrator i can go and look at that but no one else can see that stuff okay so now i will go and deploy my app and i’ve already got that that same yaml file that i’ve just shown you is already on here uh let’s see where i am so let’s do it don’t make yeah let’s contact maps look okay so i can so the way i deploy my application i’ve got this yaml file and i do cube cuddle applied and what that does is it’s uh it’s a desired state system so kubernetes is going to take the description that i’ve got in this yaml file that’s my desired state it’ll look at what’s currently running and anything that needs to change it will change so i can see i’ve already created the service because that service creates me an azure load balancer with a static ip address i want to keep that but my deployment is new so it’s deploying my application now if i do a cube cuddle get all you will see i’ve already got a few things running so here’s my my new thing which is my to-do list web application which has started 25 seconds ago all these bits and pieces of how kubernetes does the different layers of my application so that’s all up and running that all looks good and if i browse to this application now if i do tube channel just to be sure get service these are the these are the external entry points so here’s my to-do list web this is the load balancer so it gets created by aks integrates with the rest of azure creates a real azure load balancer gets me an external ip address and it tells me what ports i can use so if i switch back to my test browser this is now my hitting my aks version which is talking to postgres it’s a brand new deployment this is a brand new container using the same container image that i’ve run locally i’ve already pushed it to docker hub kubernetes is able to pull that down and because of the of the the secrets inside the configuration uh it’s already set up to connect to postgres and postgres has some data already in there so i’ve already been putting stuff in here um the one good thing about this to-do list is it really so suits my workflow you can put new items in but you can remove them so if you’ve got a workload like mine this is this is perfect for you if i go to diagnostics here you’ll see actually this is a slightly different version because i’m running the release version of net core 3 but i’m running x64 i’m running linux so it’s the same app it’s connected to postgres because it’s getting the config it needs from kubernetes so same same workflow but a really nice way to do the deployment okay so that’s a brand new app so that’s fairly straightforward what about my old app so uh as part of my uh my kind of taking the oldest apps i can find i’ve got a docker file here that’s gonna package up nerd dinner like the original dinner from 2006 or whatever it was the dockerfile syntax is the same uh this there’s a few more pieces in here because it’s a more complicated app but actually the structure is exactly the same i’m starting from uh a net framework uh image so again microsoft owned this they keep it up to date this is based on windows server core and it’s got the full net framework for me i’m copying in the package config and doing a new get restore so exactly the same patterns excuse me same patterns to build my application copy in the rest of the source code and run ms build so all fairly straightforward the application image has already got um asp.

net 47 installed and then i’m doing all this weird stuff which is to do with setting up 20:43 - iis so i can get the logs out of the container i then copy in my application down here i run some powershell to set up my app i do that again this is a weird thing that you have to do because it’s a 10 year old application and sometimes there are weird steps and then the last thing i’ve got here is i’ve got a health check which is which tells docker how to check the application inside the container is still healthy and then i copy in the app the application so there’s a few bits going on here but it’s still 48 lines half of them are white space lines compare this to a 30 page word document with lots of pictures that say now click here and now install.net and now run the asp.netrage command this is much neater and it’s much more portable because all you need to build this is docker and the source code so i would do a docker image build to make to make this happen but you’ve already seen that process exactly the same process i would run it on a windows machine docker image build gets me the build package docker image push to share it and then i’m then i’m ready to run it so again i already have this deployed to my kubernetes cluster so if i go and check this out uh oh that i haven’t shown you the yaml file so similar thing to deploy this to kubernetes it’s got its own service because in this case i’m listening on a different port but of course i could have i would ordinarily use the standard http port and i would do things by domain name if i was running several apps on one cluster so this is listening on 8080. i’ve got my container image here that i want to use this is the one that i’ve built and pushed i’m saying which ports to use i’m using exactly the same pattern to get the configuration out of out of the kubernetes cluster and into my app now um the first app that i showed you use the net standard config system with json files this is using the old.net framework configuration system with the xml config files it’s exactly the same as far as kubernetes is concerned i’m going to deploy those xml files as a secret in kubernetes and make them available to my app in this location and the app is configured to pull bits of the apple bits of the config from there okay and then the last thing i’ve got here that i need to point out is this node selector so this is a docker image it’s pushed to docker hub kubernetes doesn’t know at this point when i ask it to run the app whether it’s for windows or linux so i’ve given this hint to say this is a windows app so make sure you run it on one of my windows notes okay so let’s have a look at this so i’ve already deployed this to make sure that the demos didn’t take all afternoon morning or evening depending on where you are so that’s what you can see here i’ve already got a deployment that i that i deployed an hour or so ago uh it’s running my nerds in a web application and i can see there’s a service here and there’s my external ip so exactly the same thing i’ve already got a if i do the cube cuddle describe secret and what was it called no dinner config then you’ll see the same thing i’ve uploaded this secret with my sql server connection string it’s a connection strings.config file exactly the same principle that i would use for an ordinary.

net app i’ve got a web config file that’s looking elsewhere for the connection strings 23:41 - this is already deployed so this is now running up in uh up in kubernetes so i can browse to this go back to my test browser and if i look at this there we are there’s no dinner so this is my 10 year old application it was originally written with with mvc like many many moons ago um i can log in this is all connected to my sql server database i’ve already put some information in here so this is connected to sql azure it’s getting the data that i’ve already run if i run a new pod it’ll connect to the same container and i’ll get the same thing out so exactly the same kind of workflow for an old windows app or a brand new.net core app running on linux okay so let’s have a look here let’s go back to our slides okay so those are the demos all that stuff’s on github you can check it out so just uh just to kind of recap what i did because it’s not a very long session i’ve got my kubernetes clusters now the great thing about kubernetes is it’s kind of the same everywhere uh there are different implementations for different parts of kubernetes but the yaml descriptions are always kind of the same so i can throw this on on my local machine i can throw on vms i can run up on aks in azure i’ve got my cluster i’ve deployed my application deployed my old windows application so inside the kubernetes cluster there are the secrets that have my my credentials for my database and my windows container and kubernetes is listening at the top on a particular port and it will send my my request into the container and the container will use the secret to get the connection string and go to sql azure i also have my brand new.net core application exactly the same principles totally different technology stack so again kubernetes is listening on a public port passes traffic to my.net core container which is running on linux that uses a secret to find the connection string for postgres and it all works in the same way now the beauty of this is you’ve got an awful lot of consistency across all your applications so all the artifacts are the same every app has a docker file and an application yaml file that describes it the docker if it’s a more complicated app i’ve shown you sync apps with a single component it was a more complicated micro service type application you just have more docker files a docker file for each part and the application yaml file brings them all together the pipeline is always the same and it’s really simple it’s a bunch of command line instructions that you can plug into as your devops or jenkins or github actions or whatever you’re using docker image build docker image push cube title applied to deploy it to your cluster and then we’ve got a consistent platform so for developers who are working with administrators who are managing stuff in production we’re using the same set of tools the same set of artifacts the same platform so it’s docker everywhere kubernetes in my test and production environments and it’s dotnet all the way through so it’s a really nice way to get consistency across a whole range of different apps okay and then here is the big list of uh big list of links so today’s demo is all on github uh if you’re interested in how this stuff works and you want to go right back to the beginning if you follow that link dak4.net that’s a workshop that i do at various conferences but all the contents online you can go to that website and follow along and you know take this whole thing much more slowly uh i’m in the process of writing a book learn docker in a month of lunches that’s the link to the book the first five chapters are done um if you want and the final thing is going to give you a discount to go and buy the whole book when it’s when it’s ready so thank you very much .