2 command line tools are required to works with kubernetes:
- kubectl: https://kubernetes.io/fr/docs/tasks/tools/install-kubectl/
- helm: https://helm.sh/docs/intro/install/
In a Golang environment, we can use this tool to create a local cluster:
Otherwise, we can install https://minikube.sigs.k8s.io/docs/start/.
Create a local cluster
$ kind create cluster
By default, the namespace is
default
, but we can separate concepts by creating one.
List namespaces
$ kubectl get namespace
Show network policy
kubectl -n ${NAMESPACE} get networkpolicy
kubectl -n ${NAMESPACE} describe networkpolicy ${NETWORKPOLICY_NAME}
List nodes
A node can contains one or more containers.
- Default nodes:
$ kubectl get nodes
- By a namespace:
$ kubectl -n ${NAMESPACE} get pods
- All nodes:
$ kubectl get pods -A
List services
$ kubectl get services -A
Describe configuration / status of a pod
kubectl -n ${NAMESPACE} describe pod ${POD_NAME}
Logs
kubectl -n ${NAMESPACE} logs ${POD_NAME}
See
-p
logs flag to see previous logs.
Deployment
kubectl apply -f ${DEPLOYMENT_YAML_FILE}
Restart
kubectl -n ${NAMESPACE} rollout restart deployment/${DEPLOYMENT_NAME}
or
kubectl -n ${NAMESPACE} scale --replicas=0 deployment/${DEPLOYMENT_NAME}
kubectl -n ${NAMESPACE} scale --replicas=1 deployment/${DEPLOYMENT_NAME}
Communicate with a service exposed by the K8S from our desk
kubectl -n ${NAMESPACE} port-forward service/${SERVICE_NAME} ${LOCAL_PORT}:${DISTANT_PORT}
$ kubectl run -it hg-tool -n ${NAMESPACE} --image=scratch:latest -- bash
$ kubectl run -it --image=debian:latest hg-mysql
root@hg-mysql:/# apt-get update && apt-get install -y default-mysql-client
root@hg-mysql:/# mysql --version
mysql Ver 15.1 Distrib 10.5.11-MariaDB, for debian-linux-gnu (x86_64) using EditLine wrapper
To re-enter later, just run the following commands:
$ kubectl exec -it hg-mysql bash
root@hg-mysql:/# mysql -A -u ${DB_USER} -h ${DB_HOST} -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1685439
Server version: 8.0.23 Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| rv |
+--------------------+
24 rows in set (0.004 sec)
$ kubectl cp /tmp/foo <some-namespace>/<some-pod>:/tmp/bar
$ kubectl cp <some-namespace>/<some-pod>:/tmp/foo /tmp/bar
Using the following projet as code source: https://github.com/luksa/kubernetes-in-action.
-f
allows to specify a file, by default it's STDIN.
$ kubectl apply -f kubia-manual.yaml
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubia-manual 1/1 Running 0 3m26s
As we now have a local cluster, the
-A
is optional.
More details
$ kubectl describe pod kubia-manual
Custom output with all data
$ kubectl get pods -o wide
Retrieve deployment configuration
$ kubectl get pods kubia-manual -o yaml
See pod logs (in live)
$ kubectl logs -f kubia-manual
Expose service locally
$ kubectl port-forward kubia-manual 8080:8080
Get inside the container:
- With only one container in the pod (1/1):
$ kubectl -n ${NAMESPACE} exec -ti ${POD_NAME} -- bash
- With multiple containers in the pod (X/N):
$ kubectl -n ${NAMESPACE} exec -it ${POD_NAME} -c ${CONTAINER_NAME} -- bash
Like with Docker, we can execute a command from or in the container
$ kubectl exec -ti kubia-manual -- date
Finally, we destroy it
$ kubectl delete -f kubia-manual.yml
Labels are importants, the key to deploy pods (see label
and selector
).
As long as labels have not changed, we can use the configuration file to delete it. Otherwise, it is a manual deletion.
todo
$ helm create testing
$ helm template . # render the generated file
$ helm verify