How to do local development with Kubernetes?

2019-01-30 03:42发布

Kubernetes seems to be all about deploying containers to a cloud of clusters. What it doesn't seem to touch is development and staging environments (or such).

During development you want to be as close as possible to production environment with some important changes:

  • Deployed locally (or at least somewhere where you and only you can access)
  • Use latest source code on page refresh (supposing its a website; ideally page auto-refresh on local file save which can be done if you mount source code and use some stuff like Yeoman).

Similarly one may want a non-public environment to do continuous integration.

Does Kubernetes support such kind of development environment or is it something one has to build, hoping that during production it'll still work?

10条回答
倾城 Initia
2楼-- · 2019-01-30 03:58

As specified before by Robert, minikube is the way to go.

Here is a quick guide to get started with minikube. The general steps are:

  • Install minikube

  • Create minikube cluster (in a Virtual Machine which can be VirtualBox or Docker for Mac or HyperV in case of Windows)

  • Create Docker image of your application file (by using Dockerfile)

  • Run the image by creating a Deployment

  • Create a service which exposes your application so that you can access it.

查看更多
虎瘦雄心在
3楼-- · 2019-01-30 03:59

Another great starting point is this Vagrant setup, esp. if your host OS is Windows. The obvious advantages being

  • quick and painless setup
  • easy to destroy / recreate the machine
  • implicit limit on resources
  • ability to test horizontal scaling by creating multiple nodes

The disadvantages - you need lot of RAM, and VirtualBox is VirtualBox... for better or worse.

A mixed advantage / disadvantage is mapping files through NFS. In our setup, we created two sets of RC definitions - one that just download a docker image of our application servers; the other with 7 extra lines that set up file mapping from HostOS -> Vagrant -> VirtualBox -> CoreOS -> Kubernetes pod; overwriting the source code from the Docker image.

The downside of this is NFS file cache - with it, it's problematic, without it, it's problematically slow. Even setting mount_options: 'nolock,vers=3,udp,noac' doesn't get rid of caching problems completely, but it works most of the time. Some Gulp tasks ran in a container can take 5 minutes when they take 8 seconds on host OS. A good compromise seems to be mount_options: 'nolock,vers=3,udp,ac,hard,noatime,nodiratime,acregmin=2,acdirmin=5,acregmax=15,acdirmax=15'.

As for automatic code reload, that's language specific, but we're happy with Django's devserver for Python, and Nodemon for Node.js. For frontend projects, you can of course do a lot with something like gulp+browserSync+watch, but for many developers it's not difficult to serve from Apache and just do traditional hard refresh.

We keep 4 sets of yaml files for Kubernetes. Dev, "devstable", stage, prod. The differences between those are

  • env variables explicitly setting the environment (dev/stage/prod)
  • number of replicas
  • devstable, stage, prod uses docker images
  • dev uses docker images, and maps NFS folder with source code over them.

It's very useful to create a lot of bash aliases and autocomplete - I can just type rec users and it will do kubectl delete -f ... ; kubectl create -f .... If I want the whole set up started, I type recfo, and it recreates a dozen services, pulling the latest docker images, importing the latest db dump from Staging env and cleaning up old Docker files to save space.

查看更多
forever°为你锁心
4楼-- · 2019-01-30 04:00

We've been working on a tool to do this. Basic idea is you have remote Kubernetes cluster, effectively a staging environment, and then you run code locally and it gets proxied to the remote cluster. You get transparent network access, environment variables copied over, access to volumes... as close as feasible to remote environment, but with your code running locally and under your full control.

So you can do live development, say. Docs at http://telepresence.io

查看更多
成全新的幸福
5楼-- · 2019-01-30 04:00

Kubespary is helpful setting up local clusters. Mostly, I used vagrant based cluster on local machine.

Kubespray configuration You could tweak these variables to have the desired kubernetes version.

查看更多
劫难
6楼-- · 2019-01-30 04:07

I've just started with Skaffold

It's really useful to apply changes in the code automatically to a local cluster.

To deploy a local cluster, the best way is Minikube or just Docker for Mac and Windows, both includes a Kubernetes interface.

查看更多
贼婆χ
7楼-- · 2019-01-30 04:11

The sort of "hot reload" is something we have plans to add, but is not as easy as it could be today. However, if you're feeling adventurous you can use rsync with docker exec, kubectl exec, or osc exec (all do the same thing roughly) to sync a local directory into a container whenever it changes. You can use rsync with kubectl or osc exec like so:

# rsync using osc as netcat
$ rsync -av -e 'osc exec -ip test -- /bin/bash' mylocalfolder/ /tmp/remote/folder
查看更多
登录 后发表回答