Skip to content

Latest commit

 

History

History
421 lines (347 loc) · 9.77 KB

readme.md

File metadata and controls

421 lines (347 loc) · 9.77 KB

Kubernetes - DEVOPS 🧊

@(WebDev)[webdev]

Document using languages: portuguese and english

Dinâmica "Superficial"

Source: Introdução ao Kubernetes

Cluster é um conjunto de máquinas (Nodes). Cada máquina possui uma quantidade de vCPU e memória.

  • Pods: unidade que contém os containers provisionados
  • O Pod representa os processos rodando no cluster

ClusterNodePod|center|500x270

Services

É uma forma de agregar um conjunto de pods para então implementar políticas de visibilidade.

Type of connections

Types

Sectors

Selectors|center|400x300

Minikube

Initialization

minikube start

Kubectl

  1. Show services kubectl get svc
  2. Show pods kubectl get pods
  3. Show deployments kubectl get deployments

Creating a single pod to test

Source

  1. kubectl apply -f pod.yaml
    # kubectl apply -f pod.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: pod-exemplo
    spec:
      containers:
        - name: pod-exemplo
          image: nginx:1.17-alpine
    
  2. View log of pod by name kubectl logs pod-exemplo

Creating a deployment

Using command-line

  1. Create a deployment kubectl create deployment hello-nginx --image=nginx:1.17-alpine

    • Check it with kubectl get deployments
  2. Create a service kubectl expose deployment hello-nginx --type=LoadBalancer --port=80

    • To show it you can run kubectl get services
    NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S)
    hello-nginx LoadBalancer 10.97.138.76 pending 80:31365/TCP
  3. To test on browser use minikube service hello-nginx Browser

Using deployment.yaml

  1. create a file deployment.yaml with
    # kubectl apply -f deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: hello-nginx
      labels:
        app: nginx
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: hello-nginx
      template:
        metadata:
          labels:
            app: hello-nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.17-alpine
            ports:
            - containerPort: 80
  2. Delete previous deployment with kubectl delete deployments --all
  3. kubectl apply -f deployment.yaml

Creating a service

  1. create a file service.yaml with
    # kubectl apply -f service.yaml
    apiVersion: v1
    kind: Service
    metadata:
      name: nginx-service
    spec:
      type: LoadBalancer
      ports: 
        - port: 80
      selector:
        app: hello-nginx
  2. Run kubectl apply -f service.yaml
  3. Test on command line kubectl get services
  4. Test on browser with minikube service nginx-service

Using ConfigMap to Update nginx settings

  1. create a file configmap.yaml with
    # kubectl apply -f configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: nginx-conf
      labels:
        app: myapplication
    data:
      nginx.conf: |
    
        server {
          listen 80;
          index index.php index.html;
          root /usr/share/nginx/html;
    
          rewrite ^/google$ http://google.com permanent;
        }
  2. Add new volume and volumeMount to deployment.yaml
    spec:
      containers:
      - name: nginx
        image: nginx:1.17-alpine
        ports:
        - containerPort: 80
    
        volumeMounts:
        - mountPath:  /etc/nginx/conf.d
          name:  nginx-conf
          readOnly: true
    
      volumes:
      - name: nginx-conf
        configMap:
          name: nginx-conf
          items:
            - key: nginx.conf
              path: nginx.conf
     
    
  3. Test config with kubectl get configmaps
  4. Test on browser minikube service nginx-service

Add Mysql from zero

  1. create a folder mysql and a new file deployment.yaml
# kubectl apply -f deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name:  mysql-server
  labels:
    name:  mysql-server
spec:
  replicas: 1

  selector: # Selector using to create a Service
    matchLabels:
      app: mysql-server
      tier: db

  template: # POD 
    metadata:
      labels:
        app: mysql-server
        tier: db
    spec: # spec do POD 
      containers:
      - image:  mysql:5.7
        name:  mysql-server
        args: 
          - "--ignore-db-dir=lost+found"

        env:
        - name: MYSQL_ROOT_PASSWORD
          value: root
        ports:
        - containerPort:  3306
        volumeMounts:
         - name: mysql-persistent-storage
           mountPath: /var/lib/mysql
      
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

  1. Creating a persistent volume persistent-volume.yaml
    • kubectl apply -f persistent-volume.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

Create Secret

  1. kubectl create secret generic mysql-pass --from-literal=password='a1s2d3f4'
  2. Check it with kubectl get secrets
  3. Change deployment.yaml to read secret:
spec: # spec do POD 
  containers:
  - image:  mysql:5.7
    name:  mysql-server
    args: 
      - "--ignore-db-dir=lost+found"

    env:
    - name: MYSQL_ROOT_PASSWORD
      valueFrom:
        secretKeyRef:
          name: mysql-pass
          key: password

Criando Mysql Service

  1. Create a file service.yaml with
# kubectl apply -f service.yaml
apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  ports:
    - port: 3306
  selector:
    app: mysql-server
    tier: db
  clusterIP: None
  1. kubectl apply -f service.yaml
  2. Check services with kubectl get svc
  3. kubectl get pods
  4. Executar pod com kubectl exec -it mysql-server-866f9b567b-4bp62 bash
  5. Abrir mysql com: mysql -uroot -p e senha a1s2d3f4

Apagar volume e deployment

  1. kubectl delete PersistentVolumeClaim mysql-pv-claim
  2. kubectl delete deployment mysql

Gerenciando recursos

Testing nginx deployment.yaml:

  • limitar o uso de cpu de cada Pod com: cpu: "80m"
    • unidade de medida de uso de vCPU é chamada milicore mCPU no kubernetes
  • No caso da memória se o pode bater o limite memory: "100Mi" então ele nem sobe, é bem diferente do caso do cpu que sobe apenas deixando processamento mais lento.
  • resources:
    • requests: significa o requisito mínimo para subir o pod
    • limits: significa o máximo de recursos que pode ser consumido pelo pod
    • Qualquer coisa além disso o Kubernetes vai tentar duas estratégias:
      1. diminuir a quantidade computacional ou
      2. simplesmente matar o container para não dar um crash
# kubectl apply -f deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-nginx
  labels:
    app: nginx
spec:
  replicas: 2

  selector:
    matchLabels:
      app: hello-nginx
      
  template:
    metadata:
      labels:
        app: hello-nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.17-alpine
        ports:
        - containerPort: 80

        resources:
          requests:
            memory: "100Mi"
            cpu: "80m"
          limits:
            memory: "150Mi"
            cpu: "100m"

        volumeMounts:
        - mountPath:  /etc/nginx/conf.d
          name:  nginx-conf
          readOnly: true

      volumes:
      - name: nginx-conf
        configMap:
          name: nginx-conf
          items:
            - key: nginx.conf
              path: nginx.conf
 

HPA - Horizontal Pod Autoscaler

  1. create a folder php-apache
  2. create index.php
<?php

$x = 0.000001;
for ($i = 0; $i <= 100000000; ++$i) {
    $x += sqrt($x);
}
echo  PHP_EOL.'Code.education Rocks! '.PHP_EOL;
  1. create a Dockerfile
# docker build -t lfrichter/php-apache-hpa .
# docker run -it lfrichter/php-apache-hpa bash
# docker push lfrichter/php-apache-hpa
FROM php:7-apache
COPY index.php /var/www/html/index.php
RUN chmod a+rx index.php
  1. create HPA deployment with
    • kubectl run php-apache-hpa --image=lfrichter/php-apache-hpa --requests=cpu=200m --expose --port=80
  2. creating command to autoscaling (optional)
    • kubectl autoscale deployment php-apache-hpa --cpu-percent=20 --min=1 --max=10
  3. creating a file to autoscaling (better choice)
    1. create a file hpa.yaml with:
# kubectl apply -f hpa.yaml
# kubectl get hpa
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: php-apache-hpa
spec:
  maxReplicas: 5
  minReplicas: 1
  scaleTargetRef: 
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: php-apache-hpa
  targetCPUUtilizationPercentage: 20

Testing HPA

  1. kubectl run -it loader --image=busybox /bin/sh
    • busybox é um linux super pequeno, o mais básico para rodar algo
  2. Criar e executar um looping para essa máquina ficar chamando o php-apache
    • default é o namespace padrão do kubernetes
    • Acessar página PHP
    # wget -q -O- http://php-apache-hpa.default.svc.cluster.local;
    
    • Criar looping infinito
    while true; do wget -q -O- http://php-apache-hpa.default.svc.cluster.local; done;
    
  3. Verifique se está funcionando em watch kubectl get hpa