mirror of
https://github.com/openfaas/faasd.git
synced 2025-06-19 04:26:34 +00:00
Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
abc41d2108 | |||
9ba64783f9 | |||
95e7f52a4f | |||
8da2af9d96 | |||
82d6fc024e |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
/faasd
|
/faasd
|
||||||
hosts
|
hosts
|
||||||
|
/resolv.conf
|
||||||
|
@ -12,6 +12,7 @@ deploy:
|
|||||||
- ./bin/faasd
|
- ./bin/faasd
|
||||||
- ./bin/faasd-armhf
|
- ./bin/faasd-armhf
|
||||||
- ./bin/faasd-arm64
|
- ./bin/faasd-arm64
|
||||||
|
skip_cleanup: true
|
||||||
on:
|
on:
|
||||||
tags: true
|
tags: true
|
||||||
|
|
||||||
|
4
Makefile
4
Makefile
@ -1,6 +1,6 @@
|
|||||||
Version := $(shell git describe --tags --dirty)
|
Version := $(shell git describe --tags --dirty)
|
||||||
GitCommit := $(shell git rev-parse HEAD)
|
GitCommit := $(shell git rev-parse HEAD)
|
||||||
LDFLAGS := "-s -w -X main.Version=$(Version) -X main.GitCommit=$(GitCommit)"
|
LDFLAGS := "-s -w -X pkg.Version=$(Version) -X pkg.GitCommit=$(GitCommit)"
|
||||||
|
|
||||||
.PHONY: all
|
.PHONY: all
|
||||||
all: local
|
all: local
|
||||||
@ -11,5 +11,5 @@ local:
|
|||||||
.PHONY: dist
|
.PHONY: dist
|
||||||
dist:
|
dist:
|
||||||
CGO_ENABLED=0 GOOS=linux go build -ldflags $(LDFLAGS) -a -installsuffix cgo -o bin/faasd
|
CGO_ENABLED=0 GOOS=linux go build -ldflags $(LDFLAGS) -a -installsuffix cgo -o bin/faasd
|
||||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=6 go build -ldflags $(LDFLAGS) -a -installsuffix cgo -o bin/faasd-armhf
|
CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=7 go build -ldflags $(LDFLAGS) -a -installsuffix cgo -o bin/faasd-armhf
|
||||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags $(LDFLAGS) -a -installsuffix cgo -o bin/faasd-arm64
|
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags $(LDFLAGS) -a -installsuffix cgo -o bin/faasd-arm64
|
||||||
|
37
README.md
37
README.md
@ -1,5 +1,7 @@
|
|||||||
# faasd - serverless with containerd
|
# faasd - serverless with containerd
|
||||||
|
|
||||||
|
[](https://travis-ci.com/alexellis/faasd)
|
||||||
|
|
||||||
faasd is a Golang supervisor that bundles OpenFaaS for use with containerd instead of a container orchestrator like Kubernetes or Docker Swarm.
|
faasd is a Golang supervisor that bundles OpenFaaS for use with containerd instead of a container orchestrator like Kubernetes or Docker Swarm.
|
||||||
|
|
||||||
## About faasd:
|
## About faasd:
|
||||||
@ -58,6 +60,29 @@ cd $GOPATH/src/github.com/alexellis/faasd
|
|||||||
go build && sudo ./faasd
|
go build && sudo ./faasd
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Or get from binaries:
|
||||||
|
|
||||||
|
|
||||||
|
### Build and run faas-containerd
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# For x86_64
|
||||||
|
sudo curl -fSLs "https://github.com/alexellis/faasd/releases/download/0.1.3/faasd" \
|
||||||
|
-o "/usr/local/bin/faasd" \
|
||||||
|
&& sudo chmod a+x "/usr/local/bin/faasd"
|
||||||
|
|
||||||
|
# armhf
|
||||||
|
sudo curl -fSLs "https://github.com/alexellis/faasd/releases/download/0.1.3/faasd-armhf" \
|
||||||
|
-o "/usr/local/bin/faasd" \
|
||||||
|
&& sudo chmod a+x "/usr/local/bin/faasd"
|
||||||
|
|
||||||
|
# arm64
|
||||||
|
sudo curl -fSLs "https://github.com/alexellis/faasd/releases/download/0.1.3/faasd-arm64" \
|
||||||
|
-o "/usr/local/bin/faasd" \
|
||||||
|
&& sudo chmod a+x "/usr/local/bin/faasd"
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Look in `hosts` in the current working folder to get the IP for the gateway or Prometheus
|
Look in `hosts` in the current working folder to get the IP for the gateway or Prometheus
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
@ -81,3 +106,15 @@ echo faas-containerd gateway prometheus |xargs sudo ctr container rm
|
|||||||
|
|
||||||
echo faas-containerd gateway prometheus |xargs sudo ctr snapshot rm
|
echo faas-containerd gateway prometheus |xargs sudo ctr snapshot rm
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Links
|
||||||
|
|
||||||
|
https://github.com/renatofq/ctrofb/blob/31968e4b4893f3603e9998f21933c4131523bb5d/cmd/network.go
|
||||||
|
|
||||||
|
https://github.com/renatofq/catraia/blob/c4f62c86bddbfadbead38cd2bfe6d920fba26dce/catraia-net/network.go
|
||||||
|
|
||||||
|
https://github.com/containernetworking/plugins
|
||||||
|
|
||||||
|
https://github.com/containerd/go-cni
|
||||||
|
|
||||||
|
|
||||||
|
36
cmd/up.go
36
cmd/up.go
@ -20,18 +20,13 @@ func runUp(_ *cobra.Command, _ []string) error {
|
|||||||
|
|
||||||
wd, _ := os.Getwd()
|
wd, _ := os.Getwd()
|
||||||
svcs := []pkg.Service{
|
svcs := []pkg.Service{
|
||||||
// pkg.Service{
|
pkg.Service{
|
||||||
// Name: "faas-containerd",
|
Name: "nats",
|
||||||
// Env: []string{"snapshotter=overlayfs"},
|
Env: []string{""},
|
||||||
// Image: "docker.io/alexellis2/faas-containerd:0.3.2",
|
Image: "docker.io/library/nats-streaming:0.11.2",
|
||||||
// Mounts: []pkg.Mount{
|
Caps: []string{},
|
||||||
// pkg.Mount{
|
Args: []string{"/nats-streaming-server", "-m", "8222", "--store=memory", "--cluster_id=faas-cluster"},
|
||||||
// Src: "/run/containerd/containerd.sock",
|
},
|
||||||
// Dest: "/run/containerd/containerd.sock",
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// Caps: []string{"CAP_SYS_ADMIN", "CAP_NET_RAW"},
|
|
||||||
// },
|
|
||||||
pkg.Service{
|
pkg.Service{
|
||||||
Name: "prometheus",
|
Name: "prometheus",
|
||||||
Env: []string{},
|
Env: []string{},
|
||||||
@ -53,11 +48,28 @@ func runUp(_ *cobra.Command, _ []string) error {
|
|||||||
"read_timeout=60s",
|
"read_timeout=60s",
|
||||||
"write_timeout=60s",
|
"write_timeout=60s",
|
||||||
"upstream_timeout=65s",
|
"upstream_timeout=65s",
|
||||||
|
"faas_nats_address=nats",
|
||||||
|
"faas_nats_port=4222",
|
||||||
},
|
},
|
||||||
Image: "docker.io/openfaas/gateway:0.17.4",
|
Image: "docker.io/openfaas/gateway:0.17.4",
|
||||||
Mounts: []pkg.Mount{},
|
Mounts: []pkg.Mount{},
|
||||||
Caps: []string{"CAP_NET_RAW"},
|
Caps: []string{"CAP_NET_RAW"},
|
||||||
},
|
},
|
||||||
|
pkg.Service{
|
||||||
|
Name: "queue-worker",
|
||||||
|
Env: []string{
|
||||||
|
"faas_nats_address=nats",
|
||||||
|
"faas_nats_port=4222",
|
||||||
|
"gateway_invoke=true",
|
||||||
|
"faas_gateway_address=gateway",
|
||||||
|
"ack_wait=5m5s",
|
||||||
|
"max_inflight=1",
|
||||||
|
"faas_print_body=true",
|
||||||
|
},
|
||||||
|
Image: "docker.io/openfaas/queue-worker:0.8.4",
|
||||||
|
Mounts: []pkg.Mount{},
|
||||||
|
Caps: []string{"CAP_NET_RAW"},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
"syscall"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/alexellis/faasd/pkg/weave"
|
"github.com/alexellis/faasd/pkg/weave"
|
||||||
@ -16,6 +16,7 @@ import (
|
|||||||
"github.com/containerd/containerd/cio"
|
"github.com/containerd/containerd/cio"
|
||||||
"github.com/containerd/containerd/containers"
|
"github.com/containerd/containerd/containers"
|
||||||
"github.com/containerd/containerd/errdefs"
|
"github.com/containerd/containerd/errdefs"
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
|
||||||
"github.com/containerd/containerd/namespaces"
|
"github.com/containerd/containerd/namespaces"
|
||||||
"github.com/containerd/containerd/oci"
|
"github.com/containerd/containerd/oci"
|
||||||
@ -60,7 +61,7 @@ func (s *Supervisor) Start(svcs []Service) error {
|
|||||||
images := map[string]containerd.Image{}
|
images := map[string]containerd.Image{}
|
||||||
|
|
||||||
for _, svc := range svcs {
|
for _, svc := range svcs {
|
||||||
fmt.Printf("Preparing: %s", svc.Name)
|
fmt.Printf("Preparing: %s\n", svc.Name)
|
||||||
|
|
||||||
img, err := prepareImage(ctx, s.client, svc.Image)
|
img, err := prepareImage(ctx, s.client, svc.Image)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -94,20 +95,13 @@ func (s *Supervisor) Start(svcs []Service) error {
|
|||||||
status, _ := t.Status(ctx)
|
status, _ := t.Status(ctx)
|
||||||
fmt.Println("Status:", status.Status)
|
fmt.Println("Status:", status.Status)
|
||||||
|
|
||||||
if status.Status == containerd.Running {
|
// if status.Status == containerd.Running {
|
||||||
log.Println("need to kill", svc.Name)
|
log.Println("need to kill", svc.Name)
|
||||||
|
err := killTask(ctx, t)
|
||||||
err = t.Kill(ctx, syscall.SIGTERM)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error killing task %s, %s, %s", container.ID(), svc.Name, err)
|
return fmt.Errorf("error killing task %s, %s, %s", container.ID(), svc.Name, err)
|
||||||
}
|
}
|
||||||
time.Sleep(5 * time.Second)
|
// }
|
||||||
}
|
|
||||||
_, err = t.Delete(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error deleting task %s, %s, %s", container.ID(), svc.Name, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = container.Delete(ctx, containerd.WithSnapshotCleanup)
|
err = container.Delete(ctx, containerd.WithSnapshotCleanup)
|
||||||
@ -173,6 +167,7 @@ func (s *Supervisor) Start(svcs []Service) error {
|
|||||||
containerd.WithNewSpec(oci.WithImageConfig(image),
|
containerd.WithNewSpec(oci.WithImageConfig(image),
|
||||||
oci.WithCapabilities(svc.Caps),
|
oci.WithCapabilities(svc.Caps),
|
||||||
oci.WithMounts(mounts),
|
oci.WithMounts(mounts),
|
||||||
|
withOCIArgs(svc.Args),
|
||||||
hook,
|
hook,
|
||||||
oci.WithEnv(svc.Env)),
|
oci.WithEnv(svc.Env)),
|
||||||
)
|
)
|
||||||
@ -182,7 +177,7 @@ func (s *Supervisor) Start(svcs []Service) error {
|
|||||||
return containerCreateErr
|
return containerCreateErr
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("created", newContainer.ID())
|
log.Printf("Created container %s\n", newContainer.ID())
|
||||||
|
|
||||||
task, err := newContainer.NewTask(ctx, cio.NewCreator(cio.WithStdio))
|
task, err := newContainer.NewTask(ctx, cio.NewCreator(cio.WithStdio))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -200,7 +195,7 @@ func (s *Supervisor) Start(svcs []Service) error {
|
|||||||
writeErr := ioutil.WriteFile("hosts", hosts, 0644)
|
writeErr := ioutil.WriteFile("hosts", hosts, 0644)
|
||||||
|
|
||||||
if writeErr != nil {
|
if writeErr != nil {
|
||||||
fmt.Println("Error writing hosts file")
|
log.Println("Error writing hosts file")
|
||||||
}
|
}
|
||||||
// os.Chown("hosts", 101, 101)
|
// os.Chown("hosts", 101, 101)
|
||||||
|
|
||||||
@ -277,9 +272,55 @@ type Service struct {
|
|||||||
Name string
|
Name string
|
||||||
Mounts []Mount
|
Mounts []Mount
|
||||||
Caps []string
|
Caps []string
|
||||||
|
Args []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Mount struct {
|
type Mount struct {
|
||||||
Src string
|
Src string
|
||||||
Dest string
|
Dest string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func withOCIArgs(args []string) oci.SpecOpts {
|
||||||
|
if len(args) > 0 {
|
||||||
|
return oci.WithProcessArgs(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(_ context.Context, _ oci.Client, _ *containers.Container, s *oci.Spec) error {
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// From Stellar
|
||||||
|
func killTask(ctx context.Context, task containerd.Task) error {
|
||||||
|
wg := &sync.WaitGroup{}
|
||||||
|
wg.Add(1)
|
||||||
|
var err error
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
if task != nil {
|
||||||
|
wait, err := task.Wait(ctx)
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("error waiting on task: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := task.Kill(ctx, unix.SIGTERM, containerd.WithKillAll); err != nil {
|
||||||
|
log.Printf("error killing container task: %s", err)
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-wait:
|
||||||
|
task.Delete(ctx)
|
||||||
|
return
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
if err := task.Kill(ctx, unix.SIGKILL, containerd.WithKillAll); err != nil {
|
||||||
|
log.Printf("error force killing container task: %s", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
@ -1,2 +1 @@
|
|||||||
|
|
||||||
nameserver 8.8.8.8
|
nameserver 8.8.8.8
|
Reference in New Issue
Block a user