docs/serving/samples/helloworld-elixir
Sam O'Dell 00d6cec8ca services.serving.knative.dev -> ksvc, adding check install guide (#360)
* services.serving.knative.dev -> ksvc, adding check install guide

* Updating image to add outline

* Making Richie's suggested edits
2018-08-31 09:58:12 -07:00
..
assets Add an elixir language sample using the phoenix framework to the knat… (#298) 2018-08-08 09:53:12 -07:00
config Add an elixir language sample using the phoenix framework to the knat… (#298) 2018-08-08 09:53:12 -07:00
lib Add an elixir language sample using the phoenix framework to the knat… (#298) 2018-08-08 09:53:12 -07:00
priv/gettext Add an elixir language sample using the phoenix framework to the knat… (#298) 2018-08-08 09:53:12 -07:00
rel Add an elixir language sample using the phoenix framework to the knat… (#298) 2018-08-08 09:53:12 -07:00
test Add an elixir language sample using the phoenix framework to the knat… (#298) 2018-08-08 09:53:12 -07:00
.gitignore Add an elixir language sample using the phoenix framework to the knat… (#298) 2018-08-08 09:53:12 -07:00
Dockerfile Add an elixir language sample using the phoenix framework to the knat… (#298) 2018-08-08 09:53:12 -07:00
README.md services.serving.knative.dev -> ksvc, adding check install guide (#360) 2018-08-31 09:58:12 -07:00
mix.exs Add an elixir language sample using the phoenix framework to the knat… (#298) 2018-08-08 09:53:12 -07:00
mix.lock Add an elixir language sample using the phoenix framework to the knat… (#298) 2018-08-08 09:53:12 -07:00
service.yaml Add an elixir language sample using the phoenix framework to the knat… (#298) 2018-08-08 09:53:12 -07:00

README.md

Hello World - Elixir Sample

A simple web application written in Elixir using the Phoenix Framework. The application prints all environment variables to the main page.

Set up Elixir and Phoenix Locally

Following the Phoenix Installation Guide is the best way to get your computer set up for developing, building, running, and packaging Elixir Web applications.

Running Locally

To start your Phoenix server:

  • Install dependencies with mix deps.get
  • Install Node.js dependencies with cd assets && npm install
  • Start Phoenix endpoint with mix phx.server

Now you can visit localhost:4000 from your browser.

Recreating the sample code

  1. Generate a new project.
mix phoenix.new helloelixir

When asked, if you want to Fetch and install dependencies? [Yn] select y

  1. Follow the direction in the output to change directories into start your local server with mix phoenix.server

  2. In the new directory, create a new Dockerfile for packaging your application for deployment

    # Start from a base image for elixir
    FROM elixir:alpine
    
    # Set up Elixir and Phoenix
    ARG APP_NAME=hello
    ARG PHOENIX_SUBDIR=.
    ENV MIX_ENV=prod REPLACE_OS_VARS=true TERM=xterm
    WORKDIR /opt/app
    
    # Compile assets.
    RUN apk update \
        && apk --no-cache --update add nodejs nodejs-npm \
        && mix local.rebar --force \
        && mix local.hex --force
    COPY . .
    
    # Download and compile dependencies, then compile Web app.
    RUN mix do deps.get, deps.compile, compile
    RUN cd ${PHOENIX_SUBDIR}/assets \
        && npm install \
        && ./node_modules/brunch/bin/brunch build -p \
        && cd .. \
        && mix phx.digest
    
    # Create a release version of the application
    RUN mix release --env=prod --verbose \
        && mv _build/prod/rel/${APP_NAME} /opt/release \
        && mv /opt/release/bin/${APP_NAME} /opt/release/bin/start_server
    
    # Prepare final layer
    FROM alpine:latest
    RUN apk update && apk --no-cache --update add bash openssl-dev
    ENV PORT=8080 MIX_ENV=prod REPLACE_OS_VARS=true
    WORKDIR /opt/app
    
    # Document that the service listens on port 8080.
    EXPOSE 8080
    COPY --from=0 /opt/release .
    ENV RUNNER_LOG_DIR /var/log
    
    # Command to execute the application.
    CMD ["/opt/app/bin/start_server", "foreground", "boot_var=/tmp"]
    
  3. Create a new file, service.yaml and copy the following Service definition into the file. Make sure to replace {username} with your Docker Hub username.

    apiVersion: serving.knative.dev/v1alpha1
    kind: Service
    metadata:
      name: helloworld-elixir
      namespace: default
    spec:
      runLatest:
        configuration:
          revisionTemplate:
            spec:
              container:
                image: docker.io/{username}/helloworld-elixir
                env:
                - name: TARGET
                  value: "elixir Sample v1"
    

Building and deploying the sample

The sample in this directory is ready to build and deploy without changes. You can deploy the sample as is, or use you created version following the directions above.

  1. Generate a new secret_key_base in the config/prod.secret.exs file. Phoenix applications use a secrets file on production deployments and, by default, that file is not checked into source control. We have provides shell of an example on config/prod.secret.exs.sample and you can use the following command to generate a new prod secrets file.

    SECRET_KEY_BASE=$(elixir -e ":crypto.strong_rand_bytes(48) |> Base.encode64 |> IO.puts")
    sed "s|SECRET+KEY+BASE|$SECRET_KEY_BASE|" config/prod.secret.exs.sample >config/prod.secret.exs
    
  2. Use Docker to build the sample code into a container. To build and push with Docker Hub, run these commands replacing {username} with your Docker Hub username:

     # Build the container on your local machine
     docker build -t {username}/helloworld-elixir .
    
     # Push the container to docker registry
     docker push {username}/helloworld-elixir
    
  3. After the build has completed and the container is pushed to docker hub, you can deploy the app into your cluster. Ensure that the container image value in service.yaml matches the container you built in the previous step. Apply the configuration using kubectl:

    kubectl apply -f service.yaml
    
  4. Now that your service is created, Knative will perform the following steps:

    • Create a new immutable revision for this version of the app.
    • Network programming to create a route, ingress, service, and load balance for your app.
    • Automatically scale your pods up and down (including to zero active pods).
  5. To find the IP address for your service, use kubectl get svc knative-ingressgateway -n istio-system to get the ingress IP for your cluster. If your cluster is new, it may take sometime for the service to get asssigned an external IP address.

    kubectl get svc knative-ingressgateway -n istio-system
    
    NAME                     TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)                                      AGE
    

knative-ingressgateway LoadBalancer 10.35.254.218 35.225.171.32 80:32380/TCP,443:32390/TCP,32400:32400/TCP 1h ```

  1. To find the URL for your service, use

    kubectl get ksvc helloworld-elixir -o=custom-columns=NAME:.metadata.name,DOMAIN:.status.domain
    
    NAME                DOMAIN
    helloworld-elixir   helloworld-elixir.default.example.com
    

    Note: ksvc is an alias for services.serving.knative.dev. If you have an older version (version 0.1.0) of Knative installed, you'll need to use the long name until you upgrade to version 0.1.1 or higher. See Checking Knative Installation Version to learn how to see what version you have installed.

  2. Now you can make a request to your app to see the results. Replace {IP_ADDRESS} with the address you see returned in the previous step.

    curl -H "Host: helloworld-elixir.default.example.com" http://{IP_ADDRESS}
    
    ...
    # HTML from your application is returned.
    

    Here is the HTML returned from our deployed sample application:

    <!DOCTYPE html>
    
<html lang="en"> <head>
<title>Hello Knative</title>
<link rel="stylesheet" type="text/css" href="/css/app-833cc7e8eeed7a7953c5a02e28130dbd.css?vsn=d">
</head>
    </nav>
  </header>

  <p class="alert alert-info" role="alert"></p>
  <p class="alert alert-danger" role="alert"></p>

  <main role="main">

Welcome to Knative and Elixir

$TARGET = elixir Sample v1

Environment

  • BINDIR = /opt/app/erts-9.3.2/bin
  • DEST_SYS_CONFIG_PATH = /opt/app/var/sys.config
  • DEST_VMARGS_PATH = /opt/app/var/vm.args
  • DISTILLERY_TASK = foreground
  • EMU = beam
  • ERL_LIBS = /opt/app/lib
  • ERL_OPTS =
  • ERTS_DIR = /opt/app/erts-9.3.2
  • ERTS_LIB_DIR = /opt/app/erts-9.3.2/../lib
  • ERTS_VSN = 9.3.2
  • HELLOWORLD_ELIXIR_00001_SERVICE_PORT = tcp://10.35.241.50:80
  • HELLOWORLD_ELIXIR_00001_SERVICE_PORT_80_TCP = tcp://10.35.241.50:80
  • HELLOWORLD_ELIXIR_00001_SERVICE_PORT_80_TCP_ADDR = 10.35.241.50
  • HELLOWORLD_ELIXIR_00001_SERVICE_PORT_80_TCP_PORT = 80
  • HELLOWORLD_ELIXIR_00001_SERVICE_PORT_80_TCP_PROTO = tcp
  • HELLOWORLD_ELIXIR_00001_SERVICE_SERVICE_HOST = 10.35.241.50
  • HELLOWORLD_ELIXIR_00001_SERVICE_SERVICE_PORT = 80
  • HELLOWORLD_ELIXIR_00001_SERVICE_SERVICE_PORT_HTTP = 80
  • HELLOWORLD_ELIXIR_PORT = tcp://10.35.253.90:80
  • HELLOWORLD_ELIXIR_PORT_80_TCP = tcp://10.35.253.90:80
  • HELLOWORLD_ELIXIR_PORT_80_TCP_ADDR = 10.35.253.90
  • HELLOWORLD_ELIXIR_PORT_80_TCP_PORT = 80
  • HELLOWORLD_ELIXIR_PORT_80_TCP_PROTO = tcp
  • HELLOWORLD_ELIXIR_SERVICE_HOST = 10.35.253.90
  • HELLOWORLD_ELIXIR_SERVICE_PORT = 80
  • HELLOWORLD_ELIXIR_SERVICE_PORT_HTTP = 80
  • HOME = /root
  • HOSTNAME = helloworld-elixir-00001-deployment-84f68946b4-76hcv
  • KUBERNETES_PORT = tcp://10.35.240.1:443
  • KUBERNETES_PORT_443_TCP = tcp://10.35.240.1:443
  • KUBERNETES_PORT_443_TCP_ADDR = 10.35.240.1
  • KUBERNETES_PORT_443_TCP_PORT = 443
  • KUBERNETES_PORT_443_TCP_PROTO = tcp
  • KUBERNETES_SERVICE_HOST = 10.35.240.1
  • KUBERNETES_SERVICE_PORT = 443
  • KUBERNETES_SERVICE_PORT_HTTPS = 443
  • LD_LIBRARY_PATH = /opt/app/erts-9.3.2/lib:
  • MIX_ENV = prod
  • NAME = hello@127.0.0.1
  • NAME_ARG = -name hello@127.0.0.1
  • NAME_TYPE = -name
  • OLDPWD = /opt/app
  • OTP_VER = 20
  • PATH = /opt/app/erts-9.3.2/bin:/opt/app/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  • PORT = 8080
  • PROGNAME = opt/app/releases/0.0.1/hello.sh
  • PWD = /opt/app
  • RELEASES_DIR = /opt/app/releases
  • RELEASE_CONFIG_DIR = /opt/app
  • RELEASE_ROOT_DIR = /opt/app
  • REL_NAME = hello
  • REL_VSN = 0.0.1
  • REPLACE_OS_VARS = true
  • ROOTDIR = /opt/app
  • RUNNER_LOG_DIR = /var/log
  • RUN_ERL_ENV =
  • SHLVL = 1
  • SRC_SYS_CONFIG_PATH = /opt/app/releases/0.0.1/sys.config
  • SRC_VMARGS_PATH = /opt/app/releases/0.0.1/vm.args
  • SYS_CONFIG_PATH = /opt/app/var/sys.config
  • TARGET = elixir Sample v1
  • TERM = xterm
  • VMARGS_PATH = /opt/app/var/vm.args
</div> <!-- /container -->
<script src="/js/app-930ab1950e10d7b5ab5083423c28f06e.js?vsn=d"></script>
</html> ```

Removing the sample app deployment

To remove the sample app from your cluster, delete the service record:

kubectl delete -f service.yaml