{"id":11773,"date":"2025-08-31T20:30:31","date_gmt":"2025-08-31T20:30:31","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=11773"},"modified":"2025-08-31T20:30:31","modified_gmt":"2025-08-31T20:30:31","slug":"a-simple-web-api-in-various-languages-and-deployable-to-kubernetes-elixir","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/a-simple-web-api-in-various-languages-and-deployable-to-kubernetes-elixir\/","title":{"rendered":"A simple web API in various languages and deployable to Kubernetes (Elixir)"},"content":{"rendered":"<p>Continuing this short series of writing a simple echo service web API along with the docker and k8s requirements, we\u2019re now going to turn our attention to a Elixir implementation.<\/p>\n<p><strong>Implementation<\/strong><\/p>\n<p>I&#8217;m going to be using Visual Code and dev containers, so I created a folder echo_service which has a folder named .devcontainer with the following <em>devcontainer.json<\/em><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n{\r\n    &quot;image&quot;: &quot;elixir&quot;,\r\n    &quot;forwardPorts&quot;: &#x5B;3000]\r\n}\r\n<\/pre>\n<p>Next I opened Visual Code in the echo_service folder and it should detect the devtonainer and ask if you want to reopen in the devcontainer. To which we do.<\/p>\n<p>I&#8217;m going to use Phoenix Server (phx), so I open the terminal in Visual Code and run the following<\/p>\n<ul>\n<li>\nI needed to install phx installer, using<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nmix archive.install hex phx_new\r\n<\/pre>\n<\/li>\n<li>\nNext I want to generate a minimal phx server, hence run the following<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nmix phx.new echo_service --no-html --no-ecto --no-mailer --no-dashboard --no-assets --no-gettext\r\n<\/pre>\n<p>When this prompt appears, type y<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nFetch and install dependencies? &#x5B;Yn]\r\n<\/pre>\n<\/li>\n<li>Now cd into the newly created echo_service folder<\/li>\n<li>To check everything is working, run\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nmix phx.server\r\n<\/pre>\n<\/li>\n<\/ul>\n<p>Next we need to add a couple of controllers (well we could just use one but I&#8217;m going to create a Echo controller and a Health controller). So in lib\/echo_service_web\/controllers add the files echo_controller.ex and health_controller.ex<\/p>\n<p>The echo_controller.ex looks like this<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndefmodule EchoServiceWeb.EchoController do\r\n  use Phoenix.Controller, formats: &#x5B;:html, :json]\r\n\r\n  def index(conn, %{&quot;text&quot; =&gt; text}) do\r\n    send_resp(conn, 200, &quot;Elixir Echo: #{text}&quot;)\r\n  end\r\nend\r\n<\/pre>\n<p>The health_controller.exe should look like this<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndefmodule EchoServiceWeb.HealthController do\r\n  use Phoenix.Controller, formats: &#x5B;:html, :json]\r\n\r\n  def livez(conn, _params) do\r\n    send_resp(conn, 200, &quot;Live&quot;)\r\n  end\r\n\r\n  def readyz(conn, _params) do\r\n    send_resp(conn, 200, &quot;Ready&quot;)\r\n  end\r\nend\r\n<\/pre>\n<p>In the parent folder (i.e. lib\/echo_service_web) edit the router.exe so it looks like this<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndefmodule EchoServiceWeb.Router do\r\n  use EchoServiceWeb, :router\r\n\r\n  pipeline :api do\r\n    plug :accepts, &#x5B;&quot;json&quot;]\r\n  end\r\n\r\n  scope &quot;\/&quot;, EchoServiceWeb do\r\n    # pipe_through :api\r\n    get &quot;\/echo&quot;, EchoController, :index\r\n    get &quot;\/livez&quot;, HealthController, :livez\r\n    get &quot;\/readyz&quot;, HealthController, :readyz\r\n  end\r\nend\r\n<\/pre>\n<p>Now we can run <em>mix phx.server<\/em> again (ctrl+c twice to shut any existing running instance).<\/p>\n<p><strong>Dockerfile<\/strong><\/p>\n<p>Next up we need to create our Dockerfile<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nFROM elixir:latest\r\n\r\nRUN mkdir \/app\r\nCOPY . \/app\r\nWORKDIR \/app\r\n\r\nRUN mix local.hex --force\r\nRUN mix do compile\r\n\r\nENV PORT=8080\r\nEXPOSE 8080\r\n\r\nCMD &#x5B;&quot;mix&quot;, &quot;phx.server&quot;]\r\n<\/pre>\n<p>Note: In Linux port 80 might be locked down, hence we use port 8080 &#8211; to override the default port in phx we also set the environment variable PORT.<\/p>\n<p>To build this, run<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndocker build -t putridparrot.echo_service:v1 .\r\n<\/pre>\n<p>Don\u2019t forget to change the name to your preferred name.<\/p>\n<p>To test this, run<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndocker run -p 8080:8080 putridparrot.echo_service:v1\r\n<\/pre>\n<p><strong>Kubernetes<\/strong><\/p>\n<p>If all wen well we&#8217;ve not tested our application and see it working from a docker image, so now we need to create the deployment etc. for Kubernete&#8217;s. Let&#8217;s assume you&#8217;ve pushed you image to Docker or another container registry such as Azure &#8211; I&#8217;m call my container registry <em>putridparrotreg<\/em>.<\/p>\n<p>I&#8217;m also not going to use helm at this point as I just want a (relatively) simple yaml file to run from kubectl, so create a deployment.yaml file, we&#8217;ll store all the configurations, deployment, service and ingress in this one file jus for simplicity.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\napiVersion: apps\/v1\r\nkind: Deployment\r\nmetadata:\r\n  name: echo\r\n  namespace: dev\r\n  labels:\r\n    app: echo\r\nspec:\r\n  replicas: 1\r\n  selector:\r\n    matchLabels:\r\n      app: echo\r\n  template:\r\n    metadata:\r\n      labels:\r\n        app: echo\r\n    spec:\r\n      containers:\r\n      - name: echo\r\n        image: putridparrotreg\/putridparrot.echo_service:v1\r\n        ports:\r\n        - containerPort: 8080\r\n        resources:\r\n          requests:\r\n            memory: &quot;100Mi&quot;\r\n            cpu: &quot;100m&quot;\r\n          limits:\r\n            memory: &quot;200Mi&quot;\r\n            cpu: &quot;200m&quot;\r\n        livenessProbe:\r\n          httpGet:\r\n            path: \/livez\r\n            port: 8080\r\n          initialDelaySeconds: 30\r\n          periodSeconds: 10\r\n        readinessProbe:\r\n          httpGet:\r\n            path: \/readyz\r\n            port: 8080\r\n          initialDelaySeconds: 5\r\n          periodSeconds: 5\r\n\r\n---\r\napiVersion: v1\r\nkind: Service\r\nmetadata:\r\n  name: echo_service\r\n  namespace: dev\r\n  labels:\r\n    app: echo\r\nspec:\r\n  type: ClusterIP\r\n  selector:\r\n    app: echo \r\n  ports:\r\n  - name: http\r\n    port: 80\r\n    targetPort: 8080\r\n    protocol: TCP\r\n---\r\napiVersion: networking.k8s.io\/v1\r\nkind: Ingress\r\nmetadata:\r\n  name: echo-ingress\r\n  namespace: dev\r\n  annotations:\r\n    kubernetes.io\/ingress.class: &quot;nginx&quot;\r\n    nginx.ingress.kubernetes.io\/rewrite-target: \/\r\nspec:\r\n  rules:\r\n  - host: mydomain.com\r\n    http:\r\n      paths:\r\n      - path: \/\r\n        pathType: Prefix\r\n        backend:\r\n          service:\r\n            name: echo_service\r\n            port:\r\n              number: 80\r\n<\/pre>\n<p>Don&#8217;t forget to change the &#8220;host&#8221; and image to suit, also this assume you created a namespace &#8220;dev&#8221; for your app. See <a href=\"https:\/\/putridparrot.com\/blog\/creating-a-local-container-registry\/\" target=\"_blank\">Creating a local container registry<\/a> for information on setting up your own container registry.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Continuing this short series of writing a simple echo service web API along with the docker and k8s requirements, we\u2019re now going to turn our attention to a Elixir implementation. Implementation I&#8217;m going to be using Visual Code and dev containers, so I created a folder echo_service which has a folder named .devcontainer with the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[102,730,314],"tags":[],"class_list":["post-11773","post","type-post","status-publish","format-standard","hentry","category-docker","category-elixir","category-kubernetes"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11773","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=11773"}],"version-history":[{"count":6,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11773\/revisions"}],"predecessor-version":[{"id":11785,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11773\/revisions\/11785"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=11773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=11773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=11773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}