From 27e3998cb6e23821cefbadf66879d4179c603201 Mon Sep 17 00:00:00 2001 From: jwfang <54740235@qq.com> Date: Thu, 15 Jun 2017 18:15:52 +0800 Subject: [PATCH] add kube-node to system:nodes group, add system:kube-proxy cert for kube-proxy --- roles/kubernetes/node/tasks/main.yml | 7 +++++-- .../templates/kube-proxy-kubeconfig.yaml.j2 | 18 ++++++++++++++++++ .../templates/manifests/kube-proxy.manifest.j2 | 6 +++--- roles/kubernetes/secrets/files/make-ssl.sh | 11 +++++++++-- .../secrets/tasks/gen_certs_script.yml | 9 ++++++++- 5 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 roles/kubernetes/node/templates/kube-proxy-kubeconfig.yaml.j2 diff --git a/roles/kubernetes/node/tasks/main.yml b/roles/kubernetes/node/tasks/main.yml index f09845f76..e0558f8cd 100644 --- a/roles/kubernetes/node/tasks/main.yml +++ b/roles/kubernetes/node/tasks/main.yml @@ -30,9 +30,12 @@ - name: write the kubecfg (auth) file for kubelet template: - src: node-kubeconfig.yaml.j2 - dest: "{{ kube_config_dir }}/node-kubeconfig.yaml" + src: "{{ item }}-kubeconfig.yaml.j2" + dest: "{{ kube_config_dir }}/{{ item }}-kubeconfig.yaml" backup: yes + with_items: + - node + - kube-proxy notify: restart kubelet tags: kubelet diff --git a/roles/kubernetes/node/templates/kube-proxy-kubeconfig.yaml.j2 b/roles/kubernetes/node/templates/kube-proxy-kubeconfig.yaml.j2 new file mode 100644 index 000000000..cd305b493 --- /dev/null +++ b/roles/kubernetes/node/templates/kube-proxy-kubeconfig.yaml.j2 @@ -0,0 +1,18 @@ +apiVersion: v1 +kind: Config +clusters: +- name: local + cluster: + certificate-authority: {{ kube_cert_dir }}/ca.pem + server: {{ kube_apiserver_endpoint }} +users: +- name: kube-proxy + user: + client-certificate: {{ kube_cert_dir }}/kube-proxy.pem + client-key: {{ kube_cert_dir }}/kube-proxy-key.pem +contexts: +- context: + cluster: local + user: kube-proxy + name: kube-proxy-{{ cluster_name }} +current-context: kube-proxy-{{ cluster_name }} diff --git a/roles/kubernetes/node/templates/manifests/kube-proxy.manifest.j2 b/roles/kubernetes/node/templates/manifests/kube-proxy.manifest.j2 index 9b7d53857..d584bdd7d 100644 --- a/roles/kubernetes/node/templates/manifests/kube-proxy.manifest.j2 +++ b/roles/kubernetes/node/templates/manifests/kube-proxy.manifest.j2 @@ -27,7 +27,7 @@ spec: - --v={{ kube_log_level }} - --master={{ kube_apiserver_endpoint }} {% if not is_kube_master %} - - --kubeconfig={{kube_config_dir}}/node-kubeconfig.yaml + - --kubeconfig={{kube_config_dir}}/kube-proxy-kubeconfig.yaml {% endif %} - --bind-address={{ ip | default(ansible_default_ipv4.address) }} - --cluster-cidr={{ kube_pods_subnet }} @@ -41,7 +41,7 @@ spec: - mountPath: /etc/ssl/certs name: ssl-certs-host readOnly: true - - mountPath: {{kube_config_dir}}/node-kubeconfig.yaml + - mountPath: {{kube_config_dir}}/kube-proxy-kubeconfig.yaml name: "kubeconfig" readOnly: true - mountPath: {{kube_config_dir}}/ssl @@ -60,7 +60,7 @@ spec: {% endif %} - name: "kubeconfig" hostPath: - path: "{{kube_config_dir}}/node-kubeconfig.yaml" + path: "{{kube_config_dir}}/kube-proxy-kubeconfig.yaml" - name: "etc-kube-ssl" hostPath: path: "{{kube_config_dir}}/ssl" diff --git a/roles/kubernetes/secrets/files/make-ssl.sh b/roles/kubernetes/secrets/files/make-ssl.sh index 55ea13d1e..8fec4f314 100755 --- a/roles/kubernetes/secrets/files/make-ssl.sh +++ b/roles/kubernetes/secrets/files/make-ssl.sh @@ -80,6 +80,7 @@ if [ ! -e "$SSLDIR/ca-key.pem" ]; then cat ca.pem >> apiserver.pem fi +# Admins if [ -n "$MASTERS" ]; then for host in $MASTERS; do cn="${host%%.*}" @@ -90,16 +91,22 @@ if [ -n "$MASTERS" ]; then done fi -# Nodes and Admin +# Nodes if [ -n "$HOSTS" ]; then for host in $HOSTS; do cn="${host%%.*}" # node key openssl genrsa -out node-${host}-key.pem 2048 > /dev/null 2>&1 - openssl req -new -key node-${host}-key.pem -out node-${host}.csr -subj "/CN=kube-node-${cn}" > /dev/null 2>&1 + openssl req -new -key node-${host}-key.pem -out node-${host}.csr -subj "/CN=kube-node-${cn}/O=system:nodes" > /dev/null 2>&1 openssl x509 -req -in node-${host}.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out node-${host}.pem -days 3650 > /dev/null 2>&1 done fi +# system:kube-proxy +openssl genrsa -out kube-proxy-key.pem 2048 > /dev/null 2>&1 +openssl req -new -key kube-proxy-key.pem -out kube-proxy.csr -subj "/CN=system:kube-proxy" > /dev/null 2>&1 +openssl x509 -req -in kube-proxy.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out kube-proxy.pem -days 3650 > /dev/null 2>&1 + + # Install certs mv *.pem ${SSLDIR}/ diff --git a/roles/kubernetes/secrets/tasks/gen_certs_script.yml b/roles/kubernetes/secrets/tasks/gen_certs_script.yml index 8df2195bf..0629e3ea5 100644 --- a/roles/kubernetes/secrets/tasks/gen_certs_script.yml +++ b/roles/kubernetes/secrets/tasks/gen_certs_script.yml @@ -69,11 +69,18 @@ 'apiserver-key.pem' ] all_node_certs: "['ca.pem', + 'kube-proxy.pem', + 'kube-proxy-key.pem', {% for node in groups['k8s-cluster'] %} 'node-{{ node }}.pem', 'node-{{ node }}-key.pem', {% endfor %}]" - my_node_certs: ['ca.pem', 'node-{{ inventory_hostname }}.pem', 'node-{{ inventory_hostname }}-key.pem'] + my_node_certs: ['ca.pem', + 'kube-proxy.pem', + 'kube-proxy-key.pem', + 'node-{{ inventory_hostname }}.pem', + 'node-{{ inventory_hostname }}-key.pem' + ] tags: facts - name: Gen_certs | Gather master certs