|
| 1 | +# Microservices example |
| 2 | + |
| 3 | +This example shows you how to develop two microservices in a single repo. The example consists of a small node webserver that makes a http request to a php webserver to retrieve some data. |
| 4 | + |
| 5 | +# Step 0: Prerequisites |
| 6 | + |
| 7 | +In order to use this example, make sure you have docker installed and a docker registry where you can push to (hub.docker.com, gcr.io etc.). Make sure you are logged in to the registry via `docker login`. |
| 8 | + |
| 9 | +Exchange the image name in `.devspace/config.yaml` under `images.php.name` and `images.node.name` with the image name you want to use. Do the same thing in `node/kube/deployment.yaml` under `spec.template.spec.image`. Do **not** add a tag to these image names, because this will be done at runtime automatically. |
| 10 | + |
| 11 | +## Optional: Use self hosted cluster (minikube, GKS etc.) instead of devspace-cloud |
| 12 | + |
| 13 | +If you want to use your own cluster instead of the devspace-cloud as deployment target, make sure `kubectl` is configured correctly to access your cluster. Then just erase the `cluster` section in the `.devspace/config.yaml` and devspace will use your current `kubectl` context as deployment target. |
| 14 | + |
| 15 | +# Step 1: Start the devspace |
| 16 | + |
| 17 | +To deploy the application simply run `devspace up`. The output of the command should look similar to this: |
| 18 | + |
| 19 | +``` |
| 20 | +[INFO] Building image 'fabian1991/php' with engine 'docker' |
| 21 | +[DONE] √ Authentication successful (hub.docker.com) |
| 22 | +Sending build context to Docker daemon 8.704kB |
| 23 | +Step 1/6 : FROM php:7.1-apache-stretch |
| 24 | + ---> 93e6fb4b13e1 |
| 25 | +[...] |
| 26 | +Step 6/6 : RUN usermod -u 1000 www-data; a2enmod rewrite; chown -R www-data:www-data /var/www/html |
| 27 | + ---> Using cache |
| 28 | + ---> 4b6e6f1150d3 |
| 29 | +Successfully built 4b6e6f1150d3 |
| 30 | +Successfully tagged fabian1991/php:Tsgwdi8 |
| 31 | +The push refers to repository [docker.io/fabian1991/php] |
| 32 | +bfee725f50e2: Layer already exists |
| 33 | +[...] |
| 34 | +237472299760: Layer already exists |
| 35 | +Tsgwdi8: digest: sha256:9e85195e0793af26e15181cb771d93acdc1ad40e3126193acd26eb4eb3765a03 size: 3867 |
| 36 | +[INFO] Image pushed to registry (hub.docker.com) |
| 37 | +[DONE] √ Done building and pushing image 'fabian1991/php' |
| 38 | +[INFO] Building image 'fabian1991/node' with engine 'docker' |
| 39 | +[DONE] √ Authentication successful (hub.docker.com) |
| 40 | +Sending build context to Docker daemon 108kB |
| 41 | +Step 1/7 : FROM node:8.11.4 |
| 42 | + ---> 8198006b2b57 |
| 43 | +[...] |
| 44 | +Step 7/7 : CMD ["npm", "start"] |
| 45 | + ---> Using cache |
| 46 | + ---> ea42e151ef28 |
| 47 | +Successfully built ea42e151ef28 |
| 48 | +Successfully tagged fabian1991/node:fq2KN6i |
| 49 | +The push refers to repository [docker.io/fabian1991/node] |
| 50 | +d3f119d48426: Layer already exists |
| 51 | +[...] |
| 52 | +8c466bf4ca6f: Layer already exists |
| 53 | +fq2KN6i: digest: sha256:0b7e9393b3300f2f2cb54db442417db16ae48bf66f0061b8d36cdcc7cc84d6c0 size: 2841 |
| 54 | +[INFO] Image pushed to registry (hub.docker.com) |
| 55 | +[DONE] √ Done building and pushing image 'fabian1991/node' |
| 56 | +[INFO] Deploying devspace-node with kubectl |
| 57 | +deployment.extensions/devspace configured |
| 58 | +[DONE] √ Successfully deployed devspace-node |
| 59 | +[INFO] Deploying devspace-php with helm |
| 60 | +[DONE] √ Tiller started |
| 61 | +[DONE] √ Deployed helm chart (Release revision: 1) |
| 62 | +[DONE] √ Successfully deployed devspace-php |
| 63 | +[DONE] √ Port forwarding started on 3000:3000 |
| 64 | +[DONE] √ Sync started on /go-workspace/src/github.com/covexo/devspace/examples/microservices/node <-> /app (Pod:test/devspace-7ffbf854ff-hk4jq) |
| 65 | +[DONE] √ Sync started on /go-workspace/src/github.com/covexo/devspace/examples/microservices/php <-> /var/www/html (Pod: test/devspace-php-7f9b876786-hcwxz) |
| 66 | +root@devspace-7ffbf854ff-hk4jq:/app# |
| 67 | +``` |
| 68 | + |
| 69 | +The command built two docker images: One docker image for the node application in `node/Dockerfile` and one for the php application in `php/Dockerfile`. Then the images were pushed to the docker registry. Afterwards, the deployment.yaml in `node/kube/deployment.yaml` was deployed with kubectl and the chart in `php/chart` was deployed via helm. |
| 70 | + |
| 71 | +Furthermore a bi-directional sync was started between the local folder `/go-workspace/src/github.com/covexo/devspace/examples/microservices/node` and the `/app` folder within the node container. Also the folder `/go-workspace/src/github.com/covexo/devspace/examples/microservices/php` and the `/var/www/html` are synchronized. Whenever you change a file in either of those folders the change will be synchronized. In addition the node container port 3000 was forwarded to your local port 3000. |
| 72 | + |
| 73 | +# Step 2: Start developing |
| 74 | + |
| 75 | +A terminal to the node container should been automatically opened. You can start the server now with `npm start` in the open terminal. Now navigate in your browser to `localhost:3000` and you should see the output 'PHP say's Hello World!'. The node server just did a request to the php server in the background. |
| 76 | + |
| 77 | +Now try to change the `php/index.php` and alter the message. Simply refresh your browser and your changes should be visible! |
| 78 | + |
| 79 | +You can also change something in `node/index.js` locally and you should see something like this: |
| 80 | + |
| 81 | +``` |
| 82 | +[nodemon] 1.18.4 |
| 83 | +[nodemon] to restart at any time, enter `rs` |
| 84 | +[nodemon] watching: *.* |
| 85 | +[nodemon] starting `node index.js` |
| 86 | +Example app listening on port 3000! |
| 87 | +[nodemon] restarting due to changes... |
| 88 | +[nodemon] starting `node index.js` |
| 89 | +Example app listening on port 3000! |
| 90 | +``` |
| 91 | + |
| 92 | +Now just refresh your browser and you should see the changes immediately. |
0 commit comments