我的目标是使用 terraform 从 2 个节点创建一个 HTTP 平衡器,当我想将平衡器和应用程序服务器收到的公共 IP 地址添加到平衡器配置(shell 脚本 deploy-lb.sh)中以进行执行“terraform apply”时的初始配置时,出现了问题。
磅.tf
provisioner "local-exec" {
command = "bash files/get_public_ip.sh"
}
provisioner "file" {
source = "/tmp/lb_host.local"
destination = "/tmp/lb_host.txt"
}
provisioner "file" {
source = "/tmp/app_host.local"
destination = "/tmp/app_host.txt"
}
provisioner "remote-exec" {
script = "files/deploy-lb.sh"
}
输出.tf
output "external_ip_address_app" {
value = yandex_compute_instance.app.network_interface.0.nat_ip_address
}
output "external_ip_address_lb" {
value = yandex_compute_instance.lb.network_interface.0.nat_ip_address
}
获取公共IP地址:
#!/bin/bash
APP_HOST_IP=`/usr/bin/terraform output -raw external_ip_address_app`
LB_HOST_IP=`/usr/bin/terraform output -raw external_ip_address_lb`
echo ${APP_HOST_IP} > /tmp/app_host.local
echo ${LB_HOST_IP} > /tmp/lb_host.local
部署-lb.sh:
#!/bin/bash
APP_HOST=`cat /tmp/app_host.txt`
LB_HOST=`cat /tmp/lb_host.txt`
sudo apt update && sudo apt install nginx && sleep 3
sudo rm /etc/nginx/sites-available/default; sudo touch /etc/nginx/sites-available/load-balancer;
sudo cat > /etc/nginx/sites-available/load-balancer << EOF
upstream backend {
server ${APP_HOST}.sslip.io:9292;
}
server {
listen 80;
server_name ${LB_HOST}.sslip.io;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
EOF
sudo ln -s /etc/nginx/sites-available/load-balancer /etc/nginx/sites-enabled/load-balancer
sudo systemctl restart nginx
文件/tmp/app_host.local和/tmp/lb_host.local包含输出未定义的通知。
│ Warning: No outputs found │ │ The state file either has no outputs defined, or all the defined outputs │ are empty. Please define an output in your configuration with the `output` │ keyword and run `terraform refresh` for it to become available. If you are │ using interpolation, please verify the interpolated value is not empty. You │ can use the `terraform console` command to assist. ╵