{"id":11559,"date":"2025-08-10T14:32:07","date_gmt":"2025-08-10T14:32:07","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=11559"},"modified":"2025-08-31T20:26:57","modified_gmt":"2025-08-31T20:26:57","slug":"a-simple-web-api-in-various-languages-and-deployable-to-kubernetes-rust","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/a-simple-web-api-in-various-languages-and-deployable-to-kubernetes-rust\/","title":{"rendered":"A simple web API in various languages and deployable to Kubernetes (Rust)"},"content":{"rendered":"<p>Continuing this short series of writing a simple echo service web API along with the docker and k8s requirements, we&#8217;re now going to turn our attention to a Rust implementation.<\/p>\n<p><strong>Implementation<\/strong><\/p>\n<p>I&#8217;m using JetBrains RustRover for this project, so I created a project named <em>echo_service<\/em>.<\/p>\n<p>Next, add the following to the dependencies of Cargo.toml<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\naxum = &quot;0.7&quot;\r\ntokio = { version = &quot;1&quot;, features = &#x5B;&quot;full&quot;] }\r\nserde = { version = &quot;1&quot;, features = &#x5B;&quot;derive&quot;] }\r\n<\/pre>\n<p>and now the main.rs can be replaced with <\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nuse axum::{\r\n    routing::get,\r\n    extract::Query,\r\n    http::StatusCode,\r\n    response::IntoResponse,\r\n    Router,\r\n};\r\nuse tokio::net::TcpListener;\r\nuse axum::serve;\r\nuse std::net::SocketAddr;\r\nuse serde::Deserialize;\r\n\r\n#&#x5B;derive(Deserialize)]\r\nstruct EchoParams {\r\n    text: Option&lt;String&gt;,\r\n}\r\n\r\nasync fn echo(Query(params): Query&lt;EchoParams&gt;) -&gt; String {\r\n    format!(&quot;Rust Echo: {}&quot;, params.text.unwrap_or_default())\r\n}\r\n\r\nasync fn livez() -&gt; impl IntoResponse {\r\n    (StatusCode::OK, &quot;OK&quot;)\r\n}\r\n\r\nasync fn readyz() -&gt; impl IntoResponse {\r\n    (StatusCode::OK, &quot;Ready&quot;)\r\n}\r\n\r\n#&#x5B;tokio::main]\r\nasync fn main() {\r\n    let app = Router::new()\r\n        .route(&quot;\/echo&quot;, get(echo))\r\n        .route(&quot;\/livez&quot;, get(livez))\r\n        .route(&quot;\/readyz&quot;, get(readyz));\r\n\r\n    let addr = SocketAddr::from((&#x5B;0, 0, 0, 0], 8080));\r\n    println!(&quot;Running on http:\/\/{}&quot;, addr);\r\n\r\n    let listener = TcpListener::bind(addr).await.unwrap();\r\n    serve(listener, app).await.unwrap();\r\n\r\n}\r\n<\/pre>\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 rust:1.72-slim AS builder\r\n\r\nWORKDIR \/app\r\nCOPY . .\r\n\r\nRUN cargo build --release\r\n\r\nFROM debian:bookworm-slim\r\n\r\nRUN apt-get update &amp;&amp; apt-get install -y ca-certificates &amp;&amp; \\\r\n    rm -rf \/var\/lib\/apt\/lists\/*\r\n\r\nCOPY --from=builder \/app\/target\/release \/usr\/local\/bin\/echo_service\r\n\r\nRUN chmod +x \/usr\/local\/bin\/echo_service\r\n\r\nEXPOSE 8080\r\n\r\nENTRYPOINT &#x5B;&quot;\/usr\/local\/bin\/echo_service\/echo_service&quot;]\r\n<\/pre>\n<p><em>Note: In Linux port 80 might be locked down, hence we use port 8080 by default.<\/em><\/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><em>Don&#8217;t forget to change the name to your preferred name.<\/em><\/p>\n<p>and 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&#8217;re now going to turn our attention to a Rust implementation. Implementation I&#8217;m using JetBrains RustRover for this project, so I created a project named echo_service. Next, add the following to the dependencies of Cargo.toml axum [&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,314,191],"tags":[],"class_list":["post-11559","post","type-post","status-publish","format-standard","hentry","category-docker","category-kubernetes","category-rust"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11559","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=11559"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11559\/revisions"}],"predecessor-version":[{"id":11779,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11559\/revisions\/11779"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=11559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=11559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=11559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}