Posts Tagged - kubernetes

Bash scripts for port-forwards

The following is an example of the .sh scripts I use to forward and debug pods or features.

#!/bin/bash
# to use: sh portforward_microservice_xxx.sh env-dev | env-pre | env-pro
# it accepts additional params such as no-connectors to ignore some port forwards
# 	no-connectors - ignores forwards to XXX
args=("$@")
echo "INFO: using namespace ${args[0]}"

# only needed if we have several clusters for each env
if [ $1 == "env-pre" ]; then
	kubectl config use-context context-for-pre
elif [ $1 == "env-pro" ]; then
	kubectl config use-context context-for-pro
else	
	# default value - always DEV
	kubectl config use-context context-for-dev
fi

# add here new variables or cases to omit
no_connectors=false
for arg in "$@"; do
	case $arg in
		no-connectors)
			no_connectors=true
			shift
			;;	
		*)
			shift
			;;
	esac
done

if [ "$no_connectors" = false ]; then
	kubectl port-forward -n ${args[0]} svc/connector1 9210:9210 &
	kubectl port-forward -n ${args[0]} svc/connector2 8000:8000 &
else
	echo "INFO: ignoring connectors"
fi

# common pods we always need to call
kubectl port-forward -n ${args[0]} svc/service1 5050 &
kubectl port-forward -n ${args[0]} svc/service2 5060

Read More

Interact with your pods

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

Access a Pod via Network

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 <pod-name> <localport>:<podport>
kubectl port-forward istio 5015:80

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
  • -c if you have multiple containers in your Pod, you can choose the one to view

Read More

Error al crear imagenes en local (Minikube)

Hay un problema al intentar correr una imagen tuya propia en local con minikube. Minikube tiene su propio repositorio de imagenes y si no encuentra una imagen alli, la intenta descargar siempre del repositorio.

Solución

Set imagePullPolicy: IfNotPresent

Run the following command:

eval $(minikube docker-env)
# build again the image
# try to run it again

Read More

K8s config commands

Config

Azure

log-in into a tenant

az login --tenant 7f2c3a6b-e849-4d6f-b65d-1c0ef8b0f41c

configure local kubectl .kube config file

az aks get-credentials --resource-group my-aks --name aksname --admin

(the –admin part is optional but useful)

Contexts & Namespaces

see all configured contexts

kubectl config get-contexts

configure a context with a default namespace

kubectl config set-context <context_here> --namespace=<namespace_here>
# example
kubectl config set-context my-aks-context --namespace=aks-dev

Read More

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

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']

Read More