### Terraform Infrastructure Setup Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Instructions for setting up cloud infrastructure using Terraform, including PostgreSQL, Kubernetes cluster, and other resources. ```APIDOC ## Infrastructure with Terraform ### Description Terraform configurations in the `infra/terraform/` directory deploy the entire cloud environment, including PostgreSQL, Container Registry, k8s cluster, DNS zones, Lockbox secrets, and other resources. ### Setup Variables (infra/terraform/terraform.tfvars) ```hcl # yc_cloud_id = "b1g..." # yc_folder_id = "b1g..." # db_user = "student" # postgres_port = "6432" # postgres_host = "rc1a-xxx.mdb.yandexcloud.net" # postgres_db = "db1" # nginx_domain = "nginx.cloud-course.ru" # cert_email = "admin@example.com" ``` ### Terraform Commands #### Initialize Provider ```bash cd infra/terraform terraform init ``` #### View Plan ```bash terraform plan ``` #### Apply Configuration ```bash terraform apply ``` #### Output Values ```bash # Example: Get docker server name terraform output docker-server-name ``` ``` -------------------------------- ### Example Role Assignment Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/infra/README.md Example of assigning the 'editor' role to a service account for a specific folder. ```bash yc resource-manager folder add-access-binding **********9n9hi2qu --role editor --subject serviceAccount:**********qhi2qu ``` -------------------------------- ### Deploy Infrastructure with Terraform Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/infra/README.md Configure deployment parameters in terraform.tfvars, including cloud and folder IDs. Then run the setup script. ```bash ./setup.sh ``` -------------------------------- ### Install and Configure Yandex Cloud CLI (yc) Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Installs the Yandex Cloud CLI utility for command-line interaction. Use 'yc init' to authorize and configure your cloud environment. 'yc config list' checks settings, and 'yc compute instance list' shows available virtual machines. ```bash # Установка на Linux curl -sSL https://storage.yandexcloud.net/yandexcloud-yc/install.sh | bash ``` ```powershell # Установка на Windows (PowerShell) iex (New-Object System.Net.WebClient).DownloadString('https://storage.yandexcloud.net/yandexcloud-yc/install.ps1') ``` ```bash # Инициализация: авторизация, выбор облака и каталога yc init ``` ```bash # Проверка конфигурации yc config list ``` ```bash # Просмотр списка виртуальных машин в выбранном каталоге yc compute instance list ``` -------------------------------- ### Install yc CLI (Windows Command Prompt) Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/README.md Installs the Yandex Cloud CLI tool on Windows using the command prompt. It downloads and executes a PowerShell script and adds the yc binary to your PATH. ```cmd "@("%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe")" -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://storage.yandexcloud.net/yandexcloud-yc/install.ps1'))" && SET "PATH=%PATH%;%USERPROFILE%\yandex-cloud\bin" ``` -------------------------------- ### Terraform Cloud Infrastructure Setup Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Manage cloud infrastructure using Terraform. This includes initializing the provider, planning changes, applying configurations, and outputting resource details. ```bash # Настройка переменных (infra/terraform/terraform.tfvars) # yc_cloud_id = "b1g..." # yc_folder_id = "b1g..." # db_user = "student" # postgres_port = "6432" # postgres_host = "rc1a-xxx.mdb.yandexcloud.net" # postgres_db = "db1" # nginx_domain = "nginx.cloud-course.ru" # cert_email = "admin@example.com" # Инициализация провайдера cd infra/terraform terraform init # Просмотр плана изменений terraform plan # Применение конфигурации terraform apply # Вывод значений (например, имя docker-сервера) terraform output docker-server-name ``` -------------------------------- ### Install yc CLI (Windows PowerShell) Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/README.md Installs the Yandex Cloud CLI tool on Windows using PowerShell. It downloads and executes a PowerShell script. ```powershell iex (New-Object System.Net.WebClient).DownloadString('https://storage.yandexcloud.net/yandexcloud-yc/install.ps1') ``` -------------------------------- ### Initialize Yandex Cloud CLI Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/README.md Initializes the Yandex Cloud CLI by guiding you through authentication and configuration. You will need to obtain a token and select your cloud and catalog. ```bash yc init ``` -------------------------------- ### Install yc CLI (Linux) Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/README.md Installs the Yandex Cloud CLI tool on Linux systems. Ensure you have curl installed. ```bash curl -sSL https://storage.yandexcloud.net/yandexcloud-yc/install.sh | bash ``` -------------------------------- ### Verify Yandex Cloud CLI Configuration Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/README.md Lists the current configuration settings for the Yandex Cloud CLI. Use this to confirm your setup after initialization. ```bash yc config list ``` -------------------------------- ### Ingress Resource Configuration Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Example of a Kubernetes Ingress resource definition after variable substitution. It configures routing rules, TLS settings, and specifies the Ingress class. ```yaml # Результирующий ingress.yaml (после подстановки переменных) apiVersion: networking.k8s.io/v1 kind: Ingress metadata: namespace: ns annotations: cert-manager.io/cluster-issuer: yc-clusterissuer name: ingress-иванов spec: ingressClassName: nginx tls: - hosts: - иванов.nginx.cloud-course.ru secretName: иванов-secret rules: - host: иванов.nginx.cloud-course.ru http: paths: - backend: service: name: lab-k8s-иванов port: number: 3000 path: / pathType: Prefix ``` -------------------------------- ### Get Yandex Cloud Authentication Token Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/infra/README.md Obtain an authentication token for connecting to Yandex Cloud by setting the YC_TOKEN environment variable. ```bash export YC_TOKEN=$(yc iam create-token) ``` -------------------------------- ### Complete API Gateway OpenAPI Specification Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab1/README.md This is a complete OpenAPI specification for an API Gateway, including a GET method with a dummy integration and a POST method integrating with a Cloud Function. ```yaml openapi: 3.0.0 info: title: Sample API version: 1.0.0 paths: /: get: x-yc-apigateway-integration: type: dummy content: '*': Hello, World! http_code: 200 http_headers: Content-Type: text/plain post: x-yc-apigateway-integration: type: cloud_functions function_id: xxx_ваш_id_функции_____ service_account_id: xxxxxxxxxxx ``` -------------------------------- ### Create Service Account Key Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/infra/README.md Create a key file for the service account. This file will be used for authentication. ```bash yc iam key create \ --service-account-id <идентификатор_сервисного_аккаунта> \ --folder-name <имя_каталога_с_сервисным_аккаунтом> \ --output key.json ``` -------------------------------- ### Configure Serverless Container Environment Variables Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab2/README.md Set up environment variables for your Serverless container, including database connection details and node environment. Some values require input from your instructor. ```pre имя контейнера - todo-app-<ваша фамилия>-container vCPU: 1 (оставляем без изменений) Гарантированная доля vCPU: 5% RAM: 128МБ (оставляем без изменений) URL образа: выбираем загруженный вами образ (его имя - todo-app-<ваша фамилия>) Тег - latest Переменные окружения POSTGRES_HOST: <спросите у преподавателя> POSTGRES_PORT: 6432 NODE_ENV: production POSTGRES_DB: db1 SECTION: <ваша фамилия> Секреты Yandex Lockbox: POSTGRES_USER из секрета key_for_postres выбираем значение DBUSER POSTGRES_PASSWORD из секрета key_for_postres выбираем значение DBPASSWORD Сервисный аккаунт: image_puller Сеть: default Все остальные параметры оставляем по умолчанию: Количество одновременных вызовов экземпляра контейнера: 1 Количество подготовленных экземпляров: 0 ``` -------------------------------- ### Prepare Ingress Configuration Files Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Use `envsubst` to substitute environment variables into template files for Ingress resources. This allows for dynamic configuration of your Ingress. ```bash # Подстановка переменных в шаблоны ingress cd lab3/k8s/ingress envsubst < node-template.yaml > node.yaml envsubst < ingress-template.yaml > ingress.yaml ``` -------------------------------- ### Подстановка параметров в файлы конфигурации Kubernetes Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Использование утилиты envsubst для подстановки значений переменных окружения в файлы шаблонов конфигурации Kubernetes (todo-app-template.yaml и load-balancer-template.yaml). ```bash envsubst < todo-app-template.yaml > todo-app.yaml envsubst < load-balancer-template.yaml > load-balancer.yaml ``` -------------------------------- ### Проверка развертывания приложения Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Команды для проверки статуса подов и описания Deployment вашего приложения в кластере Kubernetes. ```bash kubectl get pods -n ns kubectl describe deployment lab-k8s-<ваша фамилия> -n ns ``` -------------------------------- ### Развертывание приложения в кластере Kubernetes Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Команда для применения конфигурации Deployment вашего приложения к кластеру Kubernetes в пространстве имен 'ns'. ```bash kubectl apply -f todo-app.yaml -n ns ``` -------------------------------- ### Проверка подключения к кластеру Kubernetes Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Команды для проверки подключения к кластеру Kubernetes и отображения информации о запущенных подах. ```bash kubectl cluster-info kubectl get pods ``` -------------------------------- ### Установка переменных окружения для шаблонов Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Установка переменных окружения STUDENT и REGISTRY_ID, которые будут использоваться для подстановки в файлы конфигурации Kubernetes. ```bash export STUDENT=<ваша фамилия> export REGISTRY_ID=<идентификатор реестра - спросите у преподавателя> ``` -------------------------------- ### Building and Publishing Docker Image to Container Registry Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Instructions for building a Docker image locally, authorizing with Container Registry, tagging the image, and pushing it to the registry for use with Serverless Containers. ```APIDOC ## Building and Publishing Docker Image to Container Registry ### Description This section details the process of building a Docker image for an application locally and uploading it to Yandex Container Registry. The uploaded image can then be used to create a Serverless container. ### Steps 1. **Build the application image:** ```bash cd lab2/docker-nodejs-sample docker build -t todo-app-:latest . ``` 2. **Verify the created image:** ```bash docker images | grep todo-app ``` 3. **Authorize with Container Registry using an OAuth token:** * Obtain a token from: `https://oauth.yandex.ru/authorize?response_type=token&client_id=1a6990aa636648e9b2ef855fa7bec2fb` ```bash docker login --username oauth --password cr.yandex ``` 4. **Tag the image for registry upload:** ```bash docker tag todo-app-:latest cr.yandex//todo-app-:latest ``` 5. **Push the image to the registry:** ```bash docker push cr.yandex//todo-app-:latest ``` ``` -------------------------------- ### Deploy Service and Ingress Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Apply the generated service and Ingress configurations to your Kubernetes cluster. ```bash kubectl apply -f node.yaml -n ns ``` ```bash kubectl apply -f ingress.yaml -n ns ``` -------------------------------- ### Получение учетных данных кластера (внешнее подключение) Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Команда для получения учетных данных для подключения к кластеру Kubernetes из внешней сети. ```bash yc managed-kubernetes cluster get-credentials unn-cloud-k8s --external ``` -------------------------------- ### Scale Deployment Replicas via Command Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Alternatively, scale the number of deployment replicas directly using the kubectl scale command. ```bash kubectl scale deploy lab-k8s-<ваша фамилия> -n ns --replicas=4 ``` -------------------------------- ### List Docker Images Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab2/README.md Verifies that the Docker image has been successfully built by listing all available Docker images. Check the image name and creation date. ```bash docker images ``` -------------------------------- ### List Compute Instances Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/README.md Lists all virtual machines in the selected Yandex Cloud catalog. This command is used to verify connectivity and view existing instances. ```bash yc compute instance list ``` -------------------------------- ### Deploy Service and Ingress Resources Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Apply Kubernetes configuration files for a ClusterIP service and an Ingress resource. This makes your service accessible via external URLs. ```bash # Развернуть ClusterIP-сервис и Ingress-ресурс kubectl apply -f node.yaml -n ns kubectl apply -f ingress.yaml -n ns ``` -------------------------------- ### Application Deployment - Kubernetes Deployment Template Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Describes a Kubernetes Deployment for the application in the 'ns' namespace. Variables like $STUDENT and $REGISTRY_ID are substituted using 'envsubst'. ```bash # Задать переменные окружения export STUDENT=иванов export REGISTRY_ID=<идентификатор_реестра> # Подстановка переменных в шаблоны cd lab3/k8s/lb envsubst < todo-app-template.yaml > todo-app.yaml envsubst < load-balancer-template.yaml > load-balancer.yaml # Развернуть приложение kubectl apply -f todo-app.yaml -n ns # Проверить статус развёртывания kubectl get pods -n ns kubectl describe deployment lab-k8s-иванов -n ns ``` ```yaml # Результирующий todo-app.yaml (после подстановки переменных) apiVersion: apps/v1 kind: Deployment metadata: name: lab-k8s-иванов namespace: ns spec: replicas: 1 selector: matchLabels: app: lab-k8s-иванов template: spec: containers: - name: lab-k8s-иванов image: cr.yandex//todo-app-with-stop:latest env: - name: SECTION value: k8s-иванов - name: POSTGRES_HOST valueFrom: secretKeyRef: name: k8s-secret key: POSTGRES_HOST - name: POSTGRES_PASSWORD valueFrom: secretKeyRef: name: k8s-secret key: DBPASSWORD ``` -------------------------------- ### Verify Endpoints Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Check if the endpoints for your service have been correctly created and are ready. ```bash kubectl get endpoints -n ns ``` -------------------------------- ### Configure Yandex Cloud Profile Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/infra/README.md Create and configure a Yandex Cloud profile using the service account key and specifying cloud and folder IDs. ```bash yc config profile create sa-terraform ``` ```bash yc config set service-account-key key.json ``` ```bash yc config set cloud-id <идентификатор_облака> ``` ```bash yc config set folder-id <идентификатор_каталога> ``` -------------------------------- ### Получение учетных данных кластера (внутреннее подключение) Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Команда для получения учетных данных для подключения к кластеру Kubernetes изнутри облака Yandex Cloud. ```bash yc managed-kubernetes cluster get-credentials unn-cloud-k8s --internal ``` -------------------------------- ### Monitor Load Balancing with Watch Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Use the 'watch' command to continuously send requests to your application's external IP and observe responses from different instances. ```bash watch curl http://xxx.xxx.xxx.xxx/name ``` -------------------------------- ### Build Docker Image for Node.js App Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab2/README.md Builds a Docker image for the Node.js application. Replace '' with your actual surname. The '.' at the end specifies the build context. ```bash docker build -t todo-app-<ваша фамилия>:latest . ``` -------------------------------- ### Copy Terraform Configuration Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/infra/README.md Copy the .terraformrc file to the user's home directory to use Terraform mirrors. ```bash cp ./.terraformrc ~/.terraformrc ``` -------------------------------- ### Substitute Environment Variables in Templates Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Use 'envsubst' to populate template files with environment variables for creating Kubernetes resource configurations. ```bash envsubst < node-template.yaml > node.yaml ``` ```bash envsubst < ingress-template.yaml > ingress.yaml ``` -------------------------------- ### Service Publication via LoadBalancer Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Assigns an external IP address to the application, enabling demonstration of load balancing across multiple pod replicas. Includes commands to deploy the LoadBalancer and view the assigned external IP. ```bash # Развернуть LoadBalancer kubectl apply -f load-balancer.yaml -n ns # Посмотреть назначенный внешний IP kubectl get svc -n ns # NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE ``` -------------------------------- ### Deploy LoadBalancer Service Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Apply the LoadBalancer configuration to expose your application outside the cluster. Ensure you replace 'ns' with your actual namespace. ```bash kubectl apply -f load-balancer.yaml -n ns ``` -------------------------------- ### PostgreSQL Persistence Module (postgres.js) Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Initializes a connection to cloud PostgreSQL and implements CRUD operations for the 'todo_items' table. Connection parameters are passed via environment variables. The 'SECTION' field is used to separate data between students. ```javascript // lab2/docker-nodejs-sample/src/persistence/postgres.js // Инициализация соединения (вызывается при старте приложения) await init(); // Подключается к хосту из POSTGRES_HOST, порт из POSTGRES_PORT (по умолчанию 5432) // Переменные окружения для Serverless Container: // POSTGRES_HOST, POSTGRES_PORT=6432, NODE_ENV=production // POSTGRES_DB=db1, POSTGRES_USER (из Lockbox), POSTGRES_PASSWORD (из Lockbox) // SECTION=<фамилия> // Получить все задачи const items = await getItems(); // SELECT * FROM todo_items // Возвращает: [{ id, name, completed }, ...] // Получить одну задачу по id const item = await getItem("0e61cace-2d30-4efe-ae56-bd64ff50aa98"); // SELECT * FROM todo_items WHERE id = $1 // Возвращает: { id, name, completed } или null // Создать задачу await storeItem({ id: "uuid-здесь", name: "Новая задача", completed: false }); // INSERT INTO todo_items(id, name, completed) VALUES($1, $2, $3) // Обновить задачу await updateItem("uuid-здесь", { name: "Обновлённая задача", completed: true }); // UPDATE todo_items SET name=$1, completed=$2 WHERE id=$3 // Удалить задачу await removeItem("uuid-здесь"); // DELETE FROM todo_items WHERE id=$1 // Завершить соединение (при корректном shutdown) await teardown(); ``` -------------------------------- ### Docker Image Build and Push Commands Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt These bash commands are used to build a Docker image for an application, log in to Yandex Container Registry, tag the image, and push it to the registry. ```bash # Сборка образа приложения cd lab2/docker-nodejs-sample docker build -t todo-app-<фамилия>:latest . # Проверка созданного образа docker images | grep todo-app # Авторизация в Container Registry через OAuth-токен # Токен получить по ссылке: https://oauth.yandex.ru/authorize?response_type=token&client_id=1a6990aa636648e9b2ef855fa7bec2fb docker login --username oauth --password <ваш_oauth_токен> cr.yandex # Присвоение тега для загрузки в реестр docker tag todo-app-<фамилия>:latest cr.yandex//todo-app-<фамилия>:latest # Загрузка образа в реестр docker push cr.yandex//todo-app-<фамилия>:latest ``` -------------------------------- ### Tag Docker Image for Container Registry Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab2/README.md Tag your local Docker image with the registry path before pushing. Replace placeholders with your specific details. ```bash docker tag todo-app-<ваша фамилия>:latest cr.yandex/<узнайте идентификатор реестра у преподавателя>/todo-app-<ваша фамилия>:latest ``` -------------------------------- ### Observe Load Balancing with Watch Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Continuously monitor the responses from a LoadBalancer service using `watch` and `curl`. This helps visualize traffic distribution across different pods. ```bash # Наблюдение за балансировкой (ответы от разных подов) watch curl http://84.201.xxx.xxx/name ``` -------------------------------- ### Scale Application Replicas Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Increase the number of application instances by editing the deployment file or using the kubectl scale command. This ensures high availability and load distribution. ```yaml replicas: 1 ``` -------------------------------- ### Scale Kubernetes Deployment Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Scale a Kubernetes deployment to a specified number of replicas. This is often done to demonstrate load balancing or to increase capacity. ```bash # Масштабирование до 4 реплик для демонстрации балансировки kubectl scale deploy lab-k8s-иванов -n ns --replicas=4 ``` -------------------------------- ### Create Yandex Cloud Service Account Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/infra/README.md Create a service account for Terraform. Use 'yc iam service-account list' to find its ID. ```bash yc iam service-account create --name sa-terraform ``` ```bash yc iam service-account list ``` -------------------------------- ### LoadBalancer Service Access Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Demonstrates how to access a Kubernetes service exposed via a LoadBalancer and how to scale the deployment. ```APIDOC ## LoadBalancer Service Access ### Description Access a Kubernetes service exposed via a LoadBalancer and scale the deployment. ### Method GET ### Endpoint `http:///name` ### Example ```bash # Check service availability curl http://84.201.xxx.xxx/name # Expected output: {"name":"lab-k8s-иванов-5d8f7b-xyz"} # Scale deployment to 4 replicas kubectl scale deploy lab-k8s-иванов -n ns --replicas=4 # Observe load balancing (responses from different pods) watch curl http://84.201.xxx.xxx/name # Delete service and deployment kubectl delete deployment lab-k8s-иванов -n ns kubectl delete service lab-k8s-иванов -n ns ``` ``` -------------------------------- ### Restart Deployment Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Force a restart of all pods managed by a specific deployment. ```bash kubectl -n ns rollout restart deployment/lab-k8s-<ваша фамилия> ``` -------------------------------- ### Demonstrate Pod Self-Healing Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Trigger a pod to stop and observe Kubernetes automatically restarting it. This showcases the self-healing capabilities of the orchestrator. ```bash # Демонстрация остановки пода и авторестарта оркестратором curl https://иванов.nginx.cloud-course.ru/stop # service stopping... # Убедиться, что k8s восстановил под kubectl get pods -n ns kubectl describe deployment lab-k8s-иванов -n ns ``` -------------------------------- ### Получение секрета подключения к PostgreSQL Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Команда для проверки доступности секрета 'k8s-secret', содержащего параметры подключения к PostgreSQL, в пространстве имен 'ns'. ```bash kubectl --namespace ns get secret k8s-secret ``` -------------------------------- ### Check Kubernetes Endpoints Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Verify the network endpoints for your services in Kubernetes. This helps confirm that pods are correctly registered and ready to receive traffic. ```bash # Проверить endpoints kubectl get endpoints -n ns ``` -------------------------------- ### Kubernetes Cluster Connection via yc Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Connects to a Yandex Managed Service for Kubernetes cluster using the 'yc' utility and configures 'kubectl'. Includes commands for internal and external connections, as well as verification steps. ```bash # Подключение с машины внутри облака (внутренняя сеть) yc managed-kubernetes cluster get-credentials unn-cloud-k8s --internal # Подключение с внешней машины (например, личный компьютер) yc managed-kubernetes cluster get-credentials unn-cloud-k8s --external # Проверка подключения kubectl cluster-info kubectl get pods -n ns # Проверка наличия секрета с параметрами PostgreSQL kubectl --namespace ns get secret k8s-secret ``` -------------------------------- ### Verify LoadBalancer Service Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Check the status and details of the deployed LoadBalancer service to confirm it has been assigned an external IP address. ```bash kubectl describe service lab-k8s-<ваша фамилия> -n ns ``` ```bash kubectl get svc -n ns ``` -------------------------------- ### Login to Yandex Container Registry Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab2/README.md Logs into the Yandex Container Registry using an OAuth token. This command is necessary to authenticate and authorize the upload of Docker images to the registry. Replace '' with your obtained Yandex OAuth token. ```bash docker login --username oauth --password <ваш токен> cr.yandex ``` -------------------------------- ### Environment Variables for PostgreSQL Connection Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab2/README.md These environment variables are used to configure the connection to a PostgreSQL database. Ensure these are set correctly for the application to establish a connection. ```plaintext POSTGRES_HOST: POSTGRES_PORT: NODE_ENV: POSTGRES_DB: POSTGRES_USER: POSTGRES_PASSWORD: ``` -------------------------------- ### Destroy Infrastructure Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/infra/README.md Navigate to the infra/terraform directory and run the 'terraform destroy' command to remove deployed resources. ```bash terraform destroy ``` -------------------------------- ### Test LoadBalancer External IP Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Send a request to the external IP address assigned to your LoadBalancer to verify that your application is responding. ```bash curl http://xxx.xxx.xxx.xxx/name ``` -------------------------------- ### Test Ingress Access Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Access your application via the Ingress domain to confirm external access and routing. ```bash curl https://<ваша фамилия>.nginx.cloud-course.ru/name ``` -------------------------------- ### API Gateway Configuration (OpenAPI) Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt API Gateway configuration to proxy POST requests from a static site to a private file upload cloud function. The function is removed from public access and can only be invoked through the gateway. ```APIDOC ## API Gateway Configuration (OpenAPI) ### Description API Gateway proxies POST requests from a static site to a private file upload cloud function. The function is then removed from public access, and invocation is only possible through the gateway. ### Configuration (Yandex Cloud Console) ```yaml openapi: 3.0.0 info: title: UNN Cloud Upload Gateway version: 1.0.0 paths: /: get: x-yc-apigateway-integration: type: dummy content: '*': Hello, World! http_code: 200 http_headers: Content-Type: text/plain post: x-yc-apigateway-integration: type: cloud_functions function_id: _function_id> service_account_id: ``` ### Post-Gateway Creation Steps 1. Copy the gateway URL (e.g., `https://xxxxxx.apigw.yandexcloud.net`). 2. Paste this URL into the `action` attribute of the form in `index.html` and republish `index.html` to the bucket. 3. Disable the "Public function" switch for `unn-cloud-function-upload-`. ``` -------------------------------- ### Manage Todo Items via REST API Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Interact with a todo-manager application's REST API to perform CRUD operations on tasks. Use HTTPS for secure communication. ```bash # Получить список задач текущего пользователя (секция) curl https://иванов.nginx.cloud-course.ru/items # [{"id":"0e61cace...","name":"item 1","completed":false}, ...] # Добавить задачу curl -X POST https://иванов.nginx.cloud-course.ru/items \ -H "Content-Type: application/json" \ -d '{"name": "Новая задача"}' # Обновить задачу curl -X PUT https://иванов.nginx.cloud-course.ru/items/ \ -H "Content-Type: application/json" \ -d '{"name": "Изменённая задача", "completed": true}' # Удалить задачу curl -X DELETE https://иванов.nginx.cloud-course.ru/items/ # Получить имя пода (для демонстрации балансировки нагрузки) curl https://иванов.nginx.cloud-course.ru/name # {"name":"lab-k8s-иванов-5d8f7b-xyz"} # Остановить текущий экземпляр пода (для демонстрации self-healing) curl https://иванов.nginx.cloud-course.ru/stop # service stopping... ``` -------------------------------- ### Access Application via HTTPS Ingress Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Access your application through a domain name configured in Ingress, using HTTPS. This demonstrates secure external access to your service. ```bash # Доступ к приложению через доменное имя (HTTPS) curl https://иванов.nginx.cloud-course.ru/name # {"name":"lab-k8s-иванов-5d8f7b-abc"} ``` -------------------------------- ### Delete Deployment and Service Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Remove your application deployment and its associated LoadBalancer service from the cluster. ```bash kubectl delete deployment lab-k8s-<ваша фамилия> -n ns ``` ```bash kubectl delete service lab-k8s-<ваша фамилия> -n ns ``` -------------------------------- ### Delete Kubernetes Service and Deployment Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Clean up Kubernetes resources by deleting the service and deployment. Ensure you specify the correct namespace. ```bash # Удаление сервиса и deployment kubectl delete deployment lab-k8s-иванов -n ns kubectl delete service lab-k8s-иванов -n ns ``` -------------------------------- ### Check LoadBalancer Service Availability Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Use curl to verify that the LoadBalancer service is accessible and to retrieve its name. This is useful for initial service verification. ```bash # Проверить доступность сервиса curl http://84.201.xxx.xxx/name # {"name":"lab-k8s-иванов-5d8f7b-xyz"} ``` -------------------------------- ### Restart Kubernetes Deployment Rollout Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Perform a rollout restart of a Kubernetes deployment without recreating the pods. This is useful for applying configuration changes or refreshing application instances. ```bash # Перезапуск deployment без пересоздания kubectl -n ns rollout restart deployment/lab-k8s-иванов ``` -------------------------------- ### Test Service Stop Endpoint Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Send a request to the '/stop' endpoint of your application via Ingress to test its functionality. ```bash curl https://<ваша фамилия>.nginx.cloud-course.ru/stop ``` -------------------------------- ### Application Endpoints Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt REST API endpoints for the todo-manager application, including task management and orchestration demonstration endpoints. ```APIDOC ## Application Endpoints (Lab 2 and Lab 3) ### Description The todo-manager application provides a REST API for managing tasks and special endpoints for demonstrating orchestration mechanisms. ### Method GET / POST / PUT / DELETE ### Endpoint `https://.nginx.cloud-course.ru/` ### Endpoints #### Get List of Tasks * **Method**: GET * **Endpoint**: `/items` * **Description**: Retrieves the list of tasks for the current user (session). * **Example Response**: `[{"id":"0e61cace...","name":"item 1","completed":false}, ...]` #### Add Task * **Method**: POST * **Endpoint**: `/items` * **Description**: Adds a new task. * **Request Body**: `{"name": "Новая задача"}` #### Update Task * **Method**: PUT * **Endpoint**: `/items/` * **Description**: Updates an existing task. * **Request Body**: `{"name": "Изменённая задача", "completed": true}` #### Delete Task * **Method**: DELETE * **Endpoint**: `/items/` * **Description**: Deletes a task. #### Get Pod Name * **Method**: GET * **Endpoint**: `/name` * **Description**: Retrieves the pod name (for load balancing demonstration). * **Example Response**: `{"name":"lab-k8s-иванов-5d8f7b-xyz"}` #### Stop Pod Instance * **Method**: GET * **Endpoint**: `/stop` * **Description**: Stops the current pod instance (for self-healing demonstration). * **Example Response**: `service stopping...` ### Example Usage ```bash # Get list of tasks curl https://иванов.nginx.cloud-course.ru/items # Add a task curl -X POST https://иванов.nginx.cloud-course.ru/items \ -H "Content-Type: application/json" \ -d '{"name": "Новая задача"}' # Update a task curl -X PUT https://иванов.nginx.cloud-course.ru/items/ \ -H "Content-Type: application/json" \ -d '{"name": "Изменённая задача", "completed": true}' # Delete a task curl -X DELETE https://иванов.nginx.cloud-course.ru/items/ # Get pod name curl https://иванов.nginx.cloud-course.ru/name # Stop current pod instance curl https://иванов.nginx.cloud-course.ru/stop ``` ``` -------------------------------- ### API Gateway POST Method Configuration Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab1/README.md Configure the POST method in API Gateway to integrate with a Cloud Function. Ensure the `function_id` and `service_account_id` are correctly specified. ```yaml post: x-yc-apigateway-integration: type: cloud_functions function_id: xxx <-идентификатор функции unn-cloud-function-upload-<ваша фамилия> service_account_id: xxxxxxxxxx <-идентификатор сервисного аккаунта ``` -------------------------------- ### Push Docker Image to Yandex Container Registry Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab2/README.md Upload your tagged Docker image to the Yandex Container Registry. Ensure you have the correct registry identifier. ```bash docker push cr.yandex/<узнайте идентификатор реестра у преподавателя>/todo-app-<ваша фамилия>:latest ``` -------------------------------- ### Assign Role to Service Account Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/infra/README.md Assign a role to the service account for specific resource access. Replace placeholders with actual values. ```bash yc add-access-binding | --role --subject serviceAccount: ``` -------------------------------- ### Delete LoadBalancer Service Source: https://github.com/open-education-club-by-yandex/unn-itmm-ycloud.git/blob/master/lab3/README.md Remove the LoadBalancer service before setting up Ingress to avoid conflicts. ```bash kubectl delete service lab-k8s-<ваша фамилия> -n ns ``` -------------------------------- ### API Gateway OpenAPI Configuration Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt This OpenAPI configuration sets up API Gateway to proxy POST requests from a static website to a cloud function. It ensures the upload function is only accessible through the gateway. ```yaml # Конфигурация API Gateway — вставляется в консоли Yandex Cloud openapi: 3.0.0 info: title: UNN Cloud Upload Gateway version: 1.0.0 paths: /: get: x-yc-apigateway-integration: type: dummy content: '*': Hello, World! http_code: 200 http_headers: Content-Type: text/plain post: x-yc-apigateway-integration: type: cloud_functions function_id: "<идентификатор_функции_unn-cloud-function-upload-фамилия>" service_account_id: "<идентификатор_сервисного_аккаунта>" # После создания шлюза: скопировать URL вида https://xxxxxx.apigw.yandexcloud.net # Вставить его в action формы в index.html и переопубликовать index.html в бакет # Снять переключатель "Публичная функция" у unn-cloud-function-upload-<фамилия> ``` -------------------------------- ### Object Storage Trigger Function (JavaScript) Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt This function is triggered by Object Storage when a new file is created. It lists all files in a specified folder and generates an HTML page with links to these files. It includes recursion protection by ignoring its own output file. ```javascript // lab1/unn-cloud-trg-function/index.js const { S3Client, ListObjectsV2Command, PutObjectCommand } = require("@aws-sdk/client-s3"); async function connect() { return new S3Client({ region: "ru-central1", endpoint: "https://storage.yandexcloud.net", credentials: { accessKeyId: process.env.accessKeyId, secretAccessKey: process.env.secretAccessKey } }); } // Список объектов в указанной папке бакета async function listFolder(s3Client, folder) { const command = new ListObjectsV2Command({ Bucket: process.env.bucket, Delimiter: '/', Prefix: folder }); const { Contents } = await s3Client.send(command); return Contents; } module.exports.handler = async function (event, context) { // Получаем имя файла из события триггера const file = event['messages'][0]['details']['object_id']; // Защита от рекурсии: список сам по себе не триггерит функцию if (file === 'list.html') return; const s3Client = await connect(); const data = await listFolder(s3Client, "upload/"); // Генерация HTML со ссылками на все загруженные файлы let html = "File list\n"; for (const item of data) { html += `${item.Key}
`; } html += ""; // Сохраняем list.html обратно в бакет await s3Client.send(new PutObjectCommand({ Bucket: process.env.bucket, Key: 'list.html', Body: html, ContentType: "text/html" })); return { statusCode: 200, body: 'list created' }; }; // Тип триггера: Object Storage → создание объекта // Бакет: unn-cloud-bucket-<фамилия> // Функция: unn-cloud-function-trg-<фамилия> // Сервисный аккаунт: bucket-acc ``` -------------------------------- ### Ingress Service Publication Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt Details on publishing a service through an Nginx Ingress controller with automatic TLS certificates. ```APIDOC ## Publish Service via Ingress (nginx) ### Description Publish a service using an Nginx Ingress controller, which routes traffic by domain names and provides automatic TLS certificates. ### Method GET / POST / PUT / DELETE ### Endpoint `https://.nginx.cloud-course.ru/` ### Example ```bash # Substitute variables in ingress templates cd lab3/k8s/ingress envsubst < node-template.yaml > node.yaml envsubst < ingress-template.yaml > ingress.yaml # Deploy ClusterIP service and Ingress resource kubectl apply -f node.yaml -n ns kubectl apply -f ingress.yaml -n ns # Check endpoints kubectl get endpoints -n ns # Access the application via domain name (HTTPS) curl https://иванов.nginx.cloud-course.ru/name # Expected output: {"name":"lab-k8s-иванов-5d8f7b-abc"} # Demonstrate stopping a pod and auto-restart by the orchestrator curl https://иванов.nginx.cloud-course.ru/stop # Expected output: service stopping... # Ensure k8s has restored the pod kubectl get pods -n ns kubectl describe deployment lab-k8s-иванов -n ns # Restart deployment without recreation kubectl -n ns rollout restart deployment/lab-k8s-иванов ``` ### Ingress Configuration Example ```yaml apiVersion: networking.k8s.io/v1 kind: Ingress metadata: namespace: ns annotations: cert-manager.io/cluster-issuer: yc-clusterissuer name: ingress-иванов spec: ingressClassName: nginx tls: - hosts: - иванов.nginx.cloud-course.ru secretName: иванов-secret rules: - host: иванов.nginx.cloud-course.ru http: paths: - backend: service: name: lab-k8s-иванов port: number: 3000 path: / pathType: Prefix ``` ``` -------------------------------- ### Object Storage Trigger Cloud Function - File List Generation Source: https://context7.com/open-education-club-by-yandex/unn-itmm-ycloud.git/llms.txt This cloud function is triggered by Object Storage when a new file is created. It lists all files in the 'upload/' folder and generates an HTML page 'list.html' with links to each file. It includes recursion protection by ignoring changes to its own output file. ```APIDOC ## unn-cloud-trg-function ### Description This function is triggered by Object Storage when a new file is created in a bucket. It retrieves a list of all files in the `upload/` folder and generates an HTML page `list.html` containing links to each file. It includes protection against recursion by ignoring changes to its own output file. ### Trigger Type Object Storage → Object Creation ### Bucket unn-cloud-bucket- ### Function unn-cloud-function-trg- ### Service Account bucket-acc ### Code Example (JavaScript) ```javascript // lab1/unn-cloud-trg-function/index.js const { S3Client, ListObjectsV2Command, PutObjectCommand } = require("@aws-sdk/client-s3"); async function connect() { return new S3Client({ region: "ru-central1", endpoint: "https://storage.yandexcloud.net", credentials: { accessKeyId: process.env.accessKeyId, secretAccessKey: process.env.secretAccessKey } }); } // List objects in a specified bucket folder async function listFolder(s3Client, folder) { const command = new ListObjectsV2Command({ Bucket: process.env.bucket, Delimiter: '/', Prefix: folder }); const { Contents } = await s3Client.send(command); return Contents; } module.exports.handler = async function (event, context) { // Get the filename from the trigger event const file = event['messages'][0]['details']['object_id']; // Recursion protection: the list itself does not trigger the function if (file === 'list.html') return; const s3Client = await connect(); const data = await listFolder(s3Client, "upload/"); // Generate HTML with links to all uploaded files let html = "File list\n"; for (const item of data) { html += `${item.Key}
`; } html += ""; // Save list.html back to the bucket await s3Client.send(new PutObjectCommand({ Bucket: process.env.bucket, Key: 'list.html', Body: html, ContentType: "text/html" })); return { statusCode: 200, body: 'list created' }; }; ``` ```