Packer && Proxmox Automation PT1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so what we're gonna do um last video we got PF sent set up we got proximox set up so now I want to deploy a virtual machine on PS or on proximox but I don't want to go in there and have to like manually set it up like we can't do that but I would much rather just have like a terraform module with my virtual machines or you know a project and then so we're going to do that we're going to get it to work all right so um what we're going to do is we're going to set up Packer and the reason we're going to do that is we can use it to provision a virtual machine and create a template in proximox and then with terraform we'll be able to use that to automate our deployments and we can just create new nodes with and later we'll do something really cool with it but right now let's go over the template all right so what I'm going to do first is I'm gonna go over the file so I just made a packer folder and we're not I haven't figured out how to do this through CI CD yet so you're just gonna have to manually do this but you shouldn't have to do this all the time so um you're going to create this container server folder right here and then you're going to create a containers-server.packer.hcl and then I'm going to walk you through this file I'm going to make this available to you but um first you're just going to create these three variables these are going to what we these are what we're going to use to log into proxbox and then we're just going to kind of configure a uh a resource it looks very similar to terraform so um we're going to use those to connect to proxmox we're going to connect to those variables and since we're doing it just over the IP address we're going to make sure it's insecure is okay and then proxmox that's the name of your proxbox node and then the ID that you want to give this template so I'm going to give this one 400 and then we're going to give it a name I'm going to call it container server and then it's just an Ubuntu Jammy image pre-configured with Docker this is an ISO file that I downloaded you just have to add this to your proximox and then just make sure you use the same ISO name and make sure I'm doing the local storage pool this is the scuzzy controller to use I'm just going to give them 100 gigabyte disk size this is the format I found that works I'm just going to do it my just my local lvm like this should all work if you're just using the default settings and haven't added any drives I'm gonna go four course four gigs of RAM and then add it to that uh virtual Bridge one that we made last video and then we're going to do Cloud init and then we're going to set up a username and password and add our SSH key so how we're going to do that is this is going to have a boot command and I'll display this how it works but it's going to go to proxbox boot up the machine it's going to fire off that command and then it's going to do this like Network boot kind of thing so what we need to do is just get the IP address of our machine so ifconfig see what mine is oh 155 perfect that's what mine is and then just go ahead and leave that the same I mean then you can just do some temporary I just do some this is just some easy username and password that I use when I do something generic and you can also do your SSH private key so Define that you're just going to cut out this this file see if it exists on your machine if not it's just a different file name and then now we're going to do some stuff on it so this is going to just go ahead and initialize it um and then it should also do an update on it um and then this one is going to go into this file directory and it's going to get these kind of boot file like boot configuration out of here um and then it's going to go here it's going to install some packages do an update it's going to install Docker and just kind of create a group for it so that way Docker will be installed and then something else we can do because we can say pseudo zero mod Dash AG or actually we're going to say sudo out get install Docker compose so that's something we can do real quick we'll add that to it and then um this last one we're gonna set up um you need to add this HTTP file so this is going to be for cloud init and then you do have to add this file just leave it blank then add this user data and then you're going to be able to copy and paste this template basically you can just uh kind of do some SSH settings you're gonna install the camera guest agent allow sudo you can add your public key so if you want to get that you just can't dot SSH and then get the Dot Public boom and then you would just add that key right there and then you'll be able to log in without a password and then I believe we are good to go I click we went through that so now the last thing we need to do is just create this credentials file and then so what you need to do is get create the token and password and so I'll show you how to do that just give me one second all right so to get that token we're going to click on Data Center go to API tokens and then you're going to add one uncheck privilege separation and go ahead and call it uh Packer you can just call it a Ci or just Packer or whatever you want and then add that and then this is going to be your username so we're going to add that to our repo which is right here and then now we gotta add our password boom and you can also set these as environment variables just prefix them with that and then it'll work so now what we're going to do is we're going to run these commands so we have to create this credentials.packer file with these values and that's what our secrets are going to get so if you look in here our secrets are going to come from that file so we got to get these so now I made these commands so what we're going to do is validate this first so let's copy that I'm going to clear this and then so let's go into that Packer folder we should have those credentials so it's validate it oh see I'm missing a comma so let's go back there boom it's time to run that again oh the configuration is valid here let me just run that figuration is valid so now we can build it let's see I already have one at this ID so I'm going to do another one so let's go ahead and launch for oh let's just do 410. I'm going to delete it but let's run this all right so now we have our template right here so we're gonna go ahead and set up our uh terraform and then go ahead and use this template that we made all right so I'm going to use gitlab a big reason I use gitlab is I think they have an incredible platform to do everything that you need to do in terms of CI CD and to automate your deployments like they have a back end you can use their git lab pipelines are really nice and they're really easy to configure and deploy Runners I'm just a really big fan of gitlab and how they do everything so I'm going to show you what I know how to do with gitlab and just the way I set things up um but yeah let's go ahead and get this project set up as you can see here's my file is what we're going to get it to look like real quick we're just going to get a nice main file going and getting this project initialized so now what we need to do is we need to set up the provider so we can use the provider to create machines on proxbox so let's create a new file called provider.tf and then I'm going to paste this proxbox provider in and so this is just the configuration for it um we just need to create these variables which we're going to do real quick and then let's create them right here so variable copy this oops copy all right I could say awesome a little terraform format there so now I have these variables in so now these are going to be sensitive information so I don't really want to set these but you know I could do this one like I could say you know this variable I could set this in my thing all right so we're going to create these variables and then we need to set these ones as sensitive so you don't want them to release these values so I'll put that there okay perfect so now what we want to do that we have these set we're going to go ahead and set these in our pipeline so I'm actually I guess yeah let's copy them like this oops not Firefox let's go to our repository here we're going to go to settings CI CD and then variables and then we're going to add a variable and then what we need to do is do TF underscore VAR underscore and then paste in the variable and then for this one you're going to do the address which is your 10.10.2 or 1.2 8006. and then you want to mask the variable you don't need to expand it just save it as a string and then we're gonna go ahead and add that all right so the proxmox URL that we need to set right here and we're going to open this up because we need this API 2 forward slash Json so this is the URL that we need so just make sure this is the IP address that you set for your proxbox and then you're going to be good to go so let's update that and then now let's go into our proxbox and we need to create a token so what we're going to do is I believe you go here I think it's actually in data center API tokens and now we're going to add one check privilege separation and then for this one for the token I do let's just call it a proxmox dash TF and then we'll give it the root privileges then we're going to add it so here's going to be our token ID so let's go create this so our proxbox API token ID add this variable so Tia underscore VAR and let's get this value paste that in there oh make sure that's right yep root at it's good to go don't do the expand we'll go ahead and mask and protect add that variable okay then let's add the next variable proximox token Secret I'm gonna go back here TF underscore VAR paste that in and then let's get that secret from here and you have to keep this open if you delete this you're gonna have to recreate a new token you're only going to be able to see that once awesome so now we are good to go so now we got all those set up so technically we should be able when we run this with our runner that we're gonna get set up we're going to be able to use these values and authenticate with our proxmox and log in so let me go ahead and close that and we have that set and then don't worry I'm going to reset mine so it doesn't really matter if you have my token or not and okay let's go ahead and set up a virtual machine all right so now we're gonna set up some a VM we're just going to create a template for right now so just create a file you can call it VMS I'm just going to do vms.tf you can call it virtual machines whatever you want and then I'm going to paste this in here and then so this is my VLAN so I have my VLAN 4 it's called Gondor so this is kind of like Gondor you know they're just kind of everyone can get in there so it's gonna be we're gonna be able to set it going to our username and password so we're going to set this in our pipeline variables and then this is just going to explain it real quick um we're gonna give it a virtual machine ID of 401 we're going to name it Gondor we're gonna do it on our proxbox node which I'm going to show you real quick shh this is our proxbox node it's called proxbox so if you named it something else that's the name of your node well let's do this side by side view true oh perfect okay so and then now um I'm just gonna give it a description this is going to be my Gondor container server I'm just going to run a couple containers on it we're going to do a cloud init um we're going to use the Gondor user and name and password um we're going to clone we're gonna this is going to be the we're going to create this machine image so this is a lot to take in so essentially we're going to describe the bare bones of a machine but we need to create this image template which we're going to do with Packer I'll show you in a minute I'm just going to give you my template and you can just run it it's kind of a pain to set up digital life has a really good video on it I'm going to link his um thing to it I basically use that and just tried to make it work for me his didn't work for me right away but I'm gonna try to give you mine see if it'll work for you we're going to have it start up on boot and this is going to start that qmu guest agent we're going to give it four course one socket I'm going to use CPU we're gonna give it uh four gigs and let them out to balloon this is going to be that network bridge that we made in the last video we're gonna put it on there with using the vertio driver give it a VLAN tag of four and then I'm going to give it an IP address of 10.10.4.10 um and then this is going to be the Gateway that it's going to use and then I'm going to use cloudflare as a name server for right now I'll probably update this later but I might be putting like my actual name server that I use for everything in here so this might just go straight to cloudflare no problem and then we are good to go so this is going to be our little template that we're going to use and uh let's go ahead we need to get a runner set up so let's do that all right so we're going to set this get ignore and just put it in the terraform folder um just do this this will just ignore any local state files or terraform configuration and then you're going to go to your gitlab project so mine's proxbox infrastructure that we made and then go to your settings CI CD and then expand the variables and then you'll see your API token ID so you're going to do that one first so we'll copy that paste that in and then the secret we'll paste that in and then I'm going to change these so don't worry about using them because you're going to get your own then the URL and this should do a plan boom so there did a plan file for us so what I'm going to do is I have this machine just to show you how it works so I'm just going to change the ID to 403 and as you can see here I'm using that containers server so let's run terraform apply I'm just going to do an auto approve so let's add the token ID token Secret and then the URL also watch proxbox over here so it's going to destroy this 402 that I mean boom destroyed it now it's going to turn it into 403 which you can see it's doing right now and then what it's going to do is clone this template so you have four CPUs four gigs of RAM still creating still creating um so I'm using this one example so just while it's created I'm going to talk about this one so this one it's going to get built and then we're going to use this IP address and then we're going to just use this to run some containers so we're going to put a runner on here so just to show it's right here we can log in so I'm going to just SSH into it and I'm in all right well we did it so we used uh Packer we created a virtual machine template we could pre-install software on there so one of the cool things you could do is you could put files on that virtual machine that you want to put on something I'm interested in using it for is like keep a live D and putting configuration files baked into that the image so that way I can just configure it maybe for a certain service um also you can use it to just you know install software like you create a portaner one install like you know that just launches and always spins up portainer instantly so you can have fun and freedom with that and then what's so cool is we use the terraform and then we're able to describe our infrastructure as code um I guess the only thing I didn't show is you could just commit that up to your repository um what we're going to do later is um add CI CD to it so that way every time we make a change to our repository and commit to it it's going to go to our runner and it's going to deploy that uh new changes to our proxbox server and that way we can you know keep everything get sourced and go from there so uh thanks everyone I hope you enjoyed the video um we got more to come so stay tuned thank you
Info
Channel: Brock Henrie
Views: 8,959
Rating: undefined out of 5
Keywords:
Id: 6JCihY6F_wA
Channel Id: undefined
Length: 21min 24sec (1284 seconds)
Published: Fri Mar 31 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.