Init for k8s playbook
parent
a9f8c13e80
commit
9082f00749
|
|
@ -0,0 +1,6 @@
|
||||||
|
----------
|
||||||
|
# В данном плейбуке происходит инициализация кластера K8s.
|
||||||
|
## В каждой таске есть свой тэг, поэтому, если необходимо исключительно присоединить новый воркер к ноде, то можно выполнить команду:
|
||||||
|
### ansible-playbook -i inventory.ini playbook.yml --tags join так это делается?
|
||||||
|
|
||||||
|
----------
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
- name: Configure Kubernetes cluster
|
||||||
|
hosts: all
|
||||||
|
become: yes
|
||||||
|
roles:
|
||||||
|
- role: common-kubernetes
|
||||||
|
tags: common
|
||||||
|
|
||||||
|
- name: Initialize master node
|
||||||
|
hosts: master
|
||||||
|
become: yes
|
||||||
|
roles:
|
||||||
|
- role: kubernetes-master
|
||||||
|
tags: master
|
||||||
|
|
||||||
|
- name: Join worker to cluster
|
||||||
|
hosts: worker
|
||||||
|
become: yes
|
||||||
|
roles:
|
||||||
|
- role: kubernetes-worker
|
||||||
|
tags: join
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
# Kubernetes Cluster Initialization Playbook
|
||||||
|
|
||||||
|
Этот плейбук предназначен для автоматизации настройки и инициализации Kubernetes-кластера. Он включает в себя роли для базовой настройки узлов, инициализации мастер-узла и присоединения worker-узлов к кластеру.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Структура плейбука
|
||||||
|
|
||||||
|
Плейбук состоит из следующих ролей:
|
||||||
|
|
||||||
|
1. **Базовая настройка (`common-kubernetes`):**
|
||||||
|
- Обновление системы.
|
||||||
|
- Отключение swap.
|
||||||
|
- Настройка ядра (модули `overlay` и `br_netfilter`).
|
||||||
|
- Установка необходимых пакетов (kubeadm, kubelet, kubectl, cri-o).
|
||||||
|
|
||||||
|
2. **Инициализация мастер-узла (`master-kubernetes`):**
|
||||||
|
- Инициализация кластера с помощью `kubeadm init`.
|
||||||
|
- Установка сетевого плагина (Flannel).
|
||||||
|
|
||||||
|
3. **Присоединение worker-узлов (`worker-kubernetes`):**
|
||||||
|
- Генерация токена для присоединения на мастер-узле.
|
||||||
|
- Присоединение worker-узлов к кластеру.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Использование
|
||||||
|
|
||||||
|
### Запуск всего плейбука
|
||||||
|
|
||||||
|
Чтобы выполнить все задачи (базовая настройка, инициализация мастер-узла и присоединение worker-узлов), выполните команду:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
ansible-playbook -i inventory.ini k8s.yml
|
||||||
|
```
|
||||||
|
---
|
||||||
|
### Запуск определённой задачи
|
||||||
|
Если необходимо только подключение новой ноды, то необходимо выполнить следующую команду:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
ansible-playbook -i inventory.ini k8s.yml --tags join
|
||||||
|
```
|
||||||
|
И эта команда присоединит все worker ноды
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
---
|
||||||
|
# defaults file for common-kubernetes
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
---
|
||||||
|
# handlers file for common-kubernetes
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
galaxy_info:
|
||||||
|
author: your name
|
||||||
|
description: your role description
|
||||||
|
company: your company (optional)
|
||||||
|
|
||||||
|
# If the issue tracker for your role is not on github, uncomment the
|
||||||
|
# next line and provide a value
|
||||||
|
# issue_tracker_url: http://example.com/issue/tracker
|
||||||
|
|
||||||
|
# Choose a valid license ID from https://spdx.org - some suggested licenses:
|
||||||
|
# - BSD-3-Clause (default)
|
||||||
|
# - MIT
|
||||||
|
# - GPL-2.0-or-later
|
||||||
|
# - GPL-3.0-only
|
||||||
|
# - Apache-2.0
|
||||||
|
# - CC-BY-4.0
|
||||||
|
license: license (GPL-2.0-or-later, MIT, etc)
|
||||||
|
|
||||||
|
min_ansible_version: 2.1
|
||||||
|
|
||||||
|
# If this a Container Enabled role, provide the minimum Ansible Container version.
|
||||||
|
# min_ansible_container_version:
|
||||||
|
|
||||||
|
galaxy_tags: []
|
||||||
|
# List tags for your role here, one per line. A tag is a keyword that describes
|
||||||
|
# and categorizes the role. Users find roles by searching for tags. Be sure to
|
||||||
|
# remove the '[]' above, if you add tags to this list.
|
||||||
|
#
|
||||||
|
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
|
||||||
|
# Maximum 20 tags per role.
|
||||||
|
|
||||||
|
dependencies: []
|
||||||
|
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
|
||||||
|
# if you add dependencies to this list.
|
||||||
|
|
@ -0,0 +1,85 @@
|
||||||
|
|
||||||
|
# Это базовое ядро, необходимое для инициализации кластера K8s
|
||||||
|
# Данную роль необходимо использовать на всех узлах кластера.
|
||||||
|
|
||||||
|
- name: Update and upgrade apt packages
|
||||||
|
apt:
|
||||||
|
update_cache: yes
|
||||||
|
upgrade: dist
|
||||||
|
autoremove: yes
|
||||||
|
- name: Disable swap
|
||||||
|
shell: |
|
||||||
|
swapoff -a
|
||||||
|
sed -i 'swap/d' /etc/fstab
|
||||||
|
- name: Load overlay kernel module
|
||||||
|
modprobe:
|
||||||
|
name: overlay
|
||||||
|
state: present
|
||||||
|
- name: Load br_netfilter kernel module
|
||||||
|
modprobe:
|
||||||
|
name: br_netfilter
|
||||||
|
state: present
|
||||||
|
- name: Ensure overlay is added to /etc/modules
|
||||||
|
lineinfile:
|
||||||
|
path: /etc/modules
|
||||||
|
line: "overlay"
|
||||||
|
create: yes
|
||||||
|
state: present
|
||||||
|
- name: Ensure br_netfilter is added to /etc/modules
|
||||||
|
lineinfile:
|
||||||
|
path: /etc/modules
|
||||||
|
line: "br_netfilter"
|
||||||
|
create: yes
|
||||||
|
state: present
|
||||||
|
- name: Enable ip_forward
|
||||||
|
sysctl:
|
||||||
|
name: net.ipv4.ip_forward
|
||||||
|
value: 1
|
||||||
|
state: present
|
||||||
|
- name: Ensure IPv4 forwarding is enabled permanently
|
||||||
|
lineinfile:
|
||||||
|
path: /etc/sysctl.conf
|
||||||
|
line: "net.ipv4.ip_forward=1"
|
||||||
|
regexp: "^net.ipv4.ip_forward="
|
||||||
|
state: present
|
||||||
|
- name: Install required packages
|
||||||
|
apt:
|
||||||
|
name:
|
||||||
|
- software-properties-common
|
||||||
|
- apt-transport-https
|
||||||
|
- ca-certificates
|
||||||
|
- gnupg2
|
||||||
|
- gpg
|
||||||
|
- curl
|
||||||
|
- iptables
|
||||||
|
state: present
|
||||||
|
- name: Download K8s GPG key
|
||||||
|
shell: |
|
||||||
|
curl -fsSL https://pkgs.k8s.io/core:/stable:/{{ k8s_version }}/deb/Release.key | gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
|
||||||
|
- name: Add K8s repository
|
||||||
|
shell: |
|
||||||
|
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/{{ k8s_version }}/deb/ /' | tee /etc/apt/sources.list.d/kubernetes.list
|
||||||
|
- name: Installing kubectl, kubeadm, kubelet
|
||||||
|
apt:
|
||||||
|
update_cache: yes
|
||||||
|
name:
|
||||||
|
- kubelet
|
||||||
|
- kubeadm
|
||||||
|
- kubectl
|
||||||
|
command: apt-mark hold kubectl kubeadm kubelet
|
||||||
|
- name: Download cri-o GPG key
|
||||||
|
shell: |
|
||||||
|
curl -fsSL https://pkgs.k8s.io/addons:/cri-o:/stable:/{{crio_version}}/deb/Release.key | gpg --dearmor -o /etc/apt/keyrings/cri-o-apt-keyring.gpg
|
||||||
|
- name: Add cri-o repository
|
||||||
|
shell: |
|
||||||
|
echo "deb [signed-by=/etc/apt/keyrings/cri-o-apt-keyring.gpg] https://pkgs.k8s.io/addons:/cri-o:/stable:/$CRIO_VERSION/deb/ /" | tee /etc/apt/sources.list.d/cri-o.list
|
||||||
|
- name: Installing cri-o
|
||||||
|
apt:
|
||||||
|
update_cache: yes
|
||||||
|
name:
|
||||||
|
- cri-o
|
||||||
|
- name: Enable and starting cri-o
|
||||||
|
systemd:
|
||||||
|
name: crio
|
||||||
|
state: started
|
||||||
|
enabled: true
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
localhost
|
||||||
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
- hosts: localhost
|
||||||
|
remote_user: root
|
||||||
|
roles:
|
||||||
|
- common-kubernetes
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
k8s_version = v1.28
|
||||||
|
crio_version = v1.21
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
Role Name
|
||||||
|
=========
|
||||||
|
|
||||||
|
Здесь производится инициализация мастер ноды.
|
||||||
|
|
||||||
|
Requirements
|
||||||
|
------------
|
||||||
|
|
||||||
|
Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.
|
||||||
|
|
||||||
|
Role Variables
|
||||||
|
--------------
|
||||||
|
|
||||||
|
A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.
|
||||||
|
|
||||||
|
Dependencies
|
||||||
|
------------
|
||||||
|
|
||||||
|
A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.
|
||||||
|
-------------
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
---
|
||||||
|
# defaults file for kubernetes-master
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
---
|
||||||
|
# handlers file for kubernetes-master
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
galaxy_info:
|
||||||
|
author: your name
|
||||||
|
description: your role description
|
||||||
|
company: your company (optional)
|
||||||
|
|
||||||
|
# If the issue tracker for your role is not on github, uncomment the
|
||||||
|
# next line and provide a value
|
||||||
|
# issue_tracker_url: http://example.com/issue/tracker
|
||||||
|
|
||||||
|
# Choose a valid license ID from https://spdx.org - some suggested licenses:
|
||||||
|
# - BSD-3-Clause (default)
|
||||||
|
# - MIT
|
||||||
|
# - GPL-2.0-or-later
|
||||||
|
# - GPL-3.0-only
|
||||||
|
# - Apache-2.0
|
||||||
|
# - CC-BY-4.0
|
||||||
|
license: license (GPL-2.0-or-later, MIT, etc)
|
||||||
|
|
||||||
|
min_ansible_version: 2.1
|
||||||
|
|
||||||
|
# If this a Container Enabled role, provide the minimum Ansible Container version.
|
||||||
|
# min_ansible_container_version:
|
||||||
|
|
||||||
|
galaxy_tags: []
|
||||||
|
# List tags for your role here, one per line. A tag is a keyword that describes
|
||||||
|
# and categorizes the role. Users find roles by searching for tags. Be sure to
|
||||||
|
# remove the '[]' above, if you add tags to this list.
|
||||||
|
#
|
||||||
|
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
|
||||||
|
# Maximum 20 tags per role.
|
||||||
|
|
||||||
|
dependencies: []
|
||||||
|
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
|
||||||
|
# if you add dependencies to this list.
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
- name: Init master node for cluster
|
||||||
|
become: yes
|
||||||
|
command: >
|
||||||
|
kubeadm init
|
||||||
|
--pod-network-cidr={{cidr_network}}
|
||||||
|
--apiserver-advertise-address {{host_ip_address}}
|
||||||
|
--control-plane-endpoint {{host_ip_address}}
|
||||||
|
|
||||||
|
# Смотрите в документацию, для того, чтобы указать правильные директории
|
||||||
|
# Данный способ применим только для none-root пользователя
|
||||||
|
|
||||||
|
- name: Ensure .kube directory exists on the target machine
|
||||||
|
command: mkdir -p {{ ansible_user_dir }}/.kube
|
||||||
|
|
||||||
|
- name: Copy kubeconfig from master node to target machine
|
||||||
|
become: yes
|
||||||
|
command: >
|
||||||
|
cp /etc/kubernetes/admin.conf {{ ansible_user_dir }}/.kube/config
|
||||||
|
|
||||||
|
- name: Set correct ownership for kubeconfig
|
||||||
|
become: yes
|
||||||
|
command: >
|
||||||
|
chown {{ ansible_user_id }}:{{ ansible_user_gid }} {{ ansible_user_dir }}/.kube/config
|
||||||
|
- name: Installing network add-on (flannel)
|
||||||
|
command: >
|
||||||
|
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
|
||||||
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
localhost
|
||||||
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
- hosts: localhost
|
||||||
|
remote_user: root
|
||||||
|
roles:
|
||||||
|
- kubernetes-master
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
cidr_network = 10.244.0.0/16
|
||||||
|
host_ip_address = 192.168.2.12
|
||||||
|
ansible_user_dir = /home/user
|
||||||
|
ansible_user_id = 1000
|
||||||
|
ansible_user_gid = 1000
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
Role Name
|
||||||
|
=========
|
||||||
|
|
||||||
|
Здесь воркер присоединяется к кластеру с помощью токена присоединения, который достаётся из мастеры ноды. Для каждого кластера рекомендуется использовать группировку по [master] [worker], а так же самим указывать необходимые значения (как минимум креды)
|
||||||
|
|
||||||
|
Requirements
|
||||||
|
------------
|
||||||
|
|
||||||
|
Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.
|
||||||
|
|
||||||
|
Role Variables
|
||||||
|
--------------
|
||||||
|
|
||||||
|
A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.
|
||||||
|
|
||||||
|
Dependencies
|
||||||
|
------------
|
||||||
|
|
||||||
|
A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.
|
||||||
|
|
||||||
|
Example Playbook
|
||||||
|
----------------
|
||||||
|
|
||||||
|
Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:
|
||||||
|
|
||||||
|
- hosts: servers
|
||||||
|
roles:
|
||||||
|
- { role: username.rolename, x: 42 }
|
||||||
|
|
||||||
|
License
|
||||||
|
-------
|
||||||
|
|
||||||
|
BSD
|
||||||
|
|
||||||
|
Author Information
|
||||||
|
------------------
|
||||||
|
|
||||||
|
An optional section for the role authors to include contact information, or a website (HTML is not allowed).
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
---
|
||||||
|
# defaults file for kubernetes-worker
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
---
|
||||||
|
# handlers file for kubernetes-worker
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
galaxy_info:
|
||||||
|
author: your name
|
||||||
|
description: your role description
|
||||||
|
company: your company (optional)
|
||||||
|
|
||||||
|
# If the issue tracker for your role is not on github, uncomment the
|
||||||
|
# next line and provide a value
|
||||||
|
# issue_tracker_url: http://example.com/issue/tracker
|
||||||
|
|
||||||
|
# Choose a valid license ID from https://spdx.org - some suggested licenses:
|
||||||
|
# - BSD-3-Clause (default)
|
||||||
|
# - MIT
|
||||||
|
# - GPL-2.0-or-later
|
||||||
|
# - GPL-3.0-only
|
||||||
|
# - Apache-2.0
|
||||||
|
# - CC-BY-4.0
|
||||||
|
license: license (GPL-2.0-or-later, MIT, etc)
|
||||||
|
|
||||||
|
min_ansible_version: 2.1
|
||||||
|
|
||||||
|
# If this a Container Enabled role, provide the minimum Ansible Container version.
|
||||||
|
# min_ansible_container_version:
|
||||||
|
|
||||||
|
galaxy_tags: []
|
||||||
|
# List tags for your role here, one per line. A tag is a keyword that describes
|
||||||
|
# and categorizes the role. Users find roles by searching for tags. Be sure to
|
||||||
|
# remove the '[]' above, if you add tags to this list.
|
||||||
|
#
|
||||||
|
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
|
||||||
|
# Maximum 20 tags per role.
|
||||||
|
|
||||||
|
dependencies: []
|
||||||
|
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
|
||||||
|
# if you add dependencies to this list.
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
- name: Generate join token on the master node
|
||||||
|
command: kubeadm token create --print-join-command
|
||||||
|
register: join_command
|
||||||
|
delegate_to: "{{ groups['master'][0] }}"
|
||||||
|
run_once: true
|
||||||
|
changed_when: false
|
||||||
|
|
||||||
|
- name: Set join token as a fact
|
||||||
|
set_fact:
|
||||||
|
join_token: "{{ join_command.stdout }}"
|
||||||
|
run_once: true
|
||||||
|
|
||||||
|
- name: Check if node is already joined
|
||||||
|
command: kubectl get nodes
|
||||||
|
register: kubectl_nodes
|
||||||
|
ignore_errors: yes
|
||||||
|
changed_when: false
|
||||||
|
delegate_to: "{{ groups['master'][0] }}"
|
||||||
|
run_once: true
|
||||||
|
|
||||||
|
- name: Join worker node to the cluster
|
||||||
|
command: "{{ join_token }}"
|
||||||
|
when: >
|
||||||
|
join_token is defined and
|
||||||
|
inventory_hostname not in kubectl_nodes.stdout
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
localhost
|
||||||
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
- hosts: localhost
|
||||||
|
remote_user: root
|
||||||
|
roles:
|
||||||
|
- kubernetes-worker
|
||||||
Loading…
Reference in New Issue