$ 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
// compile app
$ docker-compose up --build
// 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 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>"
// 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
$ curl -s -w '\n%{http_code}\n' -X DELETE \
-H 'Content-Type: application/json' \
"http://localhost:8000/employees/<employee_id>"
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
$ 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
$ 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
// 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>"
// 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"
// 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
$ curl -s -w '\n%{http_code}\n' -X DELETE \
-H 'Content-Type: application/json' \
"http://localhost:8000/employees/<employee_id>"
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
Любые вопросы на русском: Телеграм чат