πŸ‘©β€πŸ’»0705[terraform,ansible]

망지·2022λ…„ 7μ›” 5일
0
post-custom-banner

πŸ“Œ terraform - azure[git]

[root@localhost ~]# git clone https://github.com/hali-linux/azure_set.git
[root@localhost azure_set]# terraform output -raw tls_private_key > azure-key.pem
[root@localhost azure_set]# terraform output public_ip_address
"20.214.236.120"
[root@localhost azure_set]# ssh -i azure-key.pem azureuser@20.214.236.120

πŸ“Œ GCP

πŸ“™ CLI

[root@localhost ~]# mkdir gcp_cli && cd $_

[root@localhost gcp_cli]# tee -a /etc/yum.repos.d/google-cloud-sdk.repo << EOM
[google-cloud-cli]
name=Google Cloud CLI
baseurl=https://packages.cloud.google.com/yum/repos/cloud-sdk-el8-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=0
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
       https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOM

[root@localhost gcp_cli]# yum install -y google-cloud-cli
[root@localhost gcp_cli]# gcloud --version
[root@localhost gcp_cli]# gcloud init --console-only

βœ”οΈμ•„λž˜ 링크 μ§„μž…



Do you want to configure a default Compute Region and Zone? (Y/n)?  y
Which Google Compute Engine zone would you like to use as project default?
If you do not specify a zone via a command line flag while working with Compute
Engine resources, the default is assumed.
 
...

 [47] asia-northeast2-a
 [48] asia-northeast2-b
 [49] asia-northeast2-c
 [50] asia-northeast3-a
Did not print [54] options.
Too many options [104]. Enter "list" at prompt to print choices fully.
Please enter numeric choice or text value (must exactly match list item): 50

-> μ™„λ£Œ

# gcloud compute networks create new-vpc --subnet-mode=custom
# gcloud compute networks subnets create new-subnet --network=new-vpc --range=192.168.0.0/16 --region=asia-northeast3
# gcloud compute networks subnets list
# gcloud compute firewall-rules list
# gcloud compute firewall-rules create new-vpc-allow-ssh --allow=tcp:22 --description="Allow incoming traffic on TCP port 22" --direction=INGRESS --network=new-vpc --source-ranges 0.0.0.0/0
# gcloud compute firewall-rules create new-vpc-allow-http --allow=tcp:80 --description="Allow incoming traffic on TCP port 80" --direction=INGRESS --network=new-vpc --source-ranges 0.0.0.0/0
# gcloud compute images list | grep centos-cloud
# gcloud compute images describe centos-7-v20220621 \
    --project=centos-cloud
# gcloud compute machine-types list --filter="zone:( asia-northeast3-a )"
# vi httpd-gcp.txt
#!/bin/bash
yum install -y httpd
systemctl enable --now httpd
echo "Hello GCP CLI" > /var/www/html/index.html

# gcloud compute instances create web01 \
    --image=centos-7-v20220621 \
    --image-project=centos-cloud \
    --machine-type=e2-micro \
    --network=new-vpc \
    --subnet=new-subnet \
    --tags http-server,https-server \
    --zone=asia-northeast3-a \
    --metadata-from-file=startup-script=httpd-gcp.txt

βœ”οΈGCP λΈŒλΌμš°μ €μ—μ„œ 둜그인 - ν”„λ‘œμ νŠΈ ID 확인

!--  μ‚¬μš©μž 이름 μž„μ˜(lovemj)둜 λ‚΄κ°€ λ„£κΈ° --!
[root@localhost gcp_cli]# ssh-keygen -t rsa -f /root/.ssh/lovemj -C lovemj -b 2048 
# vi /root/.ssh/lovemj.pub
lovemj:ssh-rsa ~~ blahblah

# gcloud compute os-login ssh-keys add \
    --key-file=/root/.ssh/lovemj.pub \
    --project=gcp-lovemj2022 \
    --ttl=365d
# gcloud compute instances add-metadata web01 --metadata-from-file ssh-keys=/root/.ssh/lovemj.pub
# gcloud compute instances describe web01
# curl 34.64.48.211
# ssh -i /root/.ssh/lovemj lovemj@34.64.48.211
# gcloud compute instances delete web01
# gcloud compute firewall-rules list
# gcloud compute firewall-rules delete new-vpc-allow-http
# gcloud compute firewall-rules delete new-vpc-allow-ssh
# gcloud compute networks subnets delete new-subnet
# gcloud compute networks delete new-vpc

πŸ“™ terraform - GCP

# git clone https://github.com/hali-linux/gcp_set.git
# vi provider.tf
provider "google" {
  credentials = file("credentials.json")
  project = "gcp-lovemj2022"
  region = "asia-northeast3"
  zone = "asia-northeast3-a"
}

# vi main.tf

resource "google_compute_network" "custom-test" {
  name                    = "new-vpc"
  auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "network-with-private-ip-ranges" {
  name          = "new-subnet"
  ip_cidr_range = "192.168.0.0/16"
  region        = "asia-northeast3"
  network       = google_compute_network.custom-test.id
}

resource "google_compute_instance" "default" {
  name         = "vm-from-terraform"
  machine_type = "e2-micro"
  zone         = "asia-northeast3-a"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }

  network_interface {
    network = "new-vpc"
    subnetwork = "new-subnet"

    access_config {
      // Include this section to give the VM an external ip address
    }
  }

    metadata_startup_script = file("/root/gcp_set/script.txt")

    // Apply the firewall rule to allow external IPs to access this instance
    tags = ["http-server","ssh-server"]
}

resource "google_compute_firewall" "http-server" {
  name    = "default-allow-http-terraform"
  network = "new-vpc"

  allow {
    protocol = "tcp"
    ports    = ["80"]
  }

  // Allow traffic from everywhere to instances with an http-server tag
  source_ranges = ["0.0.0.0/0"]
  target_tags   = ["http-server"]
}

resource "google_compute_firewall" "ssh-server" {
  name    = "default-allow-ssh-terraform"
  network = "new-vpc"

  allow {
    protocol = "tcp"
    ports    = ["22"]
  }

  // Allow traffic from everywhere to instances with an http-server tag
  source_ranges = ["0.0.0.0/0"]
  target_tags   = ["ssh-server"]
}

# vi output.tf
output "ip" {
  value = "${google_compute_instance.default.network_interface.0.access_config.0.nat_ip}"
}






JSON - λ§Œλ“€κΈ° - νŒŒμΌμ΄λ¦„ : credentials.JSON

# terraform init
# terraform plan
# terraform apply
# terraform output ip
# gcloud compute instances add-metadata vm-from-terraform --metadata-from-file ssh-keys=/root/.ssh/lovemj.pub
# ssh -i /root/.ssh/lovemj lovemj@34.64.48.211
# terraform destroy

πŸ“Œ ansible

πŸ“™ 이둠



SSH 접속이 κ°€λŠ₯ν•œ μƒνƒœμ—¬μ•Όν•¨.(μš°λ¦¬κ°€ 원격을 μ ‘μ†ν•΄μ„œ μ„€μΉ˜ν•˜λŠ”κ±°λž‘ 같은 원리)

  • 파이썬이 μžˆμ–΄μ•Όν•¨.

πŸ“™ ansible-server

centos-node01
centos-node02
ununtu-node01
ununtu-node02

πŸ“™ ubuntuμ„œλ²„ 생성

(centosλŠ” 이미 있음)






βœ”οΈλ§Œλ“€κΈ° 클릭
βœ”οΈμƒλ‹¨ λ©”λ‰΄μ—μ„œ μ„€μ • 클릭 - λ””μŠ€ν”Œλ ˆμ΄ : λΉ„λ””μ˜€λ©”λͺ¨λ¦¬ : 9MB(μ΄ˆλ‘μƒ‰μ—μ„œ μ΅œλŒ€ν•œ μž‘κ²Œ) - μ €μž₯μ†Œ : λΉ„μ–΄μžˆμŒμ— ubuntu18.04.4 μ…‹νŒ… - μ˜€λ””μ˜€ : μ‚¬μš©ν•˜κΈ° ν•΄μ œ - λ„€νŠΈμ›Œν¬ : μ–΄λŒ‘ν„°μ— λΈŒλ¦Ώμ§€ - USB : 컨트둀러 μ‚¬μš©μ•ˆν•¨

βœ”οΈ μ–Έμ–΄ english λ‘œμΌ€μ΄μ…˜ asia korea, ν‚€λ³΄λ“œ korea 101/104 - 사이에 뭐 λ‚˜μ˜€λŠ”λ° no - host name: ubuntu κ·ΈλŒ€λ‘œ - μ‚¬μš©μžκ³„μ • μ›ν•˜λŠ” ID ; mj - account ID ;mj - password λ‘λ²ˆμž…λ ₯ - timezone 확인 ν›„ yes or no - partitioning method : use entire disk - λ””μŠ€ν¬ 확인 ν›„ yes - http proxy? blank ( κ·Έλƒ₯ μ—”ν„° ) - no automaitc update - openSSH server 슀페이슀 μ—”ν„°- boot record: yes - continue

βœ”οΈ μ„œλ²„μ§„μž…ν•΄μ„œ ip확인 ν›„ mobaxterm으둜 μ§„μž…

mj@ubuntu:~$ sudo vi /etc/ssh/sshd_config
[sudo] password for mj:

#LoginGraceTime 2m
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

mj@ubuntu:~$ sudo su -
root@ubuntu:~# passwd root
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

mj@ubuntu:~$ sudo systemctl restart sshd

πŸ“™ centos ova둜 κ°€μ Έμ˜€κΈ°

πŸ“™βœ”οΈβœοΈπŸ“’β­οΈπŸ“Œ

πŸ“Œ 기타

⭐️ ssh μ§„μž… 였λ₯˜ νŠΈλŸ¬λΈ” μŠˆνŒ…

[root@localhost gcp_set]# ssh -i /root/.ssh/lovemj lovemj@34.64.48.211
key_load_public: invalid format
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:Jrzlnn4WHpyzdHsEtoi740IoZWtJe2tbygx0zRNh3JM.
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /root/.ssh/known_hosts:4
ECDSA host key for 34.64.48.211 has changed and you have requested strict checking.
Host key verification failed.

-> know_hosts파일이 κΌ¬μ—¬μ„œ 문제 λ°œμƒ μ§€μ›Œμ„œ ν•΄κ²°ν•˜μž.
->μž¬μ§„μž…ν•΄μ„œ yesν•˜λ©΄ λ¬Έμ œμ—†μŒ.

[root@localhost gcp_set]# rm /root/.ssh/known_hosts
profile
κΎΈμ€€νžˆ, μ°¨κ·Όμ°¨κ·Ό
post-custom-banner

0개의 λŒ“κΈ€