Building a 10-Node Kubernetes Cluster on Raspberry Pi & Ubuntu Server

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] hello again everyone and welcome back to learn linux tv i have a very fun video today i am going to show you guys how to build a kubernetes cluster with raspberry pi's and i've done a similar video to this but i figured i would go ahead and update it and i've also decided to do it a little bit bigger right here is the final finished raspberry pi kubernetes cluster there's 10 nodes and a controller in there so this is a bigger cluster than the one i've created in the previous video again i just wanted to update this one but also go big or go home right well i'm already home so i may as well go big so what i'm going to show you how to do is build a 10 node kubernetes cluster and it's going to happen in this video now it doesn't matter if you don't have 10 raspberry pi's even if you only have 2 or 3 it's still fine i have a bunch of raspberry pies in the studio so i figured i would go ahead and make a 10 node cluster because again go big or go home and for this build i decided to use the 12 layer raspberry pi case from geek pie which is one of my favorites it's a little tedious to set up to be honest but once you get it set up it's pretty cool it has glowing fans in the front as you can see and again you can store 12 raspberry pi's in this case they also make a version that has four layers if you want to create a smaller cluster again you don't have to have ten nodes i do but you can have two or three pies and that's totally fine so i'm going to give you all the instructions in this video to build your own i'll show you the unboxing of the case itself i'll put it together i'm even going to show off some raspberry pi power over ethernet hats that'll enable your pies to power on over ethernet which means you won't have to dangle power cords all over the place to get your cluster going i'll show you that as well and then i'll show you how to set up and configure the actual cluster if you follow along with me by the end of this video you will actually have yourself your very own kubernetes cluster running on the raspberry pi so let's go ahead and dive in [Music] so i bet you're itching to get started but before we do there's a few notes that i would like to go over first first of all i want to go over the outline of this video and how it's structured this tutorial will be split into several sections there's time codes in the description below so that way you can go right to the section that you want to start with the first section is this section right here it's all about getting started in addition we're going to talk about what you need in order to get started in the next section i'm going to talk to you about writing the sd card each of the raspberry pi's in the cluster will obviously have an sd card so i will let you know where you can get the sd card image that we'll be using and then i'll walk you through the process of writing that image to the sd card itself in this section after that i'm going to walk you through the process of building the cluster rack that you saw in the intro it's completely optional you don't even need a case you could just have the raspberry pi's dangling from something it wouldn't even matter but having a case is a really good idea and the cluster rack that i have works pretty well too but again it's optional in the next section i'm going to walk you through the process of installing docker and after we do that i will walk you through the process of setting up your very own kubernetes cluster which is the main goal of this entire tutorial once we finish that we're not quite done because a cluster is essentially useless if we're not running anything on it so at the end of the video i'm going to show you how to run a container on your cluster so the question that you might have right now is what do you need in order to get started i mean the title of this video is about a 10 node raspberry pi cluster so are you going to be buying 10 raspberry pies well not exactly unless you want to so let's talk about what you need in order to get started right now first of all you'll need two or more raspberry pies i think three is a sweet spot and i always recommend the raspberry pi 4 as of the time i'm recording this video it's the latest and greatest you can get it with up to eight gigs of ram so if you can i recommend getting the eight gigabyte versions now if you want to save some money you can get away with a four gigabyte raspberry pi for the controller for the worker nodes you could also get away with four gigabytes as well but i do recommend eight gigabytes for the worker nodes if you can but i understand money doesn't grow on trees so just use what you have available now i haven't actually tested this on the two gigabyte version of the raspberry pi so if that's what you have your mileage will vary but again try to get eight gig raspberry pi's if you can if not and four gigs should be fine and as far as how many use as many as you can at least two preferably three but if you want to set up a 10 node raspberry pi cluster like i'll be doing in this video then you'll need a lot more and of course you will need a power adapter and sd card for each pi make sure that you actually get the fastest sd card that you can if you have a slow sd card when it comes to your raspberry pi kubernetes cluster you will feel some pain in the description down below this video i'll have a link down there and if you click on that link it'll take you over to the store page that will have a listing of all the components that i'm using in this video now another thing that you can consider buying is a cluster case similar to the one that i'm using in this video again that's optional but if you can get one you may as well in addition to that you can also choose to use power over ethernet hats as well that's also optional but they're pretty cool because you could actually power your raspberry pi over ethernet like the name implies and when you're building a cluster that's a great thing because with power over ethernet you won't have a bunch of power cords all over the place so if you do go with power over ethernet hats then you will not need the power adapter that i mentioned in the second bullet point but i'm going to leave that up to you if you have access to raspberry pi power over ethernet hats and you also have a compatible router or switch that is able to provide power over ethernet then i highly recommend you consider that and that's basically it in the next section i'm going to walk you through the process of setting up the sd card with ubuntu so i'll see you in that section [Music] all right so here i have a windows pc i want to show you the process of downloading ubuntu and writing the ubuntu image to an sd card and i figure that quite a few of you are probably running windows so this would be a good example operating system to use but the tool that i'm going to show you is available for multiple operating systems so it doesn't matter if you are running linux windows or mac you'll be able to use the same tool so what i'll do is open up a web browser and then we'll go to ubuntu.com and then we're going to go to the download section here and once you click on that you'll notice that there's a raspberry pi specific download link so that's the one that we'll go with and then we'll scroll down and we have two options we have ubuntu server 2004 and ubuntu server 2104 now what i'm going to recommend is that you go with ubuntu server 2004 it's an lts release that stands for long term support and that's a better fit for things like kubernetes now something like ubuntu server 2104 might work but you might also run into problems because generally speaking enterprise applications such as kubernetes are most oftenly only built with lts releases in mind so that's what i'm going to do i'm going to download this one right here if you download something else for a different distribution then i wish you luck unfortunately i won't be able to help you because the content in this video was only tested with ubuntu 2004 so i'm going to save the file but we're going to need a tool that we can use to write the ubuntu image to an sd card so what i'm going to do is search for usb imager and it's all one word we have a gitlab link right here i'll click on that and i'll scroll down and you'll notice that we have download options here for multiple operating systems so regardless of whether you are running linux windows mac raspberry pi os or any of the other options that you see here there's a download for you and that's one of the reasons why i like usb imager so much because i only have to show you one tool and that will work regardless of the operating system you are running so i'm going to go ahead and download this first option here for windows i'll save the file and now both files have downloaded so in my downloads folder we have the two files there so i'm going to extract usb imager so that's ready to go but we're not going to click on it just yet now in my case i use all of my sd cards for various raspberry pi linux distributions and it's often the case on windows that you have to go through an extra step that you don't have to go through on any other operating system and i don't really understand why this is but sometimes you have to manually delete the partition table in order for this to work so what i'm going to do is right click the start button and then i'm going to go to disk management and i've already inserted the sd card so if you haven't already done that then i recommend that you do and we have the sd card right here now keep in mind the process that i'm going to walk you through right now is going to erase everything on your sd card we need to dedicate the sd cards to the raspberry pi's so if you have anything important on your sd cards then i recommend you back all of that up first before you continue so what i'm going to do is delete the partitions that are on it okay now that that's done we should be good to go ahead and use usb imager to write the ubuntu image to the sd card so i'm going to double click on this and we can safely ignore this warning right here i'm going to run it anyway and then i'm going to click on the three dots right here i'm going to go to my downloads directory and select the ubuntu image that we've downloaded earlier and then here i'm going to select the sd card that i want to use for this purpose this is my only option and i know that this is my sd card because that's the name of my sd card reader so we should be good to go if you have everything selected then all we should have to do is click on the right button right here so i'll do that now and now it's writing the ubuntu image to the sd card i'll be right back as soon as this is finished all right so it looks like the process is complete on your end just repeat this process for however many sd cards and raspberry pi's you intend on setting up i also recommend that you have some sort of static lease for the ip addresses for your raspberry pi's that'll help you ensure that the ip addresses for your pies won't change so i'm going to switch over to my linux pc where i have the password to my firewall saved and i'll show you what that looks like on my end what i've done off camera is in the dhcp server i have a vlan called servers so what i did was i set up a reserved ip to match the mac address for each one of these so every time they boot up and ask for an ip they're always going to get the same ip and that's important and the thing is most residential routers nowadays allow you to set a static lease for your devices and again that is my preferred way of setting up an ip that doesn't change once you've done that and each of the raspberry pi's have booted up and they're online you can go ahead and move on to the next [Music] section [Music] so here i have the 12 yes 12 layer raspberry pi cluster case and what i'm going to do is take it out of the box and then i'll show you how to set it up of course we have this instruction manual always good to have even though add people like myself we don't really read instructions but hopefully you won't need instructions because i'm going to walk you right through it so we have a number of these sd card extensions right here which essentially allow you to have easy access to the sd card so that way you don't have to take the entire case apart in order to reach your sd cards i'll show you in a moment but we have quite a few of these as you can see and we have three fans and these fans actually glow which you'll see in a moment so we have three of these let's go ahead and put those aside of course we have the hardware that's going to be required to get this set up various screws and miscellaneous items right here that we will shortly need and this is kind of like an alignment tool that just helps you install the layers a lot easier but you'll see exactly how this comes into play in just a moment we're going to need that i have the actual case right here let me go ahead and get that unwrapped and here we have the actual case itself i just removed the plastic wrap i have these little layers here as you can see and this is what we're going to be building so what i'm going to do is remove the paper backing from both sides of both of these right here and it's a little annoying so i'm not going to have you painfully watch me chip away at this because i understand from a previous case this can be a little tedious so i'll get this off and then i'll be right back so here's one of them and now here we have the other side so first of all we do have a screwdriver in this baggie right here that we can use to get this set up so what we're going to do is we are going to take one of the sd card extenders out of the baggie so we'll need one of these per raspberry pi so we have this raspberry pi right here i've taken it out of the box and then what we're going to do is we're going to locate the sd card slot which is right here and we're just going to insert the sd card adapter here into the slot make sure that all of the holes line up as you can see and what that does is it extends the sd card slot out to the edge as you can see here which is going to make it a lot easier to reach once we have it in the case and there's actually plastic film on this there's plastic film on everything it's actually very tedious and annoying it probably took me about 10 minutes to get the paper off the side so i find myself just constantly having to peel this kind of thing off but i guess it makes sense we don't want it to get scratched on the other side we have more and there we go and what you want to do is just take a moment to make sure that everything lines up so you grab one of the plates and then the raspberry pi with everything assembled and you just line up the screw holes we're not actually going to install it directly against the board but we just want to make sure that all the holes line up so we know which way we should have it orientated and then next we're going to grab five of the smaller screws and five of the standoffs so basically these right here and that's what we're going to use to attach the raspberry pi to the board and what we're going to do is we're going to attach the standoffs to the bottom of the raspberry pi as you can see here i've already done that i just have them sitting in there and then we're going to line that up with the plate and i'm not sure if it's going to focus well but i essentially just lined up the plate with the standoffs and then we're going to secure it with screws i'll just get them started looks like i missed one right here now we could tighten that down a little bit more later it's not actually attached completely because we still have these screw holes that are kind of just you know sticking up there we need to actually mount those and get those secured and on my end i just used my thumbs to tighten everything down it's not like the raspberry pi is going to run away but essentially these little nuts i added those as you can see right in front of my finger there and i just added one for each as you can see now the raspberry pi is secure it's not going to run away if it ran away i'd be very sad in addition i have a few power over ethernet hats and here's one of them right here so essentially it allows you to power on your raspberry pi without a power cord if your switch supports power over ethernet you only need to connect an ethernet cable after you install this so i'll show you what it looks like and here is the power over ethernet hat right here and this is my favorite because it's not actually going to dangle off the back of the raspberry pi like some of them do this will actually be completely hidden once you get it installed and obviously it's completely optional but i really like having power over ethernet because well i don't have a bunch of power cords to contend with so now what i'm going to do is show you the extremely tedious and time-consuming process of getting this installed actually that's it that's literally all there is to it you just slot the power over ethernet hat onto the top of the pi try to get that even closer to the camera so you can see it and believe it or not that's actually it if you attach an ethernet cable and the other end to a switch that actually supports power over ethernet this will actually cause it to turn on and it's just amazing i love this power over ethernet hat i have a bunch of them and literally i have one on every single pie that i have in my server rack now what you do is you basically repeat the process for every single pie that you have and get it onto this little board right here and then once you have all of your pies onto these boards we could go ahead and finish the installation so what you're going to do next is just lay this flat simply line up your raspberry pi on the board just like this and repeat the process for every single one of these so we're going to just assume that i had a raspberry pi on every layer it's much easier to add the raspberry pi's to every layer before you actually finalize the build but i'll go ahead and add mine later off camera i just want to show you guys how you actually put this together you also have these right here which are going to be for the fans so i'm going to remove the rubber band from this we'll put this aside and we actually have these little pieces and of course there's plastic on everything which is actually kind of annoying but you know i get it they want to protect it that makes sense i hope you have longer fingernails and i actually forgot a layer so right here go ahead and slot that on there so next what we're going to do is we're going to grab one of the fans so i have one right here so now we have these longer screws right here so we're going to use those to secure the fans to the plastic brackets so here's the fan all we're going to do is get that lined up and go from underneath through the top and we have the longer nuts we're going to just attach those same thing on the other side so we have one of them and we're going to do the same thing down here on the other side and there you go i have the plastic brackets on this fan right here i'm going to repeat the process for the other two and then i'll be right back all right so the fans are all done so i'm just going to slot them into the case so now we have the fans installed and then we can just go ahead and put this other side of the case on right here and it's a little challenging which is actually why we have this tool right here which we can actually use by just attaching it to the back of each because it's really hard to line up without it and it's still a little challenging even with this red thing so i guess it didn't really help me all that much but and i guess i just broke it so there's that one of these pieces came off the alignment tool but whatever it doesn't matter we don't really need it anyway and very carefully what i'm going to do is attach these little bars so go underneath so i don't want it to come apart so i just attached it right here grab one of these screws to tighten this down same thing right here just pop it right through a couple more of these we have one final one right here that is actually going to go there and we're going to go ahead and secure this one with screws as well and now it's actually all finished well we haven't connected the cable yet for the fan but at least the actual shelf itself is finished now you'll definitely want to have added all of the pies to all of these little layers before you put them into the case like i mentioned earlier i only have this one right now but i will add more later on but we have the actual fan cables and we need to connect these fan cables to the raspberry pi so that the fans actually get power and if you don't connect the fans i know from experience the pies will get very hot and they might throttle you definitely want to use these fans right here they're here for a reason now here i had a lot of trouble getting the camera to focus but pay attention to the colors on the cables the black and the red there are variations of the raspberry pi power over ethernet hat but if you bought the same one that i have then it should look something like this and that's about it in the next section we're going to install docker and then after that we'll get our cluster set up so at this point you should have already written the sd card image to all the sd cards for however many raspberry pi's you intend to use in your cluster then you insert the sd cards into the raspberry pi's and power them on and after a while they should all be available on the network what we're going to do now is use ssh to connect to the raspberry pi's and configure them i'm going to connect to the first one right now so i will use the ssh command and the username is ubuntu and the ssh command is built into pop os the linux distribution that i'm using here on my laptop if you are using something like windows you could use the windows subsystem for linux that'll give you access to the ssh command on windows if you are using a mac then you already have the ssh command like i do here so again the username is ubuntu i'll type at and then i pasted in the ip address of one of the raspberry pi's because i'm too lazy to type it all out manually and then i'll press enter by default the password for the ubuntu user is simply ubuntu now i've already gone ahead and changed the password on my end what generally happens the first time that you connect is it's going to ask you for the current password which again is ubuntu and then it's going to ask you to change that password after you set the new password it's going to disconnect you from ssh yeah i know that's kind of rude but you just simply reconnect with the new password and you should be good to go now this is completely optional you can keep using the ubuntu user but i recommend that you create a new user for yourself it's just etiquette again not required but in case you want to go ahead and do that i will walk you through that right now so what we're going to do is type sudo add user and then the name of the user you want to add i'll just type my first name i'll press enter and it might ask you for your sudo password which is the same password that you set your ubuntu user's password to and now i am setting a new password for my new user i'll type it in again and you can fill out these fields if you want to they're not required i'm just simply going to press enter to skip them and for each prompt i just pressed enter so i will clear the screen the next thing we'll want to do is give our new user access to sudo otherwise it's not going to be a very useful user so i will type sudo and then user mod dash lowercase a uppercase g and the group i want to add the user to is sudo and the username that i want to add to that group is the one that i've just created it's that simple so i'll press enter we should be good to go so i can hold ctrl and press d to disconnect and then i can press the up arrow to recall the command that i used to ssh into that device i could change the username to my username type in the password and now i'm connected now notice that i have set the hostname here i did that off-camera but it's really easy to do that and i highly recommend that you set that unless you are working ahead you most likely have the default hostname so i will show you how to change that so we're going to type sudo nano etsy slash host name enter i'll type in my sudo password this will also help us test that sudo is working for our new user and there we have the host name so basically what you do is you add your desired host name to this file right here and this is node 0 1 so i named it k8s hyphen node hyphen zero one but again you can name yours whatever you'd like and to save it you hold ctrl and press o to bring up the save dialog you press enter and then ctrl x to exit out the other file that you will want to edit is slash etsy hosts and you can see that i already have a line there the second line i've added that it wasn't there before and what i did was i added a localhost ip of 127.0.1.1 and then i typed in the fully qualified domain name k8s hyphen node hyphen01.homehyphennetwork.io but you don't have to type a fully qualified domain name at all especially if you don't have a fully qualified domain name you'll notice that the name of the host itself without the domain name is there at the end and if you don't have a domain name you could just simply do that and add only that part just the host name once you have that set you hold ctrl press o and then enter and then ctrl x to exit out and the next thing that i recommend you do is install all of the updates you could do that by typing sudo apt update ampersand ampersand sudo apt dist hyphen upgrade and these are actually two commands that are separated by two ampersands so the first command sudo apt update what that's going to do is synchronize the local package repository index with the online repository servers for ubuntu just to make sure it has an up-to-date catalog of everything and then if that command is successful it's going to run the second command sudo apt disk upgrade and that command is going to actually update all of the packages on the system to make sure that everything is up to date and securely patched so i'll press enter now on my end i went ahead off camera and updated everything to save a little bit of time the process to update your ubuntu-based raspberry pi the first time will take a bit of time it depends a bit on the speed of the sd card that you have but it did seem like a long time to me even though i have fairly decent sd cards but that's going to make sure that everything is up to date so what you'll want to do on your end is repeat all of these steps for every raspberry pi that you intend to use set the host name create a user if you want to install all of the updates and once you've done all that on all the raspberry pi's we can go ahead and continue now what i'm going to do on my end is i'm going to open up a few new tabs in my terminal emulator here and in each tab i'm going to connect to one of the raspberry pi's so i'm going to set it up and then i'll be right back all right so i have a tab in my terminal emulator now open to every raspberry pi that i intend to use in this project so we have the controller here's node one node 2 3 4 5 and so on so the way that this tutorial is going to work going forward for the rest of the video is every command that i give you will need to be executed on each raspberry pi unless i tell you otherwise most of the commands are going to be the same on each even though one is technically the controller yes it is different there's going to be a few unique commands for that one but for the most part the majority of the commands are going to be the same for each so again if a command i'm going to have you run is specific to the controller or one of the nodes i will let you know otherwise just assume it's the same for each so first of all we have a file that we need to edit and we're going to use sudo because we will need root privileges i'll use nano for this video as the text editor even though i prefer vim but nano is easier to explain and the file that we want to edit is slash boot firmware and then cmdline.txt just take a moment and jot that down or you could check out the wiki article for this video which should already be online it'll have all the commands in there that we've gone over in this video so if you prefer to copy and paste you can do that go ahead and check out the description below there's going to be a link down there to that wiki article that will again have everything you need if you want to copy and paste anyway i'll press enter i'll type in my super secret password and here we have the file now because i have the font size set so high in my terminal i want to make sure that you guys are able to see it we'll need to scroll a bit to get to the end we're going to need to add some additional things to this file now we don't want to create a new line what we want to do instead is add all the things i'm going to have you add to the very end of this one line if you do this correctly there should not be a second line so i'll just simply press end on my keyboard to go to the end i press space so for me fix rtc is the last item on this line and what i'm going to do is paste the new items right here now make sure that you put a space after the last item here we don't want it to blend together and it wrapped a bit so i'll go to the left i pasted in each of the new items and if you recall fix rtc was the last thing here so i added c group underscore enable equals cpu set c group enable memory and then c group memory and then swap account we're setting that equal to one again these items will be in the description down below in the wiki article so don't worry too much about it we're putting all of this on one line and i've done that so what i'm going to do is hold ctrl press o and then enter to save the file and then ctrl x to exit out and i won't bother to explain how to save files going forward in the video i think you get the idea but we have edited that file and we have added the additional required settings to the end of the first and only line in that file and i'm going to do the same for each of the other nodes i'll do that off camera on your end just go ahead and repeat that process for every single pi that you have okay now that you have all of the options set in the command line.text file we can continue in fact what i'm going to do right now is reboot each of the pies to make sure that those c group settings are applied and we could do that by typing sudo reboot so i will do that in each and i'll give that a moment for each of these pies to reboot and then i'll be right back all right so i'm reconnecting so next what we're going to do is install docker so here's the command right here to install docker just basically curling the get.docker.com installer so we're fetching that and we're going to pipe that into sudo sh as you see here so i'll press enter i'll type in the super secret password and the script is running and there we go docker is now installed on the controller and we'll need to do that on all of the raspberry pi's so i'm going to repeat that same command on all the other ones and then i'll be right back all right so i've run the docker install command on each of the nodes it looks like they're all done so far well there you go now that docker is installed you can continue on and move on to the next step now this next command is optional what we're going to possibly do is add our user account to the docker group and i like to do this because this way i don't have to use sudo in order to interact with docker it just makes it a little bit easier so paste in the command right here we're basically just adding our user to the docker group with the user mod command as you see here it's a very quick command so i'm just doing that on each and that should do it so next we need to edit another file and i've pasted the command right here we need to edit the daemon.json file that's located in the etsy docker directory we're going to use sudo nano to edit that file and i'm not going to go ahead and spend too much time explaining this i'm just going to paste it in right here there's some additional options that we need to set for docker we need to set the storage driver max size for the logs as well as the log driver and we're going to set the c group driver to systemd we're going to allow system d to handle the management of the c groups discussing what c groups are and why we care about them is beyond the scope of this video but it is required for docker to run so i'm going to save the file and exit out so off camera what i'm going to do is edit that same file on each pie the contents will be the same on each pie and when i'm finished with that i'll be right back now the next thing we want to do is enable routing which is disabled by default and it's actually very easy to do so we're going to edit yet another file and the file is slash etsysctl.conf just like you see there so i'll press enter and this file already exists but we're looking for a very specific line so i'll scroll down a bit here and it's this one right here what we're going to do is enable ip forwarding so i'm going to uncomment that line that's the only change that i'm going to make and then i'll save the file and once you've finished making that change you can reboot the raspberry pi yet again and now what you're going to do is repeat that for every node in the cluster okay now that each pie has rebooted let's go ahead and run a quick test and make sure that everything is working so far first of all we can run systemctl status and then docker and what this will help us do is ensure that docker is running and on my end everything is green it's active and running you see that four lines down and that's what yours should say as well so i'll leave it up to you to check the status of docker on all the other pies and another thing that we could do to test is run docker run hello hyphen world and what this is going to do is test that docker is actually working by running a test container called hello world and if the container successfully runs then we know docker is definitely working so it's downloading the image and it ran it the hello world container image is very small so it downloads fast and it's already run all it does is print out the text that you see on the screen right now so the very fact that we see that text means that docker is working if you have any problems with anything so far just make sure that you followed every step correctly make sure you've set the c group options the docker is installed it's running without errors if you have any problems then go over to the community forums for the youtube channel there you can ask in the forum topic for this video to see if someone can help you out [Music] so next what we're going to do is add the kubernetes repository to every pie so we'll use sudo and then nano and the file that we want to edit is slash etsy apt sources.list dot d and then kubernetes dot list and since the file doesn't exist yet it's blank so i'll paste in the repository line it's this one right here and i do know that zeniel is not the proper name for the release of ubuntu that we're on but we're going to ignore that because this is the proper repository i guess they're just really late in renaming it but anyway i'll save the file exit out and then as you've guessed you basically just do this on every pie i'll do that off camera so go ahead and pause the video if you need to and once you're done we'll continue in order for that repository to work there is a gpg key that we will need to add again to every pi and i've pasted the command right here so essentially we are going to use the curl command to pull the gpg key straight from the google package repository and then we're going to pipe that into sudo apt-key add to add that key to our apt database and it's actually a very fast command so i'll paste that into every node and next what we need to do is run sudo apt update and that will help us make sure that apt is up to date with the repository index for the new repository that we've just added so i will copy this command because again just like with all the others we need to run this on every pie so i'll press enter and we'll wait for that to finish and this one is done now a few times in the past i've actually seen this command fail but then it would work fine if you rerun it so i'll check every host to see if any of them failed and here's an example of one of the failures that i was talking about we'll come back to node three four five six finished seven failed now i haven't actually looked too deeply into this but i've theorized that there's some sort of rate limiting with google's repositories i just had about 10 raspberry pi's technically 11 if you count the controller all reach out to the google repository at the same time and since i'm behind the same public ip i probably flagged something so again if we rerun this it should be fine sometimes you have to wait a few minutes same problem so i'll give it a moment and then i will rerun it on the failed hosts as you can see the command now worked just fine i didn't change it it's the same command that failed a few minutes ago but again if you run into this issue just wait a few minutes and re-run that command on that node and you should be fine i had to try it a number of times so it's all set on node 10 node 9 is all set so was 8 7 was one of the ones that failed and now that one works six is fine and i need to rerun it on five actually glad it failed because you guys got to see it same thing here with four that one failed as well actually surprised it hasn't failed again so far and then three two that one's already fine and that one's already fine it's basically fine if you didn't see the air and that one's fine as well so next we're actually going to install the required packages for kubernetes i'm going to start with the packages that each of these have in common that each of these will need and then we'll get into the specifics of what we're going to do exclusively on the controller i'll paste in the command right here we're running sudo apt install and then we're going to install these three packages cube adm cube ctl and cubelet and again we're going to do that on every pie so i will paste it into each give it a moment to finish and there we go we're all set on note 10 and i'm just going to check all of these i think they're good so far no it's errors the screen as i go and awesome now we have the kubernetes packages at least three of them installed on every single node as well as the controller now this command right here we are only going to run it on the controller do not run this command on the other nodes just the controller so this is the first command that is actually different depending on the use case of the pie and this is the command right here sudo cube adm init dash dash pod dash network dash cider we're setting that equal to 10.245 and we're not going to customize this ip i've tried it it makes it fail and you can do this with a custom ip it's beyond the scope of this video i think this cider block right here is going to be just fine it is the default and what this is going to do is initialize kubernetes i'll press enter alright so we have successfully initialized our kubernetes cluster and we can actually join worker nodes to this cluster now there's a bit more configuration that we'll have to do so for right now what i'm going to do is copy this join command i recommend you do the same i'm going to open up a text editor and i'm going to paste it right in here now you want to make sure that you don't upload this file anywhere or let anyone see it you especially don't want anyone to view the hash for your cluster i'm actually going to delete this entire cluster which is why i'm able to let you see what the hash is but the reason why i copied the join command into a text editor is just so i can have easy access to the join command when i'm ready for it but i'm not going to save it i don't want the join command to be visible in plain text it's just temporary so now we have the join command you can go ahead and continue now this command right here is going to be specific to the controller node as well in fact it's one of three commands that you are already provided if you scroll through the output here we have these three commands right here so what i'm going to do is copy the first one as it is i'm not going to change it and all this command does is it creates a directory called.cube a hidden directory and it's going to create that directory in my home directory i'll grab the second one i'll paste it back in and that's going to copy this file right here and it's going to copy it into this file so we're grabbing the admin config file we're going to save it locally this will allow me to manage the cluster i'll press enter easy enough scroll up change the permissions on that new file make sure that my user owns it appropriately so i'll paste that in right here simple enough and this command is also specific to the controller node i'll paste it in we are basically installing a network driver and there's several of them out there it's beyond the scope of this video to go over the differences of each i will have an entire series on kubernetes on my channel that'll go into more detail but we do need a network driver we're going to go with flannel to facilitate that requirement i'll press enter and notice that i did not include sudo with the command that was intentional so this is going to create resources inside our cluster to facilitate networking it's a requirement after all we can't have our containers communicate with one another if we don't have all the requirements met so next we can make sure that those resources have been created successfully by running cubectl also referred to as cubecontrol we're going to get pods we want to see what pods we have running in our cluster and we want to check all name spaces so cube control get pods dash dash all dash namespaces just like you see on the screen i'll press enter and all of them appear to be running now on your end if any of them are not running just give it a minute sometimes it takes a few of these a little bit longer than others to get created but on my end they're all running so now what we're going to do is we're going to grab that join command that we set aside earlier in my case it's this one right here i'm going to copy that so what we're going to do is join the nodes to the controller and before we do that we do want to make sure that all of the pods are running this is the command right here that we used to check that if any of these are not ready you don't want to continue if that requirement is met we can begin to join the nodes to the controller before we do that we can run cubectl get nodes that'll give us a listing of all the nodes that we currently have in our cluster in my case i don't have any because i haven't joined any of them yet it only shows the controller so that's expected so what i'm going to do is go to node one i will need root privileges to make this change and then i'll paste in the join command just like that i'll press enter and i typed in my password we're going to let that run and in fact i'm going to begin the process of adding that to a few of the other nodes here i'll add it to the first three and then we can go back to the controller and again run cube ctl get nodes and i can see that two of the nodes are already reporting in neither of them are ready quite yet that's okay they've only been running 22 seconds and 8 seconds respectively so sometimes they need a bit of time to get provisioned i'll clear the screen let's see if it's any different now now we have the third one showing up on the list and i'll go ahead and add the fourth and the fifth and why not one more so we have three of them ready one of them is not ready so we have up to node four in our cluster so far so basically what i do is i keep re-running this command periodically just to see what the status currently is and now we have all six of the nodes that i've added reporting in three of which are not ready yet again it just takes a bit of time so this is the last node that i've added so far i'll add the others and then back on the controller we have up to eight reporting in last two are not ready yet but already we can start running containers because we have six nodes ready technically we only need one preferably we should have at least two three is even better however many raspberry pies you can get your hands on so now we have all 10 reporting in we're just waiting on the last four to get fully provisioned and once they're ready we should be good to go to run some containers in our cluster i'm a little impatient if you couldn't already tell so i'm just going to keep checking and now they're all ready we have 10 nodes that are reporting to our controller which is awesome we could run a ton of containers on this cluster right here and in fact what i'm going to do in the next section is i'm going to show you how exactly to run some containers once you have all of the nodes added to your controller and the reporting as ready you can come along with me to the next section and we'll get some containers running [Music] so now that we have our very own kubernetes cluster it's time to put it to use so what i'm going to do is give you two examples the first one is going to be fairly simplistic and the second one is going to be a little bit more involved so right now i'm connected to my controller i basically added my typical bash config which is why you see a different prompt this time around but on the controller i have some files and in the nginx example i have some files that i'm going to show you right now and i will also have these files available in the wiki article as well if you want to copy and paste the contents there as you can see here i have two yaml files that's what the yml extension is that stands for yaml going over yaml in general is beyond the scope of this video but you don't actually have to understand yaml in its entirety to understand what we're doing today but i have a yaml file for the pod that we'll be creating and also one for the node port service as well and don't worry if you don't know what any of that means i'll be going over that in more detail here very shortly first of all i'm going to open up the yamo file for the pod and if you haven't heard the term pod before in terms of kubernetes a pod is basically what containers are deployed into and a pod can actually contain more than one container but we're not going to be running more than one container in any of the pods that we'll be creating in this video so for right now just keep in mind that a pod is what containers run inside of when they run on a kubernetes cluster and the yaml files that we'll be going over contain instructions for how kubernetes is going to create something and this file right here in particular contains all the instructions that are needed to tell kubernetes how to create the pod that we'll be using for this example so i'm going to go through this example file right now so right here on the first line we have the api version we have it set to version one we're going to leave that alone we're not going to get creative with that we're not going to say this is my api version of version 16 or whatever we want to call it this is actually something we're going to leave as is and then on the second line we are going to tell kubernetes what type of object we want to create in this case we're creating a pod and then we have a section right here where we are going to set some metadata so we're going to give this a name i'm calling this nginx example because i'll be showing you how to run an nginx container in your cluster and then we're going to create a label and a label is a key value pair so we're going to create a key named app we're going to set that equal to nginx and this is just a way that we can group things you can get creative with labels if you want to but we're not going to get too creative right now we just need a label that we can use to select resources from this deployment essentially we're creating a label under which all of the resources for this deployment will be under so it's kind of like we're categorizing everything that we're creating here in the last section we have some information about the containers that we want to create we're only going to create one in this pod right here we're going to set the name to engine x and for the image this is really important this is where we tell kubernetes where to pull the image from so what we're going to do is pull an image from a repository named linux server and in that repository we want to run a container called nginx now what i'm going to do is switch over to a browser here and let's actually go to that repository in our browser so this is the official website for linux server.io and this is a great resource for anyone that wants to run one or more containers on a raspberry pi you click on images here you can see all kinds of examples of apps that you can run these are all available as containers from linux server so if i scroll down right about here we can see the container that we are referencing right here i'll make the font size a little bit bigger here and there's the container that we'll be pulling it comes from linux server.io and there's the string that we used in the pod.yml file right there now the reason why i'm having you guys pull the image from linux server specifically is because linux server.io they actually have container builds that run on the arm platform and that's important because that's what raspberry pi is a raspberry pi is not an x86 server so if you are trying to run a general container on a raspberry pi it might work but it'll only work if the developer of that container actually made an arm version available which doesn't always happen and one of the benefits of linux server.io is that the container images that they provide are all available for raspberry pi so regardless of what you want to run if it's on this list then you should be able to run it and here it is so back in our terminal like i mentioned we're pulling the image from linux server.io that's what linux server is in this context and nginx is the name of the container image and then we have some ports here and essentially web apps are going to listen for connections on a specific port and the container port is 80 which makes sense because that's the default port that nginx listens on so we're going to apply the name nginx dash http to this particular port and that's the entirety of the file again check out the wiki article for this video and it'll have all of this code right here that you see on the screen right now you'll find it in that article so like i mentioned we have two yaml files right here we've already gone over the yaml file for the pod and i'm not going to go into the yaml file for service node port just yet let's actually get the nginx container up and running so what i'm going to do first is run cubectl getpods but we don't have any right now i just wanted to make sure that you knew what command you would use to see what pods you have running in your cluster since we haven't created any yet then of course we don't have any running now what we're going to do is actually turn that pod.yml file into an actual running application and to do that we'll again run cubectl we're going to apply we want to apply something specifically we want to apply a file and the file is going to be the pod.yml file and right here it's telling me that the pod was created and the status is currently container creating and this could take a minute or two because it needs to download the container image and once it downloads it it'll start running the container so basically what i do is i just keep checking until it tells me that it's ready and now we can see that the container is running so congratulations you have successfully deployed a container on kubernetes however we have a bit of a problem there is literally no way that we can access this container at all whatsoever yes the container is running but by default containers and kubernetes are not accessible from the outside now if you recall earlier in the video we actually created a pod network and well anything in that pod network is just not accessible if we do actually want to have communication happen between our local network and a container then what we'll need to do is facilitate a way for that communication to happen and that's where the next file comes into play the service node port file so i'm going to bring that up in an editor and here it is now just like before we have an api version of version one the kind this time is service we want to create a service and for the metadata we're going to set the name to nginx example and then here we have the spec and then here we're telling kubernetes that the type of service that we want to create is a type of node port and it's beyond the scope of this video to go over all of the different types of services that we can deploy with kubernetes keep in mind i will be launching a dedicated tutorial series on this most likely it'll be available i'm assuming summer of 2021 which is coming up worst case fall but it's coming and in that series we'll go into more detail about the types of services that we have available but there's a specific reason why we are using a service type of node port what the node port service allows us to do is map a port on the node to a port inside the container and the node port service can go from port 30 000 all the way up to 32 767. so you have quite a few ports to play with here and what i'm doing is i'm actually choosing the port that i want to have mapped to the container port if you don't do this then it's going to randomly choose a port for you and i don't like that because then you won't know what port your app is going to be on i like to set that myself now there are actually better ways of exposing applications to your network specifically we can create a load balancer and things like that but generally speaking a lot of those services actually depend on the underlying platform and an example of that is amazon web services amazon web services has its own way that you can expose apps that are running in a container same with google cloud lenode and others but again that's beyond the scope of this video i like node port because well a lot of you guys are going to be like me and you'll be running a cluster on your local network not on a cloud platform such as aws or google cloud so by using a node port you can just designate a port in your cluster to map to a port inside the container and this is what's going to expose that container to our network now you'll notice that we have the target port listed as nginx http in the pod yaml file we actually called out nginx http as the name of the port so that's why we have that and we also set up a key value pair a label of app and we set that equal to engine x so we are using a selector here and a selector allows us to well select something and the criteria that we're giving the selector is that the label named app has to be set to engine x and that's the entire file so what i'm going to do is apply this one as well and we'll do that the same way so we're going to run cubectl apply f servicenodeport.yml just like you see here and it's telling me that it has successfully created that service and to make sure that it's telling the truth we can run cube ctl get service just like that and that'll give us a listing of the services that we have of course we're going to ignore this one that's kubernetes itself but we do have the nginx example right here that we've created and as you can see it's mapping container port 80 to host port and it's wrapped here 30 080. so at this point that nginx container should be available on our network let's see if it is i'll clear the screen and switch to a browser so what i'm going to do is type in the name of the controller node in the address bar and on your end you could actually just get away with the ip address if you don't have dns so either the ip address or the domain name is what you'll use so i will type http colon slash in my case k8s controller dot home hyphen network dot io and then the port again is thirty thousand eighty so if this works i should actually see the example web page for nginx let's see and there you go as you can see right here we have the nginx default start page i'll admit that it's not the most beautiful start page i've ever seen but it does prove that the container is running and we're able to access it now let's look at this in a bit more detail though we know that this container is running but on which node is it running now the font size is a bit wrapped here i'll just make it a bit smaller but you can see that we have 10 nodes and it would be nice to know which one it's running on if we run cube ctl get pods it shows us that the container is running but it doesn't actually show us where it's running so to find that out we can actually add another option here dash o and then wide that's going to give us even more information than the normal cube ctl get pods command would give us and as you can see it's running on node 0 1. so what that means if i go back to the browser i can actually change the address here to the address of the first node so node01 again you can use the ip address on your end and that's fine so i'm going to press enter and it works but what happens if i type in the ip address or hostname of a different node a node that it's not running on it's running on node 0 1 but i'll just type a 7 there why not let's see what happens and it works so if you are brand new to kubernetes this is probably a bit perplexing we know that this container is running on node zero one and we're able to access it from the controller which i guess you could probably expect because the controller is well controlling things but we can also access it from node 0 7 in addition now the thing is it really doesn't matter which host name or ip address you use to access the container now of course it has to be a hostname or ip address of an actual instance but that's literally how it works you type in the ip address or host name of any of the nodes including the controller and you'll be able to access that container even though it's running on node 0 1 it doesn't matter if you try to access the container from node 5 node 4 node 3 the controller itself it doesn't really matter so long as it's a valid domain name or ip address of an actual node and kubernetes is smart enough to handle the rest all right so i'm going to show you guys another example we have two more yaml files right here and this is going to be for an app called smokeping which allows you to view network latency and things like that it's very useful so let's take a look at the first file now everything here is pretty much the same as the original example with the main difference being that we are introducing the concept of a volume mount and we can see that right here we have volume mounts and then we have a mount path of slash config now the slash config is very important when it comes to container images from linux server.io this is standard this is where they put all of the data by default so the logic is that if you map this directory right here from any of the containers created from that service to a path on your host then it should work out because again this is the default path and then here i'm giving it a name but then if we scroll down we have this section right here for volumes and i'm going to give the volume a name of smokeping data we want to set up a path on the host for storage and we want that directory to be slash data and again just check the wiki article for this video to grab a copy of this for yourself and when it comes to the service node port for smokeping it's basically the exact same thing again when you compare this to the nginx example i essentially just replaced every occurrence of nginx to smokeping so again it's the same thing but before we actually apply those we should delete the pod and the service that we already have so we have the nginx example right there so let's get rid of it and the command to do that is cubectl delete instead of get we're going to delete something so it's cube ctl delete pod and then the pod we want to delete is nginx example and the main reason why we're doing this is because the ports are already in use yes we can have both of these apps running simultaneously it's just simpler for this tutorial to replace one with the other but if you want to customize the ports i'll leave that up to you anyway i'll delete it and now that's deleted let's see what services we have on our kubernetes cluster and here you can see we have the nginx example service as well i'm going to go ahead and delete that and now that's gone so we can go ahead and create our smoke ping application now so i created the pod i'll do the same thing for the node port service and now that's created and this time we can see that the container is running on node 10. we see that on the right again it's wrapped but it shows k8s dash node-10 so it should actually be running let's go ahead and find out so i'm going to just change this to 10 and let's see what happens and there it is how cool is that now the main point of this second example is to show you guys how you can actually retain data for your containers generally speaking when a container goes away its data goes away as well and well you probably don't want that to be the case if you have an application and you want that application to retain its state then you'll need a place for that state to be saved so let's go into that node and if you recall from the pod.yml file i told it that i wanted to have it save its data in slash data and there you go there's all of the data now to be fair i should have actually created that in a subdirectory of data but you get the point you can customize this for example slash data smoke ping would probably have been better but it is what it is as you can see everything is working just fine we now have smoke ping at our disposal and at this point i think you have everything you need in your arsenal of tools to deploy whatever applications you would like on your cluster so there you go i hope this video was helpful i hope you had a lot of fun i had a lot of fun making this video but i always have a lot of fun when i'm working with docker kubernetes and other geeky stuff like that in fact i am going to start the process of creating an entire tutorial series on kubernetes and in that series i'll go even deeper into detail about the individual components but at least with this video you have yourself your very own cluster that you can use to learn and get started and then when i do have the tutorials out you'll be able to use this same cluster for those tutorials as well so let me know what you thought of this video in the comments down below make sure you subscribe if you haven't already done so click that like button if you like this video that lets youtube know that you want to see more content just like this and i'll see you again very soon thanks for watching [Music] my [Music] you
Info
Channel: LearnLinuxTV
Views: 34,613
Rating: undefined out of 5
Keywords: Linux, Tutorial, Review, Howto, Guide, Distro, Learn Linux, operating system, os, open-source, open source, gnu/linux, LearnLinuxTV, LearnLinux.tv, raspberry pi, pi, kubernetes, k8s, ubuntu, ubuntu server, kubernetes cluster, pi rack, home server, home rack, homelab, kubernetes tutorial, kubernetes tutorial for beginners, kubernetes cluster setup, install kubernetes, home lab, raspberry pi 4, raspberry pi cluster, kubernetes installation, docker container, raspberry pi projects, 10 node, 10
Id: MO8N79lQSWU
Channel Id: undefined
Length: 82min 58sec (4978 seconds)
Published: Fri May 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.