terraform借助aws rds数据库在k8s上启动wordpress

Hello readers, in this blog i will be deploying the Wordpress application on Kubernetes locally and using AWS RDS as database for our application by using Terraform.

读者好,在本博客中,我将在本地Kubernetes上部署Wordpress应用程序,并通过Terraform将AWS RDS用作我们应用程序的数据库。

Necessary Requirements:

必要要求:

  1. AWS CLI software configured with a profile. You can get from here.

    使用配置文件配置的AWS CLI软件。 你可以从这里得到

  2. Knowledge of AWS Cloud Computing and Terraform.

    了解AWS云计算和Terraform。
  3. Terraform setup. You can get from here.

    地形设置。 你可以从这里得到

  4. Minikube setup with kubectl. You can install minikube using this guide from here and kubectl from here.

    使用kubectl进行Minikube设置。 您可以从这里使用本指南安装minikube 这里 kubectl。

  5. You can get my code from here.

    你可以从我的代码 在这里

Steps while performing our project:

执行我们的项目的步骤:

  1. Write an Infrastructure as code using terraform, which automatically deploy the Wordpress application.

    使用terraform以代码形式编写基础结构,该结构会自动部署Wordpress应用程序。
  2. From above code , also deploy RDS service for the relational database for Wordpress application.

    从以上代码中,还可以为Wordpress应用程序的关系数据库部署RDS服务。
  3. Also create Load Balancer so that our application is accessible from our workstation.

    还要创建负载均衡器,以便可以从工作站访问我们的应用程序。

Below are the steps how you can successfully execute my code:

以下是成功执行我的代码的步骤:

Step 1: Start your minikube machine.

第1步:启动minikube机器。

minikube start

Step 2: Configure your aws profile with below cmd

步骤2:使用以下cmd配置您的AWS配置文件

aws configure
Image for post

Step 3: Now below will be steps for code completion. If you are not interested then you may skip to Step 4.

步骤3:现在下面是代码完成的步骤。 如果您不感兴趣,则可以跳到步骤4。

  • For providing provider info of kubernetes and aws.

    用于提供kubernetes和aws的提供商信息。
provider "kubernetes" {
  config_context = "minikube"
}


provider "aws" {
   region = "ap-south-1"
   profile = "apeksh"
}
  • For creating rds db instance. In this case db instance will be created in default vpc. To get more options and control according to your desire you can refer here.

    用于创建rds数据库实例。 在这种情况下,数据库实例将在默认vpc中创建。 要根据您的需求获得更多选项和控制,可以在这里参考

resource "aws_db_instance" "DataBase" {
  allocated_storage    = 20
  max_allocated_storage = 100
  storage_type         = "gp2"
  engine               = "mysql"
  engine_version       = "5.7.28"
  instance_class       = "db.t2.micro"
  name                 = var.mysql_db_name
  username             = var.mysql_username
  password             = var.mysql_password
  parameter_group_name = "default.mysql5.7"
  publicly_accessible = true
  skip_final_snapshot = true 


}
  • For creating k8s resources like secrets, deployments and service for load balancer.

    用于为负载均衡器创建k8s资源,例如机密,部署和服务。
resource "kubernetes_secret" "mysql" {
  metadata {
    name = "mysql-pass"
  }


  data = {
    password = var.mysql_password
    username = var.mysql_username
    dbname = var.mysql_db_name
    dbhost= aws_db_instance.DataBase.endpoint
  }
}


resource "kubernetes_deployment" "web_service" {
  depends_on = [aws_db_instance.DataBase]
  metadata {
    name = "wordpressdeployment"
    labels = {
      app = "wordpress"
      tier = "frontend"
    }
  }


  spec {
    replicas = 2


    selector {
      match_labels = {
        app = "wordpress"
        tier = "frontend"
      }
    }


    template {
      metadata {
        labels = {
           app = "wordpress"
           tier = "frontend"
        }
      }


      spec {
        container {
           image = "wordpress"
           name  = "web-app"
           env  {
               name = "WORDPRESS_DB_HOST"
               value_from {
                 secret_key_ref {
                   name = kubernetes_secret.mysql.metadata[0].name
                   key  = "dbhost"
                 }
                }
               }  


           env  {
               name = "WORDPRESS_DB_PASSWORD"
               value_from {
                 secret_key_ref {
                   name = kubernetes_secret.mysql.metadata[0].name
                   key  = "password"
            }
          }
        }
               


           env  {
               name = "WORDPRESS_DB_USER"
                value_from {
                 secret_key_ref {
                   name = kubernetes_secret.mysql.metadata[0].name
                   key  = "username"
                 }
                }
           }


           env  {
               name = "WORDPRESS_DB_NAME"
               value_from {
                 secret_key_ref {
                   name = kubernetes_secret.mysql.metadata[0].name
                   key  = "dbname"
                 }
                }
           }


           port {
            container_port = 80
          }
        }
      }
    }
  }
}


resource "kubernetes_service" "k8s_service" {
  depends_on = [kubernetes_deployment.web_service]
  metadata {
    name = "nodeportsvc"
  }
  spec {
    selector = {
      app = "wordpress"
      tier = "frontend"
    }
    session_affinity = "ClientIP"
    port {
      port        = 8080
      target_port = 80
    }


    type = "NodePort"
  }
}
  • For getting inputs from user like database name, username and password a variables file was created to take input from user while running code.

    为了从数据库名称,用户名和密码等用户获取输入,创建了一个变量文件以在运行代码时从用户获取输入。
variable "mysql_db_name" {
    type = string
    description = "Please enter your database name"
}


variable "mysql_username" {
    type = string
    description = "Please enter your database username"
}


variable "mysql_password" {
    type = string
    description = "Please input mysql password"
}

That’s it for coding part. Now execution part resumes.

仅此而已。 现在执行部分恢复。

Step 4: Go inside directory where your terraform files are present and run

第4步:进入存在terraform文件的目录并运行

terraform init

It will install all the necessary plugins for your code.

它将为您的代码安装所有必需的插件。

Image for post

Step 5: Now run

步骤5:现在执行

terraform apply
Image for post

Then it will ask to enter the database name, username and password and after that it will validate and prompt to enter yes to deploy whole setup.

然后它将要求输入数据库名称,用户名和密码,然后将进行验证并提示输入yes,以部署整个安装程序。

Image for post

Note: Databse creation takes around 5 minutes so be patient.

注意:创建数据库大约需要5分钟,因此请耐心等待。

Step 6: You can verify your k8s infrastructure using

步骤6:您可以使用以下方法验证您的k8s基础架构

kubectl get all
Image for post
Image for post

and AWS RDS database from AWS console.

和来自AWS控制台的AWS RDS数据库。

Image for post

Step 7: Now enter your minikube ip along with Load Balancer port no. in your browser.To get minikube ip use below command

步骤7:现在输入您的minikube ip以及Load Balancer端口号。 在浏览器中。要获取minikube ip,请使用以下命令

minikube ip

and to get port no, you can see from kubectl get all command

并获取端口号,您可以从kubectl中获取所有命令

Image for post

Step 8: After entering correct url, you will be able to see WordPress installation page if all went successfully.

步骤8:输入正确的网址后,如果一切顺利,您将能够看到WordPress安装页面。

Image for post
Image for post
Image for post
Image for post

Step 9: After Logging in, you will be able to see WordPress Dashboard and you can do anything with it.

第9步:登录后,您将能够看到WordPress仪表板,并且可以执行任何操作。

Image for post

Here’s a demo blog published using WordPress

这是使用WordPress发布的演示博客

Image for post

Step 10: For removing all your setup use command

步骤10:删除所有设置使用命令

terraform destroy

then it will prompt to say yes, enter yes to delete your whole setup in one go. Again it will take some time to destroy your whole setup. So be patient.

然后它会提示您说 ,输入是一次删除整个设置。 同样,这将需要一些时间来破坏整个设置。 所以要耐心点。

Image for post
Image for post
Image for post

Note: You can cross-verify your resources by kubectl get all command also don’t forget to turn off your minikube by command minikube stop.

注意:您可以通过kubectl get all命令交叉验证资源,也不要忘记通过命令minikube stop关闭minikube。

Image for post

So that’s it folks for this blog. Hope you liked it. See you in next blog.

就是这个博客的人们。 希望你喜欢。 下一个博客见。

Github repo: https://github.com/Apeksh742/Terraform_for_wordpress_on_k8s_with_rds_db

Github仓库https : //github.com/Apeksh742/Terraform_for_wordpress_on_k8s_with_rds_db

LinkedIn Profile: https://www.linkedin.com/in/apeksh-agarwal-0543bb192/

领英简介: https : //www.linkedin.com/in/apeksh-agarwal-0543bb192/

翻译自: https://medium.com/@apekshagarwal.742/terraform-to-launch-wordpress-on-k8s-with-aws-rds-database-62fa66cd50ec

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是在 AWS EC2 实例中使用 Terraform 部署 Nginx 的步骤: 1. 在 AWS 控制台中创建一个 IAM 用户,并为该用户授权 AWS 访问密钥和安全凭证。 2. 安装 Terraform,并配置 AWS 访问密钥和安全凭证: ``` $ terraform init $ export AWS_ACCESS_KEY_ID="your_access_key_here" $ export AWS_SECRET_ACCESS_KEY="your_secret_key_here" ``` 3. 创建一个 Terraform 项目,并在 main.tf 文件中定义以下资源: ``` provider "aws" { region = "us-west-2" } resource "aws_instance" "nginx" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" tags = { Name = "nginx-server" } provisioner "remote-exec" { inline = [ "sudo apt-get update", "sudo apt-get install -y nginx", ] } connection { type = "ssh" user = "ubuntu" private_key = file("~/.ssh/id_rsa") host = aws_instance.nginx.public_ip } lifecycle { create_before_destroy = true } # Allow HTTP traffic ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } # Allow SSH traffic ingress { from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } # Allow HTTPS traffic ingress { from_port = 443 to_port = 443 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } # Allow ICMP traffic ingress { from_port = -1 to_port = -1 protocol = "icmp" cidr_blocks = ["0.0.0.0/0"] } } ``` 上述代码中定义了一个 AWS EC2 实例和一些安全组规则,以允许 HTTP、SSH、HTTPS 和 ICMP 流量通过。还在 provisioner 部分中安装了 Nginx。 4. 运行 Terraform 命令创建实例: ``` $ terraform apply ``` 5. 在浏览器中输入实例 IP 地址,应该可以看到 Nginx 的欢迎页面。 现在,您已经成功在 AWS EC2 实例中部署了 Nginx,而且使用 Terraform 进行自动化管理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值