├── Dockerfile ├── README.md ├── k8s ├── cluster-hello-nodejs.yaml ├── eks-admin-service-account.yaml ├── hello-nodejs-service.yaml └── hello-nodejs.yaml ├── package.json └── server.js /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:8 2 | 3 | # Create app directory 4 | WORKDIR /usr/src/app 5 | 6 | COPY package*.json ./ 7 | 8 | RUN npm install -S express 9 | 10 | COPY . . 11 | 12 | EXPOSE 8080 13 | CMD [ "npm", "start" ] 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hello-nodejs-kubernetes 2 | It is a sample of nodejs with kubernetes. 3 | I will show how to make a kubenetes infra struction and start the first application based on node.js. 4 | Also, it also contains how to scale up or down and show the status in Dashboard. 5 | 6 | ## build hello-nodejs 7 | $ docker build -t hello-nodejs:v1 . 8 | 9 | ## check the built image 10 | $ docker images 11 | 12 | ## check the operation 13 | $ curl -i localhost:8080 14 | 15 | ## tagging 16 | $ docker tag hello-nodejs:v1 994942771862.dkr.ecr.eu-west-2.amazonaws.com/repository-hello-nodejs 17 | 18 | ## image push 19 | $ docker push 994942771862.dkr.ecr.eu-west-2.amazonaws.com/repository-ksdyb 20 | 21 | ## deplay and create service for hello-nodejs 22 | $ kubectl create -f k8s/hello-nodejs.yaml 23 | 24 | ## check the external ip or domain 25 | $ kubectl get service -o wide 26 | NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR 27 | hello-nodejs LoadBalancer 10.100.71.97 a74430335892d11e9bc290ab61396c5b-1565267968.eu-west-1.elb.amazonaws.com 8080:32604/TCP 61s run=hello-nodejs 28 | kubernetes ClusterIP 10.100.0.1 443/TCP 27h 29 | 30 | ## check the operation 31 | $ curl -i http://a74430335892d11e9bc290ab61396c5b-1565267968.eu-west-1.elb.amazonaws.com:8080 32 | 33 | ## If scale-up is required, use this command 34 | $ kubectl get pods 35 | NAME READY STATUS RESTARTS AGE 36 | hello-nodejs-66f54786bb-wmxbw 1/1 Running 0 10m 37 | 38 | $ kubectl scale deployment hello-nodejs --replicas=3 39 | deployment.extensions/hello-nodejs scaled 40 | 41 | $ kubectl get nodes 42 | NAME STATUS ROLES AGE VERSION 43 | ip-172-31-20-131.eu-west-1.compute.internal Ready 6h54m v1.12.7 44 | ip-172-31-20-235.eu-west-1.compute.internal Ready 6h54m v1.12.7 45 | ip-172-31-30-134.eu-west-1.compute.internal Ready 6h54m v1.12.7 46 | ip-172-31-30-142.eu-west-1.compute.internal Ready 6h54m v1.12.7 47 | 48 | ## install the packages for dashboard 49 | $ kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml 50 | 51 | $ kubectl apply -f https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/influxdb/heapster.yaml 52 | 53 | $ kubectl apply -f https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/influxdb/influxdb.yaml 54 | 55 | $ kubectl apply -f https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/rbac/heapster-rbac.yaml 56 | 57 | $ kubectl apply -f k8s/eks-admin-service-account.yaml 58 | 59 | ## check the token 60 | $ kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep eks-admin | awk '{print $1}') 61 | 62 | ## start kube proxy 63 | $ kubectl proxy 64 | 65 | ## open dashboard in a browser 66 | http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/#!/login 67 | -------------------------------------------------------------------------------- /k8s/cluster-hello-nodejs.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: eksctl.io/v1alpha5 2 | kind: ClusterConfig 3 | 4 | metadata: 5 | name: hello-nodejs 6 | region: eu-west-2 7 | 8 | nodeGroups: 9 | - name: ng-hello-nodejs 10 | instanceType: t2.medium 11 | desiredCapacity: 2 12 | minSize: 1 13 | maxSize: 8 14 | ssh: 15 | allow: true # will use ~/.ssh/id_rsa.pub as the default ssh key 16 | -------------------------------------------------------------------------------- /k8s/eks-admin-service-account.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: ServiceAccount 3 | metadata: 4 | name: eks-admin 5 | namespace: kube-system 6 | --- 7 | apiVersion: rbac.authorization.k8s.io/v1beta1 8 | kind: ClusterRoleBinding 9 | metadata: 10 | name: eks-admin 11 | roleRef: 12 | apiGroup: rbac.authorization.k8s.io 13 | kind: ClusterRole 14 | name: cluster-admin 15 | subjects: 16 | - kind: ServiceAccount 17 | name: eks-admin 18 | namespace: kube-system 19 | -------------------------------------------------------------------------------- /k8s/hello-nodejs-service.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | name: hello-nodejs 5 | labels: 6 | app: hello 7 | spec: 8 | type: LoadBalancer 9 | ports: 10 | - port: 80 11 | protocol: TCP 12 | targetPort: 8080 13 | selector: 14 | app: hello 15 | 16 | -------------------------------------------------------------------------------- /k8s/hello-nodejs.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1beta2 # for versions before 1.9.0 use apps/v1beta2 2 | kind: Deployment 3 | metadata: 4 | name: hello-nodejs 5 | spec: 6 | selector: 7 | matchLabels: 8 | app: hello 9 | strategy: 10 | type: Recreate 11 | template: 12 | metadata: 13 | labels: 14 | app: hello 15 | spec: 16 | containers: 17 | - image: 99942771862.dkr.ecr.eu-west-2.amazonaws.com/repository-ksdyb:latest 18 | imagePullPolicy: Always 19 | name: hello-nodejs 20 | ports: 21 | - containerPort: 8080 22 | name: hello-nodejs 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hello-nodejs-kubernetes", 3 | "version": "1.0.0", 4 | "description": "It is a sample of nodejs with kubernetes. I will show how to make a kubenetes infra struction and start the first application based on node.js. Also, it also contains how to scale up or down and show the status in Dashboard.", 5 | "main": "server.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "node server.js" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/kyopark2014/hello-nodejs-kubernetes.git" 13 | }, 14 | "author": "", 15 | "license": "ISC", 16 | "bugs": { 17 | "url": "https://github.com/kyopark2014/hello-nodejs-kubernetes/issues" 18 | }, 19 | "homepage": "https://github.com/kyopark2014/hello-nodejs-kubernetes#readme" 20 | } 21 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const server = express(); 3 | 4 | server.get('/', (req,res) => { 5 | res.send('Hello World!'); 6 | }); 7 | 8 | server.listen(8080, () => { 9 | console.log('This server is running on port 8080\n'); 10 | }); 11 | --------------------------------------------------------------------------------