Skip to content

Kubectl

kimschles edited this page Nov 10, 2019 · 18 revisions

https://www.katacoda.com/courses/kubernetes/launch-single-node-cluster https://www.katacoda.com/courses/kubernetes/kubectl-run-containers

get, describe and delete

Abbreviations

  • deploy is deployment
  • rs is replicaset
  • po is pod
  • svc is service
  • ep is endpoint

See it all

  • kubectl get all

See details about your cluster and its health

  • kubectl cluster-info

View the nodes in your cluster

  • kubectl get nodes

More info about nodes in your cluster

  • kubectl get nodes -o wide

See detailed information about a specific node

  • kubectl describe node <name_of_node>

Deploy a container onto the cluster

  • kubectl run <name_of_deployment> <properties>
  • kubectl run --image=<name_of_image_here> server --port=80

Check the status of your pods

  • kubectl get pods

Continue Watching Your Pods

  • kubectl get pods --watch

Delete a pod

  • kubectl delete pod <name_of_pod>

View the status of your deployment

  • kubectl get deployments

More information on your deployment

  • kubectl describe deployment <name_of_deployment>

Show the services you are running

  • kubectl get svc

Scale up the number of pods you have running

  • kubectl scale --replicas-3 deployment http

Provide a dynamic port to a container with NodePort

  • kubectl expose deployment first-deployment --port=80 --type=NodePort

See your namespaces

  • kubectl get ns

Create a new namespace

  • kubectl create ns <name_space_here>

See your contexts

  • kubectl config get-contexts

Configure a Context

  • kubectl config set-context workshop

Set a context

  • kubectl config set-context <name_space_here> --cluster <cluster_name_here> --user <user_here>

Set a context without a user

  • kubectl config set-context <name_space_here> --cluster <cluster_name_here>

Switch to a specific context

  • kubectl config use-context <name_of_context>

Deploy a specific file

  • kubectl apply -f <file>

Apply any command to a specific namespace

  • kubectl <command_here> --namespace=<name>

Kubens

Note: this is a package that needs to be downloaded https://github.com/ahmetb/kubectx

See all namespaces

  • kubens

Switch to a specific namespace

  • kubens <namespace>

See your deployments

  • kubectl get deployments

Exec into a pod

  • kubectl exec -it <name_of_pod>

Run commands in a pod (add a --)

  • kubectl exec -it <name_of_pod> -- /bin/bash

Tail yer Logs

  • kubectl logs -f <name_of_pod>

See all yer logs

  • kubectl logs <name_of_pod>

See events

  • kubectl get events

See Horizontal Pod Autoscalers

  • kubectl get hpa

show yml of namespace

  • kubectl get namespace <ns> -o yml

See elbs that are services through nginx-ingress-controller

  • kubectl get service -n infra

See all ingresses

  • kubectl get ingress --all-namespaces

Get a shell to the container running your pod:

  • kubectl exec -it <name_of_pod> -- /bin/bash

See the env vars of your pod

  • ^ Be connected to the shell (see command above)
  • printenv

Expose a service

  • kubectl expose deployment/<name_of_service> --type="NodePort" --port 8080

See the endpoints object for a service

  • kubectl describe ep <svc_name>

Get your replicasets

  • kubectl get rs

Apply your deployment

  • kubectl apply --filename=deployment.yml --record=true

See the status of your deployment

  • kubectl rollout status deployment <name_of_deployment>

See the history of your rollout

  • kubectl rollout history deploy <name_of_deployment>

Revert back to a different deployment

  • kubectl rollout undo deployment <name_of_deployment> --to-revision=1

Temporarily create a container with some basic tooling on it. It will be destroye when you exit out of it.

  • kubectl run --rm -it --image busybox tmp

echo 'test.back.slash:1|c' | nc -w1 -u datadog-statsd-datadog.kube-system 8125 <-- sends a ping to the datadog agent so you cna see it in the datadog ui

Get all pods in a cluster

  • kubectl get pod -o=custom-columns=NODE:.spec.nodeName,NAME:.metadata.name --all-namespaces

Forward a port to view in your browser

  • kubectl -n pozole port-forward <name-of-pod> 15090:15090
  • Go to http://localhost:15090/

See all resources that are available on your cluster

  • kubectl api-resources

Can I?

  • kubectl can-i <>

Switch Contexts

  • kubectl config use-context <context>

trigger a rolling restart of the aws iam auth pods to pick up the new config map changes:

  • kubectl patch ds aws-iam-authenticator -n kube-system -p '{"spec":{"template":{"metadata":{"annotations":{"rolling_update":"'$(date +%s)'"}}}}}'

Change the image of an nginx pod:

  • kubectl set image pod/nginx nginx=nginx:1.7.1

See what's on your apiserver:

  • kubectl get apiserver

See options for configuration and contexts

  • kubectl config

View the yaml of an object

  • kubectl get <object_type> <name> --export -o yaml

Check a rollout:

  • kubectl rollout status <deploy> <name>

Edit an object's properties

  • kubectl edit <object> <name>

Get available endpoints:

  • kubectl get ep

Expose a deployment on port 6262

  • kubectl expose deploy <deployment_name> --port=6262 --target-port=8080

Port-forwarding

  • `kubectl port-forward <pod_name> -n <name_space> 8080:8080

  • Count the number of pods in a nodepool:

    • k get po --all-namespaces -o wide | grep <name of nodepool> | wc -l k get po --all-namespaces -o wide | grep -v gke-wellio-highmem01pool | wc -l
  • Get the name of a nodepool:

    • k get nodes | awk '{$1}'

Find the AWS name for a node:

  • k describe <node_name> | grep ProviderID
Clone this wiki locally