Skip to content

A demo of pipelining Rust application development to Kubernetes on minikube with Skaffold.

Notifications You must be signed in to change notification settings

webmakaka/k8s-rust-skaffold-demo

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Deploying Rust in Kubernetes (minikube) with Skaffold [Actual on january 2022]

YouTube conference recording


Run Local


$ sudo apt-get install libpq-dev jq

$ sudo vi /etc/hosts

127.0.0.1 rust-web-demo-postgres

// run database
$ docker-compose up postgres

$ cargo install diesel_cli --no-default-features --features postgres
$ diesel migration run

$ cargo run

Run in docker

// compile app
$ docker-compose up --build


Using the API

PUT

// ADD NEW
$ curl -s -w '\n%{http_code}\n' -X PUT \
    -H 'Content-Type: application/json' \
    -d '{"fname":"new", "lname":"person", "age": 27, "title":"Devops Engineer"}' \
    "localhost:8000/employees"

GET

// GET ALL
$ curl -s -w '\n%{http_code}\n' "localhost:8000/employees" | jq
// GET BY ID
$ curl -s -w '\n%{http_code}\n' "localhost:8000/employees/<employee_id>"

POST

// UPDATE
$ curl -s -w '\n%{http_code}\n' -X POST \
    -H 'Content-Type: application/json' \
    -d '{"age": 29}' \
    "http://localhost:8000/employees/<employee_id>"

DELETE

// DELETE
$ curl -s -w '\n%{http_code}\n' -X DELETE \
    -H 'Content-Type: application/json' \
    "http://localhost:8000/employees/<employee_id>"

Run in kubernetes


https://shell.cloud.google.com/


// Connect to free google clouds
$ gcloud auth login
$ gcloud cloud-shell ssh

// Rust installation
$ cd ~/tmp/
$ curl https://sh.rustup.rs -sSf | sh

$ source $HOME/.cargo/env

$ rustup update

$ rustc --version
$ cargo --version

$ sudo apt install -y iputils-ping jq

$ cargo install diesel_cli --no-default-features --features postgres

$ export \
    PROFILE=${USER}-minikube \
    MEMORY=8192 \
    CPUS=4 \
    DRIVER=docker \
    KUBERNETES_VERSION=v1.23.1

Run minikube in free google clouds


$ cd ~/tmp
$ git clone https://github.com/webmakaka/k8s-rust-skaffold-demo

$ cd k8s-rust-skaffold-demo/skaffold

$ skaffold dev

Terminal 2

$ gcloud cloud-shell ssh

$ kubectl get pods
NAME                                      READY   STATUS    RESTARTS   AGE
rust-web-demo-69db76cd58-nh96h            1/1     Running   0          26s
rust-web-demo-69db76cd58-xcbvk            1/1     Running   0          26s
rust-web-demo-69db76cd58-xdnt5            1/1     Running   0          26s
rust-web-demo-postgres-677848fd6c-wc9pp   1/1     Running   0          26s

$ kubectl port-forward $(kubectl get pods|awk '/^rust-web-demo-postgres.*Running/{print$1}'|head) 5432:5432

Terminal 3

$ gcloud cloud-shell ssh

$ export \
    PROFILE=${USER}-minikube \
    MEMORY=8192 \
    CPUS=4 \
    DRIVER=docker \
    KUBERNETES_VERSION=v1.23.1

$ sudo vi /etc/hosts

127.0.0.1 rust-web-demo-postgres

$ cd ~/tmp/k8s-rust-skaffold-demo/app/

// CHECK Connection
$ psql -U diesel -h localhost -p 5432 -d rust-web-demo

$ diesel migration run

// ADD DATA
$ psql -U diesel -h localhost -p 5432 -d rust-web-demo -c "INSERT INTO employees (id, fname, lname, age, title) VALUES (1, 'some', 'person', 25, 'Software Engineer');"

// CHECK DATA
$ psql -U diesel -h localhost -p 5432 -d rust-web-demo -c 'SELECT * FROM employees'

 id | fname | lname  | age |       title       
----+-------+--------+-----+-------------------
  1 | some  | person |  25 | Software Engineer
(1 row)

$ minikube --profile ${PROFILE} ip
192.168.49.2

$ export INGRESS_HOST=192.168.49.2.nip.io

Using the API

GET

// GET ALL
$ curl -s -w '\n%{http_code}\n' "${INGRESS_HOST}/employees" | jq

returns:

{
  "results": [
    {
      "id": 1,
      "fname": "some",
      "lname": "person",
      "age": 25,
      "title": "Software Engineer"
    }
  ]
}
200


// GET BY ID
$ curl -s -w '\n%{http_code}\n' "localhost:8000/employees/<employee_id>"

PUT

// ADD NEW
// Run a few times. Not works in first run
$ curl -s -w '\n%{http_code}\n' -X PUT \
    -H 'Content-Type: application/json' \
    -d '{"fname":"new", "lname":"person", "age": 27, "title":"Devops Engineer"}' \
    "${INGRESS_HOST}/employees"

POST

// UPDATE
$ curl -s -w '\n%{http_code}\n' -X POST \
    -H 'Content-Type: application/json' \
    -d '{"age": 29}' \
    "http://localhost:8000/employees/<employee_id>"

DELETE

// DELETE
$ curl -s -w '\n%{http_code}\n' -X DELETE \
    -H 'Content-Type: application/json' \
    "http://localhost:8000/employees/<employee_id>"

P.S.

secret.yaml

apiVersion: v1
kind: Secret
metadata:
  name: rust-web-demo-database-url
type: Opaque
data:
  url: cG9zdGdyZXM6Ly9kaWVzZWw6cEE1NXcwcmQxQHJ1c3Qtd2ViLWRlbW8tcG9zdGdyZXM6NTQzMi9ydXN0LXdlYi1kZW1v

The value for key is base64 encoded DATABASE_URL and the value is base64 of postgres://diesel:pA55w0rd1@rust-web-demo-postgres:5432/rust-web-demo.




Marley

Any questions in english: Telegram Chat
Любые вопросы на русском: Телеграм чат

About

A demo of pipelining Rust application development to Kubernetes on minikube with Skaffold.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 89.8%
  • Dockerfile 5.6%
  • Shell 4.6%