Skip to content

Latest commit

 

History

History
159 lines (154 loc) · 6.59 KB

README.md

File metadata and controls

159 lines (154 loc) · 6.59 KB

Reading-for-Leisure: Kubernetes based Continuous Delivery

Project objectives

  1. Create a customized Docker container
  2. Push image to DockerHub, or Cloud based Container Registery (ECR)
  3. Project should deploy automatically to Kubernetes cluster
  4. Deployment should be to some form of Kubernetes service (can be hosted like Google Cloud Run or Amazon EKS, etc)

Introduction

If you want to find some books to read, this project can return a book name for you to have a try. This actix Microservice has multiple routes:

  1. /: return "Hello, have something to read!"
  2. /book: return a random book name to the user
  3. /version: return the version of the service

Structure Diagram

Step 1 Create the Rust project

1. Create new Rust project

  • install Rust:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source "$HOME/.cargo/env"
  • create new project
    • type: cargo new PJ1 (PJ1 is the project's name)
    cargo new PJ1
    • create main.rs and lib.rs under the src folder
    touch main.rs
    touch lib.rs
    //main.rs: the main project code
  • create a Makefile: a special file that lists a set of rules for compiling a project. These rules include targets, which can be an action make needs to take or the files/objects make will need to build, and the commands that need to be run in order to build that target.

2. Compile the code and set up Dockerfile

  • Format the code and check errors
    make format
    make lint
  • compile the code: cargo build
      cargo build
      //- Run under the root directory of the project
      //- This is the command in the Rust programming language that is used to compile a Rust project. 
      //- It compiles the project's source code and its dependencies, and produces an executable binary file.
  • set up Cargo.toml, to determine the dependencies and build configuration of the project
    [package]
    name = "find_books"
    version = "0.1.0"
    edition = "2021"
    
    # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
    
    [dependencies]
    actix-web = "4"
    rand = "0.8"
  • set up Dockerfile
    FROM rust:latest as builder
    ENV APP find_books
    WORKDIR /usr/src/$APP
    COPY . .
    RUN cargo install --path .
    
    FROM debian:buster-slim
    RUN apt-get update && rm -rf /var/lib/apt/lists/*
    COPY --from=builder /usr/local/cargo/bin/$APP /usr/local/bin/$APP
    #export this actix web service to port 8080 and 0.0.0.0
    EXPOSE 8080
    CMD ["find_books"]
    

Run this project (Microservice)

1. Run the project

cargo run
//press Control+C to quit

2. Usage

Deploy to automatic deployment platform

01. AWS ECR and AWS APP Runner

  1. On AWS Could9, create a new environment
  1. Create a private repository on AWS Elastic Container
//This is just an example of push commands, the AWS ECR will show the push commands automatically
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 881869382935.dkr.ecr.us-east-1.amazonaws.com
  • Tips: sometimes you may meet an error when building docker, you can try:
    curl -s https://gist.githubusercontent.com/wongcyrus/a4e726b961260395efa7811cab0b4516/raw/6a045f51acb2338bb2149024a28621db2abfcaab/resize.sh | bash /dev/stdin 60
    
  1. Create a new service on AWS App Runner

02. DockerHub and Minikube

  1. Push container to DockerHub
docker login -u liusuen
docker build . -t liusuen/books
docker push liusuen/books
  1. Run Minikube
minikube start //start cluster
minikube dashboard --url //view dashboard in a new terminal, will show a link like http://127.0.0.1:43485/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
//go to 'Ports' to find 43485, open the link and add 'api' in the end of the link
kubectl create deployment hello-minikube --image=registry.hub.docker.com/liusuen/books //create a deployment
kubectl get deployments //view deployment
kubectl get pods //view pods
kubectl expose deployment hello-minikube --type=LoadBalancer --port=8080//create service and expose it
//view services
kubectl get services hello-minikube
minikube service hello-minikube --url
curl http://192.168.49.2:31821 //Curl the URL shown

clean up

kubectl delete service hello-minikube
kubectl delete deployment hello-minikube
minikube stop

Reference

  1. Kubernetes
  1. Docker
  1. Other resources