Skip to content

Commit 983da7b

Browse files
committed
Add microservices example
1 parent b3f1431 commit 983da7b

16 files changed

Lines changed: 3194 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
logs/
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
version: v1alpha1
2+
cluster:
3+
cloudProvider: devspace-cloud
4+
# kubeContext: minikube
5+
devSpace:
6+
terminal:
7+
labelSelector:
8+
release: devspace-node
9+
deployments:
10+
- name: devspace-node
11+
kubectl:
12+
manifests:
13+
- node/kube/*
14+
- name: devspace-php
15+
helm:
16+
chartPath: php/chart
17+
ports:
18+
- labelSelector:
19+
release: devspace-node
20+
portMappings:
21+
- localPort: 3000
22+
remotePort: 3000
23+
sync:
24+
- containerPath: /app
25+
labelSelector:
26+
release: devspace-node
27+
localSubPath: ./node
28+
uploadExcludePaths:
29+
- Dockerfile
30+
- kube/
31+
- containerPath: /var/www/html
32+
labelSelector:
33+
release: devspace-php
34+
localSubPath: ./php
35+
uploadExcludePaths:
36+
- Dockerfile
37+
- chart/
38+
images:
39+
node:
40+
name: yourdockeruser/node
41+
build:
42+
dockerfilePath: node/Dockerfile
43+
contextPath: node/
44+
php:
45+
name: yourdockeruser/php
46+
build:
47+
dockerfilePath: php/Dockerfile
48+
contextPath: php/

examples/microservices/README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Dockerfile
2+
.devspace/
3+
chart/
4+
node_modules/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM node:8.11.4
2+
3+
RUN mkdir /app
4+
WORKDIR /app
5+
6+
COPY package.json .
7+
RUN npm install
8+
9+
COPY . .
10+
11+
CMD ["npm", "start"]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var express = require('express');
2+
var request = require('request');
3+
var app = express();
4+
5+
app.get('/', async (req, res) => {
6+
var body = await new Promise((resolve, reject) => {
7+
request('http://php/index.php', (err, res, body) => {
8+
if (err) {
9+
reject(err);
10+
return;
11+
}
12+
13+
resolve(body);
14+
});
15+
});
16+
17+
res.send(body);
18+
});
19+
20+
app.listen(3000, function () {
21+
console.log('Example app listening on port 3000!');
22+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: extensions/v1beta1
2+
kind: Deployment
3+
metadata:
4+
name: devspace
5+
spec:
6+
replicas: 1
7+
template:
8+
metadata:
9+
labels:
10+
release: devspace-node
11+
spec:
12+
containers:
13+
- name: node
14+
image: yourdockeruser/node
15+
command: ["sleep"]
16+
args: ["999999999999"]

0 commit comments

Comments
 (0)