|
| 1 | +# A Terraform plan to start a k8s cluster with Atomic |
| 2 | + |
| 3 | +resource "aws_security_group" "k8s" { |
| 4 | + name = "k8s" |
| 5 | + description = "Kubernetes traffic" |
| 6 | + |
| 7 | + ingress { |
| 8 | + from_port = 0 |
| 9 | + to_port = 0 |
| 10 | + protocol = "-1" |
| 11 | + cidr_blocks = ["0.0.0.0/0"] |
| 12 | + } |
| 13 | + |
| 14 | + egress { |
| 15 | + from_port = 0 |
| 16 | + to_port = 0 |
| 17 | + protocol = "-1" |
| 18 | + cidr_blocks = ["0.0.0.0/0"] |
| 19 | + } |
| 20 | + |
| 21 | +} |
| 22 | + |
| 23 | +resource "template_file" "kubelet" { |
| 24 | + template = "${path.module}/scripts/kubelet" |
| 25 | + |
| 26 | + vars { |
| 27 | + master_ip = "${aws_instance.master.private_ip}" |
| 28 | + } |
| 29 | + |
| 30 | + depends_on = ["aws_instance.master"] |
| 31 | +} |
| 32 | + |
| 33 | +resource "template_file" "config" { |
| 34 | + template = "${path.module}/scripts/config" |
| 35 | + |
| 36 | + vars { |
| 37 | + master_ip = "${aws_instance.master.private_ip}" |
| 38 | + } |
| 39 | + |
| 40 | + depends_on = ["aws_instance.master"] |
| 41 | +} |
| 42 | + |
| 43 | +resource "template_file" "flanneld" { |
| 44 | + template = "${path.module}/scripts/flanneld" |
| 45 | + |
| 46 | + vars { |
| 47 | + master_ip = "${aws_instance.master.private_ip}" |
| 48 | + } |
| 49 | + |
| 50 | + depends_on = ["aws_instance.master"] |
| 51 | +} |
| 52 | + |
| 53 | +resource "aws_instance" "master" { |
| 54 | + |
| 55 | + ami = "${lookup(var.master_ami, var.region)}" |
| 56 | + instance_type = "${var.master_instance_type}" |
| 57 | + key_name = "${var.key_name}" |
| 58 | + security_groups = ["${aws_security_group.k8s.name}"] |
| 59 | + |
| 60 | + connection { |
| 61 | + user = "centos" |
| 62 | + key_file = "${var.key_path}" |
| 63 | + } |
| 64 | + |
| 65 | + provisioner "file" { |
| 66 | + source = "${path.module}/scripts/etcd" |
| 67 | + destination = "/tmp/etcd" |
| 68 | + } |
| 69 | + |
| 70 | + provisioner "file" { |
| 71 | + source = "${path.module}/scripts/apiserver" |
| 72 | + destination = "/tmp/apiserver" |
| 73 | + } |
| 74 | + |
| 75 | + provisioner "file" { |
| 76 | + source = "${path.module}/scripts/flannel-config.json" |
| 77 | + destination = "/tmp/flannel-config.json" |
| 78 | + } |
| 79 | + |
| 80 | + provisioner "remote-exec" { |
| 81 | + scripts = [ |
| 82 | + "${path.module}/scripts/master.sh", |
| 83 | + ] |
| 84 | + } |
| 85 | + |
| 86 | + tags { |
| 87 | + Name = "master" |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +resource "aws_instance" "worker" { |
| 92 | + |
| 93 | + depends_on = ["aws_instance.master"] |
| 94 | + |
| 95 | + ami = "${lookup(var.ami, var.region)}" |
| 96 | + instance_type = "${var.instance_type}" |
| 97 | + key_name = "${var.key_name}" |
| 98 | + count = "${var.servers}" |
| 99 | + security_groups = ["${aws_security_group.k8s.name}"] |
| 100 | + |
| 101 | + connection { |
| 102 | + user = "centos" |
| 103 | + key_file = "${var.key_path}" |
| 104 | + } |
| 105 | + |
| 106 | + provisioner "file" { |
| 107 | + source = "${path.module}/scripts/proxy" |
| 108 | + destination = "/tmp/proxy" |
| 109 | + } |
| 110 | + |
| 111 | + provisioner "remote-exec" { |
| 112 | + inline = [ |
| 113 | + "cat <<'EOF' > /tmp/config\n${template_file.config.rendered}\nEOF", |
| 114 | + "cat <<'EOF' > /tmp/kubelet\n${template_file.kubelet.rendered}\nEOF", |
| 115 | + "cat <<'EOF' > /tmp/flanneld\n${template_file.flanneld.rendered}\nEOF" |
| 116 | + ] |
| 117 | + } |
| 118 | + |
| 119 | + provisioner "remote-exec" { |
| 120 | + scripts = [ |
| 121 | + "${path.module}/scripts/worker.sh", |
| 122 | + ] |
| 123 | + } |
| 124 | + |
| 125 | + tags { |
| 126 | + Name = "worker-${count.index}" |
| 127 | + } |
| 128 | +} |
0 commit comments