Skip to content

Instantly share code, notes, and snippets.

@nirdosh17
Last active February 28, 2024 04:12
Show Gist options
  • Save nirdosh17/378a85f2f0cbe15b627fee90a9cd1ccd to your computer and use it in GitHub Desktop.
Save nirdosh17/378a85f2f0cbe15b627fee90a9cd1ccd to your computer and use it in GitHub Desktop.
Kubernetes useful commands for CKAD exam
  • Container commands:

    • Always use command: ['sh', '-c', 'command'] for safety.
    • Do not put your command and parameters in a single string: command: ['cowsay Welcome to CKAD!']
      • The whole string 'cowsay Welcome to CKAD!' is considered as a command which will throw error
    • Common bash script with loop:
      # writes output of command to a file
      while true; do date >> /opt/time/time-check.log; sleep 2 ;done
  • Cron expressions:

    • Every minute: * * * * *
    • Every 30 minute: */30 * * * *
    • Every hour: 0 * * * *
    • Examples
  • Logs:

    • View logs of a particular container: k logs my-pod -c my-container
  • Grep:

    • [Logs] Match pattern with context('n' lines above and below):
      ✗ k logs nginx | grep OS --context=2
      2024/02/27 04:26:36 [notice] 1#1: nginx/1.25.4
      2024/02/27 04:26:36 [notice] 1#1: built by gcc 12.2.1 20220924 (Alpine 12.2.1_git20220924-r10)
      2024/02/27 04:26:36 [notice] 1#1: OS: Linux 6.6.12-linuxkit
      2024/02/27 04:26:36 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
      2024/02/27 04:26:36 [notice] 1#1: start worker processes
  • Test connectivity from another pod:

    • Netcat:
      # nc -vz <service-name> <port>
      nc -vz my-service-name 80
    • Curl
      # if curl is not present in alpine image, install using: apk add curl
      curl http://my-service:80
  • Replace existing resource with force: k replace -f <file> --force

  • Get JSON schema of a Custom Resource Definition(CRD)

    # search for openAPIV3Schema
    ✗ kubectl get crd crontabs.stable.example.com -o json
  • List API Resources, group and version

    ✗ kubectl api-resources
    NAME                              SHORTNAMES   APIVERSION                             NAMESPACED   KIND
    bindings                                       v1                                     true         Binding
    componentstatuses                 cs           v1                                     false        ComponentStatus
    configmaps                        cm           v1                                     true         ConfigMap
    
    ✗ kubectl api-resources | grep configmaps
  • List all resources, group and api version

    # view all nested fields recursively
    ✗ k explain pvc --recursive
    
    # explain specific property
    ✗ k explain pvc.spec.storageClassName --recursive
  • Output custom columns from kubectl command:

    ✗ kubectl get pod --output=custom-columns="NAME:.metadata.name,QOS:.status.qosClass
  • Helm

    Install a chart modifying some property e.g. replica count to 2

    • Find the chart and search the the field name:
      ✗ helm show values bitnami/apache | replica
      replicaCount: 1
    • Now we know the property name, we can modify it during install as:
      ✗ helm -n mercury install internal-issue-report-apache bitnami/apache --set replicaCount=2

    Other commands:

    # list repositories
    helm repo ls
    
    # list all repositories including pending
    helm repo ls -a
    
    # fetch latest versions of charts
    helm repo update
    
    # search a chart 'nginx' in all repositories
    helm search repo nginx
    
    # install a chart in a namespaace 'dev'
    helm -n dev install my-nginx-release bitnami/nginx 
    
    # delete a release and all it's resources
    helm uninstall my-nginx-release
  • Get base64 decoded secret: k describe secret <secret-name>

  • Vim

    • Save and exit wq!
    • Search: /
      • Case sensitive: /nodePort
      • Case insensitive:
        • \c will match anywhere in the string
        • E.g. /nodeport\c will match 'nodeport', 'NodePort' or other casings
      • Nagivate results: * for next occurrence # for prev
    • Line traversing skipping words
      • w: move forward to start of next word
      • b: move backward to start of prev word
    • Go to a line number: :<linenum> e.g. Goes to line 10: :10
    • Display line number: :set number
    • Jumping sentences/paragraphs:
      • (: prev sentence
      • ): next sentence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment