---
# System prepended metadata

title: TP2 Kubernetes

---

# TP2 Kubernetes

Déploiement du cluster kind en exposant le 80 & 443

```bash
cat <<EOF | kind create cluster --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  kubeadmConfigPatches:
  - |
    kind: InitConfiguration
    nodeRegistration:
      kubeletExtraArgs:
        node-labels: "ingress-ready=true"
  extraPortMappings:
  - containerPort: 80
    hostPort: 80
    protocol: TCP
  - containerPort: 443
    hostPort: 443
    protocol: TCP
EOF
```

Puis installation de nginx ingress controller
```bash
ndiruggiero@vm1:~$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static
namespace/ingress-nginx created
serviceaccount/ingress-nginx created
serviceaccount/ingress-nginx-admission created
role.rbac.authorization.k8s.io/ingress-nginx created
role.rbac.authorization.k8s.io/ingress-nginx-admission created
clusterrole.rbac.authorization.k8s.io/ingress-nginx created
clusterrole.rbac.authorization.k8s.io/ingress-nginx-admission created
rolebinding.rbac.authorization.k8s.io/ingress-nginx created
rolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
configmap/ingress-nginx-controller created
service/ingress-nginx-controller created
service/ingress-nginx-controller-admission created
deployment.apps/ingress-nginx-controller created
job.batch/ingress-nginx-admission-create created
job.batch/ingress-nginx-admission-patch created
ingressclass.networking.k8s.io/nginx created
validatingwebhookconfiguration.admissionregistration.k8s.io/ingress-nginx-admission created

ndiruggiero@vm1:~$ kubectl wait --namespace ingress-nginx \
  --for=condition=ready pod \
  --selector=app.kubernetes.io/component=controller \
  --timeout=90s
pod/ingress-nginx-controller-6bccc5966-kbzjh condition met
```

Une fois l'ingress Nginx controller installer nous pouvons commencer.


## Build et push les images docker

D'abord nous allons créer un dockerfile avec un index.html afin d'afficher "Tacos", "Burgers" ou "Pizzas"

Exemple de Dockerfile
```dockerfile
FROM nginx:1.16.1

COPY index.html /usr/share/nginx/html/

CMD ["nginx", "-g", "daemon off;"]
```

Exemple Index.html

```htmlmixed
<html>
  <body>
    <h1>Pizzas</h1>
  </body>
</html>
```

Ensuite on vas build notre images et la push sur docker hub.

```bash
#Connexion a son compte docker
docker login -u useraname

#Build l'image docker
docker build -t pizzas-image .
#Tag l'image docker
docker tag pizzas-image:latest chiroxo/pizzas-image:latest
#Push l'image docker
docker push chiroxo/pizzas-image
```

Il faut faire cette procédure pour chaque image que l'on souhaite push.

## Fichiers yaml pour déployer les images et mettre ne places des services asociés ainsi que l'ingress

Exemple de fichier yaml pour déployer les pizzas

```yaml
---
apiVersion: v1
kind: Service
metadata:
  name: pizzas-services
spec:
  selector:
    app: pizzas
  ports:
  - name: http
    port: 80
    targetPort: 80
  
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pizza-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: pizzas
  template:
    metadata:
      labels:
        app: pizzas
    spec:
      containers:
      - name: pizzas
        image: chiroxo/pizzas-image
        ports:
        - containerPort: 80
```

Exemple de l'ingress
```yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: mypizza.eatsout.com
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: pizzas-services
            port:
              number: 80
  - host: burgerandtacos.eatsout.com
    http:
      paths:
      - pathType: Prefix
        path: "/burgers"
        backend:
          service:
            name: burgers-services
            port:
              number: 80
      - pathType: Prefix
        path: "/tacos"
        backend:
          service:
            name: tacos-services
            port:
              number: 80
```
On applys nos fichiers via la commande kubectl apply et enjoy :)

Pensez à modifier votre fichier hosts afin de rajouter les hosts.

## Question 6

On augmente le nombre de ReplicaSet à trois, on apply donc le fichier yaml et donc trois pods seront déployer au total pour tacos.
Les pods seront automatiquement gérer et reconnu par le services tacos-services grâce aux labels que nous avons défini afin de match les critères.
On peut également utiliser la commande kubectl scale afin de modifier le nombre de pods déployer pour un déploiement

Voir si on a bien nos nouveau pods déployer.
```bash
kubectl get pods --namespace=default
NAME                                  READY   STATUS    RESTARTS   AGE
burgers-deployment-766bd45647-xgpjt   1/1     Running   0          55m
pizza-deployment-647d87b754-lnmtr     1/1     Running   0          55m
tacos-deployment-5557c7f44c-nvk44     1/1     Running   0          28m
tacos-deployment-5557c7f44c-p2fbp     1/1     Running   0          28m
tacos-deployment-5557c7f44c-zb55d     1/1     Running   0          55m
```

Voir les logs du déploements afin de savoir quel pods réponds aux requêtes.
```bash
 kubectl logs -f  deploy/tacos-deployment
```