{"id":23183661,"url":"https://github.com/bedus-creation/k8-learning","last_synced_at":"2025-04-05T03:44:17.978Z","repository":{"id":267949704,"uuid":"831639574","full_name":"bedus-creation/k8-learning","owner":"bedus-creation","description":null,"archived":false,"fork":false,"pushed_at":"2025-02-03T20:42:21.000Z","size":201,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-03T21:30:04.851Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bedus-creation.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2024-07-21T07:13:35.000Z","updated_at":"2025-02-03T20:42:25.000Z","dependencies_parsed_at":"2024-12-13T13:20:32.894Z","dependency_job_id":"194cb638-32f6-40de-b150-5433375d5130","html_url":"https://github.com/bedus-creation/k8-learning","commit_stats":null,"previous_names":["bedus-creation/k8-learning"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bedus-creation%2Fk8-learning","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bedus-creation%2Fk8-learning/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bedus-creation%2Fk8-learning/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bedus-creation%2Fk8-learning/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bedus-creation","download_url":"https://codeload.github.com/bedus-creation/k8-learning/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247284917,"owners_count":20913691,"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":[],"created_at":"2024-12-18T09:13:54.037Z","updated_at":"2025-04-05T03:44:17.961Z","avatar_url":"https://github.com/bedus-creation.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Kubernetes Learning\n* [x] Hello world application in Kubernetes\n* [x] Build a custom docker image for PHP, nginx application \u0026 publish to the docker Hub\n* [x] Write a deployment script for PHP,Nginx \u0026 Mysql using the custom docker image build previously\n* [x] Install laravel application and configured to run inside kubernetes clusters\n* [x] Scale the Laravel application horizontally\n* [ ] Write more advance scaling logic to scale application based on cpu use etc.\n* [ ] Load Test\n* [ ] Scale MySQL database using [Vitess](https://planetscale.com/blog/what-is-vitess)\n  * [x] Setup Vitest\n  * [ ] Sharding\n* [x] Scale MySQL database using [Percona](https://docs.percona.com/percona-operator-for-mysql/ps/kubernetes.html)\n  * [x] [setup Percona](https://github.com/bedus-creation/k8-learning/tree/main/k8s/percona#setup)\n  * [x] [Horizontal Scaling](https://github.com/bedus-creation/k8-learning/tree/main/k8s/percona#horizontal-scaling)\n* [ ] use redis as session storage and scale it\n\n## Pod, Service \u0026 Deployment\nA Pod is the smallest deployable unit in Kubernetes. It represents a single instance of a running process in your cluster.\n\n### Service\nA Service is an abstraction that defines a logical set of Pods and provides a stable endpoint to access them. It enables communication between Pods or external clients and Pods.\n\nKey Characteristics:\n* Stable Networking: Provides a consistent IP address and DNS name, even as Pods are replaced or scaled.\n* Load Balancing: Distributes traffic across multiple Pods matching its selector. \n  * Service Types:\n    * ClusterIP (default): Internal communication within the cluster. \n    * NodePort: Exposes the service on a port of each node for external access. \n    * LoadBalancer: Exposes the service externally using a cloud provider's load balancer. \n    * ExternalName: Maps the service to an external DNS name\n\n### Deployment\nA Deployment is a controller that manages Pods and ensures they are running in the desired state. It allows you to define and manage the number of replicas (instances), updates, and rollbacks for your application.\n\n### Install kubectl\nThe Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs.\n[Link](https://kubernetes.io/docs/tasks/tools/)\n\n## Hello World in Kubernetes\nWe can start create a very first deployment using a public docker image.\n```shell\nkubectl create deployment nginx-deployment --image=nginx\n```\n\nThe above deployment can be exposed outside the container \u0026 kubernetes using expose command.\n```shell\nkubectl expose deployment nginx-deploment --type=LoadBalancer --port=8080 --target-port=80\n```\nThe above command creates a service for the 'nginx-deployment' deployment with a LoadBalancer type, enabling access to our Kubernetes cluster from outside the container.\n\nYou can use following commands to get details about pods:\n\n| Commands                                      | Descriptions                   |\n|-----------------------------------------------|--------------------------------|\n| `kubectl get pods`                            | Get all the pods               |\n| `kubectl get pods -o wide`                    | get all pods with IP addresses |\n| `kubectl describe pod nignx-65cfcbff97-b8ccx` | Get the details of a pod       |\n\nYou can use following commands to get details about deployments:\n\n| Commands                           | Descriptions               | \n|------------------------------------|----------------------------|\n| `kubectl get deployments`          | Get all the deployments    |\n| `kubectl delete deployments --all` | delete all the deployments | \n\n## Services\n\n| Commands                                   | Descriptions         |\n|--------------------------------------------|----------------------|\n| `kubectl get services`                     | Get all the services |\n| `kubectl describe service nginx-deployment` | Detail of a service  |\n\nThe `kubectl get services` returns the following:\n```shell\nkubectl get services\nNAME                TYPE           CLUSTER-IP        EXTERNAL-IP    PORT(S)          AGE\nphp                 ClusterIP      192.168.194.147   \u003cnone\u003e         9000/TCP         35h\nkubernetes          ClusterIP      192.168.194.129   \u003cnone\u003e         443/TCP          35h\nnginx               NodePort       192.168.194.181   \u003cnone\u003e         8080:32539/TCP   35h\nnginx-deploment-1   LoadBalancer   192.168.194.248   198.19.249.2   8081:32479/TCP   34h\nmysql               NodePort       192.168.194.218   \u003cnone\u003e         4406:32740/TCP   105m\n```\n* nginx-deployment-1 Service:\nExternal IP: 198.19.249.2 (assigned via LoadBalancer) and Exposed Port (NodePort): 32479, so it can be accessed from `http://198.19.249.2:32539`\n\n* NodePort Service (nginx):\nFor NodePort services, the application is accessible via: `http://\u003cnode-ip\u003e:32539` Here, \u003cnode-ip\u003e refers to the IP of the node running the cluster. If it's running on orbstack: `kubectl get nodes \n-o wide` to get `\u003cnode-ip\u003e`.\n\n### Load-balancer\n\n### Minikube Dashboard\n\n| Commands            | Descriptions                 |\n|---------------------|------------------------------|\n| `minikube start`    |                              |\n| `minikube dashboard | Start the minikube dashboard | \n\n### Exposing Application\nIf your service type is ClusterIP (default), it’s only accessible within the cluster. Access via kubectl port-forward: Forward the service port to your local machine:\n```bash\nkubectl port-forward svc/nginx 8080:80\n```\n\nTo check if nginx is working\n```shell\nkubectl logs \u003cnginx-pod-name\u003e\n```\n\n### Scaling \nScaling a deployment by running the following commands:\n```shell\nkubectl scale deployment \u003cnginx-deployment\u003e --replicas=1\n```\n\n### Applying changes\n```shell\nkubectl apply -f k8s/nginx-service.yaml\n```\n\n## Managing .ENV\nKubernetes allows to create a secret based on .env file, and these secret can be referenced to create a deployment scripts.\n```shell\nkubectl create secret generic env-secret --from-env-file=./application/.env --dry-run=client -o yaml \u003e k8s/env-secret.yaml\n```\n\n\n## Debugging\nYou can use the following command to view the details of all Pods, or specify a Pod name to inspect a particular Pod.\n\n| Commands                                        | Descriptions                 |\n|-------------------------------------------------|------------------------------|\n| `kubectl describe pod`                          |                              |\n| `kubectl describe pod \u003cpodname\u003e -n \u003cnamespace\u003e` | Start the minikube dashboard | \n\n## SSH Connection\nA pod can be accessed with SSH Connection as:\n\n| Commands                                        | Descriptions                 |\n|-------------------------------------------------|------------------------------|\n| `kubectl exec -it \u003cPOD\u003e -- \u003cCOMMAND\u003e`           |                              |\n\nExample of database connection from PHP cluster:\n```php\n\u003c?php\n$servername = \"mysql\";\n$username   = \"root\";\n$password   = \"12345678\";\n$database   = \"jh\";\n$port       = 4406;\n\n$conn = new mysqli($servername, $username, $password, $database, $port);\n\nif ($conn-\u003econnect_error) {\n    die(\"Connection failed: \".$conn-\u003econnect_error);\n}\necho \"Connected successfully!\";\n```\n\n## Deploying custom docker Image with k8:\nSince k8 can't use local docker image, so we need to push it to the container registry. To build php image\n\n```bash\ndocker build -t 9813276057/application-php:v0.0.1 ./php/\n```\n\nTo build the nginx Image\n\n```bash\ndocker build -t 9813276057/application-nginx ./nginx/\n```\n\nAfter building the images, push it to the container hub:\n\n```bash\ndocker push 9813276057/application-php:v0.0.1\n```\n\n```bash\ndocker push 9813276057/application-nginx\n```\n\n### Vitess\n```bash\nDB_CONNECTION=mysql\nDB_HOST=example-vtgate-ae7df4b6\nDB_PORT=3306\nDB_DATABASE=commerce\nDB_USERNAME=user\nDB_PASSWORD=\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbedus-creation%2Fk8-learning","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbedus-creation%2Fk8-learning","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbedus-creation%2Fk8-learning/lists"}