-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.yml
193 lines (169 loc) · 11.2 KB
/
main.yml
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
---
- setup:
filter: ansible_default_ipv4
delegate_to: "{{ item }}"
delegate_facts: True
with_items: "{{ groups['etcd'] + groups['masters'] + groups['nodes'] }}"
- set_fact:
cluster_hostname: "{{ cluster_hostname | default(groups['masters'][0]) }}"
cluster_port: "{{ cluster_port | default('6443') }}"
config_path: "{{ config_path | default(lookup('env','HOME')+'/.ktrw') }}"
regenerate_certificates: "{{ regenerate_certificates | default(False) }}"
regenerate_keys: "{{ regenerate_keys | default(False) }}"
certificate_expiry: "{{ certificate_expiry | default(1826) }}"
- set_fact:
cluster_name: "{{ cluster_name | default(cluster_hostname.split('.')[0] | default('kubernetes')) }}"
- set_fact:
cluster_config_path: "{{ config_path }}/{{ cluster_name }}"
- name: Ensure tmp directories exist
file:
state: directory
path: "{{ cluster_config_path }}/{{ item }}"
mode: 0755
with_items:
- config
- pki/master
- pki/etcd
- pki/node
- name: Create openssl config
template:
src: openssl.conf.j2
dest: "{{ cluster_config_path }}/config/openssl.conf"
- name: Generate private keys
openssl_privatekey:
path: "{{ cluster_config_path }}/pki/{{ item }}-key.pem"
size: 2048
force: "{{ regenerate_keys }}"
with_items:
- master/ca
- master/apiserver
- master/admin
- master/etcd
- master/service-account
- master/kube-controller-manager
- master/kube-scheduler
- master/kube-proxy
- master/kubelet-peer
- etcd/ca
- etcd/etcd
- etcd/peer
- name: Change file permissions for private keys
file:
path: "{{ item }}"
mode: 0644
with_fileglob:
- "{{ cluster_config_path }}/pki/master/*-key.pem"
- name: Change file permissions for etcd private keys
file:
path: "{{ item }}"
mode: 0644
with_fileglob:
- "{{ cluster_config_path }}/pki/etcd/*-key.pem"
# CA Certificate
- stat:
path: "{{ cluster_config_path }}/pki/master/ca.pem"
register: result
- name: Generate CA cert
shell: "openssl req -x509 -new -sha256 -nodes -key {{ cluster_config_path }}/pki/master/ca-key.pem -days {{ certificate_expiry }} -out {{ cluster_config_path }}/pki/master/ca.pem -subj '/CN=kubernetes-ca/C=SE/L=Gothenburg/O=Kubernetes/OU=CA/ST=Vastra Gotalands Lan' -extensions v3_ca -config {{ cluster_config_path }}/config/openssl.conf"
when: not result.stat.exists or regenerate_certificates
# etcd CA certificate
- stat:
path: "{{ cluster_config_path }}/pki/etcd/ca.pem"
register: result
- name: Generate etcd CA cert
shell: "openssl req -x509 -new -sha256 -nodes -key {{ cluster_config_path }}/pki/etcd/ca-key.pem -days {{ certificate_expiry }} -out {{ cluster_config_path }}/pki/etcd/ca.pem -subj '/CN=etcd-ca/C=SE/L=Gothenburg/O=Kubernetes/OU=CA/ST=Vastra Gotalands Lan' -extensions v3_ca -config {{ cluster_config_path }}/config/openssl.conf"
when: not result.stat.exists or regenerate_certificates
# kube-apiserver certificates
- stat:
path: "{{ cluster_config_path }}/pki/master/apiserver.pem"
register: result
- name: Generate apiserver cert
shell: "openssl req -new -sha256 -key {{ cluster_config_path }}/pki/master/apiserver-key.pem -subj '/CN=kube-apiserver/C=SE/L=Gothenburg/O=Kubernetes/OU=amimof/ST=Vastra Gotalands Lan' | openssl x509 -req -sha256 -CA {{ cluster_config_path }}/pki/master/ca.pem -CAkey {{ cluster_config_path }}/pki/master/ca-key.pem -CAcreateserial -out {{ cluster_config_path }}/pki/master/apiserver.pem -days {{ certificate_expiry }} -extensions v3_req_apiserver -extfile {{ cluster_config_path }}/config/openssl.conf"
when: not result.stat.exists or regenerate_certificates
# kubelet peer certificates
- stat:
path: "{{ cluster_config_path }}/pki/master/kubelet-peer.pem"
register: result
- name: Generate kubelet-peer cert
shell: "openssl req -new -sha256 -key {{ cluster_config_path }}/pki/master/kubelet-peer-key.pem -subj '/CN=kubelet-peer/C=SE/L=Gothenburg/O=system:masters/OU=amimof/ST=Vastra Gotalands Lan' | openssl x509 -req -sha256 -CA {{ cluster_config_path }}/pki/master/ca.pem -CAkey {{ cluster_config_path }}/pki/master/ca-key.pem -CAcreateserial -out {{ cluster_config_path }}/pki/master/kubelet-peer.pem -days {{ certificate_expiry }} -extensions v3_req_client -extfile {{ cluster_config_path }}/config/openssl.conf"
when: not result.stat.exists or regenerate_certificates
# Admin certificates
- stat:
path: "{{ cluster_config_path }}/pki/master/admin.pem"
register: result
- name: Generate admin cert
shell: "openssl req -new -key {{ cluster_config_path }}/pki/master/admin-key.pem -subj '/CN=Kubernetes/C=SE/L=Gothenburg/O=system:masters/OU=amimof/ST=Vastra Gotalands Lan' | openssl x509 -req -sha256 -CA {{ cluster_config_path }}/pki/master/ca.pem -CAkey {{ cluster_config_path }}/pki/master/ca-key.pem -CAcreateserial -out {{ cluster_config_path }}/pki/master/admin.pem -days {{ certificate_expiry }} -extensions v3_req_client -extfile {{ cluster_config_path }}/config/openssl.conf"
when: not result.stat.exists or regenerate_certificates
# kube-controller-manage certificates
- stat:
path: "{{ cluster_config_path }}/pki/master/kube-controller-manager.pem"
register: result
- name: Generate kube-controller-manager cert
shell: "openssl req -new -sha256 -key {{ cluster_config_path }}/pki/master/kube-controller-manager-key.pem -subj '/CN=system:kube-controller-manager/C=SE/L=Gothenburg/O=system:kube-controller-manager/OU=amimof/ST=Vastra Gotalands Lan' | openssl x509 -req -sha256 -CA {{ cluster_config_path }}/pki/master/ca.pem -CAkey {{ cluster_config_path }}/pki/master/ca-key.pem -CAcreateserial -out {{ cluster_config_path }}/pki/master/kube-controller-manager.pem -days {{ certificate_expiry }} -extensions v3_req_client -extfile {{ cluster_config_path }}/config/openssl.conf"
when: not result.stat.exists or regenerate_certificates
# kube-scheduler certificates
- stat:
path: "{{ cluster_config_path }}/pki/master/kube-scheduler.pem"
register: result
- name: Generate kube-scheduler cert
shell: "openssl req -new -sha256 -key {{ cluster_config_path }}/pki/master/kube-scheduler-key.pem -subj '/CN=system:kube-scheduler/C=SE/L=Gothenburg/O=system:kube-scheduler/OU=amimof/ST=Vastra Gotalands Lan' | openssl x509 -req -sha256 -CA {{ cluster_config_path }}/pki/master/ca.pem -CAkey {{ cluster_config_path }}/pki/master/ca-key.pem -CAcreateserial -out {{ cluster_config_path }}/pki/master/kube-scheduler.pem -days {{ certificate_expiry }} -extensions v3_req_client -extfile {{ cluster_config_path }}/config/openssl.conf"
when: not result.stat.exists or regenerate_certificates
# kube-proxy certificates
- stat:
path: "{{ cluster_config_path }}/pki/master/kube-proxy.pem"
register: result
- name: Generate kube-proxy cert
shell: "openssl req -new -sha256 -key {{ cluster_config_path }}/pki/master/kube-proxy-key.pem -subj '/CN=system:kube-proxy/C=SE/L=Gothenburg/O=system:node-proxier/OU=amimof/ST=Vastra Gotalands Lan' | openssl x509 -req -sha256 -CA {{ cluster_config_path }}/pki/master/ca.pem -CAkey {{ cluster_config_path }}/pki/master/ca-key.pem -CAcreateserial -out {{ cluster_config_path }}/pki/master/kube-proxy.pem -days {{ certificate_expiry }} -extensions v3_req_client -extfile {{ cluster_config_path }}/config/openssl.conf"
when: not result.stat.exists or regenerate_certificates
# service-account certificates
- stat:
path: "{{ cluster_config_path }}/pki/master/service-account.pem"
register: result
- name: Generate service-account cert
shell: "openssl req -new -sha256 -key {{ cluster_config_path }}/pki/master/service-account-key.pem -subj '/CN=service-accounts/C=SE/L=Gothenburg/O=Kubernetes/OU=amimof/ST=Vastra Gotalands Lan' | openssl x509 -req -sha256 -CA {{ cluster_config_path }}/pki/master/ca.pem -CAkey {{ cluster_config_path }}/pki/master/ca-key.pem -CAcreateserial -out {{ cluster_config_path }}/pki/master/service-account.pem -days {{ certificate_expiry }} -extensions v3_req_client -extfile {{ cluster_config_path }}/config/openssl.conf"
when: not result.stat.exists or regenerate_certificates
# etcd certificates
- stat:
path: "{{ cluster_config_path }}/pki/etcd/etcd.pem"
register: result
- name: Generate etcd cert
shell: "openssl req -new -sha256 -key {{ cluster_config_path }}/pki/etcd/etcd-key.pem -subj '/CN=etcd/C=SE/L=Gothenburg/O=Kubernetes/OU=amimof/ST=Vastra Gotalands Lan' | openssl x509 -req -sha256 -CA {{ cluster_config_path }}/pki/etcd/ca.pem -CAkey {{ cluster_config_path }}/pki/etcd/ca-key.pem -CAcreateserial -out {{ cluster_config_path }}/pki/etcd/etcd.pem -days {{ certificate_expiry }} -extensions v3_req_etcd -extfile {{ cluster_config_path }}/config/openssl.conf"
when: not result.stat.exists or regenerate_certificates
# etcd-peer certificates
- stat:
path: "{{ cluster_config_path }}/pki/etcd/peer.pem"
register: result
- name: Generate etcd-peer cert
shell: "openssl req -new -sha256 -key {{ cluster_config_path }}/pki/etcd/peer-key.pem -subj '/CN=etcd-peer/C=SE/L=Gothenburg/O=Kubernetes/OU=amimof/ST=Vastra Gotalands Lan' | openssl x509 -req -sha256 -CA {{ cluster_config_path }}/pki/etcd/ca.pem -CAkey {{ cluster_config_path }}/pki/etcd/ca-key.pem -CAcreateserial -out {{ cluster_config_path }}/pki/etcd/peer.pem -days {{ certificate_expiry }} -extensions v3_req_etcd -extfile {{ cluster_config_path }}/config/openssl.conf"
when: not result.stat.exists or regenerate_certificates
- name: Generate node private keys
openssl_privatekey:
path: "{{ cluster_config_path }}/pki/node/system:node:{{ item }}-key.pem"
size: 2048
force: "{{ regenerate_keys }}"
with_items: "{{ groups['nodes'] }}"
- name: Change file permissions for private keys
file:
path: "{{ item }}"
mode: 0644
with_fileglob:
- "{{ cluster_config_path }}/pki/node/*-key.pem"
# kubelet certificates
- stat:
path: "{{ cluster_config_path }}/pki/node/system:node:{{ item }}.pem"
register: result
with_items: "{{ groups['nodes'] }}"
- name: Generate node certs
shell: "openssl req -new -key {{ cluster_config_path }}/pki/node/system:node:{{ item.1 }}-key.pem -subj '/CN=system:node:{{ hostvars[item.1].inventory_hostname_short }}/C=SE/L=Gothenburg/O=system:nodes/OU=amimof/ST=Vastra Gotalands Lan' | openssl x509 -req -sha256 -CA {{ cluster_config_path }}/pki/master/ca.pem -CAkey {{ cluster_config_path }}/pki/master/ca-key.pem -CAcreateserial -out {{ cluster_config_path }}/pki/node/system:node:{{ item.1 }}.pem -days {{ certificate_expiry }} -extfile <(printf 'subjectAltName=DNS.1:{{ hostvars[item.1].inventory_hostname_short }},DNS.2:{{ hostvars[item.1].inventory_hostname }},IP.1:{{ hostvars[item.1].ansible_default_ipv4.address }}\nbasicConstraints=CA:FALSE\nkeyUsage=critical,digitalSignature,keyEncipherment\nextendedKeyUsage=clientAuth,serverAuth')"
with_indexed_items: "{{ groups['nodes'] }}"
when: not result.results[item.0].stat.exists or regenerate_certificates
args:
executable: /bin/bash
- name: Create kubeconfig
template:
src: admin.kubeconfig
dest: "{{ cluster_config_path }}/kubeconfig"
vars:
certificate_authority_data: "{{ lookup('file', cluster_config_path+'/pki/master/ca.pem') | b64encode }}"
client_certificate_data: "{{ lookup('file', cluster_config_path+'/pki/master/admin.pem') | b64encode }}"
client_key_data: "{{ lookup('file', cluster_config_path+'/pki/master/admin-key.pem') | b64encode }}"