Fini le TP

This commit is contained in:
tuanvu
2026-02-05 22:04:05 +01:00
parent ae5c2bec37
commit d77a028d23
6 changed files with 136 additions and 3 deletions

View File

@@ -32,6 +32,11 @@
owner: "{{ app_user }}" owner: "{{ app_user }}"
mode: '0755' mode: '0755'
- name: Create document root
file:
path: "/var/www/{{ http_host }}"
dest: "/var/www/{{ http_host }}/info.php"
- name: Set up Apache virtualhost - name: Set up Apache virtualhost
template: template:
src: "files/apache.conf.j2" src: "files/apache.conf.j2"
@@ -82,6 +87,11 @@
src: "files/info.php.j2" src: "files/info.php.j2"
dest: "/var/www/{{ http_host }}/info.php" dest: "/var/www/{{ http_host }}/info.php"
- name: Move from html/index.html to demotlc.html
command: mv /var/www/{{ http_html }}/index.html /var/www/{{ http_host }}/index.html
args:
create: /var/www/{{ http_host }}/index.html
handlers: handlers:
- name: Reload Apache - name: Reload Apache
service: service:

View File

@@ -4,4 +4,5 @@ app_user: "vagrant"
http_host: "demotlc" http_host: "demotlc"
http_conf: "demotlc.conf" http_conf: "demotlc.conf"
http_port: "80" http_port: "80"
http_html: "html"
disable_default: true disable_default: true

17
objectif2/hosts Normal file
View File

@@ -0,0 +1,17 @@
[controlnode]
microk8s-controller ansible_ssh_host=192.168.56.141
[workernode]
microk8s-node1 ansible_ssh_host=192.168.56.142
[all:vars]
ansible_connection=ssh
ansible_ssh_user=vagrant
ansible_ssh_user=vagrant
ansible_ssh_pass=vagrant
ansible_become_pass=vagrant
ansible_ssh_private_key_file=~/.ssh/id_ed25519
[controlnode:vars]
ansible_python_interpreter=/usr/bin/python3
[workernode:vars]

105
objectif2/playbook.yml Executable file
View File

@@ -0,0 +1,105 @@
# https://www.reddit.com/r/ansible/comments/sn9l9d/microk8s_join_cluster_with_ansible/
# https://canonical.com/microk8s/docs/getting-started
# https://docs.ansible.com/projects/ansible/latest/collections/kubernetes/core/docsite/kubernetes_scenarios/scenario_k8s_object.html
# https://www.reddit.com/r/kubernetes/comments/1cdfq13/microk8s_clustering_connection_filed_invalid/
---
- hosts: all
become: true
vars_files:
- vars/default.yml
tasks:
- name: Update prerequisites
apt:
update_cache: true
#Install dependencies
- name: Install Snap packet
apt:
name: [ 'snap' ]
state: present
- name: Install microk8s for All Node
become: true # needed if the other tasks are not played as root
snap:
name: microk8s
classic: yes
channel: stable
# Control Node, create join key for worker joins
- hosts: "controlnode"
become: true
tasks:
# Create join key
- name: Create join key
# --token-ttl to avoid ERROR 500
shell: microk8s add-node --token-ttl 300
register: join_cluster
run_once: true
- name: Create persistent var
set_fact:
#join: "{{ join_cluster.stdout_lines[4] | regex_replace('10\\.0\\.2\\.15', '192.168.56.141') }}"
join: "{{ join_cluster.stdout_lines[4] | regex_replace('[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+', hostvars[groups['controlnode'][0]]['ansible_ssh_host']) }}"
run_once: true
# DEBUG: We want to see if we have the good IP or not, if not we cannot join the control
# Normally, it will use eht0 or docker bridge IP so we have to change
- name: Test var
debug:
var: join
# Tâche 2 : Provisionnement du Worker node
# Worker Node, join from Control Node
- hosts: "workernode"
become: true
tasks:
# Join clustor with
- name: Join cluster
# Have to use microk8s-controller here instead of a group controlnode
#shell: "{{ hostvars['microk8s-controller']['join'] }}"
# But because of it see the IP of the machine virtual, so we have to use IP of real host assigned
shell: "{{ hostvars[groups['controlnode'][0]]['join'] }} --skip-verify"
- hosts: "controlnode"
become: true
tasks:
# Tâche 3: Déploiement d'un service dans le cluster
#Install dependencies
- name: Install Snap packet
apt:
name: [ 'python3-pip' ]
state: present
- name: Install kubernetes
pip:
name: kubernetes
executable: pip3
- name: Create Nginx pod
kubernetes.core.k8s:
kubeconfig: /var/snap/microk8s/current/credentials/client.config
state: present
definition:
apiVersion: v1
kind: Pod
metadata:
name: nginx-service
namespace: default
spec:
containers:
- name: nginx
image: nginx:stable-alpine-perl
#Tache 3.3: Vérifiez que le pod est bien déployé.
- name: Verify nginx works
kubernetes.core.k8s_info:
kubeconfig: /var/snap/microk8s/current/credentials/client.config
kind: Pod
namespace: default
register: nginx_info
- name: Debug pod's infomations
debug:
var: nginx_info.resources