Методы и примеры кода для работы с платформами VPS

  1. Предоставление VPS:

    • Использование DigitalOcean API:

      import requests
      def create_droplet(api_token, region, size, image):
       headers = {'Content-Type': 'application/json', 'Authorization': f'Bearer {api_token}'}
       data = {'name': 'my-droplet', 'region': region, 'size': size, 'image': image}
       response = requests.post('https://api.digitalocean.com/v2/droplets', headers=headers, json=data)
       return response.json()
      # Example usage
      api_token = 'your-api-token'
      region = 'nyc1'
      size = 's-1vcpu-1gb'
      image = 'ubuntu-18-04-x64'
      droplet = create_droplet(api_token, region, size, image)
    • Использование API AWS EC2:

      import boto3
      def create_ec2_instance(region, image_id, instance_type):
       ec2 = boto3.resource('ec2', region_name=region)
       instance = ec2.create_instances(ImageId=image_id, InstanceType=instance_type, MinCount=1, MaxCount=1)
       return instance[0]
      # Example usage
      region = 'us-west-2'
      image_id = 'ami-0c94855ba95c71c99'
      instance_type = 't2.micro'
      ec2_instance = create_ec2_instance(region, image_id, instance_type)
  2. Развертывание приложений на VPS:

    • Использование SSH и Git:

      # Clone the repository
      git clone <repository_url>
      # Navigate to the project directory
      cd <project_directory>
      # Install dependencies (if required)
      npm install
      # Build the project
      npm run build
      # Start the application
      npm start
    • Использование Docker:

      # Pull the Docker image
      docker pull <image_name>
      # Run the Docker container
      docker run -d -p <host_port>:<container_port> <image_name>
  3. Управление ресурсами VPS:

    • Мониторинг с помощью Prometheus и Grafana:

      # Install Prometheus
      wget https://github.com/prometheus/prometheus/releases/download/v2.30.2/prometheus-2.30.2.linux-amd64.tar.gz
      tar -xvf prometheus-2.30.2.linux-amd64.tar.gz
      cd prometheus-2.30.2.linux-amd64/
      ./prometheus --config.file=prometheus.yml
      # Install Grafana
      wget https://dl.grafana.com/oss/release/grafana-8.2.5.linux-amd64.tar.gz
      tar -xvf grafana-8.2.5.linux-amd64.tar.gz
      cd grafana-8.2.5/
      # Start Grafana service
      ./bin/grafana-server --config=./conf/grafana.ini
    • Масштабирование с помощью Kubernetes:

      # Install Kubernetes
      curl -LO https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl
      chmod +x kubectl
      sudo mv kubectl /usr/local/bin/
      # Create a deployment
      kubectl create deployment my-app --image=<image_name>
      # Scale the deployment
      kubectl scale deployment my-app --replicas=<replica_count>