Realtime sync k8s pod with Okteto

ujwal dhakal
2 min readJul 4, 2022

--

Ever wonder if there are feedback loop cycle that is shorter while working with kubernetes. What if you could sync k8s pod in real-time with Okteto?.

While working with the k8s system the feedback loop cycle is quite long. I mean if we want to update our code hosted on k8s. The common way to get this done is either we can deploy the code to the desired server or we can directly ssh into the k8s pod and execute some commands. Both ways are not that efficient.

Syncing local code

In this tutorial, I will show you how to sync code with Okteto in action.

Okteto is a tool that helps to sync your code with the local/remote k8s server. This is an open-source tool & they have a premium cloud solution for the hosting k8s app.

For this tutorial, I assume you have an up-and running k8s server running & you are able to access it from your local terminal.

  1. Install Okteto on your local machine.
  2. Go to the root of your project and initialize an okteto.xml file. The content looks like this for me. Since I am working with PHP the configuration looks somewhat similar regardless of the language you work with.
name: api
namespace: "staging-namespace"
command: "php-fpm"
context: "staging-server"
container: php
sync:
- .:/var/www/html
- $HOME/.composer:/root/.composer
persistentVolume:
enabled: false

It says that I want to connect the k8s namespace with the name “staging-namespace” and context : “staging-server”. As soon as I connect to the cluster I want to sync the PHP container & use “php-fpm command”. We have also specified the path of the file that we want to sync this means we want to sync all the things that are happening inside a current folder with the one that is hosted inside k8s.

Now, as soon as you make any changes inside your file the changes will be synced with the correct container name, namespace & context of k8s.

Conclusion

Okteto is not the only tool that is capable of doing this there are lot more tools that do similar things as Okteto does. But the main purpose here is to make the feedback loop shorter by syncing code to a remote server.

--

--