docs/community/samples/serving/helloworld-elixir
Himanshu Pandey 793f2f3e12 Removed ENV PORT from all Docker files (#1290)
* Removed ENV PORT from Docker files

* Added back the ENV key
2019-05-13 08:49:40 -07:00
..
assets Add Community Sample Directory (#805) 2019-01-28 14:08:37 -08:00
config Add Community Sample Directory (#805) 2019-01-28 14:08:37 -08:00
lib Add Community Sample Directory (#805) 2019-01-28 14:08:37 -08:00
priv/gettext Add Community Sample Directory (#805) 2019-01-28 14:08:37 -08:00
rel Add Community Sample Directory (#805) 2019-01-28 14:08:37 -08:00
test Add Community Sample Directory (#805) 2019-01-28 14:08:37 -08:00
.gcloudignore Add Community Sample Directory (#805) 2019-01-28 14:08:37 -08:00
.gitignore Add Community Sample Directory (#805) 2019-01-28 14:08:37 -08:00
Dockerfile Removed ENV PORT from all Docker files (#1290) 2019-05-13 08:49:40 -07:00
README.md Removed ENV PORT from all Docker files (#1290) 2019-05-13 08:49:40 -07:00
_index.md add community _index.md files (#1219) 2019-04-22 11:48:28 -07:00
mix.exs Add Community Sample Directory (#805) 2019-01-28 14:08:37 -08:00
mix.lock Add Community Sample Directory (#805) 2019-01-28 14:08:37 -08:00
service.yaml Add Community Sample Directory (#805) 2019-01-28 14:08:37 -08:00

README.md

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
    # Phoenix works best on pre 1.7 at the moment.
    FROM elixir:1.6.6-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
    
    # Update nodejs, rebar, and hex.
    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 ca-certificates
    
    # Add a user so the server will run as a non-root user.
    RUN addgroup -g 1000 appuser && \
        adduser -S -u 1000 -G appuser appuser
    # Pre-create necessary temp directory for erlang and set permissions.
    RUN mkdir -p /opt/app/var
    RUN chown appuser /opt/app/var
    # Run everything else as 'appuser'
    USER appuser
    
    ENV MIX_ENV=prod REPLACE_OS_VARS=true
    WORKDIR /opt/app
    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 --filename 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 these commands 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.

    # In Knative 0.2.x and prior versions, the `knative-ingressgateway` service was used instead of `istio-ingressgateway`.
    INGRESSGATEWAY=knative-ingressgateway
    
    # The use of `knative-ingressgateway` is deprecated in Knative v0.3.x.
    # Use `istio-ingressgateway` instead, since `knative-ingressgateway`
    # will be removed in Knative v0.4.
    if kubectl get configmap config-istio -n knative-serving &> /dev/null; then
        INGRESSGATEWAY=istio-ingressgateway
    fi
    
    kubectl get svc $INGRESSGATEWAY --namespace istio-system
    
    NAME                     TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)                                      AGE
    xxxxxxx-ingressgateway   LoadBalancer   10.23.247.74   35.203.155.229   80:32380/TCP,443:32390/TCP,32400:32400/TCP   2d
    
  6. To find the URL for your service, use

    kubectl get ksvc helloworld-elixir --output=custom-columns=NAME:.metadata.name,DOMAIN:.status.domain
    
    NAME                DOMAIN
    helloworld-elixir   helloworld-elixir.default.example.com
    
  7. 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.

    ```shell
    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>
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <meta name="description" content="">
     <meta name="author" content="">
    
     <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 --filename service.yaml