UBE - Ressources

Installation Kubectl

curl -LO https://dl.k8s.io/release/$(curl -Ls https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
kubectl version --client

Installation Minikube

sudo apt install qemu-utils qemu-system-x86 qemu-system-gui

curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 \
  && chmod +x minikube

sudo mkdir -p /usr/local/bin/
sudo install minikube /usr/local/bin/

minikube start
minikube dashboard
minikube config view

Installation Docker

Debian / Ubuntu :
curl -fsSL https://get.docker.com -o install-docker.sh
sudo sh install-docker.sh

RHEL :
https://docs.docker.com/engine/install/rhel/#install-using-the-repository

Installation Vagrant

# Installation Ubuntu / Debian
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install vagrant


# Installation RHEL
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install vagrant

Installation VirtualBox

wget -O- -q https://www.virtualbox.org/download/oracle_vbox_2016.asc | sudo gpg --dearmour -o /usr/share/keyrings/oracle_vbox_2016.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/oracle_vbox_2016.gpg] http://download.virtualbox.org/virtualbox/debian bookworm contrib" | sudo tee /etc/apt/sources.list.d/virtualbox.list
sudo apt update
sudo apt install virtualbox-7.0

## Fix kernel module Debian 12
sudo apt install linux-headers-6.1.0-23-amd64
sudo /sbin/vboxconfig
sudo reboot

## Fix VirtualBox version + Vagrant
sudo apt install virtualbox-7.0

## Créer la configuration de la première machine (dans un dossier e.g. vm1)
vagrant init debian/bookworm64
## Configure network (configurer le réseau)
nano ./Vagrantfile
# ajouter la ligne dans les configurations 
=> config.vm.network "private_network", ip: "192.168.56.10"
## Démarer la vm
vagrant up

## Créer la configuration de la deuxième machine (dans un dossier e.g. vm2)
vagrant init debian/bookworm64
## Configure network (configurer le réseau)
nano ./Vagrantfile
# ajouter la ligne dans les configurations 
=> config.vm.network "private_network", ip: "192.168.56.11"
## Démarer la vm
vagrant up

## Connection a la machine (dans le répertoire de la machine)
vagrant ssh

## Destruction d'une machine
vagrant destroy

Installation RKE2

# Notes sur la vm vagrant
user : vagrant
password : vagrant

# Prérequis (toutes les machines)
sudo apt update
sudo apt install curl

# Master
curl -sfL https://get.rke2.io | sh -
systemctl enable rke2-server.service
mkdir -p /etc/rancher/rke2/
# Changer le nom d'hoste
hostnamectl hostname bookwormmaster
# Remplir la configuration (avec les informations présente un peut plus bas => Rke2 Config Master)
nano /etc/rancher/rke2/config.yaml
# Démarage du service => démarage de l'installation
systemctl start rke2-server.service
cat /var/lib/rancher/rke2/server/node-token
# Configurer kubectl
mkdir ~/.kube/
sudo cp /etc/rancher/rke2/rke2.yaml ~/.kube/config
sudo chmod 644 ~/.kube/config

# Agent
curl -sfL https://get.rke2.io | INSTALL_RKE2_TYPE="agent" sh -
systemctl enable rke2-agent.service
mkdir -p /etc/rancher/rke2/
# Changer le nom d'hoste
hostnamectl hostname bookwormagent
# Remplir la configuration (avec les informations présente un peut plus bas => Rke2 Config Agent)
nano /etc/rancher/rke2/config.yaml
# Démarage du service => démarage de l'installation
systemctl start rke2-agent.service

# Rke2 Config (master)
node-ip: <ip_du_noeud>
node-external-ip: <ip_du_noeud>

# Rke2 Config (agent)
server: https://<ip_master>:9345
token: <token_donne_par_master>
node-ip: <ip_du_noeud>
node-external-ip: <ip_du_noeud>



# Configure Kubectl (on master)
mkdir -p ~/.kube/
sudo cp /etc/rancher/rke2/rke2.yaml ~/.kube/config

Deployment YAML

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: nginx
        ports:
        - containerPort: 80
        env:
        - name: NAME
          value: "value"

Kubectl portforward

kubectl port-forward service/nom_du_service port_local:port_cible

Mount Volume

apiVersion: v1
kind: Pod
metadata:
  name: task-pv-pod
spec:
  volumes:
    - name: task-pv-storage
      persistentVolumeClaim:
        claimName: task-pv-claim
  containers:
    - name: task-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: task-pv-storage

Wordpress Production

apiVersion: v1
kind: Namespace
metadata:
  name: wordpress-app

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mariadb
  namespace: wordpress-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: mariadb
  template:
    metadata:
      labels:
        app: mariadb
    spec:
      containers:
      - name: mariadb
        image: mariadb:10.6.4-focal
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "somewordpress"
        - name: MYSQL_DATABASE
          value: "wordpress"
        - name: MYSQL_USER
          value: "wordpress"
        - name: MYSQL_PASSWORD
          value: "wordpress"
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: pvc-mariadb
          mountPath: /var/lib/mysql
      volumes:
      - name: pvc-mariadb
        persistentVolumeClaim:
          claimName: pvc-mariadb

---
apiVersion: v1
kind: Service
metadata:
  name: mariadb-service
  namespace: wordpress-app
spec:
  selector:
    app: mariadb
  ports:
    - protocol: TCP
      port: 3306
      targetPort: 3306


---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
  namespace: wordpress-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - name: wordpress
        image: wordpress:6.6.1-php8.2-apache
        env:
        - name: WORDPRESS_DB_HOST
          value: mariadb
        - name: WORDPRESS_DB_USER
          value: "wordpress"
        - name: WORDPRESS_DB_PASSWORD
          value: "wordpress"
        - name: WORDPRESS_DB_NAME
          value: "wordpress"
        ports:
        - containerPort: 80
          name: http
---
apiVersion: v1
kind: Service
metadata:
  name: wordpress-service
  namespace: wordpress-app
spec:
  type: ClusterIP
  selector:
    app: wordpress
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-mariadb
  namespace: wordpress-app
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: manual
  hostPath:
    path: /mnt/data

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-mariadb
  namespace: wordpress-app
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: manual
  resources:
    requests:
      storage: 5Gi

Wordpress HPA

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: wordpress-hpa
  namespace: wordpress-app
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: wordpress
  minReplicas: 1
  maxReplicas: 5
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 50