Skip to content

Commit 5c45dcb

Browse files
authored
Merge pull request #4 from bmmcwhirt/master
PKI Automation
2 parents d475671 + 5ae37d0 commit 5c45dcb

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-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/cert_data.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"CN": "${CN}",
3+
"key": {
4+
"algo": "$KEY_ALGO",
5+
"size": $KEY_SIZE
6+
},
7+
"names": [{
8+
"C": "${COUNTRY}",
9+
"L": "${LOCALITY}",
10+
"O": "${ORG}",
11+
"OU": "${ORG_UNIT}",
12+
"ST": "${STATE_PROV}"
13+
}]
14+
}

pki_scripts/pki.sh

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
declare -x COUNTRY=""
31+
declare -x STATE_PROV=""
32+
declare -x LOCALITY=""
33+
declare -x ORG=""
34+
declare -x ORG_UNIT=""
35+
declare -x KUBERNETES_PUBLIC_ADDRESS=
36+
declare -x INTERNAL_IP_BASE=172.19.180.
37+
declare -ax NODES=(1 2 3 4)
38+
declare -x KEY_ALGO="rsa"
39+
declare -x 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+
declare -x 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+
cp cert_data.json ~/pki
54+
cd ~/pki
55+
56+
57+
# gen_csr file cn
58+
# E.g. gen_csr admin-csr admin
59+
function gen_csr {
60+
CN=${2} envsubst < ../cert_data.json > ${1}-csr.json
61+
}
62+
63+
# Create the JSON config files.
64+
COUNT=0
65+
for cn in ${CSR_CN[@]}; do
66+
gen_csr ${CSR_FILE[COUNT]} ${cn}
67+
((COUNT=COUNT+1))
68+
done
69+
70+
71+
# Generate the Certificate Authority.
72+
# The ca-config.json has no real variables so it is included.
73+
cfssl gencert -initca ca-csr.json | cfssljson -bare ca
74+
75+
for cert in ${STD[@]}; do
76+
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes $cert-csr.json | cfssljson -bare $cert
77+
done
78+
79+
# Generate node certificates.
80+
for node in ${NODES[*]}; do
81+
INTERNAL_IP=${INTERNAL_IP_BASE}${node}
82+
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}
83+
done
84+
85+
# Generate API certificate.
86+
cfssl gencert \
87+
-ca=ca.pem \
88+
-ca-key=ca-key.pem \
89+
-config=ca-config.json \
90+
-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} \
91+
-profile=kubernetes \
92+
kubernetes-csr.json | cfssljson -bare kubernetes

0 commit comments

Comments
 (0)