Skip to content

Instantly share code, notes, and snippets.

@rvflash
Last active July 20, 2023 15:44
Show Gist options
  • Save rvflash/0c03c31d10d20a5ff857cb6712a0b4a1 to your computer and use it in GitHub Desktop.
Save rvflash/0c03c31d10d20a5ff857cb6712a0b4a1 to your computer and use it in GitHub Desktop.
Kubernetes

Kubernetes (k8s)

Environment

2 command line tools are required to works with kubernetes:

Local environment

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/.

Some commands

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}

Create a new pod.

$ kubectl run -it hg-tool -n ${NAMESPACE} --image=scratch:latest -- bash 

Create a pod dedicated to connect via MySQL command line tool.

$ 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)

Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace

$ kubectl cp /tmp/foo <some-namespace>/<some-pod>:/tmp/bar

Copy /tmp/foo from a remote pod to /tmp/bar locally

$ kubectl cp <some-namespace>/<some-pod>:/tmp/foo /tmp/bar

Some tools to look.

  1. Lens
  2. Krew
  3. K9S

Practices: kubectl

Using the following projet as code source: https://github.com/luksa/kubernetes-in-action.

In the chapter03: apply a configuration file

-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.

Practices: helm

todo

$ helm create testing
$ helm template . # render the generated file
$ helm verify
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment