vagrant/k8s/kubernetes-setup/master-playbook.yml

174 lines
4.7 KiB
YAML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
- hosts: all
become: true
tasks:
- name: Set timezone to Europe/Paris
community.general.timezone:
name: Europe/Paris
- name: maj fichier hosts
ansible.builtin.blockinfile:
path: /etc/hosts
block: |
192.168.56.10 k8s-master
192.168.56.11 node-1
192.168.56.12 node-2
- name: Forwarding IPv4 and letting iptables see bridged traffic
ansible.builtin.blockinfile:
path: /etc/modules-load.d/k8s.conf
create: yes
block: |
overlay
br_netfilter
- name: charge module overlay
community.general.modprobe:
state: present
name: overlay
- name: charge module overlay et br_netfilter
community.general.modprobe:
state: present
name: br_netfilter
- name: persistance des bridges
ansible.builtin.blockinfile:
path: /etc/sysctl.d/k8s.conf
create: yes
block: |
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
- name: applique les parametres sysctl
command: "sysctl --system"
- name: Recupere get-docker
get_url:
url: "https://get.docker.com"
dest: /tmp/get-docker.sh
- name: lance get-docker - installe docker, containerd ...
command: 'sh /tmp/get-docker.sh'
- name: Add vagrant user to docker group
user:
name: vagrant
group: docker
- name: cree repertoire /etc/containerd
file:
path: /etc/containerd
state: directory
- name: genere config.toml (containerd)
#command: "sudo containerd config default | sudo tee /etc/containerd/config.toml"
shell: "containerd config default | tee /etc/containerd/config.toml"
- name: configure cgroup driver pour systemd (config.toml)
replace:
path: "/etc/containerd/config.toml"
regexp: 'SystemdCgroup = false'
replace: 'SystemdCgroup = true'
backup: yes
- name: redemarre containerd
service:
name: containerd
state: restarted
enabled: yes
- name: Remove swapfile from /etc/fstab
mount:
name: "{{ item }}"
fstype: swap
state: absent
with_items:
- swap
- none
- name: Disable swap
command: swapoff -a
when: ansible_swaptotal_mb > 0
- name: Add an apt signing key for Kubernetes
apt_key:
url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
state: present
- name: Adding apt repository for Kubernetes
apt_repository:
repo: deb https://apt.kubernetes.io/ kubernetes-xenial main
state: present
filename: kubernetes.list
- name: Install Kubernetes binaries
apt:
name: "{{ packages }}"
state: present
update_cache: yes
vars:
packages:
- kubelet
- kubeadm
- kubectl
- name: Cree file kubelet
ansible.builtin.file:
path: /etc/default/kubelet
state: touch
- name: Configure node ip
lineinfile:
path: /etc/default/kubelet
line: KUBELET_EXTRA_ARGS=--node-ip={{ node_ip }}
create: yes
- name: Restart kubelet
service:
name: kubelet
daemon_reload: yes
state: restarted
# - name: nettoie config.toml
# file:
# path: /etc/containerd/config.toml
# state: absent
- name: redemarre containerd
service:
name: containerd
state: restarted
- name: Initialize the Kubernetes cluster using kubeadm
command: kubeadm init --apiserver-advertise-address="{{ node_ip }}" --apiserver-cert-extra-sans="{{ node_ip }}" --node-name k8s-master --pod-network-cidr=192.168.0.0/16
- name: Setup kubeconfig for vagrant user
command: "{{ item }}"
with_items:
- mkdir -p /home/vagrant/.kube
- cp -i /etc/kubernetes/admin.conf /home/vagrant/.kube/config
- chown vagrant:vagrant /home/vagrant/.kube/config
- name: Install calico pod network
become: false
#command: kubectl create -f https://docs.projectcalico.org/v3.24.5/getting-started/kubernetes/installation/hosted/calico.yaml
# command: kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.24.5/manifests/custom-resources.yaml
# command: "kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.24.5/manifests/tigera-operator.yaml"
command: "kubectl create -f https://docs.projectcalico.org/manifests/calico-typha.yaml"
- name: Generate join command
command: "kubeadm token create --print-join-command"
register: join_command
- name: Copy join command to local file
local_action: copy content="{{ join_command.stdout_lines[0] }}" dest="./join-command"
handlers:
- name: docker status
service:
name: docker
state: started