main.yml 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. - name: TCP Configuration
  2. include_role:
  3. name: tcp
  4. - name: Include utils/controlplane tasks
  5. include_role:
  6. name: utils/controlplane
  7. - name: Get cluster token
  8. shell: /opt/yunion/bin/ocadm token list | cut -d ' ' -f1 | sed -n '2p'
  9. register: K8S_TOKEN
  10. - name: Verify online status
  11. ansible.builtin.debug:
  12. msg: Online status is {{ online_status }} and offline_data_path is {{ offline_data_path }}
  13. - name: loading images
  14. include_role:
  15. name: utils/load-images
  16. when:
  17. - online_status == "offline"
  18. - offline_data_path | length > 0
  19. - name: setup local registry
  20. include_role:
  21. name: registry
  22. when:
  23. - online_status == "offline"
  24. - offline_data_path | length > 0
  25. - docker_insecure_registries is defined
  26. - docker_insecure_registries | length > 0
  27. - name: Tag and push images to local registry. It might take a few minutes...
  28. shell: |
  29. export version_file={{ offline_data_path }}/versions.json
  30. if ! [ -f "$version_file" ]; then
  31. echo "[ERROR] version file $version_file is empty! "
  32. exit
  33. fi
  34. imgs=( $(cat $version_file |jq '.dockers |to_entries[] |.key +":"+ .value' | xargs) )
  35. echo imgs ${imgs[@]}
  36. export registry=$(cat $version_file | jq .registry |xargs)
  37. if [ -z "$registry" ]; then
  38. echo "[ERROR] registry is empty!"
  39. exit
  40. fi
  41. push_and_tag(){
  42. local img_name_version=$1
  43. docker tag registry.cn-beijing.aliyuncs.com/$registry/$img_name_version \
  44. $insecure_registry/$registry/$img_name_version && \
  45. docker push $insecure_registry/$registry/$img_name_version
  46. }
  47. export -f push_and_tag
  48. for i in {{docker_insecure_registries | join(" ")}}
  49. do
  50. docker load -i $i
  51. insecure_registry=$i parallel --jobs $(nproc) push_and_tag ::: "${imgs[@]}"
  52. done
  53. args:
  54. executable: /bin/bash
  55. become: yes
  56. when:
  57. - docker_insecure_registries is defined
  58. - docker_insecure_registries | length > 0
  59. - online_status == "offline"
  60. - offline_data_path is defined
  61. - offline_data_path | length > 0
  62. - name: Pull ocadm images on node
  63. command: "/opt/yunion/bin/ocadm config images pull --image-repository {{ image_repository | default('registry.cn-beijing.aliyuncs.com/yunion')}} --onecloud-version {{ onecloud_version | default('latest') }} --operator-version {{ onecloud_version | default('latest') }}"
  64. register: command_result
  65. changed_when: '"Image is up to date" not in command_result.stdout or "Already exists" not in command_result.stdout'
  66. retries: 3
  67. become: true
  68. delay: 10
  69. until: command_result.rc == 0
  70. when:
  71. - K8S_TOKEN.stdout|length == 0
  72. - online_status == "online"
  73. - name: Check node is init
  74. shell: test -f /etc/kubernetes/kubelet.conf
  75. register: kubelet_result
  76. ignore_errors: yes
  77. changed_when: false
  78. failed_when: false
  79. - block:
  80. - name: Construct controlplane endpoint
  81. set_fact:
  82. controlplane_endpoint: "{{k8s_controlplane_host}}:{{ k8s_controlplane_port | default(6443) }}"
  83. - name: Construct init args
  84. set_fact:
  85. init_args: "init --control-plane-endpoint {{ controlplane_endpoint }} --mysql-host {{ db_host }} --mysql-user {{ db_user }} --mysql-password {{ db_password}}"
  86. - name: Construct init args with db_port config
  87. set_fact:
  88. init_args: "{{ init_args }} --mysql-port {{ db_port }}"
  89. when:
  90. db_port is defined
  91. - name: Construct image repository
  92. set_fact:
  93. init_args: "{{ init_args }} --image-repository {{ image_repository }}"
  94. when:
  95. image_repository is defined
  96. - name: Construct init args apiserver_advertise_address
  97. set_fact:
  98. init_args: "{{ init_args }} --apiserver-advertise-address {{ apiserver_advertise_address }} "
  99. when:
  100. apiserver_advertise_address is defined
  101. - name: Construct init args node_ip
  102. set_fact:
  103. init_args: "{{ init_args }} --node-ip {{ node_ip }}"
  104. when:
  105. node_ip is defined
  106. - name: Set host_networks_options
  107. set_fact:
  108. host_networks_options: "{{ host_networks | join(' --host-networks ') }}"
  109. when:
  110. host_networks is defined
  111. - name: Construct init args host_networks
  112. set_fact:
  113. init_args: "{{ init_args }} --host-networks {{ host_networks_options }} "
  114. when:
  115. host_networks is defined
  116. - name: construct hugepage args {{onecloud_version}}
  117. set_fact:
  118. init_args: "{{ init_args }} --enable-hugepage"
  119. when:
  120. - enable_hugepage is defined
  121. - onecloud_version is defined
  122. - onecloud_version is version('v3.10', ">=")
  123. - name: Construct onecloud version
  124. set_fact:
  125. init_args: "{{ init_args }} --onecloud-version {{ onecloud_version }} --operator-version {{ onecloud_version }}"
  126. when:
  127. onecloud_version is defined
  128. - name: Construct pod-network-cidr
  129. set_fact:
  130. init_args: "{{ init_args }} --pod-network-cidr {{ pod_network_cidr }}"
  131. when:
  132. pod_network_cidr is defined
  133. - name: Construct service-cidr
  134. set_fact:
  135. init_args: "{{ init_args }} --service-cidr {{ service_cidr }}"
  136. when:
  137. service_cidr is defined
  138. - name: Construct service-dns-domain
  139. set_fact:
  140. init_args: "{{ init_args }} --service-dns-domain {{ service_dns_domain }}"
  141. when:
  142. service_dns_domain is defined
  143. - name: Construct onecloud version
  144. set_fact:
  145. init_args: "{{ init_args }} --addon-calico-ip-autodetection-method {{ ip_autodetection_method }}"
  146. when:
  147. ip_autodetection_method is defined
  148. - name: Init ha ip
  149. set_fact:
  150. init_args: "{{ init_args }} --high-availability-vip {{ high_availability_vip }}"
  151. when:
  152. high_availability_vip is defined
  153. - name: Init keepalived version tag
  154. set_fact:
  155. init_args: "{{ init_args }} --keepalived-version-tag {{ keepalived_version_tag }}"
  156. when:
  157. keepalived_version_tag is defined
  158. - name: Init node as onecloud host agent
  159. set_fact:
  160. init_args: "{{ init_args }} --enable-host-agent"
  161. when:
  162. - join_as_host | default(false)|bool == true
  163. - block:
  164. - name: Set cgroup driver to systemd in kubelet.service
  165. lineinfile:
  166. path: /usr/lib/systemd/system/kubelet.service
  167. regexp: '^ExecStart=/usr/bin/kubelet$'
  168. line: 'ExecStart=/usr/bin/kubelet --cgroup-driver=systemd'
  169. become: true
  170. - name: reload systemd
  171. ansible.builtin.systemd:
  172. name: kubelet
  173. state: restarted
  174. daemon_reload: true
  175. become: true
  176. when:
  177. is_openeuler_based | default(false) | bool == true
  178. - name: show init args
  179. debug:
  180. msg: "/opt/yunion/bin/ocadm {{ init_args }}"
  181. - name: Use ocadm init first master node
  182. shell: |
  183. /opt/yunion/bin/ocadm {{ init_args }}
  184. become: true
  185. when: kubelet_result.rc != 0
  186. args:
  187. executable: /bin/bash
  188. - name: "Wait 500 seconds for primary master to response: {{ controlplane_endpoint }}"
  189. wait_for:
  190. host: "{{ k8s_controlplane_host }}"
  191. port: "{{ k8s_controlplane_port | default(6443)}}"
  192. delay: 1
  193. timeout: 500
  194. run_once: yes
  195. - name: Export KUBECONFIG in master's ~/.bashrc
  196. lineinfile:
  197. dest: ~/.bashrc
  198. line: "export KUBECONFIG=/etc/kubernetes/admin.conf"
  199. state: present
  200. create: yes
  201. regexp: '^export KUBECONFIG=.*'
  202. when:
  203. - shell is undefined or shell == 'bash'
  204. - name: Source kubectl bash completion in master's ~/.bashrc
  205. lineinfile:
  206. dest: ~/.bashrc
  207. line: "source <(kubectl completion bash)"
  208. state: present
  209. create: yes
  210. regexp: '.*kubectl completion bash.*'
  211. when:
  212. - shell is undefined or shell == 'bash'
  213. - name: Include utils/k8s/kubelet/extra-args tasks
  214. include_role:
  215. name: utils/k8s/kubelet/extra-args