Облачная экосистема - Евгений Сергеевич Штольц 20 стр.


* AWS Code Deploy автоматизирует развёртывание и возвращение к предыдущему состоянию продакшна в случаи ошибок.

* AWS CodeStar сервис объединяет в себе основные возможности предыдущих сервисов.

Настраиваем удалённое управление

сервер артефактов

aws s3 ls s3://name_backet aws s3 sync s3://name_backet name_fonder exclude *.tmp # в папку будет скачены файлы из бакета, например, сайт

Теперь, нам нужно скачать плагин работы с AWS:

esschtolts@cloudshell:~/terraform/aws (agile-aleph-203917)$ ./../terraform init | grep success

Terraform has been successfully initialized!

Теперь нам нужно получить доступы к AWS, для того кликаем по имени вашего пользователя шапки WEB-интерфейса, кроме My account, появится пункт My Security Credentials, выбрав который, переходим Access Key > Create New Access Key. Создадим EKS (Elastic Kuberntes Service):

esschtolts@cloudshell:~/terraform/aws (agile-aleph-203917)$ ./../terraform apply

var="token=AKIAJ4SYCNH2XVSHNN3A" -var="key=huEWRslEluynCXBspsul3AkKlinAlR9+MoU1ViY7"

Удаляем все:

$ ../terraform destroy

Создание кластера в GCP

node pool объединение нод в кластер с

resource "google_container_cluster" "primary" {

name = "tf"

location = "us-central1"

$ cat main.tf # состояние конфигурации

terraform {

required_version = "> 0.10.0"

}

terraform {

backend "s3" {

bucket = "foo-terraform"

key = "bucket/terraform.tfstate"

region = "us-east-1"

encrypt = "true"

}

}

$ cat cloud.tf# конфигурации облака

provider "google" {

token = "${var.hcloud_token}"

}


$ cat variables.tf# переменные и получение токенов

variable "hcloud_token" {}


$ cat instances.tf# создание ресурсов

resource "hcloud_server" "server" { ....


$ terraform import aws_acm_certificate.cert arn:aws:acm:eu-central-1:123456789012:certificate/7e7a28d2-163f-4b8f-b9cd-822f96c08d6a

$ terraform init # Инициализация конфигов

$ terraform plan # Проверка действий

$ terraform apply # Запуск действий

Отладка:

essh@kubernetes-master:~/graylog$ sudo docker run name graylog link graylog_mongo:mongo link graylog_elasticsearch:elasticsearch \

p 9000:9000 -p 12201:12201 -p 1514:1514 \

e GRAYLOG_HTTP_EXTERNAL_URI="http://127.0.0.1:9000/" \

d graylog/graylog:3.0

0f21f39192440d9a8be96890f624c1d409883f2e350ead58a5c4ce0e91e54c9d

docker: Error response from daemon: driver failed programming external connectivity on endpoint graylog (714a6083b878e2737bd4d4577d1157504e261c03cb503b6394cb844466fb4781): Bind for 0.0.0.0:9000 failed: port is already allocated.

essh@kubernetes-master:~/graylog$ sudo netstat -nlp | grep 9000

tcp6 0 0 :::9000:::* LISTEN 2505/docker-proxy

essh@kubernetes-master:~/graylog$ docker rm graylog

graylog

essh@kubernetes-master:~/graylog$ sudo docker run name graylog link graylog_mongo:mongo link graylog_elasticsearch:elasticsearch \

p 9001:9000 -p 12201:12201 -p 1514:1514 \

e GRAYLOG_HTTP_EXTERNAL_URI="http://127.0.0.1:9001/" \

d graylog/graylog:3.0

e5aefd6d630a935887f494550513d46e54947f897e4a64b0703d8f7094562875

https://blog.maddevs.io/terrafom-hetzner-a2f22534514b

Для примера, создадим один инстанс:

$ cat aws/provider.tf

provider "aws" {

region = "us-west-1"

}

resource "aws_instance" "my_ec2" {

ami = "${data.aws_ami.ubuntu.id}"

instance_type = "t2.micro"

}


$ cd aws

$ aws configure

$ terraform init

$ terraform apply auto-approve

$ cd ..

provider "aws" {

region = "us-west-1"

}

resource "aws_sqs_queue" "terraform_queue" {

name = "terraform-queue"

delay_seconds = 90

max_message_size = 2048

message_retention_seconds = 86400

receive_wait_time_seconds = 10

}

data "aws_route53_zone" "vuejs_phalcon" {

name = "test.com."

private_zone = true

}

resource "aws_route53_record" "www" {

zone_id = "${data.aws_route53_zone.vuejs_phalcon.zone_id}"

name = "www.${data.aws_route53_zone.selected.name}"

type = "A"

ttl = "300"

records = ["10.0.0.1"]

}

resource "aws_elasticsearch_domain" "example" {

domain_name = "example"

elasticsearch_version = "1.5"

cluster_config {

instance_type = "r4.large.elasticsearch"

}

snapshot_options {

automated_snapshot_start_hour = 23

}

}

resource "aws_eks_cluster" "eks_vuejs_phalcon" {

name = "eks_vuejs_phalcon"

role_arn = "${aws_iam_role.eks_vuejs_phalcon.arn}"


vpc_config {

subnet_ids = ["${aws_subnet.eks_vuejs_phalcon.id}", "${aws_subnet.example2.id}"]

}

}

output "endpoint" {

value = "${aws_eks_cluster.eks_vuejs_phalcon.endpoint}"

}

output "kubeconfig-certificate-authority-data" {

value = "${aws_eks_cluster.eks_vuejs_phalcon.certificate_authority.0.data}"

}

provider "google" {

credentials = "${file("account.json")}"

project = "my-project-id"

region = "us-central1"

}

resource "google_container_cluster" "primary" {

name = "my-gke-cluster"

location = "us-central1"

remove_default_node_pool = true

initial_node_count = 1

master_auth {

username = ""

password = ""

}

}

output "client_certificate" {

value = "${google_container_cluster.primary.master_auth.0.client_certificate}"

}

output "client_key" {

value = "${google_container_cluster.primary.master_auth.0.client_key}"

}

output "cluster_ca_certificate" {

value = "${google_container_cluster.primary.master_auth.0.cluster_ca_certificate}"

}

$ cat deployment.yml

apiVersion: apps/v1

kind: Deployment

metadata:

name: phalcon_vuejs

namespace: development

spec:

selector:

matchLabels:

app: vuejs

replicas: 1

template:

metadata:

labels:

app: vuejs

spec:

initContainers:

 name: vuejs_build

image: vuejs/ci

volumeMounts:

 name: app

mountPath: /app/public

command:

 /bin/bash

 -c

 |

cd /app/public

git clone essch/vuejs_phalcon:1.0 .

npm test

npm build

containers:

 name: healtcheck

image: mileschou/phalcon:7.2-cli

args:

 /bin/sh

 -c

 cd /usr/src/app && git clone essch/app_phalcon:1.0 && touch /tmp/healthy && sleep 10 && php script.php

readinessProbe:

exec:

command:

 cat

 /tmp/healthy

initialDelaySeconds: 5

periodSeconds: 5

livenessProbe:

exec:

command:

 cat

 /tmp/healthy

initialDelaySeconds: 15

periodSeconds: 5

voumes:

 name: app

command:

 cat

 /tmp/healthy

initialDelaySeconds: 5

periodSeconds: 5

livenessProbe:

exec:

command:

 cat

 /tmp/healthy

initialDelaySeconds: 15

periodSeconds: 5

voumes:

 name: app

emptyDir: {}

Вот и мы создали AWS EC2 инстанс. Мы опустили указание ключей, так как AWS API уже авторизован и эта авторизация будет использоваться Terraform.

Также, для использования кода, Terraform поддерживает переменные, данные и модули.

Создадим отдельную сеть:

resource "aws_vpc" "my_vpc" {

cidr_block = "190.160.0.0/16"

instance_target = "default"

}

resource "aws_subnet" "my_subnet" {

vpc_id = "${aws_vpc.my_vpc.id}"

cidr_block = "190.160.1.0/24"

}

$ cat gce/provider.tf

provider "google" {

credentials = "${file("account.json")}"

project = "my-project-id"

region = "us-central1"

}

resource "google_compute_instance" "default" {

name = "test"

machine_type = "n1-standard-1"

zone = "us-central1-a"

}

$ cd gce

$ terraform init

$ terraform apply

$ cd ..

Для распределенной работы поместим состояние в AWS S3 состояние инфраструктуры (так же можно помещать другие данные), но для безопасности в другой регион:

terraform {

backend "s3" {

bucket = "tfstate"

key = "terraform.tfstate"

region = "us-state-2"

}

}

provider "kubernetes" {

host = "https://104.196.242.174"

username = "ClusterMaster"

password = "MindTheGap"

}


resource "kubernetes_pod" "my_pod" {

spec {

container {

image = "Nginx:1.7.9"

name = "Nginx"

port {

container_port = 80

}

}

}

}

Команды:

terraform init # скачивание зависимостей в соответствии с конфигами, проверка их

terraform validate # проверка синтаксиса

terraform plan # детально посмотреть, как будет изменена инфраструктура и почему именно так, например,

будет ли изменена только мета информация у сервиса или будет пересоздан сам сервис, что часто недопустимо для баз данных.

terraform apply # применение изменений

Общая часть для всех провайдеров ядро.

$ which aws

$ aws fonfigure # https://www.youtube.com/watch?v=IxA1IPypzHs


$ cat aws.tf

# https://www.terraform.io/docs/providers/aws/r/instance.html

resource "aws_instance" "ec2instance" {

ami = "${var.ami}"

instance_type = "t2.micro"

}


resource "aws_security_group" "instance_gc" {

}


$cat run.js

export AWS_ACCESS_KEY_ID="anaccesskey"

export AWS_SECRET_ACCESS_KEY="asecretkey"

export AWS_DEFAULT_REGION="us-west-2"

terraform plan

terraform apply


$ cat gce.tf # https://www.terraform.io/docs/providers/google/index.html#

# Google Cloud Platform Provider


provider "google" {

credentials = "${file("account.json")}"

project = "phalcon"

region = "us-central1"

}


#https://www.terraform.io/docs/providers/google/r/app_engine_application.html

resource "google_project" "my_project" {

name = "My Project"

project_id = "your-project-id"

org_id = "1234567"

}


resource "google_app_engine_application" "app" {

project = "${google_project.my_project.project_id}"

location_id = "us-central"

}


# google_compute_instance

resource "google_compute_instance" "default" {

name = "test"

machine_type = "n1-standard-1"

zone = "us-central1-a"


tags = ["foo", "bar"]


boot_disk {

initialize_params {

image = "debian-cloud/debian-9"

}

}


// Local SSD disk

scratch_disk {

}


network_interface {

network = "default"


access_config {

// Ephemeral IP

}

}


metadata = {

foo = "bar"

}


metadata_startup_script = "echo hi > /test.txt"


service_account {

scopes = ["userinfo-email", "compute-ro", "storage-ro"]

}

}


Расширяемость с помощью external resource, в качестве которого может быть скрипт на BASH:

data "external" "python3" {

program = ["Python3"]

}

Создание кластера машин с помощью Terraform

Создание кластера с помощью Terraform рассматривается в Создание инфраструктуры в GCP. Сейчас уделим больше внимания самому кластеру, а не инструментам по его созданию. Создам через панель администратора GCE проект (отображается в шапке интерфейса) node-cluster. Ключ для Kubernetes я скачал IAM и администрирование > Сервисные аккаунты > Создать сервисный аккаунт и при создании выбрал роль Владелец и положил в проект под названием kubernetes_key.JSON:

eSSH@Kubernetes-master:~/node-cluster$ cp ~/Downloads/node-cluster-243923-bbec410e0a83.JSON ./kubernetes_key.JSON

Скачал terraform:

essh@kubernetes-master:~/node-cluster$ wget https://releases.hashicorp.com/terraform/0.12.2/terraform_0.12.2_linux_amd64.zip >/dev/null 2>/dev/null

essh@kubernetes-master:~/node-cluster$ unzip terraform_0.12.2_linux_amd64.zip && rm -f terraform_0.12.2_linux_amd64.zip

Archive: terraform_0.12.2_linux_amd64.zip

inflating: terraform

essh@kubernetes-master:~/node-cluster$ ./terraform version

Terraform v0.12.2


Добавили провайдера GCE и запустил скачивание "драйверов" к нему:

essh@kubernetes-master:~/node-cluster$ cat main.tf

provider "google" {

credentials = "${file("kubernetes_key.json")}"

project = "node-cluster"

region = "us-central1"

}essh@kubernetes-master:~/node-cluster$ ./terraform init


Initializing the backend


Initializing provider plugins

 Checking for available provider plugins

 Downloading plugin for provider "google" (terraform-providers/google) 2.8.0


The following providers do not have any version constraints in configuration,

so the latest version was installed.


To prevent automatic upgrades to new major versions that may contain breaking

changes, it is recommended to add version = "" constraints to the

corresponding provider blocks in configuration, with the constraint strings

suggested below.


* provider.google: version = "~> 2.8"


Terraform has been successfully initialized!

Назад Дальше