Skip to content

Commit 419102c

Browse files
committed
Add pki automation.
1 parent d475671 commit 419102c

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

pki_scripts/ca-config.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"signing": {
3+
"default": {
4+
"expiry": "8760h"
5+
},
6+
"profiles": {
7+
"kubernetes": {
8+
"usages": ["signing", "key encipherment", "server auth", "client auth"],
9+
"expiry": "8760h"
10+
}
11+
}
12+
}
13+
}

pki_scripts/pki.sh

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/bin/bash
2+
######################################################
3+
# Tutorial Author: Abel Perez Martinez
4+
# Tutorial: https://github.com/abelperezok/kubernetes-raspberry-pi-cluster-hat
5+
# Script Author: Bryan McWhirt
6+
# Description:
7+
# This is a bash script that automates the
8+
# creation of the PKI. Make sure you understand
9+
# what is being done or you will run into issues.
10+
# I wrote this as it was tedious to do every time
11+
# I started over to experiment.
12+
#
13+
# Usage:
14+
# Fill in your information for:
15+
# COUNTRY, STATE_PROV, LOCALITY, ORG, ORG_UNIT,
16+
# KUBERNETES_PUBLIC_ADDRESS
17+
# Verify you INTERNAL_IP_BASE matches the one here.
18+
# Abel's documentation uses 172.19.181. but mine
19+
# was 172.19.180.
20+
#
21+
# Copy this file and ca-config.json to ~/ on the
22+
# orchistrator node.
23+
#
24+
# chmod 740 ~/pki.sh
25+
#
26+
# cd ~
27+
#
28+
# ./pki.sh
29+
######################################################
30+
COUNTRY=""
31+
STATE_PROV=""
32+
LOCALITY=""
33+
ORG=""
34+
ORG_UNIT=""
35+
KUBERNETES_PUBLIC_ADDRESS=
36+
INTERNAL_IP_BASE=172.19.180.
37+
declare -ax NODES=(1 2 3 4)
38+
KEY_ALGO="rsa"
39+
KEY_SIZE=2048
40+
declare -ax CSR_FILE=(ca admin p1 p2 p3 p4\
41+
kube-controller-manager kube-proxy\
42+
kube-scheduler kubernetes service-account)
43+
declare -ax CSR_CN=(Kubernetes admin system:node:p1\
44+
system:node:p2 system:node:p3 system:node:p4\
45+
system:kube-controller-manager system:kube-proxy\
46+
system:kube-scheduler kubernetes service-accounts)
47+
48+
KUBERNETES_HOSTNAMES=kubernetes,kubernetes.default,kubernetes.default.svc,kubernetes.default.svc.cluster,kubernetes.svc.cluster.local
49+
50+
# Make the pki directory and copy in the ca config.
51+
mkdir -p ~/pki
52+
cp ca-config.json ~/pki
53+
cd ~/pki
54+
55+
56+
# gen_csr file cn
57+
# E.g. gen_csr admin-csr admin
58+
function gen_csr {
59+
CN=${2} envsubst < ../cert_data.json > ${1}-csr.json
60+
}
61+
62+
# Create the JSON config files.
63+
COUNT=0
64+
for cn in ${CSR_CN[@]}; do
65+
gen_csr ${CSR_FILE[COUNT]} ${cn}
66+
((COUNT=COUNT+1))
67+
done
68+
69+
70+
# Generate the Certificate Authority.
71+
# The ca-config.json has no real variables so it is included.
72+
cfssl gencert -initca ca-csr.json | cfssljson -bare ca
73+
74+
for cert in ${STD[@]}; do
75+
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes $cert-csr.json | cfssljson -bare $cert
76+
done
77+
78+
# Generate node certificates.
79+
for node in ${NODES[*]}; do
80+
INTERNAL_IP=172.19.181.${node}
81+
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -hostname=p${node},${INTERNAL_IP} -profile=kubernetes p${node}-csr.json | cfssljson -bare p${node}
82+
done
83+
84+
# Generate API certificate.
85+
cfssl gencert \
86+
-ca=ca.pem \
87+
-ca-key=ca-key.pem \
88+
-config=ca-config.json \
89+
-hostname=10.32.0.1,${INTERNAL_IP_BASE}254,rpi-k8s-master,rpi-k8s-master.local,${KUBERNETES_PUBLIC_ADDRESS},127.0.0.1,${KUBERNETES_HOSTNAMES} \
90+
-profile=kubernetes \
91+
kubernetes-csr.json | cfssljson -bare kubernetes

0 commit comments

Comments
 (0)