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 namespaceShow 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 -AList services
$ kubectl get services -ADescribe configuration / status of a pod
kubectl -n ${NAMESPACE} describe pod ${POD_NAME}Logs
kubectl -n ${NAMESPACE} logs ${POD_NAME}See
-plogs 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 wrapperTo 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/barUsing the following projet as code source: https://github.com/luksa/kubernetes-in-action.
-fallows 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 3m26sAs we now have a local cluster, the
-Ais optional.
More details
$ kubectl describe pod kubia-manualCustom output with all data
$ kubectl get pods -o wideRetrieve deployment configuration
$ kubectl get pods kubia-manual -o yamlSee pod logs (in live)
$ kubectl logs -f kubia-manualExpose service locally
$ kubectl port-forward kubia-manual 8080:8080Get 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} -- bashLike with Docker, we can execute a command from or in the container
$ kubectl exec -ti kubia-manual -- dateFinally, we destroy it
$ kubectl delete -f kubia-manual.ymlLabels 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