From b27639842ea4285734c7d3191693e683ddad0066 Mon Sep 17 00:00:00 2001 From: Daniele Polencic Date: Thu, 9 Nov 2017 12:24:48 +0000 Subject: [PATCH 1/4] templated chart --- chart/templates/_helpers.tpl | 3 +++ chart/templates/configmap.yaml | 6 ++++++ chart/templates/deployment.yaml | 16 ++++++++++++---- chart/templates/ingress.yaml | 11 ++++++----- chart/templates/service.yaml | 6 +++--- chart/values.yaml | 1 + 6 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 chart/templates/_helpers.tpl create mode 100644 chart/templates/configmap.yaml create mode 100644 chart/values.yaml diff --git a/chart/templates/_helpers.tpl b/chart/templates/_helpers.tpl new file mode 100644 index 0000000..8695952 --- /dev/null +++ b/chart/templates/_helpers.tpl @@ -0,0 +1,3 @@ +{{- define "name" -}} +{{- printf "%s-%s" .Release.Name .Chart.Name -}} +{{- end -}} diff --git a/chart/templates/configmap.yaml b/chart/templates/configmap.yaml new file mode 100644 index 0000000..8a2b594 --- /dev/null +++ b/chart/templates/configmap.yaml @@ -0,0 +1,6 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ template "name" . }} +data: + version: "3" diff --git a/chart/templates/deployment.yaml b/chart/templates/deployment.yaml index 6e01939..931dcbb 100644 --- a/chart/templates/deployment.yaml +++ b/chart/templates/deployment.yaml @@ -1,17 +1,25 @@ apiVersion: extensions/v1beta1 kind: Deployment metadata: - name: kube-hello-world + name: {{ template "name" . }} spec: - replicas: 1 + replicas: 3 template: metadata: labels: - name: kube-hello-world + name: {{ template "name" . }} + annotations: + checksum: {{ include (print $.Chart.Name "/templates/configmap.yaml") . | sha256sum }} spec: containers: - name: hello-world-nodejs - image: first-container + image: {{ .Values.image }} imagePullPolicy: IfNotPresent ports: - containerPort: 8080 + env: + - name: VERSION + valueFrom: + configMapKeyRef: + name: {{ template "name" . }} + key: version diff --git a/chart/templates/ingress.yaml b/chart/templates/ingress.yaml index 8738078..5075a70 100644 --- a/chart/templates/ingress.yaml +++ b/chart/templates/ingress.yaml @@ -1,13 +1,14 @@ apiVersion: extensions/v1beta1 kind: Ingress metadata: - name: kube-hello-world-ingress + name: {{ template "name" . }} spec: rules: - - http: + - host: {{ .Release.Name }}.hello-world.dev + http: paths: - - backend: - serviceName: kube-hello-world-service + - path: / + backend: + serviceName: {{ template "name" . }} servicePort: 80 - path: / diff --git a/chart/templates/service.yaml b/chart/templates/service.yaml index 669242b..ecc10a5 100644 --- a/chart/templates/service.yaml +++ b/chart/templates/service.yaml @@ -2,12 +2,12 @@ apiVersion: v1 kind: Service metadata: labels: - name: kube-hello-world-service - name: kube-hello-world-service + name: {{ template "name" . }} + name: {{ template "name" . }} spec: ports: - name: exposed-port port: 80 targetPort: 8080 selector: - name: kube-hello-world + name: {{ template "name" . }} diff --git a/chart/values.yaml b/chart/values.yaml new file mode 100644 index 0000000..7af1c12 --- /dev/null +++ b/chart/values.yaml @@ -0,0 +1 @@ +image: first-container From 5179c575a103c74ca87270b40ef5daea3ab554fd Mon Sep 17 00:00:00 2001 From: Daniele Polencic Date: Fri, 10 Nov 2017 15:09:36 +0000 Subject: [PATCH 2/4] fixes for gce --- chart/templates/deployment.yaml | 2 +- chart/templates/ingress.yaml | 5 ++--- chart/templates/service.yaml | 4 ++-- chart/values.yaml | 2 +- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/chart/templates/deployment.yaml b/chart/templates/deployment.yaml index 931dcbb..2f5dd3b 100644 --- a/chart/templates/deployment.yaml +++ b/chart/templates/deployment.yaml @@ -3,7 +3,7 @@ kind: Deployment metadata: name: {{ template "name" . }} spec: - replicas: 3 + replicas: 1 template: metadata: labels: diff --git a/chart/templates/ingress.yaml b/chart/templates/ingress.yaml index 5075a70..9bbc1e4 100644 --- a/chart/templates/ingress.yaml +++ b/chart/templates/ingress.yaml @@ -4,10 +4,9 @@ metadata: name: {{ template "name" . }} spec: rules: - - host: {{ .Release.Name }}.hello-world.dev - http: + - http: paths: - - path: / + - path: /* backend: serviceName: {{ template "name" . }} servicePort: 80 diff --git a/chart/templates/service.yaml b/chart/templates/service.yaml index ecc10a5..5c4e539 100644 --- a/chart/templates/service.yaml +++ b/chart/templates/service.yaml @@ -6,8 +6,8 @@ metadata: name: {{ template "name" . }} spec: ports: - - name: exposed-port - port: 80 + - port: 80 targetPort: 8080 selector: name: {{ template "name" . }} + type: NodePort diff --git a/chart/values.yaml b/chart/values.yaml index 7af1c12..6f945c4 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -1 +1 @@ -image: first-container +image: learnk8s/node-hello-world From 569c15b16f927412551f82f08538d8b1475e32b8 Mon Sep 17 00:00:00 2001 From: Daniele Polencic Date: Sun, 12 Nov 2017 16:43:23 +0000 Subject: [PATCH 3/4] ignore paths in ingress --- chart/templates/ingress.yaml | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/chart/templates/ingress.yaml b/chart/templates/ingress.yaml index 9bbc1e4..e7116b6 100644 --- a/chart/templates/ingress.yaml +++ b/chart/templates/ingress.yaml @@ -3,11 +3,7 @@ kind: Ingress metadata: name: {{ template "name" . }} spec: - rules: - - http: - paths: - - path: /* - backend: - serviceName: {{ template "name" . }} - servicePort: 80 + backend: + serviceName: {{ template "name" . }} + servicePort: 80 From f8a6f2ab93045c15eacb25fc064c29aa9ea8224c Mon Sep 17 00:00:00 2001 From: Daniele Polencic Date: Sun, 12 Nov 2017 19:09:32 +0000 Subject: [PATCH 4/4] demo --- Dockerfile | 2 +- index.js | 22 ++++++-- kube/deployment-v2.yaml | 27 +++++++++ kube/deployment.yaml | 27 +++++++++ kube/service-v2.yaml | 13 +++++ kube/service.yaml | 12 ++++ package.json | 3 +- yarn.lock | 118 +++++++++++++++++++++++++++++++++++++++- 8 files changed, 217 insertions(+), 7 deletions(-) create mode 100644 kube/deployment-v2.yaml create mode 100644 kube/deployment.yaml create mode 100644 kube/service-v2.yaml create mode 100644 kube/service.yaml diff --git a/Dockerfile b/Dockerfile index afcade6..2bf5b05 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM node +FROM node:8-alpine WORKDIR /app COPY . . RUN yarn install diff --git a/index.js b/index.js index d80b979..db2d556 100644 --- a/index.js +++ b/index.js @@ -1,15 +1,29 @@ const express = require('express') const app = express() -const port = process.env.PORT || 8080; +const Redis = require('ioredis') + +const VERSION = '2.0.0' + +const redis = new Redis() + +redis.on('error', () => {}) app.get('/', (req, res) => { res.send('Hello World!') }) app.get('/version', (req, res) => { - res.send(process.env.VERSION || 'No version') + res.send(VERSION) +}) + +app.get('/healthz', (req, res) => { + if (redis.status === 'ready') { + res.sendStatus(200) + } else { + res.sendStatus(500) + } }) -app.listen(port, () => { - console.log(`Example app listening on port ${port}!`) +app.listen(3000, () => { + console.log('Example app listening on port 3000!') }) diff --git a/kube/deployment-v2.yaml b/kube/deployment-v2.yaml new file mode 100644 index 0000000..02d5344 --- /dev/null +++ b/kube/deployment-v2.yaml @@ -0,0 +1,27 @@ +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: helloworld-v2 +spec: + replicas: 1 + template: + metadata: + labels: + app: helloworld + version: v2 + spec: + containers: + - name: web + image: learnk8s/demo:2.0.0 + ports: + - containerPort: 3000 + livenessProbe: + initialDelaySeconds: 2 + periodSeconds: 5 + httpGet: + path: /healthz + port: 3000 + - name: redis + image: redis:4.0.2-alpine + ports: + - containerPort: 6379 diff --git a/kube/deployment.yaml b/kube/deployment.yaml new file mode 100644 index 0000000..002a79f --- /dev/null +++ b/kube/deployment.yaml @@ -0,0 +1,27 @@ +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: helloworld +spec: + replicas: 1 + template: + metadata: + labels: + app: helloworld + version: v1 + spec: + containers: + - name: web + image: learnk8s/demo:1.0.0 + ports: + - containerPort: 3000 + livenessProbe: + initialDelaySeconds: 2 + periodSeconds: 5 + httpGet: + path: /healthz + port: 3000 + - name: redis + image: redis:4.0.2-alpine + ports: + - containerPort: 6379 diff --git a/kube/service-v2.yaml b/kube/service-v2.yaml new file mode 100644 index 0000000..3d07fcc --- /dev/null +++ b/kube/service-v2.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Service +metadata: + name: helloworld-v2 +spec: + ports: + - nodePort: 32001 + port: 80 + targetPort: 3000 + selector: + app: helloworld + version: v2 + type: NodePort diff --git a/kube/service.yaml b/kube/service.yaml new file mode 100644 index 0000000..53f681b --- /dev/null +++ b/kube/service.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Service +metadata: + name: helloworld +spec: + ports: + - nodePort: 32000 + port: 80 + targetPort: 3000 + selector: + app: helloworld + type: NodePort diff --git a/package.json b/package.json index 039f407..2a590fe 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "author": "", "license": "MIT", "dependencies": { - "express": "^4.16.2" + "express": "^4.16.2", + "ioredis": "^3.2.1" }, "scripts": { "test": "node test.js", diff --git a/yarn.lock b/yarn.lock index ee44c67..4d9fde3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -17,6 +17,10 @@ balanced-match@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" +bluebird@^3.3.4: + version "3.5.1" + resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.1.tgz#d9551f9de98f1fcda1e683d17ee91a0602ee2eb9" + body-parser@1.18.2: version "1.18.2" resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.18.2.tgz#87678a19d84b47d859b83199bd59bce222b10454" @@ -43,6 +47,10 @@ bytes@3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.0.0.tgz#d32815404d689699f85a4ea4fa8755dd13a96048" +cluster-key-slot@^1.0.6: + version "1.0.8" + resolved "https://registry.yarnpkg.com/cluster-key-slot/-/cluster-key-slot-1.0.8.tgz#7654556085a65330932a2e8b5976f8e2d0b3e414" + concat-map@0.0.1: version "0.0.1" resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" @@ -63,7 +71,7 @@ cookie@0.3.1: version "0.3.1" resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.3.1.tgz#e7e0a1f9ef43b4c8ba925c5c5a96e806d16873bb" -debug@2.6.9: +debug@2.6.9, debug@^2.2.0: version "2.6.9" resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" dependencies: @@ -84,6 +92,10 @@ defined@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/defined/-/defined-1.0.0.tgz#c98d9bcef75674188e110969151199e39b1fa693" +denque@^1.1.0: + version "1.2.2" + resolved "https://registry.yarnpkg.com/denque/-/denque-1.2.2.tgz#e06cf7cf0da8badc88cbdaabf8fc0a70d659f1d4" + depd@1.1.1, depd@~1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.1.tgz#5783b4e1c459f06fa5ca27f991f3d06e7a310359" @@ -173,6 +185,10 @@ finalhandler@1.1.0: statuses "~1.3.1" unpipe "~1.0.0" +flexbuffer@0.0.6: + version "0.0.6" + resolved "https://registry.yarnpkg.com/flexbuffer/-/flexbuffer-0.0.6.tgz#039fdf23f8823e440c38f3277e6fef1174215b30" + for-each@~0.3.2: version "0.3.2" resolved "https://registry.yarnpkg.com/for-each/-/for-each-0.3.2.tgz#2c40450b9348e97f281322593ba96704b9abd4d4" @@ -240,6 +256,34 @@ inherits@2, inherits@2.0.3, inherits@~2.0.3: version "2.0.3" resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" +ioredis@^3.2.1: + version "3.2.1" + resolved "https://registry.yarnpkg.com/ioredis/-/ioredis-3.2.1.tgz#4c10bcce9659fdb0af923b0e7915208fe023d3f0" + dependencies: + bluebird "^3.3.4" + cluster-key-slot "^1.0.6" + debug "^2.2.0" + denque "^1.1.0" + flexbuffer "0.0.6" + lodash.assign "^4.2.0" + lodash.bind "^4.2.1" + lodash.clone "^4.5.0" + lodash.clonedeep "^4.5.0" + lodash.defaults "^4.2.0" + lodash.difference "^4.5.0" + lodash.flatten "^4.4.0" + lodash.foreach "^4.5.0" + lodash.isempty "^4.4.0" + lodash.keys "^4.2.0" + lodash.noop "^3.0.1" + lodash.partial "^4.2.1" + lodash.pick "^4.4.0" + lodash.sample "^4.2.1" + lodash.shuffle "^4.2.0" + lodash.values "^4.3.0" + redis-commands "^1.2.0" + redis-parser "^2.4.0" + ipaddr.js@1.5.2: version "1.5.2" resolved "https://registry.yarnpkg.com/ipaddr.js/-/ipaddr.js-1.5.2.tgz#d4b505bde9946987ccf0fc58d9010ff9607e3fa0" @@ -266,6 +310,70 @@ is-symbol@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/is-symbol/-/is-symbol-1.0.1.tgz#3cc59f00025194b6ab2e38dbae6689256b660572" +lodash.assign@^4.2.0: + version "4.2.0" + resolved "https://registry.yarnpkg.com/lodash.assign/-/lodash.assign-4.2.0.tgz#0d99f3ccd7a6d261d19bdaeb9245005d285808e7" + +lodash.bind@^4.2.1: + version "4.2.1" + resolved "https://registry.yarnpkg.com/lodash.bind/-/lodash.bind-4.2.1.tgz#7ae3017e939622ac31b7d7d7dcb1b34db1690d35" + +lodash.clone@^4.5.0: + version "4.5.0" + resolved "https://registry.yarnpkg.com/lodash.clone/-/lodash.clone-4.5.0.tgz#195870450f5a13192478df4bc3d23d2dea1907b6" + +lodash.clonedeep@^4.5.0: + version "4.5.0" + resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef" + +lodash.defaults@^4.2.0: + version "4.2.0" + resolved "https://registry.yarnpkg.com/lodash.defaults/-/lodash.defaults-4.2.0.tgz#d09178716ffea4dde9e5fb7b37f6f0802274580c" + +lodash.difference@^4.5.0: + version "4.5.0" + resolved "https://registry.yarnpkg.com/lodash.difference/-/lodash.difference-4.5.0.tgz#9ccb4e505d486b91651345772885a2df27fd017c" + +lodash.flatten@^4.4.0: + version "4.4.0" + resolved "https://registry.yarnpkg.com/lodash.flatten/-/lodash.flatten-4.4.0.tgz#f31c22225a9632d2bbf8e4addbef240aa765a61f" + +lodash.foreach@^4.5.0: + version "4.5.0" + resolved "https://registry.yarnpkg.com/lodash.foreach/-/lodash.foreach-4.5.0.tgz#1a6a35eace401280c7f06dddec35165ab27e3e53" + +lodash.isempty@^4.4.0: + version "4.4.0" + resolved "https://registry.yarnpkg.com/lodash.isempty/-/lodash.isempty-4.4.0.tgz#6f86cbedd8be4ec987be9aaf33c9684db1b31e7e" + +lodash.keys@^4.2.0: + version "4.2.0" + resolved "https://registry.yarnpkg.com/lodash.keys/-/lodash.keys-4.2.0.tgz#a08602ac12e4fb83f91fc1fb7a360a4d9ba35205" + +lodash.noop@^3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/lodash.noop/-/lodash.noop-3.0.1.tgz#38188f4d650a3a474258439b96ec45b32617133c" + +lodash.partial@^4.2.1: + version "4.2.1" + resolved "https://registry.yarnpkg.com/lodash.partial/-/lodash.partial-4.2.1.tgz#49f3d8cfdaa3bff8b3a91d127e923245418961d4" + +lodash.pick@^4.4.0: + version "4.4.0" + resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3" + +lodash.sample@^4.2.1: + version "4.2.1" + resolved "https://registry.yarnpkg.com/lodash.sample/-/lodash.sample-4.2.1.tgz#5e4291b0c753fa1abeb0aab8fb29df1b66f07f6d" + +lodash.shuffle@^4.2.0: + version "4.2.0" + resolved "https://registry.yarnpkg.com/lodash.shuffle/-/lodash.shuffle-4.2.0.tgz#145b5053cf875f6f5c2a33f48b6e9948c6ec7b4b" + +lodash.values@^4.3.0: + version "4.3.0" + resolved "https://registry.yarnpkg.com/lodash.values/-/lodash.values-4.3.0.tgz#a3a6c2b0ebecc5c2cba1c17e6e620fe81b53d347" + media-typer@0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748" @@ -370,6 +478,14 @@ raw-body@2.3.2: iconv-lite "0.4.19" unpipe "1.0.0" +redis-commands@^1.2.0: + version "1.3.1" + resolved "https://registry.yarnpkg.com/redis-commands/-/redis-commands-1.3.1.tgz#81d826f45fa9c8b2011f4cd7a0fe597d241d442b" + +redis-parser@^2.4.0: + version "2.6.0" + resolved "https://registry.yarnpkg.com/redis-parser/-/redis-parser-2.6.0.tgz#52ed09dacac108f1a631c07e9b69941e7a19504b" + resolve@~1.4.0: version "1.4.0" resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.4.0.tgz#a75be01c53da25d934a98ebd0e4c4a7312f92a86"