×
Community Blog How to Install Fn Project on ECS Ubuntu 18.04

How to Install Fn Project on ECS Ubuntu 18.04

In this article, we will show you how to install and set up a serverless Fn project on an Alibaba Cloud ECS instance installed with Ubuntu 18.04.

By Sai Sarath Chandra, Alibaba Cloud Community Blog author

Fn project is an event-driven, open-source serverless computing platform that you can use and run anywhere. With Fn, you can deploy your functions to an Fn server, which automatically executes and manages these functions. Each function is run in a Docker container enabling the platform to provide a broad range of support for development languages such as Java, JavaScript (Node), Go, and Python. You can run the server locally on your computer, or on a server at your on-premises data center or in the cloud. The Fn project has a strong enterprise focus with an extended emphasis on security, scalability, and observability.

There are several components to an Fn Project.

  • Fn Server: This is a Function as a Service (FaaS) system that helps to developers to create, build and deploy functions. It is fast, reliable, and is container-native.
  • The Fn Load Balancer: This is very important for the serverless functions to scale and route traffic. Fn Load balancer is responsible for scaling in or out a particular function and ensuring the optimal performance
  • Fn FDK: FDK stands for Function Development Kit. It can used to help with developing functions very quickly in all languages and testing them.
  • Fn Flow: Flow simplifies the orchestration of the higher level workflows of functions by helping in handling complicated models and building with long JSON and YAML templates. Flow also helps in visualization of all the function calls in the dashboard, stack logs, and to name a few.

Setting up Your Server

We need to create ECS instances, there are several basic steps which I will go over below:

The Steps involved in creating instances:

  • Basic Configurations:

1.Billing method

Make sure you choose the Pay-As-You-Go billing method if you are prototyping the application

2.Image

You need to select Ubuntu 18.04 (64-bit)

1

  • Networking

1.Choose what works best for your situation. Note that you can always open the ports at a later time.

  • System Configurations

1.I chose password-based authentication as an example in this tutorial. You can choose this or another authentication method based on your requirements.

  • Grouping
  • Preview

1.The complete information for your selected configuration. You can also save this configuration as a template for the future to simplify the configuration process.

2.If you want to release the instance at the later point of time select Auto-Release schedule.

Now, let's connect to the instance using SSH, as shown below.

ssh root@<IP ADDRESS>
The authenticity of host 'AAA.CCC.BBB.DDD' can't be established.
ECDSA key fingerprint is SHA256:/il4fRYvgAake/ENoBgJSZvdO8DxY7H99oeIuye8AEo.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ' AAA.CCC.BBB.DDD ' (ECDSA) to the list of known hosts.

root@<IP ADDRESS>'s password: 
Welcome to Ubuntu 18.04 LTS (GNU/Linux 4.15.0-23-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

Welcome to Alibaba Cloud Elastic Compute Service !

From the above code script, you can see the latest installed ubuntu version along with the welcome message, we can update the package entries by running the sudo apt-get update command, and upgrade any obsolete packages. We can know what packages are to be updated and removed by running the sudo apt-get upgrade command. After which, you'll see similar message as below, where it asks for your consent to upgrade:

The following packages were automatically installed and are no longer required:
  libopts25 sntp
Use 'sudo apt autoremove' to remove them.
The following packages have been kept back:
  linux-generic linux-headers-generic linux-image-generic
The following packages will be upgraded:
  amd64-microcode apparmor apt apt-utils base-files bind9-host binutils binutils-common binutils-x86-64-linux-gnu
  bsdutils chrony console-setup console-setup-linux cpp cpp-7 cups-bsd cups-client cups-common curl
  distro-info-data dnsutils dpkg dpkg-dev fdisk friendly-recovery g++ g++-7 gcc gcc-7 gcc-7-base gcc-8-base
  grub-common grub-pc grub-pc-bin grub2-common guile-2.0-libs initramfs-tools initramfs-tools-bin
  initramfs-tools-core intel-microcode iperf keyboard-configuration kmod language-pack-en language-pack-en-base
  language-pack-gnome-en language-pack-gnome-en-base libapparmor1 libapt-inst2.0 libapt-pkg5.0
  libarchive-zip-perl libarchive13 libasan4 libatomic1 libavahi-client3 libavahi-common-data libavahi-common3
  libbind9-160 libbinutils libblkid1 libcc1-0 libcilkrts5 libcryptsetup12 libcups2 libcupsimage2 libcurl4
  libdns-export1100 libdns1100 libdpkg-perl libfdisk1 libgcc-7-dev libgcc1 libglib2.0-0 libglib2.0-data libgomp1
  libirs160 libisc-export169 libisc169 libisccc160 libisccfg160 libitm1 libjpeg-turbo8 libkmod2 liblsan0
  liblwres160 libmount1 libmpx2 libmysqlclient20 libnss-systemd libpam-systemd libparted2 libperl5.26
  libplymouth4 libpng16-16 libpolkit-gobject-1-0 libpython3-stdlib libpython3.6 libpython3.6-minimal
  libpython3.6-stdlib libquadmath0 librados2 librbd1 libsmartcols1 libstdc++-7-dev libstdc++6 libsystemd0
  libtsan0 libubsan0 libudev1 libuuid1 libx11-6 libx11-data libxml2 linux-libc-dev lshw man-db mount netplan.io
  networkd-dispatcher nplan openssh-client openssh-server openssh-sftp-server parted perl perl-base
  perl-modules-5.26 plymouth plymouth-theme-ubuntu-text python-apt-common python3 python3-apt python3-distupgrade
  python3-minimal python3-requests python3-update-manager python3.6 python3.6-minimal sntp ssh systemd
  systemd-sysv tzdata ubuntu-release-upgrader-core udev update-manager-core util-linux uuid-runtime
148 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.
Need to get 79.6 MB of archives.
After this operation, 312 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y

Input Y, and if you want to remove the unused packages, run the sudo apt autoremove command. This command will also give the similar prompt asking for the same things as what we saw before. The upgrade will take a while, there might be some warning you might see while installing like the message below:

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
  LANGUAGE = (unset),
  LC_ALL = (unset),
  LC_CTYPE = "UTF-8",
  LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").

The fix is check for locale, of the target system, by running locale, you will see the below output:

locale
LANGUAGE=
LC_CTYPE=C.UTF-8
LC_NUMERIC="C.UTF-8"
LC_TIME="C.UTF-8"
LC_COLLATE="C.UTF-8"
LC_MONETARY="C.UTF-8"
LC_MESSAGES="C.UTF-8"
LC_PAPER="C.UTF-8"
LC_NAME="C.UTF-8"
LC_ADDRESS="C.UTF-8"
LC_TELEPHONE="C.UTF-8"
LC_MEASUREMENT="C.UTF-8"
LC_IDENTIFICATION="C.UTF-8"
LC_ALL=

You need to set the LC_ALL and Language by running the following command:

export LANGUAGE="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"

Before we proceed any other we need to point the pip registry to the global site, this can be done by running the following command:

~root@iZa2d4spx9n9e90jofcyxoZ:~# sudo nano /root/.pip/pip.conf

Replace the output with the below text:

## Note, this file is written by cloud-init on first boot of an instance
## modifications made here will not survive a re-bundle.
###[m
[global]
## index-url=http://ap-south-1.mirrors.cloud.aliyuncs.com/pypi/simple/
index-url=https://pypi.python.org/simple/
[install]
## trusted-host=ap-south-1.mirrors.cloud.aliyuncs.com
trusted-host=pypi.python.org

Now install all the required tools and other packages using the following command:

sudo apt-get install git python-pip python-setuptools build-essential libssl-dev libffi-dev python-dev software-properties-common

Now we will install Docker. To do this, first, we will add the key and the repository:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
OK
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"

Then we need to update the repository by inputting this: ~root@iZa2d4spx9n9e90jofcyxoZ:~# sudo apt update. We will set and make sure that stable version from the docker repository, we can also verify that using the docker command: ~root@iZa2d4spx9n9e90jofcyxoZ:~# apt-cache policy docker-ce. The output will be like this:

docker-ce:
  Installed: (none)
  Candidate: 5:18.09.0~3-0~ubuntu-bionic
  Version table:
     5:18.09.0~3-0~ubuntu-bionic 500
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
     18.06.1~ce~3-0~ubuntu 500
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
     18.06.0~ce~3-0~ubuntu 500
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
     18.03.1~ce~3-0~ubuntu 500
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages

We will install the docker by running the sudo apt install docker-ce command, and then verify the installation by running the sudo systemctl status docker command. The output will show active (running). See the example below:

docker.service - Docker Application Container Engine[m
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat CST; 4s ago
     Docs: https://docs.docker.com[m
 Main PID: 25119 (dockerd)
    Tasks: 9
   CGroup: /system.slice/docker.service
           └─25119 /usr/bin/dockerd -H unix://

We need to make some changes to the configuration file, edit the config file using the sudo vi /lib/systemd/system/docker.service command, and then update the "ExecStart" with the line /usr/bin.dockerd-H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock. After that, we will reload the docker daemon and restart the docker service:

sudo systemctl daemon-reload
sudo systemctl restart docker.service

Once we see the status as running, we can check the other environment details by running the following command:

curl http://localhost:2375/version
{"Platform":{"Name":"Docker Engine - Community"},"Components":[{"Name":"Engine","Version":"18.09.0","Details":{"ApiVersion":"1.39","Arch":"amd64","BuildTime":"ABCD","Experimental":"false","GitCommit":"4d60db4","GoVersion":"go1.10.4","KernelVersion":"4.15.0-23-generic","MinAPIVersion":"1.12","Os":"linux"}}],"Version":"18.09.0","ApiVersion":"1.39","MinAPIVersion":"1.12","GitCommit":"4d60db4","GoVersion":"go1.10.4","Os":"linux","Arch":"amd64","KernelVersion":"4.15.0-23-generic","BuildTime":"ABCD"}

We need to install "screen" which helps to run multiple processes in Linux. To install screen run the apt install screen command, then login to docker hub by running the following command:

docker login

Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: <your username>
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Installing the Fn Server

The below command will install all the required dependencies, once done you will see the ASCII art of the Fn Installtion

curl -LSs https://raw.githubusercontent.com/fnproject/cli/master/install | sh
fn version 0.5.29

        ______
       / ____/___
      / /_  / __ \
     / __/ / / / /
    /_/   /_/ /_/`

We need to install the fn-cli for linux, that can be achieved by the curl -0 https://github.com/fnproject/cli/releases/download/0.5.29/fn_linux command, and then run the Fn Server:

fn start
Unable to find image 'fnproject/fnserver:latest' locally
latest: Pulling from fnproject/fnserver

After all the images are downloaded the containers will start the fn server, we will create a "Go" runtime with the below command:

fn init --runtime go hello
Creating function at: /hello
Function boilerplate generated.

Next, change to the created function directory with the cd hello/ command. After that, we can deploy the function locally by running the following command, the below command will build and deploy the application on the localhost:

fn deploy --app myapp –local

Deploying hello to app: myapp
Bumped to version 0.0.2
Building image hello:0.0.2 ...................................................................
Updating function hello using image hello:0.0.2...

Now we can invoke the function using the fn invoke myapp hello command. You will see the output like this: {"message":"Hello World"} . And after that the Fn-Project is installed on the Ubuntu machine with dockers and has deployed a test dunction. You can access this function at the following address: http://<public-ip>:8080/myapp/hello.

0 0 0
Share on

Alibaba Clouder

2,599 posts | 762 followers

You may also like

Comments