{"id":1044,"date":"2019-01-10T18:35:29","date_gmt":"2019-01-10T18:35:29","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw93\/?p=1044"},"modified":"2019-01-24T04:04:58","modified_gmt":"2019-01-24T04:04:58","slug":"get-started-with-openfaas-and-kind","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw93\/index.php\/2019\/01\/10\/get-started-with-openfaas-and-kind\/","title":{"rendered":"Get started with OpenFaaS and KinD"},"content":{"rendered":"<header class=\"post-header\">\n<section class=\"post-meta\"><time class=\"post-date\" datetime=\"2019-01-10\">10 JANUARY 2019<\/time>\u00a0on\u00a0<a href=\"https:\/\/blog.alexellis.io\/tag\/openfaas\/\">openfaas<\/a>,\u00a0<a href=\"https:\/\/blog.alexellis.io\/tag\/serverless\/\">serverless<\/a>,\u00a0<a href=\"https:\/\/blog.alexellis.io\/tag\/open-source\/\">open source<\/a>,\u00a0<a href=\"https:\/\/blog.alexellis.io\/tag\/kind\/\">kind<\/a>,\u00a0<a href=\"https:\/\/blog.alexellis.io\/tag\/kubernetes\/\">kubernetes<\/a><\/section>\n<\/header>\n<section class=\"post-content\">\n<div class=\"kg-card-markdown\">\n<p>In this post I want to show you how I&#8217;ve started deploying OpenFaaS with the new tool from the Kubernetes community named Kubernetes in Docker or\u00a0<a href=\"https:\/\/github.com\/kubernetes-sigs\/kind\">KinD<\/a>. You can read my introductory blog post\u00a0<a href=\"https:\/\/blog.alexellis.io\/be-kind-to-yourself\/\">Be KinD to Yourself<\/a>\u00a0here.<\/p>\n<p>The mission of\u00a0<a href=\"https:\/\/github.com\/openfaas\/faas\">OpenFaaS<\/a>\u00a0is to Make Serverless Functions Simple. It is open-source and built by developers, for developers in the open with a growing and welcoming community. With OpenFaaS can run stateless microservices and functions with a single control-plane that focuses on ease of use on top of Kubernetes. The widely accepted OCI\/Docker image format is used to package and deploy your code and can be run on any cloud.<\/p>\n<p>Over the past two years\u00a0<a href=\"https:\/\/kenfdev.o6s.io\/github-stats-page#\/\">more than 160 developers have contributed to code<\/a>, documentation and packaging. A large number of them have also written blog posts and\u00a0<a href=\"https:\/\/github.com\/openfaas\/faas\/blob\/master\/community.md\">held events all over the world<\/a>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/openfaas\/media\/master\/OpenFaaS_Magnet_3_1_png.png\" width=\"90%\" \/><\/p>\n<blockquote><p>Find out more\u00a0<a href=\"https:\/\/www.openfaas.com\/blog\">about OpenFaaS on the blog<\/a>\u00a0or GitHub\u00a0<a href=\"https:\/\/github.com\/openfaas\/faas\">openfaas\/faas<\/a><\/p><\/blockquote>\n<h2 id=\"prereqs\">Pre-reqs<\/h2>\n<p>Unlike prior development environments for Kubernetes such as Docker for Mac or minikube &#8211; the only requirement for your system is Docker which means you can install this almost anywhere you can get\u00a0<code>docker<\/code>\u00a0installed.<\/p>\n<p>This is also a nice experience for developers because it&#8217;s the same on MacOS, Linux and Windows.<\/p>\n<p>On a Linux host or Linux VM type in\u00a0<code>$ curl -sLS https:\/\/get.docker.com | sudo sh<\/code>.<\/p>\n<p>Download\u00a0<a href=\"https:\/\/www.docker.com\/products\/docker-desktop\">Docker Desktop<\/a>\u00a0for Windows or Mac.<\/p>\n<h2 id=\"createyourcluster\">Create your cluster<\/h2>\n<h3 id=\"installkubectl\">Install\u00a0<code>kubectl<\/code><\/h3>\n<p>The\u00a0<code>kubectl<\/code>\u00a0command is the main CLI needed to operate Kubernetes.<\/p>\n<ul>\n<li><a href=\"https:\/\/kubernetes.io\/docs\/tasks\/tools\/install-kubectl\/\">Install and Set Up kubectl<\/a><\/li>\n<\/ul>\n<p>I like to install it via the\u00a0<a href=\"https:\/\/kubernetes.io\/docs\/tasks\/tools\/install-kubectl\/#install-kubectl-binary-using-curl\">binary release here<\/a>.<\/p>\n<h3 id=\"getkind\">Get\u00a0<code>kind<\/code><\/h3>\n<p>You can get the latest and greatest by running the following command (if you have\u00a0<a href=\"https:\/\/golang.org\/dl\/\">Go installed locally<\/a>)<\/p>\n<pre><code>$ go get sigs.k8s.io\/kind\r\n<\/code><\/pre>\n<p>Or if you don&#8217;t want to install Golang on your system you can grab a binary from the\u00a0<a href=\"https:\/\/github.com\/kubernetes-sigs\/kind\/releases\">release page<\/a>.<\/p>\n<h3 id=\"createoneormoreclusters\">Create one or more clusters<\/h3>\n<p>Another neat feature of\u00a0<code>kind<\/code>\u00a0is the ability to create one or more named clusters. I find this useful because OpenFaaS ships plain YAML files and a helm chart and I need to test both independently on a clean and fresh cluster. Why try to remove and delete all the objects you created between tests when you can just spin up an entirely fresh cluster in about the same time?<\/p>\n<pre><code>$ kind create cluster --name openfaas\r\n\r\nCreating cluster 'kind-openfaas' ...\r\n \u2713 Ensuring node image (kindest\/node:v1.12.2) \ud83d\uddbc \r\n \u2713 [kind-openfaas-control-plane] Creating node container \ud83d\udce6 \r\n \u2713 [kind-openfaas-control-plane] Fixing mounts \ud83d\uddfb \r\n \u2713 [kind-openfaas-control-plane] Starting systemd \ud83d\udda5\r\n \u2713 [kind-openfaas-control-plane] Waiting for docker to be ready \ud83d\udc0b \r\n\u2808\u2871 [kind-openfaas-control-plane] Starting Kubernetes (this may take a minute) \u2638 \r\n\r\n<\/code><\/pre>\n<p>Now there is something you must not forget if you work with other remote clusters. Always, always switch context into your new cluster before making changes.<\/p>\n<pre><code>$ export KUBECONFIG=\"$(kind get kubeconfig-path --name=\"openfaas\")\"\r\n<\/code><\/pre>\n<h2 id=\"installopenfaaswithhelm\">Install OpenFaaS with helm<\/h2>\n<h3 id=\"installhelmandtiller\">Install helm and tiller<\/h3>\n<p>The easiest way to install OpenFaaS is to use the\u00a0<code>helm<\/code>\u00a0client and its server-side equivalent\u00a0<code>tiller<\/code>.<\/p>\n<ul>\n<li>Create a ServiceAccount for Tiller:<\/li>\n<\/ul>\n<pre><code>$ kubectl -n kube-system create sa tiller \\\r\n &amp;&amp; kubectl create clusterrolebinding tiller \\\r\n      --clusterrole cluster-admin \\\r\n      --serviceaccount=kube-system:tiller\r\n<\/code><\/pre>\n<ul>\n<li>Install the helm CLI<\/li>\n<\/ul>\n<pre><code>$ curl -sLSf https:\/\/raw.githubusercontent.com\/helm\/helm\/master\/scripts\/get | sudo bash\r\n<\/code><\/pre>\n<ul>\n<li>Install the Tiller server component<\/li>\n<\/ul>\n<pre><code>helm init --skip-refresh --upgrade --service-account tiller\r\n<\/code><\/pre>\n<blockquote><p>Note: it may take a minute or two to download\u00a0<code>tiller<\/code>\u00a0into your cluster.<\/p><\/blockquote>\n<h3 id=\"installtheopenfaascli\">Install the OpenFaaS CLI<\/h3>\n<pre><code>$ curl -sLSf https:\/\/cli.openfaas.com | sudo sh\r\n<\/code><\/pre>\n<p>Or on Mac use\u00a0<code>brew install faas-cli<\/code>.<\/p>\n<h3 id=\"installopenfaas\">Install OpenFaaS<\/h3>\n<p>You can install OpenFaaS with authentication on or off, it&#8217;s up to you. Since your cluster is running locally you may want it turned off. If you decide otherwise then\u00a0<a href=\"https:\/\/docs.openfaas.com\/\">checkout the documentation<\/a>.<\/p>\n<ul>\n<li>Create the\u00a0<code>openfaas<\/code>\u00a0and\u00a0<code>openfaas-fn<\/code>\u00a0namespaces:<\/li>\n<\/ul>\n<pre><code>$ kubectl apply -f https:\/\/raw.githubusercontent.com\/openfaas\/faas-netes\/master\/namespaces.yml\r\n<\/code><\/pre>\n<ul>\n<li>Install using the helm chart:<\/li>\n<\/ul>\n<pre><code>$ helm repo add openfaas https:\/\/openfaas.github.io\/faas-netes &amp;&amp; \\\r\n    helm repo update &amp;&amp; \\\r\n    helm upgrade openfaas --install openfaas\/openfaas \\\r\n      --namespace openfaas  \\\r\n      --set basic_auth=false \\\r\n      --set functionNamespace=openfaas-fn \\\r\n      --set operator.create=true\r\n<\/code><\/pre>\n<p>The command above adds the OpenFaaS helm repository, then updates the local helm library and then installs OpenFaaS locally without authentication.<\/p>\n<blockquote><p>Note: if you see\u00a0<code>Error: could not find a ready tiller pod<\/code>\u00a0then wait a few moments then try again.<\/p><\/blockquote>\n<p>You can fine-tune the settings like the timeouts, how many replicas of each service run, what version you are using and more using the\u00a0<a href=\"https:\/\/github.com\/openfaas\/faas-netes\/tree\/master\/chart\/openfaas\">Helm chart readme<\/a>.<\/p>\n<h2 id=\"checkopenfaasisready\">Check OpenFaaS is ready<\/h2>\n<p>The helm CLI should print a message such as:\u00a0<code>To verify that openfaas has started, run:<\/code><\/p>\n<pre><code>$ kubectl --namespace=openfaas get deployments -l \"release=openfaas, app=openfaas\"\r\n<\/code><\/pre>\n<p>The KinD cluster will now download all the core services that make up OpenFaaS and this could take a few minutes if you&#8217;re on WiFi, so run the command above and look out for &#8220;AVAILABLE&#8221; turning to 1 for everything listed.<\/p>\n<h2 id=\"accessopenfaas\">Access OpenFaaS<\/h2>\n<p>Now that you&#8217;ve setup a cluster and OpenFaaS it&#8217;s time to access the UI and API.<\/p>\n<p>First forward the port of the gateway to your local machine using\u00a0<code>kubectl<\/code>.<\/p>\n<pre><code>$ kubectl port-forward svc\/gateway -n openfaas 8080:8080\r\n<\/code><\/pre>\n<blockquote><p>Note: If you already have a service running on port 8080, then change the port binding to\u00a0<code>8888:8080<\/code>\u00a0for instance. You should also run\u00a0<code>export OPENFAAS_URL=http:\/\/127.0.0.1:8888<\/code>\u00a0so that the CLI knows where to point to.<\/p><\/blockquote>\n<p>You can now use the OpenFaaS CLI and UI.<\/p>\n<p>Open the UI at\u00a0<a href=\"http:\/\/127.0.0.1:8080\/\">http:\/\/127.0.0.1:8080<\/a>\u00a0and deploy a function from the\u00a0<em>Function store<\/em>\u00a0&#8211; a good example is &#8220;CertInfo&#8221; which can check when a TLS certificate will expire.<\/p>\n<p>Downloading your chosen image may take a few seconds or minutes to deploy depending on your connection.<\/p>\n<ul>\n<li>Invoke the function then see its statistics and other information via the CLI:<\/li>\n<\/ul>\n<pre><code>$ faas-cli list -v\r\n<\/code><\/pre>\n<ul>\n<li>Deploy figlet which can generate ASCII text messages, try it out.<\/li>\n<\/ul>\n<pre><code>$ faas-cli store deploy figlet\r\n$ echo Hi! | faas-cli invoke figlet\r\n<\/code><\/pre>\n<p>You can use the\u00a0<code>describe<\/code>\u00a0verb for more information and to find your URL for use with\u00a0<code>curl<\/code>\u00a0or other tools and services.<\/p>\n<pre><code>$ faas-cli describe figlet\r\n<\/code><\/pre>\n<h3 id=\"usetheopenfaascrd\">Use the OpenFaaS CRD<\/h3>\n<p>You can also use the OpenFaaS Custom Resource Definition or CRD by typing in:<\/p>\n<pre><code>$ kubectl get functions -n openfaas-fn\r\n<\/code><\/pre>\n<p>When you create a new function for OpenFaaS you can use the CLI which calls the RESTful API of the OpenFaaS API Gateway, or generate a CRD YAML file instead.<\/p>\n<ul>\n<li>Here&#8217;s an example with Node.js:<\/li>\n<\/ul>\n<p>Change the\u00a0<code>--prefix<\/code>\u00a0do your own Docker Hub account or private Docker registry.<\/p>\n<pre><code>$ mkdir -p ~\/dev\/kind-blog\/ &amp;&amp; \\\r\n  cd ~\/dev\/kind-blog\/ &amp;&amp; \\\r\n  faas-cli template store pull node10-express &amp;&amp; \\\r\n  faas-cli new --lang node10-express --prefix=alexellis2 openfaas-loves-crds\r\n<\/code><\/pre>\n<p>Our function looks like this:<\/p>\n<pre><code>$ cat openfaas-loves-crds\/handler.js\r\n\r\n\"use strict\"\r\n\r\nmodule.exports = (event, context) =&gt; {\r\n    let err;\r\n    const result =             {\r\n        status: \"You said: \" + JSON.stringify(event.body)\r\n    };\r\n\r\n    context\r\n        .status(200)\r\n        .succeed(result);\r\n}\r\n<\/code><\/pre>\n<p>Now let&#8217;s build and push the Docker image for our function<\/p>\n<pre><code>$ faas-cli up --skip-deploy -f openfaas-loves-crds.yml \r\n<\/code><\/pre>\n<p>Followed by generating a CRD file to apply via\u00a0<code>kubectl<\/code>\u00a0instead of through the OpenFaaS CLI.<\/p>\n<pre><code>$ faas-cli generate crd  -f openfaas-loves-crds.yml \r\n---\r\napiVersion: openfaas.com\/v1alpha2\r\nkind: Function\r\nmetadata:\r\n  name: openfaas-loves-crds\r\n  namespace: openfaas-fn\r\nspec:\r\n  name: openfaas-loves-crds\r\n  image: alexellis2\/openfaas-loves-crds:latest\r\n<\/code><\/pre>\n<p>You can then pipe this output into a file to store in Git or pipe it directly to\u00a0<code>kubectl<\/code>\u00a0like this:<\/p>\n<pre><code>$ faas-cli generate crd  -f openfaas-loves-crds.yml | kubectl apply -f -\r\nfunction.openfaas.com \"openfaas-loves-crds\" created\r\n\r\n$ faas-cli list -v\r\nFunction                      \tImage                                   \tInvocations    \tReplicas\r\nopenfaas-loves-crds           \talexellis2\/openfaas-loves-crds:latest   \t0              \t1    \r\n<\/code><\/pre>\n<h2 id=\"wrappingup\">Wrapping up<\/h2>\n<p>KinD is not the only way to deploy Kubernetes locally, or the only way to deploy OpenFaaS, but it&#8217;s quick and easy and you could even create a bash script to do everything in one shot.<\/p>\n<ul>\n<li>If you&#8217;d like to keep learning then checkout the\u00a0<a href=\"https:\/\/github.com\/openfaas\/workshop\">official workshop<\/a>\u00a0which has been followed by hundreds of developers around the world already.<\/li>\n<li>Join Slack if you&#8217;d like to chat more or contribute to the project\u00a0<a href=\"https:\/\/docs.openfaas.com\/community\">Slack<\/a><\/li>\n<\/ul>\n<p>You can also\u00a0<a href=\"https:\/\/docs.openfaas.com\/community\">read the docs<\/a>\u00a0to find out how to deploy to GKE, AKS, DigitalOcean Kubernetes, minikube, Docker Swarm and more.<\/p>\n<\/div>\n<\/section>\n<p><a href=\"https:\/\/blog.alexellis.io\/get-started-with-openfaas-and-kind\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>10 JANUARY 2019\u00a0on\u00a0openfaas,\u00a0serverless,\u00a0open source,\u00a0kind,\u00a0kubernetes In this post I want to show you how I&#8217;ve started deploying OpenFaaS with the new tool from the Kubernetes community named Kubernetes in Docker or\u00a0KinD. You can read my introductory blog post\u00a0Be KinD to Yourself\u00a0here. The mission of\u00a0OpenFaaS\u00a0is to Make Serverless Functions Simple. It is open-source and built by developers, &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw93\/index.php\/2019\/01\/10\/get-started-with-openfaas-and-kind\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Get started with OpenFaaS and KinD&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1044","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/1044","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/comments?post=1044"}],"version-history":[{"count":2,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/1044\/revisions"}],"predecessor-version":[{"id":1150,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/1044\/revisions\/1150"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/media?parent=1044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/categories?post=1044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/tags?post=1044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}