1.3.1

Terraform

image-20250114193421856

一种代码工具,自定义云和本地资源,对文件进行版本控制,重复使用和共享,可使用一直的工作流程在整个生命周期内配置和管理所有基础设施。

Why use Terraform

Simplicity in keeping track of infrastructure 简化基础设施管理

  • 使用声明式语法,可以清晰定义和跟踪基础设施状态。
  • 通过代码文件轻松管理变更历史。

Easier collaboration 更容易的协作

  • 代码存储在版本控制系统(如 Git)中,支持团队共同编辑和审查。
  • 减少人为错误,提升协作效率。

Reproducibility可重复性

  • 相同代码可用于创建开发、测试、生产等多个环境。
  • 确保不同环境的一致性。

Ensure resources are removed确保资源被清理

  • 自动化管理资源生命周期,包括资源销毁。
  • 避免遗留资源浪费成本或引发安全问题。

What Terraform is not

**无法管理和更新基础设施中的代码 ** Does not manage and update code on infrastructure

  • Terraform 专注于基础设施的配置管理,而不负责应用程序代码的部署或更新。

无法更改不可变资源 Does not allow changes to immutable resources

  • 对于标记为不可变的资源(如某些云服务配置),Terraform 无法直接进行修改,需要销毁并重新创建。

**无法管理未定义在 Terraform 文件中的资源 ** Cannot manage resources not defined in Terraform files

  • Terraform 只能管理其配置文件中明确定义的资源,无法识别或管理手动创建的资源。

key Terraform Commands

初始化项目

1
terraform init
  • 下载和配置必要的 providers。
  • 初始化 Terraform 工作目录。

预览计划

1
terraform plan
  • 显示即将执行的更改计划。
  • 可通过 -out 参数保存计

应用更改

1
terraform apply
  • 执行计划,创建或更改资源。

  • 如果之前保存了计划文件:

    1
    terraform apply tfplan

销毁资源

1
terraform destroy
  • 删除所有由当前配置文件管理的资源。

  • 可通过 -auto-approve 跳过确认:

    1
    terraform destroy -auto-approve

1.3.2

格式化

terraform fmt 格式化。这里的project要是在google clouddashboard里面找到的Project ID

image-20250114214029923

image-20250114205945746

设置环境变量(Win)

$env:GOOGLE_CREDENTIALS="H:\data-engineering-zoomcamp\01-docker-terraform\terraform\keys\my-creds.json"

env:GOOGLE_CREDENTIALS=”H:\data-engineering-zoomcamp\01-docker-terraform\hw1\terraform\my-creds.json”

image-20250114210432008

设置完毕后可以通过echo $env:GOOGLE_CREDENTIALS来检查

image-20250114210524803

进行初始化

terraform init

image-20250114210715439

image-20250114210841037

初始化产生的文件,这里的windows版本和我的电脑环境一致

terraform plan

1
2
3
4
5
6
7
8
9
10
11
12
13
14
esource "google_storage_bucket" "demo-bucke" { # demo-bucke 是 local variable name 访问的时候用google_storage_bucket.demo-bucket 来访问
name = "taxi-rides-ny-447211-terra-bucket" # 这个名字必须是唯一的
location = "US"
force_destroy = true

lifecycle_rule {
condition {
age = 3
}
action {
type = "Delete"
}
}
}

使用terraform plan

image-20250114213107226

terraform apply

当运行terraform apply之后会出现下面这个.tfstate文件

image-20250114213219962

image-20250114213824114

image-20250114214214955

terraform destory

摧毁指令,可以通过输入no来退出

image-20250114214142666

![image-20250114214241655](E:\hexo\blog\source_posts\Week1 DE Zoomcamp2.assets\image-20250114214241655.png)

编写.gitignore防止上传隐私文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log
crash.*.log

# Exclude all .tfvars files, which are likely to contain sensitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
*.tfvars
*.tfvars.json

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# Ignore transient lock info files created by terraform apply
.terraform.tfstate.lock.info

# Include override files you do wish to add to version control using negated pattern
# !example_override.tf

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

# Ignore CLI configuration files
.terraformrc
terraform.rc

*.json

1.3.3

类似于创建环境变量,之后引用,例如一下文件:

image-20250114224836878

1.4.1

Google Cloud Platform

image-20250114225557260

创建key

ssh-keygen -t rsa -f gcp -C phoebe -b 2048

image-20250114230002964

上传public key

image-20250114230152846

创建instance(实例)

Advance里面可以选择OS

image-20250115104201419

git bash链接

创建成功实例后,查看external IP然后键入ssh -i ~/.ssh/gcp phoebe@external IPimage-20250115104310020

ssh -i ~/.ssh/gcp phoebe@34.57.229.219

git bash中下载Anaconda安装包

执行 wget https://repo.anaconda.com/archive/Anaconda3-2024.10-1-Linux-x86_64.shimage-20250115104950840

安装Anaconda

bash Anaconda3-2024.10-1-Linux-x86_64.sh

image-20250115105126316

之后按找要求操作,一路Enter和输入yes 会问要不要初始化,也是yes,之后要关闭shell重新打开才能生效

创建config.ssh目录下

1
2
3
4
5
6
7
8
9
10
11
12
phoebe@LiChunNan MINGW64 ~/.ssh
$ cd ./.ssh

phoebe@LiChunNan MINGW64 ~/.ssh
$ ls
gcp gcp.pub id_rsa id_rsa.pub known_hosts known_hosts.old '新建 Microsoft Publisher Document.pub'

phoebe@LiChunNan MINGW64 ~/.ssh
$ touch config

phoebe@LiChunNan MINGW64 ~/.ssh
$ code config

image-20250115105525582

配置ssh对服务器的访问

编写config文件

image-20250115110202664

git bash中执行ssh de-zoomcamp,这里的de-zoomcamp正是我们上面给Host的名字

image-20250115110336311

python

image-20250115125522213

安装docker

image-20250115130223037

1
2
sudo apt-get update
sudo apt-get install docker.io

ssh with vscode

在拓展里下载Remote - SSH

image-20250115125852016

image-20250115125943714

image-20250115130000822

image-20250115131717211

克隆课程

git clone https://github.com/DataTalksClub/data-engineering-zoomcamp.git

image-20250115131802796

进行如下配置,之后重新连接

1
2
3
sudo groupadd docker
sudo gpasswd -a $USER docker
sudo service docker restart

image-20250115132023454

重启之后可以使用docker

image-20250115132239045

使用exit退出

安装docker compose

根目录下

image-20250115132511097

1
2
3
4
mkdir bin
cd bin
wget https://github.com/docker/compose/releases/download/v2.32.3/docker-compose-linux-x86_64 -O docker-compose
chmod +x docker-compose

image-20250115133017677

根目录下

nano .bashrc

进去后到最后一行,输入

export PATH="${HOME}/bin:${PATH}"

之后ctrl+O保存ctrl+X退出

再执行source .bashrc

image-20250115133500597
image-20250115133554678

在环境中运行docker-compose

1
2
3
cd data-engineering-zoomcamp/
cd 01-docker-terraform/2_docker_sql/
docker-compose up -d

image-20250115133703599

根目录安装pgcli

pip install pgcli

image-20250115133903428

输入pgcli -h localhost -U root -d ny_taxi,之后输入密码root即可执行。ctrl+d退出

image-20250115134059156

之后使用pip uninstall pgcli删除,会安装conda pgcli

conda pgcli

由于我遇到了一直在Solving environment的问题,所以使用了

conda update --all --yes

然后

conda install -c conda-forge pgcli

image-20250115135853802

pip install -U mycli

image-20250115145545413

成功登录,跟之前一样

image-20250115184626387

port forward to local machine

image-20250115184519549

image-20250115172051082

这里有个坑,要用winpty pgcli -h localhost -U root -d ny_taxi

视频里没有winpty原因不做深究,可以像之前一样设置变量来解决,不详述

image-20250115173200527

Jupyter Notebook

image-20250115174816705

下载数据wget https://github.com/DataTalksClub/nyc-tlc-data/releases/download/yellow/yellow_tripdata_2021-01.csv.gz

image-20250115174735122

解压数据gzip -d yellow_tripdata_2021-01.csv.gz

image-20250115175122424

之后还要在git bash中输入

1
2
3
sudo apt-get install build-dep python-psycopg2
sudo apt-get install libpq-dev
pip install psycopg2-binary

之后执行文件,再次在instance中查看表格是否创建成功,结果如图

image-20250115175927174

下载terraform

1
2
3
4
5
6
cd
cd bin
wget https://releases.hashicorp.com/terraform/1.10.4/terraform_1.10.4_linux_amd64.zip
sudo apt-get install unzip
unzip terraform_1.10.4_linux_amd64.zip
rm terraform_1.10.4_linux_amd64.zip

image-20250115180232257

image-20250115181152702

Google Cloud SDK Authentication

都在terraform_basic文件夹下进行

1
2
3
4
5
export GOOGLE_APPLICATION_CREDENTIALS=~/.gc/my-creds.json
gcloud auth activate-service-account --key-file $GOOGLE_APPLICATION_CREDENTIALS
terraform init
terraform plan
terraform apply # error here

image-20250115182928731

image-20250115183106407

image-20250115183233133

关闭链接

sudo shutdown now

image-20250115183428622

停止后再启动,需要修改IP

查找新的External IP

image-20250115183704245

修改.ssh/config

image-20250115183757491

启动

ssh de-zoomcamp

1.4.2

本章介绍了如何通过 GitHub Codespace 进行配置,步骤非常简单明了。效果与前序1.2章节基本相同。