Rails on Hetzner with Kamal - Deployments from Scratch PT. 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's it going I'm going to do a speed run here through how to set up rails with hetner and Cloud flare to get you set up on something that should cost you about well five $5 a month to get your rails application up and running pretty easily um we can do it really easily with rails and you can also do it with anything that uses Docker so remix I don't know about next but I'm sure it's possible so let's just do a quick run through this so sign up for hetner um jump into the cloud console we're just going to spin up a quick easy VPS here to get started so let's do a new project just rails project in here add a server um let's do Oregon since that's where I'm closest to bun 2 is easy we don't really have to worry so much about the OS um because Kamal handles all the installation stuff for us I'm going to go with the shared c v CPUs because it's easier cheaper dedicated you could go with for production application they're not that much more expensive $1.99 is not bad um but we're just going to go with the cheapest one right now I'm going to get an ipv4 as well which is an extra 50 cents a month just because I'm old fashion guy I'm going to add SSH key that I already have uh with one password have a bunch of SSH keys in here um but that's pretty easy you guys can figure that out firewalls we'll get to in a second backups we want backups daily backups we're going to be using SQL light so we're going to want to have that data backup placement groups don't worry about that clock pck let's give a little name test rails and we'll Cate and buy that all right it's getting spun up so you see we have the IP address here the IPv6 address here uh we want to get the firewalls set up because for security we only want traffic to be able to reach our server from cloud flare we're using Cloud flare for DNS and caching and all that good stuff um so everything we want to only come from cloudflare that includes the icmp here's a cloudflare IPS this is really easy to get just search Google cloudflare you can spell it correctly unlike me ip addresses it's the first thing get the ipv4 addresses and we'll also add the IPv6 but anyway add ipv4 add rule add another rule for TCP um we want 443 for um secure connections that's all we need to run it um so we'll just want to have these three you probably want to lock down the SSH a little bit better but for now it's fine I'm not really super worried about it um let's add the IPv6 addresses as well for cloud flare and we're going to apply it to the existing server we have test rails we'll name it allow from cloud flare all right that's it everything set up for our our server next stop is cloudflare let's do that next so now we're in cloudflare which is what we use or what I'm going to be using to manage DNS because they handle a lot of caching etc for free they give you a free SSL certificate it's really easy to set up and it's really good um service and works super well um I already registered a domain simple host.net um obviously you can add your own you can create your own domains um but I'm not going to show you how to do that but find out whatever you want your domain to be I'm going to I called M simple host.net let hit the manage button and let's go to update DNS um so right now all we need to do is update the DNS to point at our server since we're just managing one server right now this is super easy we just add um an a record pointing to the IP address of the server that we have if we were doing multiple servers we could set up a load balance B ER and then we would set the DNS to the load balancer and then load balancer would handle um sending traffic to the individual servers but honestly we can scale pretty far vertically um with a single server and it makes our infrastructure set up a lot easier so that's what we're going to do right now um so we're going to do the root domain we're going to send that to our IP address here we want it to be proxied add that and then let's do the um IPv6 as well good measure and we hit save and we're all set up um we might want to set up www um just because some people automatically add that when they type in things even though it's stri this in add that then we add another one for this IPv6 and then we have everything set up with the DNS and because we want everything to go through SSL we don't want to anything to be unsecure let's go down here to SSL TLS we go to the edge certificate we go to overview I already already change this but usually it starts with flexible and flexible is where SSL is handled by Cloud flare they terminate the SSL and then unsecured traffic is sent to your server which is fine for us because we block everything except for cloud flare but just for more security we can add the full setting which means that it encrypts the traffic from user to cloudflare and then also from cloud flare to our servers and I'll show you how to handle that with the Cal setup is super easy because traffic already uses its own self-signed certificate so this is full this is great um we can check out Edge certificates here and you can see it automatically created these certificates for us for free it's great then let's say we always want to use this is might be clicked off for you but let's click that on always use https we want that and then we want to do automatic https rewrites so that any traffic that gets sent to our domain automatically is created um securely on https and that's it for cloudflare pretty straightforward let's get up let's go next to the rails setup and come all okay so now we're just going to generate a simple rails app from the beginning so you can see all the steps that I add to the config files and everything to get it working exactly the way we want so let's just do a new one rails new simple host CSS why not install rails here um and then what we're going to want to do is quickly add a um we add a just home controller and then 's get inside ring I'm going quickly add a home controller and we just update the routes to the [Music] root and then just up B the old index controller simple host how to host things simply I'll add all my notes to this website once I'm done great so just quickly make sure this works all right simple hosting it's running correctly a local host which is great so now we're ready to go let's check out the docker file here we need to do a little something to to make everything work with the data directory which is what we want to use for SQL light so right here um I want to make a directory this is just to hold our data and let's just have rails own that as well and then let's add a uh M here database URL equals let me just copy this over from something else this is just to hold the data directory to add a data directory and to point SQL light to that and then let's update the database to get the um from M database zero that's all we need to do to get that set up [Music] um and then all we need to do here is do Cal init the init command sets sets our configuration at config deploy um so we're going to call so this is just a yaml file you can update it for your own um configuration I'm calling M simple host I have my image inside of dockerhub um you can do whatever you want you can um use any hosting service uh digital ocean AWS they all have um Docker Registries they're all pretty easy to use Docker Hub is super simple um so you sign up there create a repository and then put your username here this is for the Registries for the imos my username is S Johnson 540 and then I'll get to setting up these environment variables in a second but we'll be putting our registry password inside a m file and we're also going to want the rails master key which is from the M file as well and this just shows that it's a secret and it pulls it from there you can do clear which means it's just my my non secret hello thing and you don't need that um for now we just need the rails master key and the Cal registry password or you could just call it anything you want so for servers um this is where it gets a little bit more complicated um for servers we want obviously the IP address that we used earlier let's do this but what we really want to do is actually um use uh we want to actually just name these which is going to be easier for later if we want to set up jobs and also we want to be able to add um labels for traffic to work correctly with our scheme of encrypting all of our traffic so this host is the host that we want and I'll add I'll put all this stuff somewhere else um I did this earlier so I didn't have to type all this out but these are all traffic labels okay traffic is what Kamal uses to handle traffic handle the web traffic it's uh proxy uh it can be used is a load balancer it also handles SSH uh not SSH sorry SSL and um a lot of other things for you this is pretty simple um basically all we want to do is say that when the host is simple host.net um which is the name of this is just going to be the DNS name that you bought um ours is simple host.net or mine um we want to we want to Route the secure entry point which means 443 to web secure which we'll set up in a second and we want to um set up a rule that says um it's using TLS um we don't actually need this because we're not doing Port 80 so we can remove this all we care about is secure traffic so we only should be handling secure traffic not um anything from Port 80 so that means we need to publish the 443 address because by default traffic just does 80 copy this and so we're publishing these this say 443 443 that's what we want and then we just need to make sure that we have the entry point and the entry point is basically where the traffic gets routed from um from the outside world to traffic It's Complicated because the name is called traffic and I'm saying traffic but that is how you set it up to handle https queries the last thing we need is the volumes so this is just Docker volumes so if you know Docker volumes this makes should make sense to you but we want to mount the um data volume which is what we're using for our SQL light data every time a Docker container comes up because we want to be able to access the database obviously and we don't want it to delete itself every time the docker container goes down um You can call this this is just a named volume you can name this whatever you want and also you can store it on the server itself if you'd like but this makes it easy to just use it through Docker volumes and so that means that we are set up with the volumes which will handle the data traffic which handles the web traffic and make sure that it's passed to our server so we have everything set up is go into your M file and add your environment variables this is going to be the token or password that you use to log into your Docker registry and then this is going to be um the rails your rails master key I'm going to update them um but I'm not going to show it to you okay so we got that all set up and now we're going to run Kamal setup which is going to go onto the docker which is going to go onto the server install Docker and then also what it's going to do is um build your Docker file for the first time so you can see that it's building Docker and this is just going to take a little while because it's installing everything at the beginning I'm going to fast forward to this part for you okay so is running successfully as you can see um the beginning the first time always takes the longest because it's setting up to server Etc um and then from this point on you can just run Comm all deploy and that will build rebuild the docker container restart your containers that are already running and traffic will handle uh moving from the old to the new so now that's running let's check to see if it's working this is what is on Local Host and let's see about simple host there it is it's up and running um it's running on cloud flare we have https and now all you need to do to deploy is Run come all deploy anytime you change and that's it you're ready to go
Info
Channel: Sam Johnson
Views: 1,248
Rating: undefined out of 5
Keywords:
Id: g5BvjOElMtY
Channel Id: undefined
Length: 19min 19sec (1159 seconds)
Published: Fri Feb 09 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.