# Rust on Kubernetes: A Beginner’s Guide to Developing and Deploying Rust Applications

[**Rust**](https://www.rust-lang.org/) is a powerful and safe systems programming language that has been gaining popularity among developers due to its emphasis on safety, speed, and concurrency. [**Kubernetes**](https://kubernetes.io/), on the other hand, is an open-source container orchestration platform that automates deploying, scaling, and managing containerized applications. By combining **Rust** and **Kubernetes**, you can create highly performant, scalable, and secure applications that are easy to deploy and manage.

In this blog post, we’ll walk through the process of developing and deploying a simple Rust application on Kubernetes. This tutorial is aimed at beginner to intermediate Rust developers who want to learn more about using Rust with Kubernetes.

#### Here are some prerequisites…

* Basic understanding of [Rust](https://www.rust-lang.org/) programming and the Rust toolchain
    
* Familiarity with [Docker](https://www.docker.com/) and containerisation
    
* Some experience with [Kubernetes](https://kubernetes.io/) and its concepts
    

### Let's get started!

#### Step 1: Creating a simple Rust application

First, let’s create a simple Rust application that serves an HTTP endpoint. We’ll use the `[warp](https://github.com/seanmonstar/warp)` web framework for this purpose. Create a new Rust project using `cargo`:

```bash
$ cargo new rust_on_k8s
$ cd rust_on_k8s
```

Add the following dependencies to your `Cargo.toml` file:

```ini
[dependencies]
warp = "0.3"
tokio = { version = "1", features = ["full"] }
```

Now, let's go and replace the contents of `src/main.rs` with the following code:

```rust
use warp::Filter;

#[tokio::main]
async fn main() {
    let hello = warp::path!("hello" / String)
    .map(|name| format!("Hello, {}!", name));
    
    let routes = hello;
    println!("Starting server at 127.0.0.1:8000");
    warp::serve(routes).run(([127, 0, 0, 1], 8000)).await;
}
```

This simple application in only 10 lines of code (counting empty lines :P), listens on port 8000 and echos with a greeting when you visit `/hello/your_name`.

To test the application, run and try to access using your browser:

`$ cargo run`

#### Step 2: Dockerizing the Rust application

To deploy our Rust application on Kubernetes, we need to containerize it using Docker. Create a `Dockerfile` in the root of the project with the following content:

\# Using the official Rust image as a builder

```dockerfile
FROM rust:1.58 as builder

WORKDIR /usr/src/app

COPY . .

RUN cargo build --release

# Using a minimal image for the runtime
FROM debian:buster-slim

COPY --from=builder /usr/src/app/target/release/rust_on_k8s /usr/local/bin/rust_on_k8s

CMD ["rust_on_k8s"]
```

Build the Docker image:

`$ docker build -t rust_on_k8s:latest .`

#### Step 3: Deploying the Rust application on Kubernetes

To deploy the Rust application on Kubernetes, create a file named `rust_on_k8s.yaml` with the following content:

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
    name: rust-on-k8s
spec:
    replicas: 3
selector:
    matchLabels:
        app: rust-on-k8s
template:
metadata:
    labels:
        app: rust-on-k8s
spec:
    containers:
        - image: rust_on_k8s:latest
        name: rust-on-k8s
        ports:
        - containerPort: 8000

---
apiVersion: v1
kind: Service
metadata:
    name: rust-on-k8s
ports:
- port: 80
protocol: TCP
targetPort: 8000
spec:
    selector:
        app: rust-on-k8s
    type: LoadBalancer
```

This **YAML** file defines a **Deployment** and a **Service**. The **Deployment** specifies that we want three replicas of our Rust application, each running in a separate container. The **Service** creates a load balancer to distribute incoming traffic evenly among the replicas and expose the application on port 80.

Before applying this configuration, you need to push the Docker image to a container registry accessible by your Kubernetes cluster. For example, you can use Docker Hub, Google Container Registry, or Amazon Elastic Container Registry.

**Here is some information on docker push:** [https://docs.docker.com/engine/reference/commandline/push/](https://docs.docker.com/engine/reference/commandline/push/)

Assuming you’ve pushed the Docker image to Docker Hub, update the \`image\` field in the \`rust\_on\_k8s.yaml\` file to point to the correct image:

containers:  
`- name: rust-on-k8s   image: your_dockerhub_username/rust_on_k8s:latest   ports:   - containerPort: 8000`

Now, apply the configuration to your Kubernetes cluster:

`$ kubectl apply -f rust_on_k8s.yaml`

After a few moments, your Rust application should be up and running on Kubernetes. You can check the status of the deployment and service with the following commands:

`$ kubectl get deployments   $ kubectl get services`

When the service shows an external IP address, you can access the Rust application by navigating to `[http://EXTERNAL_IP/hello/your_name](http://EXTERNAL_IP/hello/your_name.)`[.](http://EXTERNAL_IP/hello/your_name.)

### Conclusion

In this blog post, we walked you through creating a simple Rust application, containerising it with Docker, and deploying it on Kubernetes. Of course that the application we developed and deploy is quite simple and in a production large scale environment there is a lot more we would need to worry about, but at least here we managed to cover the base that you can build on top of.

By combining Rust’s safety and performance with Kubernetes’ scalability and ease of management, you can easily build powerful, secure, and reliable applications for modern cloud environments.

Keep experimenting and learning about Rust and Kubernetes to unlock their full potential, and don’t forget to share your knowledge with the community!

Happy developing!

**Be sure to leave a LIKE and of course comments are always welcome!**

Cheers!
