{"id":21178740,"url":"https://github.com/sajjadhz/fastapiapp","last_synced_at":"2025-04-13T16:12:15.623Z","repository":{"id":185600473,"uuid":"524641971","full_name":"Sajjadhz/fastapiapp","owner":"Sajjadhz","description":"simple FastAPI app for kubernetes cluster with k8s manifests, helm chart and jenkins","archived":false,"fork":false,"pushed_at":"2023-08-11T12:51:45.000Z","size":7066,"stargazers_count":5,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-13T16:12:07.160Z","etag":null,"topics":["ansible","devops","fastapi","helm-chart","jenkins","kubernetes","lxc-containers","rancher"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Sajjadhz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-14T10:44:56.000Z","updated_at":"2025-02-25T06:28:56.000Z","dependencies_parsed_at":null,"dependency_job_id":"c80ad1a1-fed2-42e4-83a3-eaadda8b62f6","html_url":"https://github.com/Sajjadhz/fastapiapp","commit_stats":null,"previous_names":["sajjadhz/fastapiapp"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sajjadhz%2Ffastapiapp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sajjadhz%2Ffastapiapp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sajjadhz%2Ffastapiapp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sajjadhz%2Ffastapiapp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sajjadhz","download_url":"https://codeload.github.com/Sajjadhz/fastapiapp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248741195,"owners_count":21154255,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ansible","devops","fastapi","helm-chart","jenkins","kubernetes","lxc-containers","rancher"],"created_at":"2024-11-20T17:23:49.245Z","updated_at":"2025-04-13T16:12:15.600Z","avatar_url":"https://github.com/Sajjadhz.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Introduction\nThis repository include a sample DevOps task in which we are going to install sample python application in different ways by using docker-compose, kubectl command for k8s manifests, helm command for helm chart and using rancher cluster management tool UI. Also we are using Jenkins file to build docker image and push docker image to docker registry.\n\n\n### Setting up K8s Cluster using LXC/LXD \n___\n\u003e **Note:** For development purpose and not recommended for Production use\n\n#### Installing the LXC on Ubuntu \n```\n$ sudo apt-get update \u0026\u0026 sudo apt-get install lxc -y\n$ sudo systemctl status lxc\n$ lxd init\n```\n**Provide default option for all except these:**\n\nName of the new storage pool [default=default]: local\n\nName of the storage backend to use (btrfs, ceph, dir, lvm, zfs) [default=zfs]: dir\n\nWhat should the new bridge be called? [default=lxdbr0]: lxdfan0\n\nWhat IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: 240.235.0.1/24\n\n\n#### Let's create profile for k8s cluster\nMake sure to clone this repo and run these commands moving into lxd-provisioning directory\n```\n$ lxc profile create k8s\n$ cat k8s-profile-config | lxc profile edit k8s\n$ lxc profile list\n+---------+---------+\n|  NAME   | USED BY |\n+---------+---------+\n| default | 0       |\n+---------+---------+\n| k8s     | 0       |\n+---------+---------+\n```\n\n#### It's time to create k8s cluster\n```\n$ kubelx provision\n```\n\n```\n$ lxc list\n+----------+---------+------------------------+------+-----------+-----------+---------------+\n|   NAME   |  STATE  |          IPV4          | IPV6 |   TYPE    | SNAPSHOTS |   LOCATION    |\n+----------+---------+------------------------+------+-----------+-----------+---------------+\n| kmaster  | RUNNING | 240.235.0.12 (eth0)    |      | CONTAINER | 0         | sajjad-ubuntu |\n|          |         | 10.244.0.1 (cni0)      |      |           |           |               |\n|          |         | 10.244.0.0 (flannel.1) |      |           |           |               |\n+----------+---------+------------------------+------+-----------+-----------+---------------+\n| kworker1 | RUNNING | 240.235.0.57 (eth0)    |      | CONTAINER | 0         | sajjad-ubuntu |\n|          |         | 10.244.1.1 (cni0)      |      |           |           |               |\n|          |         | 10.244.1.0 (flannel.1) |      |           |           |               |\n+----------+---------+------------------------+------+-----------+-----------+---------------+\n| kworker2 | RUNNING | 240.235.0.243 (eth0)   |      | CONTAINER | 0         | sajjad-ubuntu |\n|          |         | 10.244.2.1 (cni0)      |      |           |           |               |\n|          |         | 10.244.2.0 (flannel.1) |      |           |           |               |\n+----------+---------+------------------------+------+-----------+-----------+---------------+\n```\n\n#### Verify\n##### Exec into kmaster node\n```\n$ lxc exec kmaster bash\n```\n#### Verifying Nodes\n```\n$ kubectl get nodes\nkmaster    Ready    control-plane,master   16d   v1.22.0\nkworker1   Ready    \u003cnone\u003e                 16d   v1.22.0\nkworker2   Ready    \u003cnone\u003e                 16d   v1.22.0\n```\n\n#### Verifying cluster version\n```\n$ kubectl cluster-info\nKubernetes control plane is running at https://240.235.0.12:6443\nCoreDNS is running at https://240.235.0.12:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy\n```\n\n#### Let's deploy sample nginx \n```\n$ kubectl create deploy nginx --image nginx\ndeployment.apps/nginx created\n\n$ kubectl get all\nNAME                         READY   STATUS    RESTARTS   AGE\npod/nginx-6799fc88d8-ng7f8   1/1     Running   0          10s\n\nNAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE\nservice/kubernetes   ClusterIP   10.96.0.1    \u003cnone\u003e        443/TCP   10m\n\nNAME                    READY   UP-TO-DATE   AVAILABLE   AGE\ndeployment.apps/nginx   1/1     1            1           10s\n\nNAME                               DESIRED   CURRENT   READY   AGE\nreplicaset.apps/nginx-6799fc88d8   1         1         1       10s\n```\n\n#### Creating Service for deployment nginx\n```\n$ kubectl expose deploy nginx --port 80 --type NodePort\nservice/nginx exposed\n\n$ kubectl get service\nNAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE\nkubernetes   ClusterIP   10.96.0.1     \u003cnone\u003e        443/TCP        11m\nnginx        NodePort    10.96.21.25   \u003cnone\u003e        80:30310/TCP   4s\n```\n\n#### Exit out from kmaster node\n```\n$ exit\n```\n#### Try accessing Nginx through any of the worker node's IP address\n```\n$ curl -I 240.235.0.243:30310\nHTTP/1.1 200 OK\nServer: nginx/1.19.3\nDate: Mon, 12 Oct 2020 07:56:50 GMT\nContent-Type: text/html\nContent-Length: 612\nLast-Modified: Tue, 29 Sep 2020 14:12:31 GMT\nConnection: keep-alive\nETag: \"5f7340cf-264\"\nAccept-Ranges: bytes\n```\n##### We can access nginx.. !!!\n\n#### To access k8s cluster without execing into kmaster node\n\n##### Download the kubectl command into your local, I have already present..!\n```\n$ which kubectl\n/usr/bin/kubectl\n```\n##### Create .kube directory\n```\n$ mkdir ~/.kube\n```\n##### copy config from kmaster into .kube directory\n```\n$ lxc file pull kmaster/etc/kubernetes/admin.conf ~/.kube/config\n$ ls -l ~/.kube\ntotal 8\n-rw------- 1 root root 5570 Oct 12 08:05 config\n```\n##### Try to access k8s cluster without execing into kmaster node.\n```\n$ kubectl get nodes\nNAME       STATUS   ROLES                  AGE   VERSION\nkmaster    Ready    control-plane,master   16d   v1.22.0\nkworker1   Ready    \u003cnone\u003e                 16d   v1.22.0\nkworker2   Ready    \u003cnone\u003e                 16d   v1.22.0\n```\n\n### Running app using docker-compose\n___\nMove to the directory where docker-compose.yaml belongs.\n\n#### Run docker-compose command\n```\n$ docker-compose up\n[+] Running 1/0\n ⠿ Container fastapiapp-fastapiapp-1  Created                                                                                                                                                         0.0s\nAttaching to fastapiapp-fastapiapp-1\nfastapiapp-fastapiapp-1  | INFO:     Will watch for changes in these directories: ['/srv']\nfastapiapp-fastapiapp-1  | INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)\nfastapiapp-fastapiapp-1  | INFO:     Started reloader process [1] using StatReload\nfastapiapp-fastapiapp-1  | INFO:     Started server process [8]\nfastapiapp-fastapiapp-1  | INFO:     Waiting for application startup.\nfastapiapp-fastapiapp-1  | INFO:     Application startup complete.\nfastapiapp-fastapiapp-1  | INFO:     172.22.0.1:41930 - \"GET / HTTP/1.1\" 404 Not Found\nfastapiapp-fastapiapp-1  | INFO:     172.22.0.1:41930 - \"GET /favicon.ico HTTP/1.1\" 404 Not Found\n```\n#### Verify\nOn your browser go to the http://0.0.0.0:8000/api/v1/checkapi you should see:\n\n```\n{\n  \"message\": \"Hello World\"\n}\n```\n#### Stop and remove the container\nMake sure to stop the container.\n```\ndocker-compose down\n```\n\n### Deploying app on k8s cluster using kubectl command\n___\nIn order to deploy the app on k8s cluster first you need to build docker image and push it to your private registry and provide image pull secret for k8s cluster to be able to pull the image and edit k8s manifests for your own docker private registry.\n\n#### Build docker image and push to registry\nReplace Your_Docker_Registry with your docker registry name.\n```\ndocker build . -t Your_Docker_Registry/fastapiapp:latest\ndocker push Your_Docker_Registry/fastapiapp:latest\n```\nforexample:\n```\ndocker build . -t sajjadhz/fastapiapp:latest\ndocker push sajjadhz/fastapiapp:latest\n```\n#### Create k8s secret and provide docker registry credentials as follow\nReplace \u003cpath/to/.docker/config.json\u003e and give docker config path on your host.\n```\nkubectl create secret docker-registry regcred --from-file=.dockerconfigjson=\u003cpath/to/.docker/config.json\u003e\n```\nforexample:\n```\nkubectl create secret docker-registry regcred --from-file=.dockerconfigjson=~/.docker/config.json\n```\n#### Verify\n```\nkubectl get secret regcred --output=\"jsonpath={.data.\\.dockerconfigjson}\" | base64 --decode \n```\n#### App deployment\nNow edit deployment manifest and replace image registry with your own private registry and run following kubectl commands:\n\n```\nkubectl apply -f manifests/deployment-fastapiapp.yaml\nkubectl apply -f manifests/service-fastapiapp.yaml\n```\n#### Verify\n```\n$ kubeclt get all\npod/fastapiapp-deployment-6d68d7597-5jzl8   1/1     Running     0          2m21s\npod/fastapiapp-deployment-6d68d7597-mswj4   1/1     Running     0          2m21s\npod/fastapiapp-deployment-6d68d7597-s2rbd   1/1     Running     0          2m21s\n```\n\nYou can also verify your app deployment on your browsert, enter $NODE_IP:30007/api/v1/checkapi forexample http://240.235.0.12:30007/api/v1/checkapi you should see:\n\n```\n{\n  \"message\": \"Hello World\"\n}\n```\n\n### Installing app by using helmchart\n___\nMove to charts directory and edit values and replace image registry with your own private registry and run following command:\n#### Get helm template before installing app\nYou can use helm template command to see what will be deployed on your cluster before installing the app\n```\n$ helm template fastapiapp ./fastapiapp\n```\n#### Run helm lint before installing app\nYou can also use lint command to verify you chart\n```\n$ helm lint fastapiapp ./fastapiapp/\n==\u003e Linting fastapiapp\n[INFO] Chart.yaml: icon is recommended\n\n==\u003e Linting ./fastapiapp/\n[INFO] Chart.yaml: icon is recommended\n\n2 chart(s) linted, 0 chart(s) failed\n\n```\n#### Run helm install with dry-run flag\n```\n$ helm install fastapiapp ./fastapiapp --dry-run --debug \n```\n#### Install app\nNow you can install the app\n```\n$ helm install fastapiapp ./fastapiapp\n```\n\n#### Verify\n##### Verify by kubectl command\n```\n$ kubectl get all\npod/fastapiapp-5cd89c79b5-cndq6             1/1     Running     0          6h25m\npod/fastapiapp-5cd89c79b5-ngcwb             1/1     Running     0          6h25m\npod/fastapiapp-5cd89c79b5-z2xg2             1/1     Running     0          6h25m\n```\n##### Verify on browser\nAt the end of helm install command helm provides 3 commands to run to get node IP and nodeport as follows:\n```\nNOTES:\n1. Get the application URL by running these commands:\n  export NODE_PORT=$(kubectl get --namespace default -o jsonpath=\"{.spec.ports[0].nodePort}\" services fastapiapp)\n  export NODE_IP=$(kubectl get nodes --namespace default -o jsonpath=\"{.items[0].status.addresses[0].address}\")\n  echo http://$NODE_IP:$NODE_PORT\n```\nRun them and at the end you should get something like this: http://240.235.0.12:30007/ go to the http://240.235.0.12:30007/api/v1/checkapi you should see:\n\n```\n{\n  \"message\": \"Hello World\"\n}\n```\n\n##### Verify by helm test command\nRun helm test command as follow, you should see succeeded message:\n```\n$ helm test fastapiapp\nNAME: fastapiapp\nLAST DEPLOYED: Fri Aug  4 14:33:48 2023\nNAMESPACE: default\nSTATUS: deployed\nREVISION: 3\nTEST SUITE:     fastapiapp-test-connection\nLast Started:   Fri Aug  4 14:34:00 2023\nLast Completed: Fri Aug  4 14:34:06 2023\nPhase:          Succeeded\n\n```\n\nand if you run following command you can see status of tes-connection pod as completed is installation steps were OK:\n\n```\n$ kubectl get all\nNAME                                        READY   STATUS      RESTARTS   AGE\npod/fastapiapp-5cd89c79b5-cndq6             1/1     Running     0          6h25m\npod/fastapiapp-5cd89c79b5-ngcwb             1/1     Running     0          6h25m\npod/fastapiapp-5cd89c79b5-z2xg2             1/1     Running     0          6h25m\npod/fastapiapp-test-connection              0/1     Completed   0          5h4m\n```\n\n### To Do Next\n___\n- Jenkins-Git configurations\n- Jenkins-Docker configurations\n- Rancher-Git configurations \n- Rancher installation and configurations\n- Jenkins installation and configurations\n- Adding stateful app to the cluster\n- Backup System (Velero)\n- Disaster Recovery\n- Adding Ingress\n- Adding monitoring (EFK Stack)\n- Adding multiple microservices to the cluster and API GW\n- Adding configuratin management tool (Ansible)\n- Updating and installing Helm chart by Jenkins\n- Image security check by SonarQube","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsajjadhz%2Ffastapiapp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsajjadhz%2Ffastapiapp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsajjadhz%2Ffastapiapp/lists"}