From 59176ebbb9390e079e95e7e6cd3cc4089c3ff8ca Mon Sep 17 00:00:00 2001 From: rongzhang Date: Sat, 18 Aug 2018 22:05:35 +0800 Subject: [PATCH] Add kubeadm controlplaneEndpoint Nginx LB(default) Other LB by kubeadm controlplane --- inventory/sample/group_vars/all.yml | 2 +- roles/kubernetes/kubeadm/tasks/main.yml | 15 ++++++++++++--- roles/kubernetes/master/tasks/kubeadm-setup.yml | 6 ++++++ .../templates/kubeadm-config.v1alpha1.yaml.j2 | 3 +++ .../templates/kubeadm-config.v1alpha2.yaml.j2 | 3 +++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/inventory/sample/group_vars/all.yml b/inventory/sample/group_vars/all.yml index 65e8c6590..fbb80b5a2 100644 --- a/inventory/sample/group_vars/all.yml +++ b/inventory/sample/group_vars/all.yml @@ -133,4 +133,4 @@ bin_dir: /usr/local/bin #kube_read_only_port: 10255 # Does coreos need auto upgrade, default is true -#coreos_auto_upgrade: true \ No newline at end of file +#coreos_auto_upgrade: true diff --git a/roles/kubernetes/kubeadm/tasks/main.yml b/roles/kubernetes/kubeadm/tasks/main.yml index 6d0441542..c0d436b66 100644 --- a/roles/kubernetes/kubeadm/tasks/main.yml +++ b/roles/kubernetes/kubeadm/tasks/main.yml @@ -70,7 +70,10 @@ regexp: 'server:' line: ' server: {{ kube_apiserver_endpoint }}' backup: yes - when: not is_kube_master and kubeadm_discovery_address != kube_apiserver_endpoint + when: + - kubeadm_config_api_fqdn is not defined + - not is_kube_master + - kubeadm_discovery_address != kube_apiserver_endpoint notify: restart kubelet - name: Update server field in kube-proxy kubeconfig @@ -80,10 +83,16 @@ | {{ bin_dir }}/kubectl replace -f - delegate_to: "{{groups['kube-master']|first}}" run_once: true - when: is_kube_master and kubeadm_discovery_address != kube_apiserver_endpoint + when: + - kubeadm_config_api_fqdn is not defined + - is_kube_master + - kubeadm_discovery_address != kube_apiserver_endpoint - name: Restart all kube-proxy pods to ensure that they load the new configmap shell: "{{ bin_dir }}/kubectl delete pod -n kube-system -l k8s-app=kube-proxy" delegate_to: "{{groups['kube-master']|first}}" run_once: true - when: is_kube_master and kubeadm_discovery_address != kube_apiserver_endpoint + when: + - kubeadm_config_api_fqdn is not defined + - is_kube_master + - kubeadm_discovery_address != kube_apiserver_endpoint diff --git a/roles/kubernetes/master/tasks/kubeadm-setup.yml b/roles/kubernetes/master/tasks/kubeadm-setup.yml index 519c4d65d..69ad06e4f 100644 --- a/roles/kubernetes/master/tasks/kubeadm-setup.yml +++ b/roles/kubernetes/master/tasks/kubeadm-setup.yml @@ -91,6 +91,12 @@ kubeadmConfig_api_version: v1alpha2 when: kubeadm_output.stdout|version_compare('v1.11.0', '>=') +# Nginx LB(default), If kubeadm_config_api_fqdn is defined, use other LB by kubeadm controlPlaneEndpoint. +- name: set kubeadm_config_api_fqdn define + set_fact: + kubeadm_config_api_fqdn: "{{ apiserver_loadbalancer_domain_name|default('lb-apiserver.kubernetes.local') }}" + when: loadbalancer_apiserver is defined + - name: kubeadm | Create kubeadm config template: src: "kubeadm-config.{{ kubeadmConfig_api_version }}.yaml.j2" diff --git a/roles/kubernetes/master/templates/kubeadm-config.v1alpha1.yaml.j2 b/roles/kubernetes/master/templates/kubeadm-config.v1alpha1.yaml.j2 index 401892ca9..237e371d4 100644 --- a/roles/kubernetes/master/templates/kubeadm-config.v1alpha1.yaml.j2 +++ b/roles/kubernetes/master/templates/kubeadm-config.v1alpha1.yaml.j2 @@ -3,6 +3,9 @@ kind: MasterConfiguration api: advertiseAddress: {{ ip | default(ansible_default_ipv4.address) }} bindPort: {{ kube_apiserver_port }} +{% if groups['kube-master'] | length > 1 and kubeadm_config_api_fqdn is defined %} + controlPlaneEndpoint: {{ kubeadm_config_api_fqdn }} +{% endif %} etcd: endpoints: {% for endpoint in etcd_access_addresses.split(',') %} diff --git a/roles/kubernetes/master/templates/kubeadm-config.v1alpha2.yaml.j2 b/roles/kubernetes/master/templates/kubeadm-config.v1alpha2.yaml.j2 index 9a72fde75..09422d0b4 100644 --- a/roles/kubernetes/master/templates/kubeadm-config.v1alpha2.yaml.j2 +++ b/roles/kubernetes/master/templates/kubeadm-config.v1alpha2.yaml.j2 @@ -3,6 +3,9 @@ kind: MasterConfiguration api: advertiseAddress: {{ ip | default(ansible_default_ipv4.address) }} bindPort: {{ kube_apiserver_port }} +{% if groups['kube-master'] | length > 1 and kubeadm_config_api_fqdn is defined %} + controlPlaneEndpoint: {{ kubeadm_config_api_fqdn }} +{% endif %} etcd: external: endpoints: