Level-up Your DevOps with GitHub Actions and Kubernetes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right i'm super excited because my next best friend in the entire world rob richardson is here rob how is it going going real well i'm so excited to be here this is such a fun show i we just made some really ridiculous videos so i'm glad we had a minute or two to play some of them but rob you are going to be talking about leveling up your devops with github actions and kubernetes these are all things that are i mean i know the githubs the kubernetes the pods of things the dockers i don't know so i'm super excited for it so take it away awesome well here's the part where i show you all my slides except for i don't have any this is it uh you can reach me on twitter at rob underscore rich or definitely net conf and let's do all the things let's take a look at the code which is already online it is right here uh github.com robert level up devops github actions kubernetes except for this project is empty so let's go build some code so that we can deploy it to kubernetes dot net new i love how in.net new we can go create a new project we could definitely use visual studio for this but if we just say dotnet uh let me actually put this back up at the top dot net new mvc dash dash name level up dev ops dash dash output dot to be in the current folder then it will go scaffold up a new project and you know net 5 so we've got a net 5 project spinning up i love that it will net restore on the way through as well and so now we've got our project let's open it up in vs code and yeah this is a regular.net project here's our home controller here's our views and so our home view let's say uh welcome to.net conf perfect so now we want to containerize this app the first step to getting anything into kubernetes is to start with a docker container so let's build a container our first step here we'll create a new file and we'll call this dot git ignore this is get ignore yeah there we go this is all the things that we don't want to commit to our repository so the bin folder the object folder star.user star.suo the vs folder these are all the things that we definitely don't want to end up in our github repository and then the first step to building a docker container is to build a very similar file a docker ignore now this dot docker ignore surprisingly has exactly the same format in fact if we don't have a dot docker ignore but we do have a dot git ignore docker will use that instead but there are a few more things that we don't want to end up in our container bin object user seo all of those things but we also don't want our appsettings.development.json to end up in a production container so let's add that appsettings.development.json we also don't want this launch settings.json launch settings dot json this launch settings.json is all of the details about how to debug our app we don't necessarily need to debug it when we launch it in a production container so we'll add that to our docker ignore next step we want to build a docker file now it's important that this is docker file and not dockerfile.txt so if you did create a dockerfile.txt come in here and rename it pull the dot txt off and we're golden okay from now we want to still stand on the shoulders of giant so let's head over to docker hub hub.docker.com and we will search for dotnet cool.net here in the dotnet repository we have a bunch of repositories that are linked places where we can go to get more things.net samples is really cool if you want to look at lots of different docker files that's a great place to go but we want to build so let's open up the.net sdk docker hub repository we'll scroll down and we want 5.0 so there is the the container that we will start with now scrolling down a little bit farther i see that there is an alpine linux tag which is really really small so i'll use the alpine linux tag so from uh dot net sdk five dash alpine perfect next step we want to do all of the standard build things so we'll do a net restore and then a dot net build we'll do it in release mode dot net test dot net publish now we'll also do this in release mode and we'll probably do this to the dist folder nothing magic about the disk folder but it's a convenient spot but we want to do all of these in dockerland so let's run these and that run command will then just get all of those things done well we do need to have it build our source code so let's copy in our source code and we will copy it from the directory where we run docker build on our local machine to the directory that we're currently in in our image well where is that directory let's just set this workdir src okay so uh the src directory will copy in all of our content will.netrestore.netbuild.nettest.net publish that's not that unlike a right-click publish inside visual studio but that'll give us all the files that we need to be able to deploy to iis or apache or in this case kubernetes okay we do then let's say cmb this will run as the container starts up and we will say level up up dev ops dll and this is now in the dist directory we do need a few more environment variables and other configuration details we'll need to tell it that we want to run it on port 80 we'd like asp.net to start up on port 80 we'd like to run it in production mode you know just the normal environment variable doohickeys i'll copy those in so you don't need to watch me type now this docker file would be perfect it works just fine but there's a few things that we can optimize to make it a little bit better right now we've got our source code in it right now uh anytime we build it we have all the build tools so let's head back out to docker hub and let's go to the.net asp.net repo now asp.net doesn't include the build tools it does include net and it does include all of the things that we need to be able to run our system so let's do that here from here and we will use alpine as well now we have a multi-stage build let's call this one our build server that's using that term and this one is our production server they're images they're not servers but you know we'll build this build server and based on that content in the dist folder we will then copy it into this second image and start to use that now in the second image we don't have our source code we don't have build tools this image will be a lot smaller but now we do need to copy the content into place let's copy the dist folder into our current folder and we can name this app now but this would copy it from our local machine we don't want to copy it from our local machine we want to copy it from build build well where is the build stage let's just name this as build so now as we go through and do all the things once we get to this dist folder we will copy the content from that build stage dist folder into the current directory and now this one will be a lot lot lighter perfect there's one more piece every command in this docker file is a different layer that will get cached right now with dot net restore and with copy and then dot net restore if i change one cshtml file or one javascript file it will need to re-restore all my nuget packages so i really want to run.net restore before i copy i still need to say what is the manifest that you're copying from so level up devops.cs proj into the current folder then i'll run.net restore then i'll copy everything else and then build test publish so now if i'm just changing a c sharp file or a cs proj file i don't need to re-restore my nuget packages my build is faster score now we've got a docker file that allows us to build up this image that exactly maps to the site that we want to build all of our source code comes in and we'll only continue if our tests pass if our tests don't pass we don't end up continuing we don't end up with a new image perfect if you write a failing test i can't publish your image so we've got all the content we have our docker file we have our docker ignore file we have our git ignore file and we have our website let's commit it so level up dev ops let's stage all the changes we can come double check and make sure we aren't submitting too much that you know we're not submitting the object or bin folder once we're comfortable with that all of these are new files so diffing it you know we had nothing and now we have the file once we're good let's commit cool uh git status bit push origin main cool backup on github we can now refresh and we have all of the things perfect we did forget one more thing and it's one of the things that i always have to go reach for uh we need to go grab a kubernetes yaml file now this kubernetes yaml file is of course the um the file that we used last time so you know here's our last time thing we've got a service we've got a deployment we've got an ingress and each of those reference whatever the project was last time let's switch this out with level up dev ops and swap that into place now here in our deployment we specify the number of containers we want to launch in this case one and we do have some variables that we'll want to swap in at build time same thing with our service our service will reach out to listen on port 80 and forward all the traffic to port 80 in those containers and then our ingress will map dns using http application routing in aks up to our service perfect so in hindsight we probably should have committed this in one spot but here is the kubernetes yaml file let's also go grab a readme because that'll be helpful to add to our project as well and that'll just talk about the stuff that we've got great kubernetes yaml file commit that hit push and we've got all of our content up on github now the next step is to go build an automated build and that's where github actions is really really powerful as i come into github actions i'm presented with various templates various examples and so i can come in here and i can pick from lots of things if i'm using dino or pylint or cmake or d i can come in here and look for lots of stuff now it thinks i have a jekyll site that's interesting let's look for docker um here we go let's set up this workflow now ultimately it's going to create this docker one let's call it docker and kubernetes cooper netease oh uh let's actually look at one thing on our way in um this wasn't quite an empty repository i did come in here into settings and i went into secrets and i added a few new secrets i clicked on new repository secret i gave it a name and a value and so i created a secret for my acr username password and url um my azure container registry i also created a secret for my azure kubernetes service url and my cubeconfig that i got out of my user directory with those secrets in place i know that those won't ever end up in source control they aren't part of my source but i can use those as part of my build coming in here into the github docs i just do dollar at curly curly and then i can name my secret and that will pull that secret into my build file great let's come back into our build and do exactly that now i probably want to build on the main branch i probably don't want to automatically publish on puller requests so i'll remove that docker build and push kubernetes deploy okay so it starts on ubuntu latest that's perfect we start out by doing a checkout and then we can run some content now this is going to run a docker build let's add this and we will run some more stuff it's going to do a docker build of course the docker file is docker file the tag well you know maybe let's not tag it this way let's tag it with dollar uh secrets dot acr url and then level up devops and then the version which we will say is uh github dot sha at github dot sha that is the current git hash that we'll use so we're gonna do a docker build and we'll automatically tag it next thing we want to do a docker push docker push let's push this exact image and the cool part because we have our repository url right in there then we've just got this in place so now one of the things that we need to do though is we need to log into our acr uh our azure container registry that's where the marketplace is really helpful log in to docker registry here's one docker login now we can click through to look at the full marketplace listing but this one looks pretty sweet let's just copy it in place and set it right here okay yeah we need to indent a little bit better but we've got it okay so docker login we can log in with username password registry let's log into the registry we'll use secrets.acr a c r url and that will get us there username let's go grab our acr uh username our password yep let's log in with our acr password and now we are logged in to docker log out afterwards sure that works so now we're logged into docker so when we do a docker push we know that that is going up to the proper spot we're also going to want to do a cube ctl apply we'll apply the k8s.yaml file and that will get our content into kubernetes so we probably need to log into kubernetes as well let's go back to the marketplace and go search for a kubernetes login uh let's say i want to kubernetes set context here's a kubernetes set context awesome let's grab this content and set it in place now there's a couple of ways that we could log into kubernetes um we could log in with a cubeconfig that's what we'll use we could also log in with a context a kds url cada secret but cubeconfig works okay so what is our cubeconfig well we just happen to have this cubeconfig in our secrets so let's go grab this and set that uh cube secret no cube config in place and now we're logged into kubernetes so now when we do a cube ctl apply that will go into place but we do have a few of these variables inside this kubernetes file that we need to swap into place we have aks url we have image label acr url so let's do this command now it is a little bit hairy so i'll just copy and paste it into place but this command will do a sed a sed so in the file kds.yaml go find all the places where it says acr url and swap it with the secret go find all the aks urls and swap that with aks url go find all the image labels and swap them with the github shop we're doing this specifically after we push the image so that there's no possible way that this secret could end up in the image add to that that we're doing it right before we apply it to make sure that all of those pieces are in place great so now our build is doing exactly that docker build docker push cube ctl apply after we get all of our secrets into place in that kubernetes elmo file to be able to dock or push we log into our docker registry in this case azure container registry and to do a cube ctl apply we log into our kubernetes context let's start committing this create docker kubernetes that sounds like a good one create a build process we're going to commit it to the main branch and then straight away it's now part of our repository it's just this file here in our repository so if we do something like git pull oh get get branch dash u origin main hit pull now it's just one of the files in our repository and we could edit this with vs code we could edit it with any editor that we like it's just one of the files in our repository but it being one of the files in our repository it also kicks off a build we have the build light right here that's cool we can also go into actions and reach for it but let's go straight into the details of this build now it's doing all of the processes it's going to log into docker it's going to set the context and now it's starting to do the docker build so it after pulling the images now it's copying in our cs proj it's going to do a net build it looks like that build succeeded very nice.net test all of our uh no tests worked and ultimately ooh dot net publish dash c release oh i have a typo right there let's go back into our docker file and this should be dash oh dist okay good call uh fix typo we'll add that to the registry we will uh commit it get push origin main and up pops another build this will be awesome unexpected but kind of perfect let's go in the other way to github actions to get a feel for how we can do there from actions here's all of our workflows now in this case we only have one workflow so let's dig into that and we could choose to edit our workflow if we wanted to or we can dig straight into the build details here's the build details and let's take a look at it and now it's going to do all the things once it pulls all of the docker base images then it will kick off our build so workdir src that's the first line of our docker file perfect now it's going to copy in all of our content then it will do a net restore in this case there aren't any new nuget packages that we don't already have dot net build our build succeeded very nice dot net publish is our publish going to succeed it did very nice now we're building up our production container that worked out great now let's start to push it up to our azure container registry some of our layers already existed but let's push up our new thing that worked out great and now our job is complete perfect we got a green check so if we go into cube ctl get all we can take a look at all of those resources that recently got pushed up to kubernetes this has been launched for 16 seconds our pod is running our service now it may take a little bit for dns to propagate to get to our kubernetes ingress controller so we can actually come out here to kubernetes documentation where we look at port forwarding we can actually port forward straight into our service to take a look at doing that a little bit faster so let's do that cube ctl port forward service slash level up dev ops 8080 to port 80 in azure and now that we have this port forward we can come over here to localhost 8080 and we will get straight into our website here is our welcometo.net conf that we built from scratch and built up that uh build pipeline and set that in place now that's interesting what if we want a build pipeline for say pull requests okay let's come back in here to our workflows folder and we'll create a new file dockerpr.yaml and in our pull request build we probably want to do pretty much what we've done here okay so in this case we'll say on branches not main so you know all of the other branches and then also on pull request for all branches if you're doing a pull request i'll kick off this build now in this build we probably don't want to cube ctl apply i don't want to update production when you kick off a build so to that end i don't need to change all my secrets in the yaml file i definitely don't need to push it do i want to tag it with my acr url no but i'll tag it with uh dash pr just so that i know that i can tag this and i definitely do want to build it but because i don't need to push to kubernetes i don't need to log in i don't need to log into docker and now i've got this pr build set perfect while we're here when we're about to do this commit let's actually come in here to our build and we will look at this build process let's go create a status badge on our readme copy status badge markdown i'm going to set that in place right here on my uh build and let's put it right there and let's commit both of these things pr build and build status oh yes i do want to stage all the things now let's commit get push origin main and now the magic it is so much fun to watch these builds go it is really cool let's go back into our build yes we kicked off a build and now we get to do all the things once we've got an agent we do exactly that process let's go check out our let's pull the base images let's copy our code into a place let's go kick off a docker build and inside the docker build we'll do a.netrestore.netbuild.net publish and then once we've got that image set in place we'll cube ctl apply it up into kubernetes and we've got our site ready to go what's really really cool is now if you come to uh github.com rob rich slash level up dash devops github dash actions dash kubernetes we have all of the code including all of the builds they're here right now online and they're ready to go if you're watching this online you can definitely grab me on twitter at rob underscore rich or now that we're live what questions do we have beautiful rob now i gotta say there's some mastery of your yaml i'm very impressed from scratch knowing where to go i'm still a newbie when it comes to yaml but i am excited to see this and definitely we'll take a look at this i gotta say a lot of people i'm hearing flawless demos in here loving it from scratch shredhar says this demo is exactly what i needed everything from scratch also jacob over here just wanted to say presentation is superb enjoyed jokes structure and content yes a lot of people in twitch as well coming in with flawless demos so flawless that we didn't even have any questions awesome so glad it worked well awesome rob thank you so much i cannot wait to go figure out more github actions and finally figure out how kubernetes works one day one day yeah and if you'd like to hit me up on twitter and let's get on twitch and uh pair on this it could be really fun to help you get a leveled up in kubernetes that would be awesome all right rob we'll have a good night and i will talk to you later sounds great thanks everybody
Info
Channel: dotNET
Views: 2,664
Rating: 5 out of 5
Keywords: .NET
Id: TMbXwNtuAJo
Channel Id: undefined
Length: 28min 35sec (1715 seconds)
Published: Fri Nov 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.