Syntax highlighting of devops/kubernetes/kubectl

<<TableOfContents()>>

= kubectl =

Установка: [[ https://kubernetes.io/ru/docs/tasks/tools/install-kubectl/ ]]

Доп. конфигурация: [[ https://kubernetes.io/ru/docs/tasks/tools/install-kubectl/#дополнительная-конфигурация-kubectl ]]

[[https://kubernetes.io/docs/reference/kubectl/#resource-types|Resource Types|class=" moin-https"]]

[[https://kubernetes.io/docs/reference/kubectl/cheatsheet/|kubectl cheat sheet|class=" moin-https"]] | [[https://kubernetes.io/ru/docs/reference/kubectl/cheatsheet/|ru|class=" moin-https"]]

{{{#!highlight bash
# --- ~/.bash_aliases
alias k=kubectl
alias kcf='kubectl create -f'
alias kcd='kubectl config set-context $(kubectl config current-context) --namespace'
alias kcdc="kubectl config view -o jsonpath='{.contexts[].context.namespace}' && echo ''"

}}}

== CLI ==

=== General ===

{{{#!highlight bash
# --- show cluster state
k cluster-info
# --- show components statuses
k get componentstatuses
# --- --- or
k get cs
# --- port forward
kubectl port-forward kubia 8080:8080
# --- top command for pods
kubectl top pod
# --- restart deployment
kubectl rollout restart deployment my-deployment

}}}

=== run ===

{{{{#!highlight bash
# --- quick pod run
k run nginx --image=nginx

# --- 
kubectl run httpie --image alpine/httpie  \
  --namespace prometheus \
  --env="DNS_DOMAIN=cluster" --env="POD_NAMESPACE=default"
  -- sleep 99999

# --- быстрый запуск множества реплик модуля
kubectl run test --image busybox --replicas 5 -- sleep 99999

# --- быстрый запуск модуля с циклической командой (имитация нагрузки)
kubectl run -it --rm --restart=Never loadgenerator --image=busybox -- sh -c \
  "while true; do wget -O – -q http://kubia.default; done"

# --- быстрый запуск модуля с назначением метки
kubectl run busybox -l app=busybox --image busybox -- sleep 999999

# --- запуск модуля с заданной учетной записью
k run test --image=luksa/kubectl-proxy --overrides='{ "spec": { "serviceAccount": "admin" } }'

# --- запуск пода с заданным nodeSelector
kubectl run test --image ubuntu \
  --overrides='{"spec": { "nodeSelector": {"role": "kdk"}}}' \
  -- sleep 999999

# --- quick start postgres with resources and env variables
#     --requests and --limits are deprecated 
kubectl run postgres --image postgres \
  --requests='cpu=50m,memory=50Mi' \
  --limits='cpu=100m,memory=100Mi' \
  --env='POSTGRES_PASSWORD=secret'

}}}}

=== get ===

{{{#!highlight bash
# --- get all resources in all namespaces
k get all --all-namespaces

# --- get secret with decode
#     -json for jq
#     -r for remove quotes
k get secret kdkop -o json | jq -r .data.snap01 | base64 --decode

}}}

=== delete ===

{{{#!highlight bash
# --- delete almost all resources from current namespace
kubectl delete all --all
# --- delete with custom grace-period
kubectl delete po mypod --grace-period=5
# --- force delete without grace-period
kubectl delete po mypod --grace-period=0 --force

}}}



=== curl ===

{{{#!highlight bash
k run test --image=luksa/kubectl-proxy
k exec -it test -- sh
curl  localhost:8001/api/v1/namespaces/kdk-dev/pods \
  -X POST \
  -H  'Content-Type: application/yaml' \
  -d '---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: clusterkopfpeerings.kopf.dev
spec:
  scope: Cluster
  group: kopf.dev
  names:
    kind: ClusterKopfPeering
    plural: clusterkopfpeerings
    singular: clusterkopfpeering
  versions:
    - name: v1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            status:
              type: object
              x-kubernetes-preserve-unknown-fields: true
'

}}}

=== apply ===

{{{#!highlight bash
apiVersion: v1
kind: Pod
metadata:
  name: httpie
spec:
  nodeSelector:
    role: kdk
  serviceAccountName: ignite
  containers:
    - name: main
      image: alpine/httpie
      command: ["sleep", "9999999"]

}}}

=== debug ===

Подключиться к контейнеру ''app'' пода ''POD_NAME'' эфимерным котнейнером ''busybox''
Можно видеть процессы контейнера ''app''

{{{#!highlight bash
kubectl debug -it -c debugger --target=app --image=busybox ${POD_NAME}
ps auxf

}}}

Создаем копию пода ''POD_NAME'' под названием ''test-pod'' и подключаемся к нему.
Внутри отладчика видим все процессы всех контейнеров пода

{{{#!highlight bash
kubectl debug -it -c debugger --image=busybox \
  --copy-to test-pod \
  --share-processes \
  ${POD_NAME}
ps auxf

}}}