Introduction to Kubernetes
(k8s) with Node.js
.
- Kubernetes (or minikube)
- kubectl
- Docker
- 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
andminikube stop
Re-using local Docker daemon with minikube:
eval $(minikube docker-env)
Building Docker image:
cd src/gateway
docker build -t my-co/gateway:v1 .
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
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.
kubectl get pod
kubectl delete pod <pod-name>
kubectl get pod
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
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.
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.