Skip to content

Commit a602bbb

Browse files
committed
way too much
1 parent 4a11e3b commit a602bbb

File tree

7 files changed

+90
-48
lines changed

7 files changed

+90
-48
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.tfstate
22
*.tfstate.backup
33
terraform.tfvars
4-
etcd_discovery_url.txt
4+
etcd_discovery_url.txt
5+
kube_token.txt

Makefile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
plan: etcd_discovery_url.txt
1+
plan: etcd_discovery_url.txt kube_token.txt
22
terraform plan
33

44
etcd_discovery_url.txt:
55
curl -s https://discovery.etcd.io/new?size=3 > etcd_discovery_url.txt
66

77
destroy:
88
terraform destroy
9-
rm etcd_discovery_url.txt
9+
rm etcd_discovery_url.txt kube_token.txt
1010

11-
apply: etcd_discovery_url.txt
11+
apply: etcd_discovery_url.txt kube_token.txt
1212
terraform apply
13+
14+
kube_token.txt:
15+
openssl rand -base64 8 |md5 |head -c8 > kube_token.txt
16+
echo >> kube_token.txt

README.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,15 @@ When you create a cluster, it will output something like:
4545

4646
```
4747
Outputs:
48-
49-
kubernetes-api-server = http://<IP>:8080
50-
```
51-
52-
You can use this to configure `kubectl`:
53-
54-
```
55-
kubectl config set-cluster aws-test --insecure-skip-tls-verify=true --server=$kubernetes-api-server
56-
kubectl config set-context aws-test --cluster=aws-test
57-
kubectl config use-context aws-test
48+
kubernetes-api-server =
49+
# Use these commands to configure kubectl
50+
kubectl config set-cluster testing --insecure-skip-tls-verify=true --server=IP
51+
kubectl config set-credentials admin --token='4c98e411'
52+
kubectl config set-context testing --cluster= testing --user=admin
53+
kubectl config use-context testing
5854
```
5955

60-
where $kubernetes-api-server is the url from the output.
61-
56+
Run these commands to configure `kubectl`. You can see these commands again by running `terraform output kubernetes-api-server`
6257

6358
Test this by running `kubectl get nodes`
6459

kubernetes.tf

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,30 @@ resource "aws_security_group" "kubernetes" {
5454
}
5555
}
5656

57-
resource "aws_security_group_rule" "allow_all" {
57+
resource "aws_security_group_rule" "allow_ssh" {
58+
type = "ingress"
59+
from_port = 22
60+
to_port = 22
61+
protocol = "tcp"
62+
cidr_blocks = ["0.0.0.0/0"]
63+
security_group_id = "${aws_security_group.kubernetes.id}"
64+
}
65+
66+
resource "aws_security_group_rule" "allow_kube_api" {
67+
type = "ingress"
68+
from_port = 6443
69+
to_port = 6443
70+
protocol = "tcp"
71+
cidr_blocks = ["0.0.0.0/0"]
72+
security_group_id = "${aws_security_group.kubernetes.id}"
73+
}
74+
75+
resource "aws_security_group_rule" "allow_all_cluster" {
5876
type = "ingress"
5977
from_port = 0
6078
to_port = 65535
6179
protocol = "-1"
62-
cidr_blocks = ["0.0.0.0/0"]
80+
source_security_group_id = "${aws_security_group.kubernetes.id}"
6381
security_group_id = "${aws_security_group.kubernetes.id}"
6482
}
6583

@@ -76,13 +94,20 @@ resource "template_file" "kubernetes" {
7694
filename = "templates/kubernetes.sh"
7795

7896
vars = {
79-
etcd_dicovery_url = "${replace(file("etcd_discovery_url.txt"), "/\n*/", "")}"
97+
etcd_dicovery_url = "${replace(file("etcd_discovery_url.txt"), "/\n/", "")}"
8098
containers_cidr = "${var.containers_cidr}"
8199
kubernetes_version = "${var.kubernetes_version}"
82100
portal_net = "${var.portal_net}"
83101
}
84102
}
85103

104+
resource "template_file" "tokens" {
105+
filename = "templates/tokens.csv"
106+
107+
vars = {
108+
token = "${replace(file("kube_token.txt"), "/\n/", "")}"
109+
}
110+
}
86111

87112
resource "aws_instance" "etcd" {
88113
ami = "${var.ami}"
@@ -147,6 +172,9 @@ resource "aws_instance" "master" {
147172

148173
provisioner "remote-exec" {
149174
inline = [
175+
"cat <<'EOF' > /tmp/tokens.csv\n${template_file.tokens.rendered}\nEOF",
176+
"sudo mkdir -p mkdir /etc/kubernetes",
177+
"sudo mv /tmp/tokens.csv /etc/kubernetes/tokens.csv",
150178
"echo 'PRIVATE_IP=${self.private_ip}' > /tmp/network.env",
151179
"echo 'PUBLIC_IP=${self.public_ip}' >> /tmp/network.env",
152180
"sudo mv /tmp/network.env /etc/network.env",
@@ -172,7 +200,7 @@ resource "aws_instance" "worker" {
172200
}
173201

174202
tags {
175-
Name = "kubernetes-${var.cluster_name}-master"
203+
Name = "kubernetes-${var.cluster_name}-worker"
176204
Cluster = "${var.cluster_name}"
177205
Role = "worker"
178206
}
@@ -195,7 +223,17 @@ resource "aws_instance" "worker" {
195223
}
196224
}
197225

226+
resource "template_file" "kubectl-config" {
227+
filename = "templates/kubectl-config.sh"
228+
vars = {
229+
cluster_name = "${var.cluster_name}"
230+
token = "${replace(file("kube_token.txt"), "/\n/", "")}"
231+
server = "${aws_instance.master.public_ip}"
232+
}
233+
}
198234

199235
output "kubernetes-api-server" {
200-
value = "http://${aws_instance.master.public_ip}:8080"
236+
value = "${template_file.kubectl-config.rendered}"
201237
}
238+
239+

scripts/master.sh

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ cat <<EOF > /etc/systemd/system/etcd2.service.d/50-etcd.conf
1717
[Service]
1818
Environment=ETCD_PROXY=on
1919
EnvironmentFile=/etc/kubernetes.env
20-
EOF
2120
22-
start etcd2
21+
[Install]
22+
WantedBy=multi-user.target
23+
EOF
2324

2425
mkdir -p /opt/bin
2526

@@ -29,25 +30,6 @@ chmod +x /opt/bin/install-kubernetes
2930
cp $DIR/wupiao /opt/bin/wupiao
3031
chmod +x /opt/bin/wupiao
3132

32-
/opt/bin/wupiao http://127.0.0.1:2379/v2/members
33-
34-
mkdir -p /etc/systemd/system/flanneld.service.d
35-
cat <<EOF > /etc/systemd/system/flanneld.service.d/50-network-config.conf
36-
[Unit]
37-
Requires=etcd2.service
38-
After=etcd2.service
39-
40-
[Service]
41-
EnvironmentFile=/etc/kubernetes.env
42-
ExecStartPre=/opt/bin/wupiao http://127.0.0.1:2379/v2/members
43-
ExecStartPre=/usr/bin/etcdctl --no-sync set /coreos.com/network/config '{ "Network": "${KUBERNETES_CONTAINERS_CIDR}", "Backend":{"Type": "vxlan"} }'
44-
45-
[Install]
46-
WantedBy=multi-user.target
47-
EOF
48-
49-
start flanneld
50-
start docker
5133

5234
cat <<EOF > /etc/systemd/system/install-kubernetes.service
5335
[Unit]
@@ -70,8 +52,8 @@ cat <<EOF > /etc/systemd/system/kube-apiserver.service
7052
[Unit]
7153
Description=Kubernetes API Server
7254
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
73-
After=install-kubernetes.service
74-
Requires=install-kubernetes.service
55+
After=install-kubernetes.service docker.service flanneld.service
56+
Requires=install-kubernetes.service docker.service flanneld.service
7557
7658
[Service]
7759
EnvironmentFile=/etc/kubernetes.env
@@ -82,6 +64,7 @@ ExecStart=/opt/bin/kube-apiserver \
8264
--etcd-servers=http://127.0.0.1:2379 \
8365
--logtostderr=true \
8466
--insecure-port=8080 \
67+
--token_auth_file=/etc/kubernetes/tokens.csv \
8568
--v=2 \
8669
--portal-net=${KUBERNETES_PORTAL_NET}
8770
Restart=on-failure
@@ -91,7 +74,20 @@ RestartSec=10
9174
WantedBy=multi-user.target
9275
EOF
9376

94-
start kube-apiserver
77+
mkdir -p /etc/systemd/system/flanneld.service.d
78+
cat <<EOF > /etc/systemd/system/flanneld.service.d/50-network-config.conf
79+
[Unit]
80+
Requires=etcd2.service
81+
After=etcd2.service
82+
83+
[Service]
84+
EnvironmentFile=/etc/kubernetes.env
85+
ExecStartPre=/opt/bin/wupiao http://127.0.0.1:2379/v2/members
86+
ExecStartPre=/usr/bin/etcdctl --no-sync set /coreos.com/network/config '{ "Network": "${KUBERNETES_CONTAINERS_CIDR}", "Backend":{"Type": "vxlan"} }'
87+
88+
[Install]
89+
WantedBy=multi-user.target
90+
EOF
9591

9692
cat <<EOF > /etc/systemd/system/kube-scheduler.service
9793
[Unit]
@@ -114,8 +110,6 @@ RestartSec=10
114110
WantedBy=multi-user.target
115111
EOF
116112

117-
start kube-scheduler
118-
119113
cat <<EOF > /etc/systemd/system/kube-controller-manager.service
120114
[Unit]
121115
Description=Kubernetes Controller Manager
@@ -138,4 +132,6 @@ RestartSec=10
138132
WantedBy=multi-user.target
139133
EOF
140134

141-
start kube-controller-manager
135+
for S in etcd2 flanneld kube-apiserver kube-scheduler kube-controller-manager; do
136+
start $S
137+
done

templates/kubectl-config.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
# Use these commands to configure kubectl
3+
kubectl config set-cluster ${cluster_name} --insecure-skip-tls-verify=true --server=https://${server}:6443
4+
kubectl config set-credentials admin --token='${token}'
5+
kubectl config set-context ${cluster_name} --cluster=${cluster_name} --user=admin
6+
kubectl config use-context ${cluster_name}
7+

templates/tokens.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
${token},admin,admin

0 commit comments

Comments
 (0)