Serverless
SERVERLESS
serverless deployment + notes for microservices/monolith apps
- workflow for deploying kubernetes for dev and production
why?
- deploy a definition as an API!!!!!!
- the world is moving toward smaller and smaller units of computing
the recent explosion in popularity of container technology (Docker in particular) and in orchestration technology (such as Kubernetes, Mesos, Consul and so on) this pattern has become much more viable to implement from a technical standpoint.
things to know
kubectlis like git cli, you run kubernetes commands through it docshelm(andtiller) is like git’s aliases (and more), you can chainkubectlcommandsminikubeis a local dev server equivalent in kubernetes GKE is Google Kubernetes Engine, for production, run throughgcloudclikubelessis a serverless framework for kubernetes
overview (FaaS) (Functions as a Service)
serverless computing == containerized computing == smaller units == faster, space saving To get started with serverless computing, you only need three ingredients:
- A Kubernetes cluster (place to host your serverless server LOL) (a container, because it is kubernetes)
- Kubeless (used to deploy your code to the cluster)
- Some code
2.1 have cluster (with options)
- locally (test and dev)
- minikube alt ref alt tut
- needs a VM (e.g. Virtual Box)
-
minikube startif error, might be trouble with your VM- needs kubectl (here)
- do not double click and interact with the VM through VirtualBox GUI, as once you close the Virtual window, you stop the server.
-
kubectl cluster-infoto see where your server iskubectl get nodesto see all your containers in the cluster (running apps on your virtual OS)kubectl describe nodeto see details
-
install something and run it
- (app/microservices) install something with Helm
- needs Helm and Tiller (like gulp for kubernetes, helm->client, tiller->server)
- install with
brew install kubernetes-helm
- install with
- deploy your app onto your kubernetes cluster (example with MongoDB)
helm install stable/mongodbto install locally with no connection to the outside worldhelm install stable/mongodb --set serviceType=NodePort- once it is installed, a VERY IMPORTANT set of instructions will be returned. (refer to code block below)
kubectl proxyto start hosting to the outside world- open
localhost:8001and see endpoints - you can now use your MongoDB on Kubernetes!!
- (extra) what is happening on my kubernetes cluster!?
minikube dashboardto see dashboard docs
- needs Helm and Tiller (like gulp for kubernetes, helm->client, tiller->server)
- (serverless) install kubeless
-
needs kubeless (AWS Lambda-like plugin for kubernetes)
- install with download or cli
- shitty bits (bash commands)
- define bash var
RELEASEexport RELEASE=$(curl -s https://api.github.com/repos/kubeless/kubeless/releases/latest | grep tag_name | cut -d '"' -f 4)
- create namespace on your kubernetes cluster for kubeless to run things on
kubectl create ns kubeless
- install kubeless on your kubernetes (pick the right install)
kubectl create -f https://github.com/kubeless/kubeless/releases/download/$RELEASE/+ yaml_option- options:
kubeless-$RELEASE.yamlis used for RBAC Kubernetes cluster. (minikube - if you get stuff withkubectl clusterrolesthen it’s RBAC)kubeless-non-rbac-$RELEASE.yamlis used for non-RBAC Kubernetes cluster.kubeless-openshift-$RELEASE.yamlis used to deploy Kubeless to OpenShift (1.5+).
- TLDR
export RELEASE=$(curl -s https://api.github.com/repos/kubeless/kubeless/releases/latest | grep tag_name | cut -d '"' -f 4)kubectl create ns kubelesskubectl create -f https://github.com/kubeless/kubeless/releases/download/$RELEASE/kubeless-$RELEASE.yaml// for kubernetes cluster on GKE
- check to see if it works
kubectl get pods -n kubelesskubectl get deployment -n kubelesskubectl get customresourcedefinition
- define bash var
-
deploy a kubeless
functionon your kubernetes cluster (how)- write that function
# your function def hello(event, context): print event return event['data'] - deploy with kubeless cli
kubeless function deploy function-name --runtime python2.7 --from-file test.py --handler test.hello- erm function name cannot use underscores
- check
- from cli
kubeless function call function-name --data 'hello world finally!!'
- from cli
- write that function
-
use
kubeless uithe equivalent of postman for kubeless- from cli
- install with
kubectl create -f https://raw.githubusercontent.com/kubeless/kubeless-ui/master/k8s.yaml - to be continued
- might not work when you set
- install with
- from github repo
- clone github repo
- follow instructions and
yarn run devto start
- from cli
-
link the
function(essentially an app now) to a http endpoint (one of the ways) docs- needs kubernetes ingress enabled docs
- ‘install/enable’ with
minikube addons enable ingress- enables default ingress (Nginx Ingress controller) - check
kubectl get pod -n kube-system -l app=nginx-ingress-controller(might need to wait a while)
- ‘install/enable’ with
kubectl get podsto see if your function is up and runningkubectl get svcto get all services in your kubernetes clusterkubeless trigger http listto check for existing http triggers- create a trigger (link to the function)
kubeless trigger http create test-endpoint --function-name function-namewheretest-endpointis the endpoint name andfunction-nameis the kubeless function that is deployed
- check
kubectl get ingshows you where it is hosted- for this example it is hosted on
function-name.192.168.99.100.nip.io
- for this example it is hosted on
- access endpoint
- curl:
curl --data 'hello world super freaking finally!' --header "Host: test-endpoint.192.168.99.100.nip.io" --header "Content-Type:text/html" 192.168.99.100/echo(didn’t work for me) TODO to be continued later curl —data ‘hello world super freaking finally!’ —header “Host: test-endpoint.192.168.99.100.nip.io” —header “Content-Type:text/html http://192.168.99.100:31106/ - kubeless ui
kubectl proxyto start opening a localhost connection between the minikube cluster and the pc- run the
kubeless uiapp (i only used the github clone ->yarn install->yarn dev)
- curl:
- needs kubernetes ingress enabled docs
-
connect to the world with
kubectl proxydocs
-
- (app/microservices) install something with Helm
-
kubectl proxyto start hosting to the outside world -
open
localhost:8001and see endpoints -
minikube dashboardto see dashboard docs
MBP17079:~ li_qun_tang$ helm install stable/mongodb
NAME: sullen-mole
LAST DEPLOYED: Wed May 16 20:22:32 2018
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/Secret
NAME TYPE DATA AGE
sullen-mole-mongodb Opaque 1 0s
==> v1/PersistentVolumeClaim
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
sullen-mole-mongodb Bound pvc-6d259685-58fb-11e8-a566-080027cecb76 8Gi RWO standard 0s
==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
sullen-mole-mongodb ClusterIP 10.101.7.183 <none> 27017/TCP 0s
==> v1beta1/Deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
sullen-mole-mongodb 1 1 1 0 0s
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
sullen-mole-mongodb-74f8b689f9-5k2pt 0/1 ContainerCreating 0 0s
NOTES:
** Please be patient while the chart is being deployed **
MongoDB can be accessed via port 27017 on the following DNS name from within your cluster:
sullen-mole-mongodb.default.svc.cluster.local
To get the root password run:
export MONGODB_ROOT_PASSWORD=$(kubectl get secret --namespace default sullen-mole-mongodb -o jsonpath="{.data.mongodb-root-password}" | base64 --decode)
To connect to your database run the following command:
kubectl run sullen-mole-mongodb-client --rm --tty -i --image bitnami/mongodb --command -- mongo --host sullen-mole-mongodb -p $MONGODB_ROOT_PASSWORD
To connect to your database from outside the cluster execute the following commands:
export POD_NAME=$(kubectl get pods --namespace default -l "app=mongodb" -o jsonpath="{.items[0].metadata.name}")
kubectl port-forward --namespace default $POD_NAME 27017:27017 &
mongo --host 127.0.0.1 -p $MONGODB_ROOT_PASSWORD
- on google kubernetes engine (GKE) (production)
- on packet cloud (production)