Skip to content

Instantly share code, notes, and snippets.

@chadmayfield
Last active June 3, 2019 17:08
Show Gist options
  • Save chadmayfield/2ec65e661fbeabacfc2565959fb84f89 to your computer and use it in GitHub Desktop.
Save chadmayfield/2ec65e661fbeabacfc2565959fb84f89 to your computer and use it in GitHub Desktop.

My Kuberentes Command Reference

This is my Kubernetes reference page. Most are commands that I don't want to forget, links to credits where due.

Get detailed cluster information

kubectl cluster-info dump

Get list of Kubernetes node IP addresses

kubectl get nodes -o jsonpath='{ $.items[*].status.addresses[?(@.type=="InternalIP")].address }'

Deploy Kubernetes Dashboard & Retrieve Token/Port

VER="$(curl -sL -o /dev/null -w "%{url_effective}" https://github.com/kubernetes/dashboard/releases/latest | awk -F "/" '{print $NF}')"
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/${VER}/src/deploy/recommended/kubernetes-dashboard.yaml
kubectl delete service -n kube-system kubernetes-dashboard
kubectl expose deployment -n kube-system kubernetes-dashboard --type=NodePort
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: admin-user
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: admin-user
  namespace: kube-system
EOF
kubectl describe secret -n kube-system $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')
kubectl get svc -n kube-system -l k8s-app=kubernetes-dashboard -o jsonpath='{.spec.ports[].nodePort}'

kubectl get pods --all-namespaces --field-selector 'status.phase==Failed' -o json | kubectl delete -f -

kubectl get svc --all-namespaces -o go-template='{{range .items}}{{range.spec.ports}}{{if .nodePort}}{{.nodePort}}{{"\n"}}{{end}}{{end}}{{end}}'

Working with Kubernetes Field Selectors

kubectl get pods -n <namespace> --field-selector=status.phase!=Running
kubectl get pods -n <namespace> --field-selector=status.phase!=Running,spec.restartPolicy=Always

Run command in pod (exec) on current host

kubectl exec -it --namespace=prometheus "$(kubectl get po -o wide -n prometheus -l app=prometheus,component=node-exporter --no-headers=true | grep "$(hostname -f)" | awk '{print $1}')" -- df -h

Get rook OSD status

kubectl get pod -n rook-ceph -l app=rook-ceph-osd -o=jsonpath='{range .items[*]}{.status.conditions[?(@.type=="Ready")].status}{end}'

Run rook Ceph command in Toolbox

kubectl -n rook-ceph exec $(kubectl get pods -n rook-ceph -l app=rook-ceph-tools -o jsonpath='{.items[0].metadata.name}') -- ceph status (other Ceph commands: ceph osd status, ceph health detail, ceph quorum_status --format json-pretty, ceph mon dump, ceph df, ceph osd df, ceph osd pool ls detail)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment