Skip to content

Commit e85c370

Browse files
committed
Initial commit
1 parent d1c09cb commit e85c370

16 files changed

+385
-0
lines changed

README

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Terraform plan for Atomic based k8s cluster
2+
===========================================
3+
4+
Currently only supports AWS
5+
6+
$ terraform apply

k8s.tf

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module "k8s" {
2+
source = "./k8s"
3+
key_name = "k8s"
4+
key_path = "~/.ssh/id_rsa_k8s"
5+
region = "eu-west-1"
6+
servers= "2"
7+
instance_type = "t2.micro"
8+
master_instance_type = "t2.micro"
9+
}
10+
11+
output "master" {
12+
value = "${module.k8s.master_address}"
13+
}
14+
15+
output "workers" {
16+
value = "${module.k8s.worker_addresses}"
17+
}

k8s/main.tf

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
}

k8s/outputs.tf

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
output "master_address" {
2+
value = "${aws_instance.master.0.public_dns}"
3+
}
4+
output "worker_addresses" {
5+
value = ["${join(",", aws_instance.worker.*.public_dns)}"]
6+
}

k8s/scripts/apiserver

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
###
2+
# kubernetes system config
3+
#
4+
# The following values are used to configure the kube-apiserver
5+
#
6+
7+
# The address on the local server to listen to.
8+
KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0"
9+
10+
# The port on the local server to listen on.
11+
# KUBE_API_PORT="--port=8080"
12+
13+
# Port minions listen on
14+
# KUBELET_PORT="--kubelet_port=10250"
15+
16+
# Comma separated list of nodes in the etcd cluster
17+
KUBE_ETCD_SERVERS="--etcd_servers=http://127.0.0.1:2379"
18+
19+
# Address range to use for services
20+
KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16"
21+
22+
# default admission control policies
23+
#KUBE_ADMISSION_CONTROL="--admission_control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota"
24+
KUBE_ADMISSION_CONTROL="--admission_control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"
25+
26+
# Add your own!
27+
KUBE_API_ARGS=""

k8s/scripts/config

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
###
2+
# kubernetes system config
3+
#
4+
# The following values are used to configure various aspects of all
5+
# kubernetes services, including
6+
#
7+
# kube-apiserver.service
8+
# kube-controller-manager.service
9+
# kube-scheduler.service
10+
# kubelet.service
11+
# kube-proxy.service
12+
# logging to stderr means we get it in the systemd journal
13+
KUBE_LOGTOSTDERR="--logtostderr=true"
14+
15+
# journal message level, 0 is debug
16+
KUBE_LOG_LEVEL="--v=2"
17+
18+
# Should this cluster be allowed to run privileged docker containers
19+
KUBE_ALLOW_PRIV="--allow_privileged=false"
20+
21+
# How the controller-manager, scheduler, and proxy find the apiserver
22+
KUBE_MASTER="--master=http://${master_ip}:8080"
23+
###
24+
# The following values are used to configure the kubernetes controller-manager
25+
26+
# defaults from config and apiserver should be adequate
27+
28+
# Add your own!
29+
KUBE_CONTROLLER_MANAGER_ARGS=""

k8s/scripts/controller-manager

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
###
2+
# The following values are used to configure the kubernetes controller-manager
3+
4+
# defaults from config and apiserver should be adequate
5+
6+
# Add your own!
7+
KUBE_CONTROLLER_MANAGER_ARGS=""

k8s/scripts/etcd

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# [member]
2+
ETCD_NAME=default
3+
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
4+
#ETCD_SNAPSHOT_COUNTER="10000"
5+
#ETCD_HEARTBEAT_INTERVAL="100"
6+
#ETCD_ELECTION_TIMEOUT="1000"
7+
ETCD_LISTEN_PEER_URLS="http://0.0.0.0:2380"
8+
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"
9+
#ETCD_MAX_SNAPSHOTS="5"
10+
#ETCD_MAX_WALS="5"
11+
#ETCD_CORS=""
12+
#
13+
#[cluster]
14+
#ETCD_INITIAL_ADVERTISE_PEER_URLS="http://localhost:2380"
15+
# if you use different ETCD_NAME (e.g. test), set ETCD_INITIAL_CLUSTER value for this name, i.e. "test=http://..."
16+
#ETCD_INITIAL_CLUSTER="default=http://localhost:2380"
17+
#ETCD_INITIAL_CLUSTER_STATE="new"
18+
#ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
19+
ETCD_ADVERTISE_CLIENT_URLS="http://0.0.0.0:2379"
20+
#ETCD_DISCOVERY=""
21+
#ETCD_DISCOVERY_SRV=""
22+
#ETCD_DISCOVERY_FALLBACK="proxy"
23+
#ETCD_DISCOVERY_PROXY=""
24+
#
25+
#[proxy]
26+
#ETCD_PROXY="off"
27+
#
28+
#[security]
29+
#ETCD_CERT_FILE=""
30+
#ETCD_KEY_FILE=""
31+
#ETCD_CLIENT_CERT_AUTH="false"
32+
#ETCD_TRUSTED_CA_FILE=""
33+
#ETCD_PEER_CERT_FILE=""
34+
#ETCD_PEER_KEY_FILE=""
35+
#ETCD_PEER_CLIENT_CERT_AUTH="false"
36+
#ETCD_PEER_TRUSTED_CA_FILE=""
37+
#
38+
#[logging]
39+
#ETCD_DEBUG="false"
40+
# examples for -log-package-levels etcdserver=WARNING,security=DEBUG
41+
#ETCD_LOG_PACKAGE_LEVELS=""

k8s/scripts/flannel-config.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Network": "10.20.0.0/16",
3+
"SubnetLen": 24,
4+
"Backend": {
5+
"Type": "vxlan",
6+
"VNI": 1
7+
}
8+
}

k8s/scripts/flanneld

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Flanneld configuration options
2+
3+
# etcd url location. Point this to the server where etcd runs
4+
FLANNEL_ETCD="http://${master_ip}:2379"
5+
6+
# etcd config key. This is the configuration key that flannel queries
7+
# For address range assignment
8+
FLANNEL_ETCD_KEY="/coreos.com/network"
9+
10+
# Any additional options that you want to pass
11+
#FLANNEL_OPTIONS=""

k8s/scripts/kubelet

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
###
2+
# kubernetes kubelet (minion) config
3+
4+
# The address for the info server to serve on (set to 0.0.0.0 or "" for all interfaces)
5+
KUBELET_ADDRESS="--address=0.0.0.0"
6+
7+
# The port for the info server to serve on
8+
# KUBELET_PORT="--port=10250"
9+
10+
# You may leave this blank to use the actual hostname
11+
KUBELET_HOSTNAME=""
12+
13+
# location of the api-server
14+
KUBELET_API_SERVER="--api_servers=http://${master_ip}:8080"
15+
16+
# Add your own!
17+
KUBELET_ARGS="--register-node=true"

k8s/scripts/master.sh

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
sudo cp /tmp/etcd /etc/etcd/etcd.conf
4+
sudo cp /tmp/config /etc/kubernetes/config
5+
sudo cp /tmp/apiserver /etc/kubernetes/apiserver
6+
7+
for s in etcd kube-apiserver kube-controller-manager kube-scheduler; do
8+
sudo systemctl restart $s
9+
sudo systemctl enable $s
10+
sudo systemctl status $s
11+
done
12+
13+
sudo etcdctl set coreos.com/network/config < /tmp/flannel-config.json
14+
15+
sudo systemctl restart flanneld
16+
sudo systemctl enable flanneld
17+
sudo systemctl status flanneld
18+
19+
sudo systemctl reboot

k8s/scripts/proxy

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
###
2+
# kubernetes proxy config
3+
4+
# default config should be adequate
5+
6+
# Add your own!
7+
#KUBE_PROXY_ARGS="--master=http://MASTERIP:8080"

k8s/scripts/scheduler

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
###
2+
# kubernetes scheduler config
3+
4+
# default config should be adequate
5+
6+
# Add your own!
7+
KUBE_SCHEDULER_ARGS=""

k8s/scripts/worker.sh

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
sudo cp /tmp/config /etc/kubernetes/config
4+
sudo cp /tmp/kubelet /etc/kubernetes/kubelet
5+
sudo cp /tmp/proxy /etc/kubernetes/proxy
6+
sudo cp /tmp/flanneld /etc/sysconfig/flanneld
7+
8+
for s in kube-proxy kubelet flanneld; do
9+
sudo systemctl restart $s
10+
sudo systemctl enable $s
11+
sudo systemctl status $s
12+
done
13+
14+
sudo systemctl reboot

0 commit comments

Comments
 (0)