GithubHelp home page GithubHelp logo

rogervaas / example-kubernetes-nodejs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from risingstack/example-kubernetes-nodejs

0.0 2.0 0.0 25 KB

Introduction to Kubernetes with Node.js

JavaScript 100.00%

example-kubernetes-nodejs's Introduction

example-kubernetes-nodejs

Introduction to Kubernetes (k8s) with Node.js.

Requirements

  • Kubernetes (or minikube)
  • kubectl
  • Docker

Helpers

  • Re-using local Docker daemon with minikube: eval $(minikube docker-env) (run it once before Docker build)
  • On OSX: To base64: pbpaste | base64 | pbcopy and From base64: pbcopy | base64 --decode
  • minikube start and minikube stop

Tasks

1. Build Docker image

Re-using local Docker daemon with minikube:

eval $(minikube docker-env)

Building Docker image:

cd src/gateway
docker build -t my-co/gateway:v1 .

2. Create Secret, Deployment and Service in Kubernetes

kubectl create -f k8s
kubectl get deployment
kubectl get deployment <deployment-name> -o yaml
kubectl edit deployment <deployment-name>
kubectl get secret
kubectl get secret <secret-name> -o yaml
kubectl get service
kubectl get service <service-name> -o yaml

3. Get running pods

kubectl get pod
kubectl describe pod <pod-name>

Edit replica number in gateway Deployment and get pods again.
WARNING: Always edit yaml files in Kubernetes via kubectl, it's not synchronized with your local k8s folder.

4. Kill one pod

kubectl get pod
kubectl delete pod <pod-name>
kubectl get pod

5. Test networking

Go to inside a running pod:

kubectl get pod
kubectl exec <pod-name> -it -- sh
nslookup kubernetes gateway
curl <host>:3001
curl <ip>:3001
env | grep GATEWAY

From minikibe:

minikube service gateway

6. Set Horizontal Pod Autoscaler (HPA)

Do you have a running Heapster (collects metrics for autoscaler)?

kubectl get pod -n kube-system

If not, enable it:

$ minikube addons list
- addon-manager: enabled
- dashboard: enabled
- kube-dns: enabled
- heapster: disabled
- registry-creds: disabled

# minikube must be running for these commands to take effect
$ minikube addons enable heapster
heapster was successfully enabled

# this may take some time
$ kubectl get pod -n kube-system
NAME                          READY     STATUS              RESTARTS   AGE
heapster-2b7mt                1/1       Running             0          4m
kubectl autoscale deployment gateway --cpu-percent=50 --min=1 --max=10
kubectl get hpa
kubectl get hpa gateway -o yaml
ab -n 10000 -c 100 <IP>

HPA runs every 2 minutes by default.

7. Templating

Checkout Helm and anchor.

Install helm, then:

$ helm init
Creating /Users/foo/.helm
Creating /Users/foo/.helm/repository
Creating /Users/foo/.helm/repository/cache
Creating /Users/foo/.helm/repository/local
Creating /Users/foo/.helm/plugins
Creating /Users/foo/.helm/starters
Creating /Users/foo/.helm/cache/archive
Creating /Users/foo/.helm/repository/repositories.yaml
$HELM_HOME has been configured at /Users/foo/.helm.

# this will take a while
$ kubectl get pod -n kube-system
NAME                             READY     STATUS    RESTARTS   AGE
tiller-deploy-1936853538-3brdf   1/1       Running   0          7m

Backup your infrastructure:

node chart.js

Your templates will be saved in gateway/templates/

helm package gateway

On a fresh cluster run:

helm install gateway

and your infrastructure should be restored.

example-kubernetes-nodejs's People

Contributors

shadowbeetle avatar tamashodi avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.