Rails World 2026: Kamal Deep Dive (Josef Strzibny) Video: https://www.youtube.com/watch?v=CsWQD81i5-c Talk page: https://www.rubyevents.org/talks/kamal-deep-dive Unedited machine transcript, generated with Whisper. It contains recognition errors (names, technical terms). Check the video before quoting. ---- Hi, I'm Josef, and I'm Kamal Holik. Nothing seemed wrong at first. I just deployed a few apps and wrote a blog post, but it got worse. the first ever book on Kamal. And I even made a video course of this ridiculous boat video. And it become a serious condition. I was here this year published in IX magazine with my Kamal article. And I started to give Kamal talks like the one in Wroclaw Ruby, where I shared some not so known facts about Kamal. And this brings me to Austin Texas to here. But my journey to Austin Texas started earlier because I joined a new company. Of course, the company is SERP API. If you don't know what we do, we give live search data for your AI as a JSON or as a markdown. You can have it your way. I also work on an application called SERP Trail. SEO tracker. And last time I checked, it was still on Rails, not in Rust. You can check it out. It also comes with Kamal configuration. And this brings me also to the topic of today, my first time in America. Now, this is not AI swap. This is two size of coffee in Americas. know that I went to a conference, but we shall not speak its names. And it was in LA, California. So if you were on my last talk, you know that I'm an unapologetic fan of Chef Gordon Ramsey, which is a celebrity chef. And why I mention him for my talk is because we know that America is known for one thing, and it's fast food. And there is a secret with Chef Gordon Ramsay that he actually... Yeah. I actually don't like him for the... I don't like him for the fancy stuff dining, but for all the wrong reasons. And he likes fast food too, right? He likes in and out burgers. And because it's a chain in California, I had to go and try it. So I order essentially a portion for one. should also finish. But no, I actually got help by my colleagues from SERP API. And after the conference, I went to Hollywood, of course, because you are in LA, right? Walking this pavement with the stars. And then I saw that, oh, you can actually drive a Ferrari like a movie star, right? I thought, what a tourist But actually, after a closer look, they have a Ferrari California as a model there. So I got sold, you know. But there was one problem. They give you this very scary-looking waiver to sign. And I was like, Josef, if you are going to read it, you are not going to sign it. And so I didn't. I didn't read it. I just signed it. And next thing I know, I was sitting in a Ferrari for the very first time. And I realized that maybe I also have addiction for expensive cars. But after chatting with ChatGPT about my financials, I decided I will just stay with the addiction I can afford. And I will take you today with me to explore Kamal a little bit. a couple of deep dives. We, of course, explored the most insisting command, which is Kamal deploy. And we will go all the way from SSH to proxy request. Now, if you are here by accident, you don't know even what Kamal is, it's important to know how Kamal sees your application, because there are application roles and accessories. So Kamal, of course, uses Docker. So you have your Docker image. And then you would have, for example, running Puma as your web server. It would be your first role, the so-called web role, the primary role. And then you can have more roles like for jobs or for something else. And then there are some other accessories like your database or headless browser or whatever. And that have different lifecycle. not what we will really talk about. It's also important to realize that we have Kamal, the CLI, and Kamal proxy that will run on your server. So you are running Kamal deploy in your laptop, and then we are over SSH running some commands, Kamal proxy, of course, running there. So let's go, and let's go for first dive. So that's SSH and Docker. If you already deployed with Kamal, Now, using the screen, it's my favorite feature of Kamal because you see everything that's happening. But even before you actually run the command, of course, we need something to transfer that command to code. So for that, we use Tor in Kamal. If that sounds familiar, yes, it's also used in Rails. So Kamal simply subclasses from Tor and then implement the command. Kamal deploy. There are many sub-commands that can be also invoked from the command line, but if it's in code, there will be this invoke method, and then we go further. That leads me to SSH kit. Also might sound familiar, because we know it's from Capacity's AnoTimes, and that actually runs the SSH command for Kamal. So Kamal includes a little bit, a small patch that I will talk about, and a small DSL. So we have DSL for running, of course, some code on hosts on servers and some code locally but Kamal because you have roles that we also have to have on roles yeah so we extend it just a little bit more so essentially we have this configuration for Kamal and then in the end we are running some commands it's mostly like docker commands on the on the server and the code itself for the commands come from command Kamal commands namespace so for example very simple example would be docker v for well it's docker installed if you have something more complex it would be full docker run and you can notice it almost look like a shell yeah there are some symbols there are some things there's some methods but essentially it looks like a shell that's because the docker method actually builds an array and puts docker symbol at the beginning. It does it because it actually registered Docker as a sshkit command. And when it does, it means that we are skipping the user bin environment binary and the logs get cleaner, right? Because it still works even by just showing out the Docker. I talked about methods. So for example, let's look at how sensitive data are handled. So we will have this class for sensitive. And if we delegate, well, if we need to string, right? It will be unreducted. But if you are just inspecting, we will get the reduction. So this is the example, for example, for registry password. Yeah, but we are still having this string of tokens as an array. Now we can combine the commands. We know it's array. So it's just operations on arrays, right? We can compact, collect, type. Now we want pipe in, you know, pipe from shell. Well, it's the same thing. be divided by a pipe. We can also have a new shell like shc, you know, or for getDocker, we now would use that shell method with any one of them, still just erase, and we could create like docker install command, which is like pipe getDocker to sh. And finally, when we have that, yeah, Kamal keeps it for as an for as long as it's possible, but then it has to pass it to sshkit for actually running it. If you would study the camel code, you would also see this execute method. Now, the execute method actually comes from net ssh backend for sshkit, and it would actually really run the command. Now, the asterisk is there because it accepts positional arguments, command like that and it will execute. And we have also come out as a constant because now that we have both the Tor and SSHKit, which also used instant exec, we are losing the self. So we still need to be able to ask for global configuration for all the global stuff. So that's why we have that. And how is it done concurrently on many hosts? For that, I will return to the SSHKit patch. SSHKit would normally run threads each join, so just joining all threads, but Kamal does something a little bit different. It still joins, but it rescues for all the exceptions from all the errors. This is so that we really run all the code everywhere on every host, and we just collect the errors instead of just bailing out. And finally, so we have threads, yes, but what about TCP connection? We also should share them because they are expensive to create. So yes, they are shared because all these little commands from Docker login to Docker stop. If they are running on concurrent roles, then they can be actually reused for connection. So there will be a pool and it will get reused. Now the second dive, building and pushing images. So of course, Docker is our application, it's build kit part of docker kind of or mobile project which is the upstream for docker builds your image right so essentially we will run docker build x build the build x command is a bit weird because this is actually a plugin to to docker so we need to talk about docker builder drivers so there's docker and docker container docker is the original one that was the docker build right simple one And we also have Docker container, which Kamal uses because we need a little bit more. We need multiple architectures and we need exports and we need multi-node and so on. So the builder is the name instance to run. So when we are configuring in the YAML file, maybe you are saying builder something. So that would be the instance. of that Docker container thing. And isn't it a little bit confusing? For the first time, I think, yes, because of the naming. So we run camel deploy. As part of it, we are building the image. We are running docker build.exe that uses the build.exe plugin that will spin up a new container. That's why it's called docker container. And the build kit inside the container will then run your Docker file. build the image. Now if that happens on a remote builder, it's actually completely the same. Kamal can just do it as long as Docker is pre-installed on that host and that's it. And that brings me to registries because part of that command is of course upload the image to registry. So we have the usual Docker Hub, right? Then you can have different provider. But what's interesting is the local registry, right? We can now run registries. So how it's done is that we really just run an extra container, which is the Kamal Docker registry, and we're really running the registry image. There is nothing special about that. Except maybe one special thing, because what about the ports, right? How our hosts, how our servers see a local port on our laptop. So you might know a local forwarding that you might used before when you needed to take a database port from the server and see it locally for some inspection. And so essentially here is just the opposite. So it's remote forwarding, something like SSHR. Kamel does not shell out SSHR. Of course, it's implemented in SSH gem. But essentially, this is how it works. It will be like some kind of port forwarding in Kamel. And now the push and pull, right? How the image gets on there. So for the push, yeah, we are running Docker BuildX Build. So we already seen that, right? Of course, because it's push, but it also, it builds and then pushes, right? And for pull, if we sort out the port I talked about, now the servers see our port, so we can just pull the image on host. and there is no problem. So, next we need a service registration. So, Kamal has this configuration file, but Kamal Proxy doesn't have a configuration specifically. Every instruction for it would be actually Docker exec from Kamal CLI because we have this imperative way of doing things. need it for the proxy as well. And on camel proxy, we'll essentially run camel proxy deploy. That's the command for the proxy. Now, since we are passing everything in one docker exec, we need to pass all the flags. So from TSO to logging, buffering, whatever we need, we just pass it on. So how it looks like, we have docker exec camel proxy. That means the first one is like, okay, run something on this container. and then Kamal proxy, that will be the binary, deploy our service here, searcher.web, and then we pass everything else. Now the target, that's also important, that would be the container ID and port. And now, of course, when we are passing it imperatively, what happens if the proxy goes down? So there's Kamal proxy config named volume. I will keep the configuration so Kamal proxy can load it up. other service actions so the deploy of course we can remove the service later or we can stop it maintenance mode and resume it and that brings me to zero downtown deployments that are very essential to how camel works and runs so let's return to the docker exact camel proxy command because when we are running a new version we just run this again with the same service name we just use a new deploy target so we change the tiger target which is the container id and the port and then if you are running proxy we insert the proxy to deploy so first we run the container is running and proxy can actually do the switch so in camel we use blue green deployment so simple case single host we will be booting the new the Vibro container, which is our Puma that needs to be served traffic. We pass it over to Karma Proxy, which will run health check. And once done, it can switch over the traffic to the new green ones. And of course it has to drain the request for the previous ones. Now we also have to do something with assets because we have two versions at the same time. So essentially we unpack the assets to the same directory And then we copy two volumes for both the old one and new ones. The copy N ensures that the first one that let's copy over are not overwritten. So we have to just follow the right order and that's it. Both containers can serve both assets. They need to be fingerprinted, of course, but then everything works. Log is even easier. when we would have concurrent deploys, we are kind of locking it, so nothing bad happens. And this is just really creating a directory on the server, and that's it. Now, what happens when we have more hosts and we need to coordinate them, right? So it's kind of the same thing, but a bit more complex. For that, we will dive even deeper to the great barriers, the barrier or what's also called the web barrier, web according to the roles that we are protecting. How it looks like? So it's part of actually health check namespace and we will have an invariant which is like immutable variable and that's our invariant which we can set once. So the barrier is either open or it's closed and when we set it we don't care about errors, but only once it can be set to true. So what it does is that we will start booting all web roles container and the first one that boots correctly, that will open the barrier and then let everybody, everything else boots up. So only one is needed and everything gets switched over and then the switch happens like we talked before. How it looks in code. So we wait At barrier, if we are QR, which means we are not web role, starting new version. And of course, if we are web role, that we are the gatekeeper, so we release the barrier. Then we can stop it. The start new version. Also, what you would expect, we upload the environment, file the new secrets. We run up run, which means we are starting container. And then if we are running the proxy, we let the proxy to actually do the change with deploy. Now for running deploys, of course, Kamal makes it easy to run many, many servers. So we just slice them up. You know, you've probably seen it in the config with the limit. And this is just in the code. So we can provide a number, digit or percentage. And this will work. And finally, for final dive today, we have, we need to learn how to proxy the request, right? This is a bit of a Go territory. It's not Ruby. It seems to be the topic of this conference. But don't worry, because I will teach you Go just now. So if you heard about Go when it was released, it was all about Go routines. and channels right so what are go routines they are concurrent functions uh that are scheduled scheduled by go scheduler so they happen automatically all over the all available threads it can be one or many cpus and it will all work so essentially there will be somewhere in the code will be go keyword that's maybe why go language its name and so for example here in the net http package if you are I think the new ReactFest will be like go serve and so on. It's simplified. But if you go to camel proxy code, you will not actually find the go keyword at all because it will use this package, then it will run the middleware there, and then this handler will be called. And part of this handler, we will simply find the right service for the ReactFest and service. And the second feature of go that's so important are channels. running functions, maybe they want to coordinate something. So essentially, it's something like first-in, first-out, QE, let's say. The goroutines can write the channels or they can read from channels. We don't know which goroutine picks up a new value in the channel, but if the channel is closed, all goroutines will know it. So first thing that's implemented like that is health check, channel, the values in the channels are actually just the interval, our specified interval. And when the channel gets new value, we can go and run the check, the actual check, right? So in code, we will have a new ticker with our interval from the configuration. And we have this for loop that is kind of running forever. very known in Go code. It's everywhere there. So basically, these cases are waiting for some new value or some new information from channels. So here, when we get the new value in ticker C, we just go and check it. Then the camel proxy also does buffering. So when the rectus is too big, you know, this is kind of protection against low clients. So some part of that is memory, some part is written on disk, but in any case, it will be buffered up and sent to Puma later. Draining is maybe more interesting. So I told you about draining, we have to drain requests, but how does it look like actually? This is the same select, I just visualized it in different ways, so we would have this request context done. So if request is done, we can let it finish. But if you have deadline, have to cancel it. In code, if we look at the drain function, first, there is something about in-flight requests, about hijacked in-flight requests. So that's our web socket side, that's our action cable. We just kill it immediately, we don't bother to do anything. But with the others, we have this base for requests to complete, like this named block, and this will actually, for all these requests in parallel, wait for either the request is done or we are hitting a deadline. If we are hitting a deadline, we go outside of that name block and for whatever is remaining, we just cancel with error draining. Yeah. And maintenance mode, that's actually also very interesting. So we have this pause channel and when we create it, all these requests are waiting. These gore teams are waiting for the channel. it, all of them know about it and can continue. So we don't need any particular value, it's just closed and it's done. So setState, there will be this close/pause channel, and somewhere inside the request, we will again have the same select, just waiting for our channels. So what we learned? We learned that Kamala is based on Tor and SSHKit, how it keeps the commands as arrays until the last moment where it has to execute it. And then the Kamal proxy comes in because you have to set it up with a command to register the service. So it can then do blue-green style deployment, maybe coordinating many servers. And then finally, we also learn Go. So maybe your brain is fried already, like a chicken sandwich. And yeah, I cannot stop talking about Kamal. This is my problem. So even when I was in California, I talked to this girl, right? I was explaining that, yeah, Kamal is this simple deploy tool you can replace your Kubernetes clusters with, you know, and then save a bunch of money. And she was like, sir, this is a Wendy's. And some say I am doing America wrong with all these fast foods. It's potentially right. Texas barbecue. And I will also finally drive a proper American muscle car. So not a Ferrari anymore. So long live Kamal. And God bless America. So thank you. Enjoy the rest of the race world. Bye. Bye. Bye.