How To Deploy NGINX on a Kubernetes Cluster

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
kubernetes is a very awesome technology it's exciting to work with it's just great and in this video we're going to work with kubernetes specifically i'm going to show you how to deploy an nginx container on a kubernetes cluster but not just any kubernetes cluster it's a cluster that we will be creating in this very video so let's go ahead and get started [Music] for this particular video off-camera i went ahead and set up three lynodes one to act as the server and then two others to act as nodes for the master server i recommend at least four gigabytes of ram for the individual worker nodes they can have one gigabyte of ram a piece that's totally fine i also recommend that you take a moment to secure your lenods as well there's already documentation on the node's platform that you can read and videos you can watch that detail the process of securing your server and the topics include but are not limited to installing all available updates securing ssh and more but just make sure that you've done your due diligence to secure your lenods and we'll get started on my end i've already gone ahead and used ssh to connect to each of the three servers so on this tab i have the master and then i have the first node and then the second node i've already installed all of the updates on each of these three servers and i've also set the hostname as well which is important because we want to make sure that we're able to differentiate which host is which there's a video already on lenode's channel that goes over the process of setting your host name so let's go ahead and get started now the way that this tutorial is going to be laid out is that everything i tell you needs to be done on each of the three lenods unless i tell you otherwise the majority of the setup is going to be the same on each until we get near the end and the first thing on our agenda is disabling swap because kubernetes actually requires swap b turned off and well that's easy just run sudo swap off dash a and of course since i'm logged in as root i don't need sudo but you get the idea this command will actually turn off swap on the system we can see that swap has zero available space and then we'll do the same on each of these as well and we should be good to go now first and foremost you'll want to make sure that any previous installation of docker or kubernetes is removed if you've just created your lynodes for the very first time then this doesn't apply to you but if you are recycling or reusing a you'll lenovo want to make sure that you've removed any previous docker or kubernetes packages that you might have installed for example a command such as this would remove other components of docker i'll leave it up to you to remove any packages that aren't necessary first of all on each of the three servers what we're going to do is install some packages and some of these might already be installed so we want to make sure that app transport https ca certificates curl and software properties common are all installed on each of the servers and in my case it looks like only one of those four was actually necessary so what i'll do is just copy this command and the results should be the same on each because they were all created with the same image and there we go so what we'll do first before we add the actual repository is add the gpg key for that repository and to do that we can use curl dash f pass uppercase s uppercase l just like that and then the url we're just using curl to grab the gpg key for the docker repository then we're using the apt key add command to add it to the system and now we can add the actual repository for docker so we're just using the add apt repository to add the docker repository as you see here and then this right here allows us to basically insert the release code name into the repository right there so i'll press enter it's already updating the repository index and it's done if you have any problems or see any errors on your end just check the sources list file to make sure that there's no typos and to do that we'll use cat etsy app sources dot list take a look at the end of the file and we can see that we have the download.docker.com repository right here everything seems to work just fine for me in my case so we can go ahead and continue but if you see any problems at your end just make sure that everything was typed exactly as i typed it in the previous command normally you would run sudo apt update but that's not necessary for us because it automatically did that when we added the repository so next what we can do is actually install docker so we'll run sudo apt install docker hyphen ce just like you see on the screen right now and i just press enter to go ahead and accept the default of yes to install the package and we can see that it's installed and then we could check the status by running systemctl status and then docker and we can see that it's running and it's also enabled as well next we're going to repeat the same commands on the other two nodes and what i'll do is just paste the first of the commands into node one and that was to add the key to that again here then i'll add the repository and here as well and then let's install the package and now each of the three servers should have all of the changes that we've made so far so i'll clear the screens on each and then we'll continue to the next step now the next command is actually optional i highly recommend that you create a user for yourself on your lenode so you're not actually using the root user and off camera i've already done that i've created the user j on each of these three servers on your end you should consider adding whatever your username is for your local user account to the docker group so that way your user is able to manage docker containers without the need of sudo so for that we'll use user mod dash a uppercase g then the name of the group which is docker and then the name of the user that we want to add to that group and i'll just copy this command here and just run it on each of these servers now if you are already logged in as that local user on your end i recommend you log out and log in again in the next step we're going to run an actual docker container and if you haven't already added your user to that group or you are not logged in as root as i am you'll need to run sudo in front of this command but otherwise you could leave that out what we're going to do is run docker run and then hello hyphen world just like that i'll press enter and it's going to download that image and run it and then on each of the servers here we should test this out it worked on that one and this one and we've already run it on this one so i know that docker is all set and ready to go now the next command is fairly long so what i'm going to do is paste it in and on your end i highly recommend that you copy this command from the documentation page that matches this video because by doing so you'll be less likely to encounter a typo that makes the process fail for some reason now next we need to create a new file on the system and what this is going to do is configure docker to use systemd as a c group driver instead of the default which is normally c group fs so you might need to use sudo if you are not logged in as root as i am but basically just type the name of your preferred text editor and then the path to the file is going to be slash etsy then docker then demon with an a dot json just like that and i recommend that you copy the contents of this file from the documentation page that matches this video because by doing so you'll be less likely to encounter a typo and i'll go ahead and paste that right here and there we have the contents so i'll save the file ctrl o and then enter and then ctrl x and then on each of these three we'll need to do the same thing and there we go then next what we're going to do is create a new directory and this is actually going to be a systemd directory for docker itself and i'll just copy this here all right so we should have that new directory on each of our three servers at this point then next systemctl and then daemon reload and then we'll also want to restart docker as well and to be safe we should check the status make sure it's actually running we didn't make any mistakes that one's good and that one's good and that one's good as well so next we have another gpg key that we'll need to install on each of the three servers and this one is actually for the kubernetes repository because now we're going to get started on installing the packages that are required for kubernetes so we'll run curl dash s and then the url so we'll just curl the key from this url and then we'll pipe that into sudo apt key add so press enter so far so good and as you probably have guessed we need to run that on the other two as well so next on each of the three servers we could add the repository that's necessary for kubernetes and i'll paste that command right here so essentially we're just echoing the repository line into a new file that slash etsy slash apt slash sources.list dot d slash kubernetes dot list as you see on the screen right now this file didn't exist before but will exist as soon as we run this command and then we can run apt update to refresh the repository index and that one's done now we have the repository on each of the three servers next on all three of these servers we're going to install the following packages so apt install cubelet cube adm and cubectl now next what i'm going to do is hold the packages that we've just installed at their current versions to avoid any surprises such as the packages for kubernetes being updated before we're ready for them to be updated and for that we can run apt hyphen mark then hold cubelet cube adm and cubectl and we'll do the same thing here and here now the next command that we're going to run is going to set up the kubernetes control plane and this should be done specifically on the master not the other two nodes so this is the first command that is actually targeting one of the three servers instead of all of them the control plane is a very important component of kubernetes and is responsible for allocating resources to your cluster it also manages the health of your cluster as well and it even ensures that the minimum requirements are being met so what i'm going to do now is switch to my local user so we can go ahead and initialize our cluster with this command right here this is going to set up the pod network and we have to have a pod network in order to have a cluster and this command should take care of that and what this is going to do is basically initialize our entire cluster so i'll press enter so now our cluster is actually initialized we have three commands right here that we should run as our local user and what this will do is set up the configuration file to be used by our local user so our local user can interact with the cluster so what i'm going to do is just copy each of these three commands here and paste them in i'll grab the second then i'll grab the last one and we should be good to go so at this point we can run cubectl git and then nodes and we have our cluster right here but it's telling us that it's not ready and why isn't it ready well in our previous command we did initialize our cluster and we use this command right here to do that so basically we set up our network we chose a network citer for it and we told cube adm to initialize based on that which it did but it's not actually able to set up the cluster without a network policy driver which we'll need to install next now there's several different network policy drivers that we can use it's beyond the scope of this video to go over all of them but to keep it simple what i'm going to do is show you how to initialize a network based on calico and there's two commands for this i'll paste in the first right here we'll run cube ctl create and then dash f which expects a yaml file the ammo file is located at this url so i'll press enter and i know that went by very fast but you could check out the documentation page for that specific command and you can copy and paste that into your terminal and the same with the second command which is going to be this one right here it tells us that it was created so if we run cube ctl and then get nodes we see that it's still not ready but that's okay sometimes it takes some time after we initialize and then we set up the network policy driver another command that we can run to see what's exactly going on in the background is this one right here i'll paste it in we're going to get pods and we want to use all namespaces because we want to see everything so i'll press enter you see that there's actually quite a few here and they all say ready and running that's really all we need so let's check the status again we'll get nodes and now our cluster is actually ready to go now earlier in the output when we initialize the cluster i'll scroll up a bit we saw this join command right here and you may need to scroll up quite a bit to find it but basically what that is is a command that we can use to join a node to the cluster so go back to the end again cube ctl get and then nodes we don't have any we only have master master is ready but master by itself isn't really enough so on node 1 i will paste in that join command i just copied it from the output in the other tab again you might have to scroll up a bit to find it i'll press enter and that one appears to be done let's see what happens it says that it's not ready but it did add it that should be okay sometimes it takes a bit of time and we'll paste that join command here on the second worker note as well and apparently that one is finished so back here on the master let's get nodes again and node one has become ready so we know the process is working so we just need to give node two some time to come up and be online as well and now both worker nodes are ready the master is ready we should by all intents and purposes have a fully working cluster now and we can test that by creating our first deployment and a full walkthrough of setting up a deployment is beyond the scope of this particular video but what i'm going to do right now is show you how to deploy nginx to our cluster which will show that it's working and then from there you can look at other things that you can deploy and put your cluster to use so first of all let's run cubectl and then get deployments we don't have any currently and that makes sense because we have yet to create one and we can create one right now with the command that i'm about to show you which is going to be cubectl we're going to create a deployment and we will call the deployment nginx and then dash dash image we need to tell it what image we want to use for the deployment and we'll set that equal to nginx press enter it tells me that it was created so if i get deployments again it shows that we have the deployment right here it's not yet ready again it takes some time it's only been up for five seconds according to the output so we'll give it a moment and then we'll check it again and now we see that it's running to get more information about our deployment we can run cube ctl then describe scribe gives us more details about something and then what we want to get more details on is a deployment and the deployment that we want to get more information on in particular is nginx you get a bunch of information here as you can see and now what we can do is make the nginx deployment accessible from the internet by default kubernetes does not like to make things available publicly and that's great because we don't want things to be publicly available unless they need to be publicly available in this particular case let's assume that this nginx container is actually something that's extremely important to our organization and we want other people to be able to access it so what we can do is run cubectl we can create a service and the particular type of service that we want to create is called a node port service so we will type node port right here call it engine x dash dash tcp set it equal to 80 colon 80. press enter it tells us that the service was created so cube ctl git and then service and we can see the nginx node port service there on the last line now here we see a port of 31672 so let's see if it's working so what i'm going to do here is paste in the actual public ip of the master and the colon let's grab that port we'll paste that right here and let's see if it works and it says welcome to nginx as you can see right here we have created an nginx deployment on our kubernetes cluster and we've also made it publicly accessible as well it's also pretty interesting if we grab an ip address of any of the other nodes for example i will grab the ip address of node 2 and i will replace the ip address right here with that one instead and press enter we can see that it still works now the first time we used the ip address of the master server and the second time one of the nodes and it doesn't matter what i p address we use with node port any ip address in the cluster should know how to actually transfer you to the appropriate resource that's actually running the container that you're trying to connect to that port number is actually opened cluster not on a particular host that's why i'm able to add any of the ip addresses here with that port and then it allows me to access the container of course there's other types of services and deployments that you can set up but i'll leave it up to you to look around and see what kinds of awesome things you can run on your new cluster because now that you have a working cluster the sky's the limit so go ahead and have some fun so i hope this video was as much fun for you guys as it was for me making it i love working with kubernetes and in this video we created our very own cluster and we deployed an nginx container to our cluster it was great so click that subscribe button if you haven't already done so we have some fantastic videos coming very soon and i'll see you next time thanks for [Music] watching you
Info
Channel: Linode
Views: 14,124
Rating: undefined out of 5
Keywords: linode, linux, alternative cloud, cloud computing, kubernetes tutorial, kubernetes deployment, nginx docker image, kubernetes cluster, install kubernetes, kubernetes, k8s, NGINX, cloud computing architecture, cloud computing basics, kubernetes getting started, kubernetes pods, docker, kubernetes container, how to run nginx server with k8s, how to run nginx on kubernetes, nginx k8s tutorial, k8s nginx tutorial, k8s tutorial, docker and kubernetes, nginx docker, nginx k8s
Id: HTNt6YjkZ7M
Channel Id: undefined
Length: 24min 26sec (1466 seconds)
Published: Wed Mar 31 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.