Migrate docker-compose to k8s

Steps to go from a docker-compose file, to build an Image out of the file, upload it to DockerHub, and run it with Kubernetes (minikube).

Set up

Install Kompose

# linux
curl -L https://github.com/kubernetes/kompose/releases/download/v1.22.0/kompose-linux-amd64 -o kompose
chmod +x kompose
sudo mv ./kompose /usr/local/bin/kompose

Clone App and Host It into DockerHub

Clone the files, go to the directory where your Dockerfile is and run:

# mariocodes is my DockerHub username
# kubernetes-custom-java-maven-app is the name of the repo I created at DockerHub
docker build -f Dockerfile -t mariocodes/kubernetes-custom-java-maven-app .

Read More

K8s debugging commands

See logs:

kubectl logs <pod-name>

Modifiers:

  • -c If you have multiple containers in your Pod, you can choose the container to view.
  • -f continuously stream logs back to the terminal without exiting.

You can also use the exec command to execute commands in running containers:

# attach to bash
kubectl exec -it <pod-name> -- bash

# attach to running process
kubectl attach -it <pod-name>

Read More

Common errors in k8s

STATUS CrashLoopBackOff

CrashLoopBackOff and no log appears. This happens as the container has no command to run so it start up and then immediately exits.

Give your image a command.

more here

apiVersion: v1
kind: Pod
metadata:
  name: twocontainers
spec:
  containers:
  - name: container1
    image: python:3.6-alpine
	command: ['sh', '-c', 'echo cont1 > index.html && python -m http.server 8082']
  - name: container2
    image: python:3.6-alpine
	command: ['sh', '-c', 'echo cont1 > index.html && python -m http.server 8083']

Access your pods

This details various ways that you can interact with the code and data running inside your pod.

Using Port Forwarding

If you want access to a specific pod, even if it’s not serving traffic on the internet. To achieve this, Kubernetes has support built into it.

kubectl port-forward kuard 8080:8080

A secure tunnel is created from your local machine, though the K8s master, to the instance of the Pod running on one of the worker nodes.

Getting More Info with Logs

When you need to debug.

# downloads the current logs from the running instance
kubectl logs kuard

# continuously streams logs
kubectl logs -f kuard

# get logs from a previous instance of the container. Useful if your containers are continuously restarting at startup. 
kubectl logs kuard --previous

Read More

i18n & l10n

i18n is just a nomenclature for Internationalization. i18n involves, among other things, the ability to display translated content. It prepares a digital product for localization by for example, separate the content into strings so they are ready to be translated and delivered.

The same goes for L10n. This is a nomenclature for Localization. L10n involves translating content, adapting graphics and finalizing the producto for each regional market.

Reference(s)

https://www.oneskyapp.com/blog/i18n-need-to-know-internationalization/

K8s install Minikube

(Oficial Doc: https://minikube.sigs.k8s.io/docs/start/)

Technology to quickly set up a Kubernetes cluster locally to learn how to use Kubernetes.

Installation

How to install on a fresh Ubuntu installation:

Install prerrequisites and kubectl

sudo apt-get update && sudo apt-get install -y apt-transport-https curl

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list

sudo apt-get update
sudo apt-get install -y kubectl

Prepare kubectl to make it executable

cd /usr/bin
sudo chmod +x ./kubectl

Here you can either install a Hypervisor or Docker. I’m going with Docker.

# remove old versions
sudo apt-get remove docker docker-engine docker.io containerd runc

# docker prerrequisites
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg lsb-release

# add docker key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

# setup stable repository
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# install docker
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

# verify docker installation
sudo docker run hello-world

(optional) run docker without sudo

sudo groupadd docker
sudo gpasswd -a $USER docker

Install Minikube

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

# add user to docker group
sudo usermod -aG docker $USER && newgrp docker

# confirm installation
minikube start
minikube dashboard

Commands

minikube start
minikube stop
minikube status

minikube ip
minikube logs
minikube ssh

minikube help

Launch single node K8s cluster

# start VM with cluster running inside
minikube start --wait=false
# cluster details and health status
kubectl cluster-info
# shows nudes in the cluster
kubectl get nodes

kubectl create deployment first-deployment --image=katacoda/docker-http-server
kubectl expose deployment first-deployment --port=80 --type=NodePort

This starts a VM for you with a K8s cluster running inside. This cluster can be interacted with using kubectl. This is the main approach for managing K8s and the apps running on top of the cluster.

Launch multi cluster

# create a cluster for dev environment
minikube start -p dev

# see a list of all profiles
minikube profile list

# change to dev profile
minikube profile dev

Reference(s)

https://docs.docker.com/engine/install/ubuntu/ https://k8s-docs.netlify.app/en/docs/tasks/tools/install-minikube/ https://minikube.sigs.k8s.io/docs/start/ https://katacoda.com/courses/kubernetes/launch-single-node-cluster

SQL Indexes

Don’t create indexes in every column. This slows things down on insert, delete or update operations.

You may create an index for columns that are common in WHERE, ORDER BY or GROUP BY clauses. You may also consider adding an index in columns that are used to relate other tables JOIN.

Index are best used for big domain fields such as ids, names, surnames. Don’t use them for male/female fields.

A good recommendation is to measure performance time before and after the creation of that index. If your index doesn’t improve performance remove it as it causes overhead.

Reference(s)

https://stackoverflow.com/questions/7744038/decision-when-to-create-index-on-table-column-in-database

Advanced SQL

UNION

The union sentence is used to accumulate results for two SELECT sentences.

SELECT column1, column2 FROM table1
UNION
SELECT column1, column2 FROM table2

We have the following tables

company1

per name surname
1 ANTONIO PEREZ
2 PEDRO RUIZ

company2

per name surname
1 LUIS LOPEZ
2 ANTONIO PEREZ

Read More