Skip to content

Commit 14d6d0d

Browse files
authored
Kube (brentley#3)
* changing az labels * fix startup * updating to read from env * updating entrypoint * entrypoint bash
1 parent 28c3ae2 commit 14d6d0d

File tree

5 files changed

+91
-36
lines changed

5 files changed

+91
-36
lines changed

Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ENV NODE_ENV ${NODE}
1010
# copy package info early to install npms and delete npm command
1111
WORKDIR /usr/src/app
1212
COPY package*.json ./
13-
RUN apk -U add curl nodejs nodejs-npm && \
13+
RUN apk -U add curl jq bash nodejs nodejs-npm && \
1414
npm install && apk del --purge nodejs-npm && \
1515
rm -rvf /var/cache/* /root/.npm /tmp/*
1616

@@ -19,4 +19,4 @@ COPY . .
1919
HEALTHCHECK --interval=10s --timeout=3s \
2020
CMD curl -f -s http://localhost:3000/health/ || exit 1
2121
EXPOSE 3000
22-
CMD [ "node", "server.js" ]
22+
ENTRYPOINT ["bash","/usr/src/app/startup.sh"]

kubernetes/deployment.yaml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: ecsdemo-nodejs
5+
labels:
6+
app: ecsdemo-nodejs
7+
namespace: default
8+
spec:
9+
replicas: 1
10+
selector:
11+
matchLabels:
12+
app: ecsdemo-nodejs
13+
strategy:
14+
rollingUpdate:
15+
maxSurge: 25%
16+
maxUnavailable: 25%
17+
type: RollingUpdate
18+
template:
19+
metadata:
20+
labels:
21+
app: ecsdemo-nodejs
22+
spec:
23+
containers:
24+
- image: brentley/ecsdemo-nodejs:latest
25+
imagePullPolicy: Always
26+
name: ecsdemo-nodejs
27+
ports:
28+
- containerPort: 3000
29+
protocol: TCP

kubernetes/service.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: ecsdemo-nodejs
5+
spec:
6+
selector:
7+
app: ecsdemo-nodejs
8+
ports:
9+
- protocol: TCP
10+
port: 80
11+
targetPort: 3000
12+

server.js

+6-34
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,12 @@ var app = express();
55
var fs = require('fs');
66
var code_hash = fs.readFileSync('code_hash.txt','utf8');
77
console.log (code_hash);
8+
console.log('The IPADDRESS is:', process.env.IP);
9+
console.log('The message is:', process.env.AZ);
10+
console.log('The hash is: %s', code_hash);
811

9-
// internal-ip: detect the correct IP based on default gw
10-
var internalip = require('internal-ip');
11-
var ipaddress = internalip.v4.sync();
12-
13-
// use ipaddress to find interface netmask
14-
var ifaces = require('os').networkInterfaces();
15-
for (var dev in ifaces) {
16-
// ... and find the one that matches the criteria
17-
var iface = ifaces[dev].filter(function(details) {
18-
return details.address === `${ipaddress}` && details.family === 'IPv4';
19-
});
20-
if(iface.length > 0) ifacenetmask = iface[0].netmask;
21-
}
22-
23-
// ip: separate out the network using the subnet mask
24-
var ipnet = require('ip');
25-
var network = ipnet.mask(`${ipaddress}`, `${ifacenetmask}`)
12+
var ipaddress = process.env.IP;
13+
var message = process.env.AZ;
2614

2715
// morgan: generate apache style logs to the console
2816
var morgan = require('morgan')
@@ -31,28 +19,12 @@ app.use(morgan('combined'));
3119
// express-healthcheck: respond on /health route for LB checks
3220
app.use('/health', require('express-healthcheck')());
3321

34-
// label the AZ based on which subnet we are on
35-
switch (network) {
36-
case '10.0.100.0':
37-
var az = '1a';
38-
break;
39-
case '10.0.101.0':
40-
var az = '1b';
41-
break;
42-
case '10.0.102.0':
43-
var az = '1c';
44-
break;
45-
default:
46-
var az = 'unknown'
47-
break;
48-
}
49-
5022
// main route
5123
app.get('/', function (req, res) {
5224
res.set({
5325
'Content-Type': 'text/plain'
5426
})
55-
res.send(`Node.js backend: Hello! from ${ipaddress} in AZ-${az} commit ${code_hash}`);
27+
res.send(`Node.js backend: Hello! from ${message} commit ${code_hash}`);
5628
// res.send(`Hello World! from ${ipaddress} in AZ-${az} which has been up for ` + process.uptime() + 'ms');
5729
});
5830

startup.sh

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
3+
set -x
4+
5+
IP=$(hostname -i)
6+
NETWORK=$(echo ${IP} | cut -f3 -d.)
7+
8+
case "${NETWORK}" in
9+
100)
10+
zone=a
11+
color=Crimson
12+
;;
13+
101)
14+
zone=b
15+
color=CornflowerBlue
16+
;;
17+
102)
18+
zone=c
19+
color=LightGreen
20+
;;
21+
*)
22+
zone=unknown
23+
color=Yellow
24+
;;
25+
esac
26+
27+
# kubernetes sets routes differently -- so we will discover our IP differently
28+
if [[ ${IP} == "" ]]; then
29+
IP=$(hostname -i)
30+
fi
31+
32+
# Am I on ec2 instances?
33+
if [[ ${zone} == "unknown" ]]; then
34+
zone=$(curl -m2 -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r '.availabilityZone' | grep -o .$)
35+
fi
36+
37+
export CODE_HASH="$(cat code_hash.txt)"
38+
export IP
39+
export AZ="${IP} in AZ-${zone}"
40+
41+
# exec container command
42+
exec node server.js

0 commit comments

Comments
 (0)