# π¬π§ OpenFaas is also a PaaS - Part 1
And it's amazing. But, what does it mean in reality? I wasn't aware of that (because I didn't read all the documentation (1), and I'm so sorry for that, Alex). But you can deploy your micro services or web applications on OpenFaaS like on any other (good) PaaS. If I developed a micro service, and I'm able to deploy it on Clever Cloud (2) or Heroku or any PaaS 12 factors compliant without any change (3).
- 1: OpenFaaS blog post reference: https://www.openfaas.com/blog/stateless-microservices/ (opens new window)
- 2: try it, you will love it https://www.clever-cloud.com/en/ (opens new window)
- 3: except deployment files configuration
But, let's see how to do that. The easiest way is to Dockerize your application.
# First: create an ExpressJS web application
The Part 2 (next blog post) will be about how to deploy Vert-x micro service from GitLab CI to OpenFaaS.
First, create a hello-world
directory with 2 files:
index.js
package.json
index.js
const express = require('express')
const app = express()
const port = process.env.PORT || 8080
app.use(express.json())
app.get('/', (req, res, next) => {
res.send("<h1>π Hello World π</h1>")
})
app.get('/spock', (req, res, next) => {
res.send({
message:"πLive long and prosper!"
})
})
app.listen(port, () => console.log(`π listening on port ${port}`))
Your web application must listen on the
8080
http port
package.json
{
"name": "hello-world",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node ./index.js"
},
"license": "MIT",
"dependencies": {
"express": "^4.16.4"
}
}
# Then, we will prepare our service for deployment on OpenFaaS
For that you need 2 files:
Dockerfile
we are going to "dockerize" the web applicationconfig.deploy.yml
the file needed by the OpenFaaS CLI to deploy the web application (and you can name it with another name if you want)
Dockerfile
FROM node:12.7.0-alpine
COPY . .
RUN npm install
CMD ["npm", "start"]
- Best practice: your service should write a lock file to
/tmp/.lock
. It allows to write health check for OpenFaaS. You can do that with theDockerfile
or in you source code int the http start-up.- for example:
RUN touch /tmp/.lock
- better explained here: https://docs.openfaas.com/reference/workloads/#stateless-microservices (opens new window)
config.deploy.yml
version: 1.0
provider:
name: openfaas
gateway: ${OPENFAAS_URL}
functions:
hello-world:
lang: dockerfile
handler: ./
image: ${DOCKER_HANDLE}/hello-world:latest
lang: dockerfile
we'll use de Docker template
# Now, it's time to deploy
It's pretty simple, here are the commands to deploy:
export OPENFAAS_URL="http://openfaas.test:8080" # this is my OpenFaas instance
export OPENFASS_TOKEN="put here the OpenFaaS token"
export DOCKER_PASSWORD="your Docker hub password"
export DOCKER_HANDLE="your Docker hub handle"
echo -n ${DOCKER_PASSWORD} | docker login --username ${DOCKER_HANDLE} --password-stdin
echo -n ${OPENFASS_TOKEN} |faas-cli login --username=admin --password-stdin
faas-cli build -f config.deploy.yml
faas-cli push -f config.deploy.yml
faas-cli deploy -f config.deploy.yml
# or you can do it with only one command:
# faas-cli up -f config.deploy.yml
And now you can access to:
- your "web page": http://openfaas.test:8080/function/hello-world (opens new window)
- your service: http://openfaas.test:8080/function/hello-world/spock (opens new window)
π Now you have your own PaaS. See you soon for the next part.
Last Articles
- π«π· Type Result en Kotlin | 2020-10-31 | Kotlin
- π«π· Type Result en Kotlin | 2020-10-31 | Kotlin
- π¬π§ Every GitLab Page deserves a real CI/CD | 2020-07-23 | GitLab CI
- π«π· Lit-Element, commencer doucement | 2020-07-20 | WebComponent
- π¬π§ Build quickly and host easily your Docker images with GitLab and GitLab CI | 2020-06-02 | GitLab CI
- π¬π§ Deploy quickly on Clever Cloud with GitLab CI | 2020-05-31 | GitLab CI
- π«π· Borg Collective, mes jouets pour apprendre Knative | 2020-05-30 | Knative
- π¬π§ Borg Collective, Toys to learn Knative | 2020-05-30 | Knative
- π«π· M5Stack, une petit device IOT bien sympathique, programmable en Python | 2020-05-09 | IOT
- π«π· Knative, l'outil qui rend Kubernetes sympathique | 2020-05-02 | kubernetes