HelloWorld

micro-notes

OpenFaaS

OpenFaaS in Docker Swarm

git clone https://github.com/openfaas/faas
docker swarm init --advertise-addr wlp2s0
./deploy_stack.sh --no-auth

Build OpenFaaS

# get license-check into gateway dir
wget https://raw.githubusercontent.com/teamserverless/license-check/master/get.sh
sh get.sh
mv license-check ./gateway/

# open gateway/Dockerfile and replace line of license-check 
# COPY license-check /usr/bin/

# compile gateway & watchdog
./build.sh

Deploy OpenFaaS

faas-cli deploy -f \ 
    https://raw.githubusercontent.com/openfaas/faas/master/stack.yml
faas-cli remove -f \
    https://raw.githubusercontent.com/openfaas/faas/master/stack.yml

Examples

faas list
echo edwardspc|faas invoke hubstats
echo "Hello World" |faas invoke wordcount
echo |faas invoke nodeinfo
echo "Hello World "|faas invoke markdown
echo Hello |faas invoke base64
echo Hello |faas invoke echoit

Build Example

faas template store ls
faas new --lang python3 hello-openfaas --prefix=edwardspc
vim ./hello-openfaas/handler.py
# return "Hello World"

faas build -f hello-openfaas.yml
faas deploy -f hello-openfaas.yml
faas ls
echo | faas-cli invoke hello-openfaas

Inception Example

docker pull alexellis/inception:2019-02-17
faas deploy --image alexellis/inception:2019-02-17 \
    --name inception \
    --fprocess "python3 index.py"
export URL=https://upload.wikimedia.org/wikipedia/commons/6/61/Humpback_Whale_underwater_shot.jpg
echo -n $URL | faas invoke inception |  jq '.[] '
echo -n $URL | faas invoke inception |  jq '.[] | select(.score > 0.05)'

Hello OpenFaas

# Create a python3 template
# Check Build Example
$ cat Dockerfile 
FROM ubuntu:16.04

RUN apt-get update \
  && apt-get install -y python3-pip python3-dev \
  && apt-get install -y vim net-tools iproute iputils-ping tree dnsutils traceroute curl wget \
  && cd /usr/local/bin \
  && ln -s /usr/bin/python3 python \
  && pip3 install --upgrade pip
$ cat Dockerfile 
FROM edwardspc/ubuntu16.04-python3:1.0

# Allows you to add additional packages via build-arg
ARG ADDITIONAL_PACKAGE

# Alternatively use ADD https:// (which will not be cached by Docker builder)
RUN apt-get install -y ${ADDITIONAL_PACKAGE} \
    && echo "Pulling watchdog binary from Github." \
    && curl -sSL https://github.com/openfaas/faas/releases/download/0.13.0/fwatchdog > /usr/bin/fwatchdog \
    && chmod +x /usr/bin/fwatchdog 

# Add non root user
RUN useradd -ms /bin/bash app

WORKDIR /home/app/

COPY index.py           .
COPY requirements.txt   .

RUN chown -R app /home/app

USER app
ENV PATH=$PATH:/home/app/.local/bin
RUN pip install --user -r requirements.txt

RUN mkdir -p function
RUN touch ./function/__init__.py

WORKDIR /home/app/function/
COPY function/requirements.txt	.
RUN pip install --user -r requirements.txt

WORKDIR /home/app/

USER root
COPY function           function
RUN chown -R app:app ./
USER app

ENV fprocess="python3 index.py"
EXPOSE 8080

HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1

CMD ["fwatchdog"]

GUI

OpenFaaS Portal

Prometheus

Utilities

# docker default images
grep image docker-compose.yml | awk '{print $2}'|xargs -n1 docker pull

# docker images
docker pull alpine:3.8
docker pull golang:1.10
docker pull golang:1.10.4
docker pull openfaas/faas-swarm:0.6.1

# upload docker images
for i in *.tar; do docker load -i $i; done

# upload docker images
docker push <user-login>/watchdog:build
docker push <user-login>/gateway:latest-dev

# update docker-compose.yml
sed 's/openfaas\/gateway:0.12.0/edwardspc\/gateway:latest-dev/g' \
    docker-compose.yml > docker-compose-updated.yml

Delete OpenFaas

docker swarm leave --force