Kubernetes
This wiki will house basic Kubernetes commands.
microk8s
Here we'll use microk8s to set up a Kubernetes node on a VPS to manage its container microservices.
Additional Kubernetes nodes can later be added.
sudo snap install microk8s --classic
Add user to microk8s group:
sudo usermod -aG microk8s $USER
Making an alias to kubectl will make following general Kubernetes tutorials easier. To do that, add alias kubectl="microk8s.kubectl" to ~/.bashrc.
kubectl
To set up another machine as control plane for that server, first export the Kubernetes configuration from the server:
microk8s config | xclip -selection clipboard
To send it to a file instead:
microk8s config > kubeconfig.yaml
Then on the other machine that will become control plane, install kubectl:
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
Put the previously saved config in the path:
export KUBECONFIG=/home/md/dev/kubernetes/kubeconfig.yaml
Confirm it can see the cluster with:
kubectl get nodes
Kompose
Kompose can help with converting Docker Compose yaml into Kubernetes yaml.
kompose convert -f docker-compose.yaml
kubectl apply -f .
kubectl get po
This doesn't work as well with more complicated setups.
Namespaces
To create a new namespace:
kubectl create namespace whoogle
To check all namespaces:
kubectl get all --all-namespaces
Before moving a pod from one namespace to another, delete the replicaset:
kubectl delete replicaset.apps/whoogle-69db8b69dc
Then apply the new namespace:
kubectl apply -f whoogle-deployment.yaml --namespace whoogle
Deleting
Delete everything:
kubectl delete pods,services,deployments --all
Delete individual parts specified by namespace:
kubectl delete pod portainer-agent-6f9dcf54cf-q9bt5 -n portainer
kubectl delete service portainer-agent -n portainer
kubectl delete service portainer-agent-headless -n portainer
kubectl delete deployment portainer-agent -n portainer
Services
To show all services:
kubectl get svc --all-namespaces
To create a service in a namespace:
kubectl apply -f whoogle-service.yaml -n whoogle
When using Kompose to convert Docker Compose yaml to Kubernetes yaml, if there is a reddis container that it doesn't include a service for, the main container may not be able to reach redis. In the case of Teddit, had to create a corresponding redis-service.yaml with clusterIP: "None":
apiVersion: v1
kind: Service
metadata:
name: redis
spec:
clusterIP: "None"
selector:
io.kompose.service: redis
Before the Teddit container was able to reach Redis at all.
Deployments
Get info about deployments:
kubectl describe deployment whoogle -n whoogle
Exec into container
Example:
Make sure you have the correct pod name:
kubectl get pods -n teddit
NAME READY STATUS RESTARTS AGE
redis-6b84587c-f4n6h 1/1 Running 9 (34h ago) 2d19h
teddit-74b4b64749-twvz8 1/1 Running 0 23m
kubectl exec -it teddit-74b4b64749-twvz8 -c teddit -n teddit -- sh
Logs
To do the equivalent of docker compose logs -f, first make sure you have the correct pod name:
kubectl get pods -n whoogle
NAME READY STATUS RESTARTS AGE
whoogle-69db8b69dc-jt7vq 1/1 Running 0 154m
Then make sure to specify the correct pod name and the namespace:
kubectl logs -f whoogle-69db8b69dc-jt7vq -n whoogle
or look for pods of a specific namespace to get logs for:
kubectl get pods -n portainer
NAME READY STATUS RESTARTS AGE
portainer-agent-6f9dcf54cf-zttht 0/1 CrashLoopBackOff 9 (4m35s ago) 26m
Then search for logs using the pod name and namespace:
kubectl logs pod/portainer-agent-6f9dcf54cf-zttht -n portainer
Locally managing cluster created with Rancher
Downloading the kubeconfig file from Rancher and using export KUBECONFIG=/path/to/your/config did not work due to tls: failed to verify certificate: x509: certificate signed by unknown authority error.
Had to copy the -----BEGIN CERTIFICATE----- block into a text file and then run cat cert.txt | openssl x509 -outform PEM | base64 -w0 and copy that PEM format output into the certificate-authority-data: field of the kubeconfig yaml.