-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-k3s.sh
119 lines (107 loc) · 3.1 KB
/
install-k3s.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash
wait_lb() {
while true
do
curl --output /dev/null --silent -k "https://${k3s_url}:6443"
if [[ "$?" -eq 0 ]]; then
break
fi
sleep 5
echo "wait for LB"
done
}
render_traefik_config(){
cat << 'EOF' > "$TRAEFIK_CONFIG"
--
apiVersion: helm.cattle.io/v1
kind: HelmChartConfig
metadata:
name: traefik
namespace: kube-system
spec:
valuesContent: |-
additionalArguments:
- "--log.level=DEBUG"
- "--certificatesresolvers.letsencrypt.acme.email=roche@sixfeetup.com"
- "--certificatesresolvers.letsencrypt.acme.storage=/data/acme.json"
- "--certificatesresolvers.letsencrypt.acme.tlschallenge=true"
EOF
}
render_staging_issuer(){
STAGING_ISSUER_RESOURCE=$1
cat << 'EOF' > "$STAGING_ISSUER_RESOURCE"
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
name: letsencrypt-staging
namespace: cert-manager
spec:
acme:
# The ACME server URL
server: https://acme-staging-v02.api.letsencrypt.org/directory
# Email address used for ACME registration
email: ${certmanager_email_address}
# Name of a secret used to store the ACME account private key
privateKeySecretRef:
name: letsencrypt-staging
# Enable the HTTP-01 challenge provider
solvers:
- http01:
ingress:
class: traefik
EOF
}
render_prod_issuer(){
PROD_ISSUER_RESOURCE=$1
cat << 'EOF' > "$PROD_ISSUER_RESOURCE"
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
name: letsencrypt-prod
namespace: cert-manager
spec:
acme:
# The ACME server URL
server: https://acme-v02.api.letsencrypt.org/directory
# Email address used for ACME registration
email: ${certmanager_email_address}
# Name of a secret used to store the ACME account private key
privateKeySecretRef:
name: letsencrypt-prod
# Enable the HTTP-01 challenge provider
solvers:
- http01:
ingress:
class: traefik
EOF
}
apt-get update
apt-get install -y software-properties-common unzip git nfs-common jq
DEBIAN_FRONTEND=noninteractive apt-get upgrade -y
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
rm -rf aws awscliv2.zip
echo "Cluster init!"
until (curl -sfL https://get.k3s.io | sh -s - --cluster-init --tls-san ${k3s_url} --tls-san ${k3s_tls_san}); do
echo 'k3s did not install correctly'
sleep 2
done
until kubectl get pods -A | grep 'Running'; do
echo 'Waiting for k3s startup'
sleep 5
done
kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.11.0/cert-manager.yaml
# Wait cert-manager to be ready
until kubectl get pods -n cert-manager | grep 'Running'; do
echo 'Waiting for cert-manager to be ready'
sleep 15
done
render_staging_issuer /root/staging_issuer.yaml
render_prod_issuer /root/prod_issuer.yaml
kubectl create -f /root/prod_issuer.yaml
kubectl create -f /root/staging_issuer.yaml
cp /etc/rancher/k3s/k3s.yaml /root/kubeconfig.yaml
sed -i 's/127.0.0.1/${k3s_url}/' /root/kubeconfig.yaml
sed -i 's/default/ec2dev-cluster/' /root/kubeconfig.yaml
aws secretsmanager update-secret --secret-id ec2dev-kubeconfig --secret-string file:///root/kubeconfig.yaml