Skip to content

Instantly share code, notes, and snippets.

@mikejoh
Last active March 13, 2024 08:02
Show Gist options
  • Save mikejoh/449c50058bbded6c1634b66f45accff3 to your computer and use it in GitHub Desktop.
Save mikejoh/449c50058bbded6c1634b66f45accff3 to your computer and use it in GitHub Desktop.

Revisions

  1. mikejoh revised this gist Jan 16, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions kubectl-one-liners.md
    Original file line number Diff line number Diff line change
    @@ -34,6 +34,7 @@ kubectl label pod POD that=thing
    Select pods based on selector across all namespaces:
    ```
    kubectl get pods --all-namespaces --selector this=label
    kubectl get pods --all-namespaces -l this=label
    ```
    Create a single Pod without a Deployment (`--restart=Never`) and a ReplicaSet:
    ```
  2. mikejoh revised this gist Jan 16, 2020. No changes.
  3. mikejoh revised this gist Jan 16, 2020. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions kubectl-one-liners.md
    Original file line number Diff line number Diff line change
    @@ -61,4 +61,8 @@ kubectl patch deployment nginx -p '{"spec":{"template":{"spec":{"containers":[{
    Sort Kubernetes events on creation timestamp:
    ```
    kubectl get events --sort-by=.metadata.creationTimestamp
    ```
    Output the number of Pods `foo` and skip Pods `bar` that are running per node:
    ```
    kubectl get pods -o wide | grep foo | grep Running | grep -v bar | awk '{print $7}' | sort | uniq -c | sort -nr | wc -l
    ```
  4. mikejoh revised this gist Sep 24, 2018. 1 changed file with 64 additions and 1 deletion.
    65 changes: 64 additions & 1 deletion kubectl-one-liners.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,64 @@
    kubectl get events --sort-by=.metadata.creationTimestamp
    # kubectl one-liners

    Enable `kubectl` completion (needs the `bash-completion` package):
    ```
    source <(kubectl completion bash)
    ```
    Dry-run, outputs Service (--expose) and a Deployment in yaml:
    ```
    kubectl run --image=apache \
    --port=80 \
    --replicas=3 \
    --restart='Always' \
    --expose \
    --requests='cpu=100m,memory=256Mi' \
    --limits='cpu=200m,memory=512Mi' \
    --labels=app=apache,version=1 \
    --dry-run=true \
    -o yaml
    ```
    In a running container run `date`:
    ```
    kubectl exec POD -- bash -c "date"
    kubectl exec POD -- date
    kubectl exec POD date
    ```
    Remove label `this` from a pod:
    ```
    kubectl label pod POD this-
    ```
    Add label `that=thing` to a pod:
    ```
    kubectl label pod POD that=thing
    ```
    Select pods based on selector across all namespaces:
    ```
    kubectl get pods --all-namespaces --selector this=label
    ```
    Create a single Pod without a Deployment (`--restart=Never`) and a ReplicaSet:
    ```
    kubectl run nginx --image=nginx --restart=Never
    ```
    Create a single Pod with a Deployment (and a ReplicaSet):
    ```
    kubectl run nginx --image=nginx --replicas=1
    ```
    How the `--restart` flag behaves with `kubectl run`:
    ```
    --restart=Never Creates a single Pod without a Deployment or a ReplicaSet. You can achive this by creating a single Pod manifest and apply it.
    --restart=OnFailure Creates a Pod and a Job.
    --restart=Always Default, creates a Deployment and a ReplicaSet object.
    ```
    Copy file to/from a Pod:
    ```
    kubectl cp POD:/path/to/file.txt ./file.txt
    kubectl $HOME/file.txt POD:/path/to/file.txt
    ```
    Patch a Deployment with a new image:
    ```
    kubectl patch deployment nginx -p '{"spec":{"template":{"spec":{"containers":[{ "name":"nginx", "image":"nginx:1.13.1"}]}}}}'
    ```
    Sort Kubernetes events on creation timestamp:
    ```
    kubectl get events --sort-by=.metadata.creationTimestamp
    ```
  5. mikejoh created this gist Sep 24, 2018.
    1 change: 1 addition & 0 deletions kubectl-one-liners.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    kubectl get events --sort-by=.metadata.creationTimestamp