An end to end workflow on how to deploy a Live View application to K8s

Hey. I’ve written an article about how to create an end to end deployment for a live view application in Kubernetes. (Actually, this one GitHub - dwyl/phoenix-liveview-counter-tutorial: 🤯 beginners tutorial building a real time counter in Phoenix 1.7.14 + LiveView 1.0 ⚡️ Learn the fundamentals from first principals so you can make something amazing! 🚀 · GitHub)

It has taken me some hours to be able to get it properly running, from the creation of the application with a release, fill in the necessary environment variables, create a public image and download it, testing all the steps, and I am using different tools for it in a Mac M1.

I thought it would be interesting to share it with all of you (so I can also find it when I need it in the future)

I hope you like it (suggestions and improvements are welcome)

You can find the article here:
https://medium.com/@chargio/creating-a-simple-elixir-phoenix-application-to-deploy-in-kubernetes-2eff2c8e367c

Using Phoenix 1.7 with LiveView 0.18.

Hey. I’ve updated the article with a second part in which I update the Phoenix Version, and publish it to a public K8s.

https://medium.com/@chargio/f0c1ccfd49e6

I hope it is useful.

I’ve been looking for something like this. Thank you :slight_smile:

@sergio-ocon Thanks for sharing. I am developing a chat application (using OpenAi) in Elixir / Phoenix and LiveView. I must make sure it will be production ready, meaning that the application will be scalable horizontally (adding nodes, machines etc ..). Do you think it will be possible, especially when it comes to LiveView and socket communication between the user and the servers? Holding some state across VM’s (pods) ? Thank you.

Hey.

If you are deploying your application in K8s you will need two things:

  1. You need to link the nodes so they talk to each other. There are some examples in libcluster that you can use
  2. You will need to put your state some place that can’t be destroyed. Of course you can use a genserver, and an umbrella application, but the containers are ephemeral, and will be destroyed if needed, so you need to take into account that your state machine can disappear.

I thought there was a way to configure K8 to direct the live view traffic to the same pods, did I misunderstand something (I am a K8 newbie so might talking BS)?

That would not make the backends to talk to each other. They will be independent versions of the application.
That means that if you connect twice, you can be connected to different instances with different counters.
You need to use the capabilities of the BEAM to connect them to each other. You can do it manually, or connect them automatically. If you are using K8s, the expectation is that the number of replicas is managed by K8s, so it makes sense to use a library to connect them automatically.

Gotcha, thank you!

@sergio-ocon I understood that a websocket connection between a client and a node/instance can be maintained if the load balancer in front of the nodes supports websockets, correct ? At least for an active current session.

I don’t know.
As far as I know, the connection is kept open, so it should be kept alive and pointing to the same endpoint, but any refresh could change that.
If you do it properly, your backend should be able to sync appropriately and it would work.

I’ve created a blog post to discuss this topic
https://medium.com/@chargio/moving-from-a-single-pod-to-multiple-pods-in-k8s-with-a-simple-elixir-application-69b0f1cb71e8

Thank you very much, I appreciate that.