You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.7 KiB

  1. ## Kubernetes on AWS with Terraform
  2. **Overview:**
  3. This project will create:
  4. * VPC with Public and Private Subnets in # Availability Zones
  5. * Bastion Hosts and NAT Gateways in the Public Subnet
  6. * A dynamic number of masters, etcd, and worker nodes in the Private Subnet
  7. * even distributed over the # of Availability Zones
  8. * AWS ELB in the Public Subnet for accessing the Kubernetes API from the internet
  9. **Requirements**
  10. - Terraform 0.8.7 or newer
  11. **How to Use:**
  12. - Export the variables for your AWS credentials or edit `credentials.tfvars`:
  13. ```
  14. export TF_VAR_AWS_ACCESS_KEY_ID="www"
  15. export TF_VAR_AWS_SECRET_ACCESS_KEY ="xxx"
  16. export TF_VAR_AWS_SSH_KEY_NAME="yyy"
  17. export TF_VAR_AWS_DEFAULT_REGION="zzz"
  18. ```
  19. - Update `contrib/terraform/aws/terraform.tfvars` with your data. By default, the Terraform scripts use CoreOS as base image. If you want to change this behaviour, see note "Using other distrib than CoreOs" below.
  20. - Create an AWS EC2 SSH Key
  21. - Run with `terraform apply --var-file="credentials.tfvars"` or `terraform apply` depending if you exported your AWS credentials
  22. Example:
  23. ```commandline
  24. terraform apply -var-file=credentials.tfvars
  25. ```
  26. - Terraform automatically creates an Ansible Inventory file called `hosts` with the created infrastructure in the directory `inventory`
  27. - Ansible will automatically generate an ssh config file for your bastion hosts. To connect to hosts with ssh using bastion host use generated ssh-bastion.conf.
  28. Ansible automatically detects bastion and changes ssh_args
  29. ```commandline
  30. ssh -F ./ssh-bastion.conf user@$ip
  31. ```
  32. - Once the infrastructure is created, you can run the kubespray playbooks and supply inventory/hosts with the `-i` flag.
  33. Example (this one assumes you are using CoreOS)
  34. ```commandline
  35. ansible-playbook -i ./inventory/hosts ./cluster.yml -e ansible_user=core -e bootstrap_os=coreos -b --become-user=root --flush-cache
  36. ```
  37. ***Using other distrib than CoreOs***
  38. If you want to use another distribution than CoreOS, you can modify the search filters of the 'data "aws_ami" "distro"' in variables.tf.
  39. For example, to use:
  40. - Debian Jessie, replace 'data "aws_ami" "distro"' in variables.tf with
  41. data "aws_ami" "distro" {
  42. most_recent = true
  43. filter {
  44. name = "name"
  45. values = ["debian-jessie-amd64-hvm-*"]
  46. }
  47. filter {
  48. name = "virtualization-type"
  49. values = ["hvm"]
  50. }
  51. owners = ["379101102735"]
  52. }
  53. - Ubuntu 16.04, replace 'data "aws_ami" "distro"' in variables.tf with
  54. data "aws_ami" "distro" {
  55. most_recent = true
  56. filter {
  57. name = "name"
  58. values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-*"]
  59. }
  60. filter {
  61. name = "virtualization-type"
  62. values = ["hvm"]
  63. }
  64. owners = ["099720109477"]
  65. }
  66. - Centos 7, replace 'data "aws_ami" "distro"' in variables.tf with
  67. data "aws_ami" "distro" {
  68. most_recent = true
  69. filter {
  70. name = "name"
  71. values = ["dcos-centos7-*"]
  72. }
  73. filter {
  74. name = "virtualization-type"
  75. values = ["hvm"]
  76. }
  77. owners = ["688023202711"]
  78. }
  79. **Troubleshooting**
  80. ***Remaining AWS IAM Instance Profile***:
  81. If the cluster was destroyed without using Terraform it is possible that
  82. the AWS IAM Instance Profiles still remain. To delete them you can use
  83. the `AWS CLI` with the following command:
  84. ```
  85. aws iam delete-instance-profile --region <region_name> --instance-profile-name <profile_name>
  86. ```
  87. ***Ansible Inventory doesnt get created:***
  88. It could happen that Terraform doesnt create an Ansible Inventory file automatically. If this is the case copy the output after `inventory=` and create a file named `hosts`in the directory `inventory` and paste the inventory into the file.
  89. **Architecture**
  90. Pictured is an AWS Infrastructure created with this Terraform project distributed over two Availability Zones.
  91. ![AWS Infrastructure with Terraform ](docs/aws_kubespray.png)