AWS
This commit is contained in:
parent
478035c6e3
commit
509b3bf63b
128
balancer/aws.py
Normal file
128
balancer/aws.py
Normal file
@ -0,0 +1,128 @@
|
||||
from aws_cli import aws_access_key_id, aws_secret_access_key, aws_session_token, DEFAULT_VPC
|
||||
from user_data import user_data
|
||||
import boto3, time
|
||||
|
||||
|
||||
PREFIX = '444465'
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
ec2 = boto3.resource(
|
||||
'ec2',
|
||||
region_name='us-east-1',
|
||||
aws_access_key_id=aws_access_key_id,
|
||||
aws_secret_access_key=aws_secret_access_key,
|
||||
aws_session_token=aws_session_token,
|
||||
)
|
||||
|
||||
client = boto3.client(
|
||||
'elbv2',
|
||||
region_name='us-east-1',
|
||||
aws_access_key_id=aws_access_key_id,
|
||||
aws_secret_access_key=aws_secret_access_key,
|
||||
aws_session_token=aws_session_token,
|
||||
)
|
||||
|
||||
ec2_client = boto3.client(
|
||||
'ec2',
|
||||
region_name='us-east-1',
|
||||
aws_access_key_id=aws_access_key_id,
|
||||
aws_secret_access_key=aws_secret_access_key,
|
||||
aws_session_token=aws_session_token,
|
||||
)
|
||||
|
||||
key_pair = ec2.create_key_pair(
|
||||
KeyName=PREFIX + '_key'
|
||||
)
|
||||
|
||||
security_group = ec2.create_security_group(
|
||||
Description=PREFIX + '_group',
|
||||
GroupName=PREFIX + '_group',
|
||||
VpcId=DEFAULT_VPC,
|
||||
)
|
||||
|
||||
inbound_rules = security_group.authorize_ingress(
|
||||
GroupId=security_group.group_id,
|
||||
CidrIp='0.0.0.0/0',
|
||||
IpProtocol='tcp',
|
||||
FromPort=80,
|
||||
ToPort=80,
|
||||
)
|
||||
|
||||
instance1, instance2 = ec2.create_instances(
|
||||
ImageId='ami-0b5eea76982371e91',
|
||||
MinCount=2,
|
||||
MaxCount=2,
|
||||
InstanceType='t2.micro',
|
||||
KeyName=key_pair.name,
|
||||
UserData=user_data,
|
||||
NetworkInterfaces = [
|
||||
{
|
||||
'SubnetId': "subnet-0288a7255f0fdfe0e",
|
||||
'DeviceIndex': 0,
|
||||
'AssociatePublicIpAddress': True,
|
||||
'Groups': [security_group.id]
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
while True:
|
||||
time.sleep(1)
|
||||
instance1 = ec2.Instance(instance1.id)
|
||||
instance2 = ec2.Instance(instance2.id)
|
||||
if instance1.state['Code'] == 16 and instance2.state['Code'] == 16:
|
||||
break
|
||||
|
||||
|
||||
target_group = client.create_target_group(
|
||||
Name=PREFIX + '-TargetGroup',
|
||||
Protocol='TCP',
|
||||
Port=80,
|
||||
VpcId=DEFAULT_VPC,
|
||||
TargetType='instance'
|
||||
)
|
||||
|
||||
registered_targets = client.register_targets(
|
||||
TargetGroupArn=target_group['TargetGroups'][0]['TargetGroupArn'],
|
||||
Targets=[
|
||||
{
|
||||
'Id': instance1.id,
|
||||
'Port': 80,
|
||||
},
|
||||
{
|
||||
'Id': instance2.id,
|
||||
'Port': 80,
|
||||
},
|
||||
]
|
||||
)
|
||||
|
||||
allocation = ec2_client.allocate_address(
|
||||
Domain='vpc'
|
||||
)
|
||||
|
||||
load_balancer = client.create_load_balancer(
|
||||
Name=PREFIX + '-Balancer',
|
||||
SubnetMappings=[
|
||||
{
|
||||
'SubnetId': instance1.subnet_id,
|
||||
'AllocationId': allocation['AllocationId'],
|
||||
},
|
||||
],
|
||||
Scheme='internet-facing',
|
||||
Type='network',
|
||||
IpAddressType='ipv4',
|
||||
)
|
||||
|
||||
listener = client.create_listener(
|
||||
LoadBalancerArn=load_balancer['LoadBalancers'][0]['LoadBalancerArn'],
|
||||
Protocol='TCP',
|
||||
Port=80,
|
||||
DefaultActions=[
|
||||
{
|
||||
'Type': 'forward',
|
||||
'TargetGroupArn': target_group['TargetGroups'][0]['TargetGroupArn'],
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
print(f'{allocation["PublicIp"]}:80')
|
5
balancer/aws_cli.py
Normal file
5
balancer/aws_cli.py
Normal file
@ -0,0 +1,5 @@
|
||||
aws_access_key_id = ""
|
||||
aws_secret_access_key = ""
|
||||
aws_session_token = ""
|
||||
|
||||
DEFAULT_VPC = ''
|
151
balancer/hetzner.py
Normal file
151
balancer/hetzner.py
Normal file
@ -0,0 +1,151 @@
|
||||
from hcloud import Client
|
||||
from hcloud.networks.domain import NetworkSubnet
|
||||
from hcloud.locations.domain import Location
|
||||
from hcloud.server_types.domain import ServerType
|
||||
from hcloud.images.domain import Image
|
||||
from hcloud.load_balancers.domain import (
|
||||
LoadBalancerService,
|
||||
LoadBalancerServiceHttp,
|
||||
LoadBalancerHealthCheck,
|
||||
LoadBalancerHealtCheckHttp,
|
||||
LoadBalancerTarget
|
||||
)
|
||||
from hcloud.load_balancer_types.domain import LoadBalancerType
|
||||
|
||||
|
||||
TOKEN = 'KccUEiddxtzGoLWSNC3V8tylq7MYHCjdnShtgasQ8jSbHqCjGoaa6Rq7yoz4uS23'
|
||||
SSH_PUBKEY = ''
|
||||
|
||||
cloud_init = """#cloud-config
|
||||
packages:
|
||||
- apt-transport-https
|
||||
- ca-certificates
|
||||
- curl
|
||||
- gnupg-agent
|
||||
- software-properties-common
|
||||
|
||||
runcmd:
|
||||
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
|
||||
- add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
|
||||
- apt-get update -y
|
||||
- apt-get install -y docker-ce docker-ce-cli containerd.io
|
||||
- curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
|
||||
- chmod +x /usr/local/bin/docker-compose
|
||||
- systemctl start docker
|
||||
- systemctl enable docker
|
||||
- git clone https://git.wmi.amu.edu.pl/s444465/pzc-hetzner.git
|
||||
- cd pzc-hetzner/balancer
|
||||
- docker build -t webservice .
|
||||
- docker run -d -p 80:8080 -t webservice"""
|
||||
|
||||
|
||||
PREFIX = '444465'
|
||||
key_name = f'{PREFIX}-key'
|
||||
net_name = f'{PREFIX}-vnet'
|
||||
|
||||
if __name__ == '__main__':
|
||||
client = Client(token=TOKEN)
|
||||
ssh_key = client.ssh_keys.get_by_name(key_name) or None
|
||||
if not ssh_key:
|
||||
ssh_key = client.ssh_keys.create(name=key_name, public_key=SSH_PUBKEY)
|
||||
|
||||
vnet = client.networks.get_by_name(net_name) or None
|
||||
if not vnet:
|
||||
vnet = client.networks.create(
|
||||
name=net_name,
|
||||
ip_range='10.10.10.0/24',
|
||||
subnets=[
|
||||
NetworkSubnet(ip_range='10.10.10.0/24',
|
||||
network_zone='eu-central',
|
||||
type='cloud')
|
||||
]
|
||||
)
|
||||
|
||||
server1 = client.servers.create(
|
||||
name=f'{PREFIX}-webservice-1',
|
||||
server_type=ServerType('cx11'),
|
||||
image=Image(name='ubuntu-20.04'),
|
||||
ssh_keys=[ssh_key],
|
||||
networks=[vnet],
|
||||
location=Location('hel1'),
|
||||
user_data=cloud_init
|
||||
)
|
||||
server1.action.wait_until_finished()
|
||||
print(f'Server 1 created: {server1.action.complete}')
|
||||
server1 = client.servers.get_by_name(f'{PREFIX}-webservice-1')
|
||||
|
||||
server2 = client.servers.create(
|
||||
name=f'{PREFIX}-webservice-2',
|
||||
server_type=ServerType('cx11'),
|
||||
image=Image(name='ubuntu-20.04'),
|
||||
ssh_keys=[ssh_key],
|
||||
networks=[vnet],
|
||||
location=Location('hel1'),
|
||||
user_data=cloud_init
|
||||
)
|
||||
server2.action.wait_until_finished()
|
||||
print(f'Server 2 created: {server2.action.complete}')
|
||||
server2 = client.servers.get_by_name(f'{PREFIX}-webservice-2')
|
||||
|
||||
|
||||
server3 = client.servers.create(
|
||||
name=f'{PREFIX}-webservice-3',
|
||||
server_type=ServerType('cx11'),
|
||||
image=Image(name='ubuntu-20.04'),
|
||||
ssh_keys=[ssh_key],
|
||||
networks=[vnet],
|
||||
location=Location('hel1'),
|
||||
user_data=cloud_init
|
||||
)
|
||||
server3.action.wait_until_finished()
|
||||
print(f'Server 3 created: {server3.action.complete}')
|
||||
server3 = client.servers.get_by_name(f'{PREFIX}-webservice-3')
|
||||
|
||||
load_balancer = client.load_balancers.create(
|
||||
name=f'{PREFIX}-load-balancer',
|
||||
load_balancer_type=LoadBalancerType(name='lb11'),
|
||||
location=Location('hel1'),
|
||||
network=vnet,
|
||||
targets=[
|
||||
LoadBalancerTarget(
|
||||
type='server',
|
||||
server=server1,
|
||||
use_private_ip=True,
|
||||
),
|
||||
LoadBalancerTarget(
|
||||
type='server',
|
||||
server=server2,
|
||||
use_private_ip=True,
|
||||
),
|
||||
LoadBalancerTarget(
|
||||
type='server',
|
||||
server=server3,
|
||||
use_private_ip=True,
|
||||
)
|
||||
],
|
||||
services=[
|
||||
LoadBalancerService(
|
||||
protocol='http',
|
||||
listen_port=80,
|
||||
destination_port=80,
|
||||
health_check=LoadBalancerHealthCheck(
|
||||
protocol='http',
|
||||
port=80,
|
||||
interval=15,
|
||||
timeout=10,
|
||||
retries=3,
|
||||
http=LoadBalancerHealtCheckHttp(
|
||||
path='/factors/10',
|
||||
status_codes=['2??', '3??'],
|
||||
tls=False,
|
||||
)
|
||||
),
|
||||
http=LoadBalancerServiceHttp(
|
||||
cookie_name='HCLBSTICKY',
|
||||
cookie_lifetime=300,
|
||||
sticky_sessions=True,
|
||||
certificates=[],
|
||||
)
|
||||
),
|
||||
]
|
||||
)
|
12
balancer/user_data.py
Normal file
12
balancer/user_data.py
Normal file
@ -0,0 +1,12 @@
|
||||
user_data = f'''
|
||||
#!/bin/bash
|
||||
sudo yum update -y
|
||||
sudo yum install git -y
|
||||
git clone https://git.wmi.amu.edu.pl/s444465/pzc-hetzner
|
||||
cd pzc-hetzner/balancer
|
||||
sudo yum install docker -y
|
||||
sudo service docker start
|
||||
sudo usermod -a -G docker ec2-user
|
||||
docker build -t webservice .
|
||||
docker run -d -p 80:8080 -t webservice
|
||||
'''
|
Loading…
Reference in New Issue
Block a user