kubernetes.io > Documentation > Reference > kubectl CLI > kubectl Cheat Sheet
kubernetes.io > Documentation > Tasks > Monitoring, Logging, and Debugging > Get a Shell to a Running Container
kubernetes.io > Documentation > Tasks > Access Applications in a Cluster > Configure Access to Multiple Clusters
kubernetes.io > Documentation > Tasks > Access Applications in a Cluster > Accessing Clusters using API
kubernetes.io > Documentation > Tasks > Access Applications in a Cluster > Use Port Forwarding to Access Applications in a Cluster
show
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
show
kubectl config view
show
KUBECONFIG=~/.kube/config:~/.kube/kubconfig2
show
# create a file named role.yml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods", "pods/log"]
verbs: ["get", "watch", "list"]
# create the role
kubectl apply -f role.yml
show
# create a file named role-binding.yml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: pod-reader
namespace: default
subjects:
- kind: User
name: dev
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
show
kubectl config set-context --current --namespace=ggckad-s2
show
kubectl config set-context gce --user=cluster-admin --namespace=foo \
&& kubectl config use-context gce
show
kubectl get svc -n kube-system
show
kubectl get po --all-namespaces
show
kubectl get pods -o wide
show
kubectl get deployment my-deployment
show
kubectl get pods
show
kubectl get po nginx -o yaml
show
kubectl describe po nginx
show
kubectl logs nginx
show
kubectl get pod my-pod -o yaml
show
kubectl get nodes
# or, get more information about the nodes
kubectl get nodes -o wide
show
kubectl describe nodes
show
kubectl get services --sort.by=.metadata.name
show
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
show
kubectl create namespace web
show
kubectl get namespaces
show
kubectl run nginx --image=nginx
# or
kubectl run nginx2 --image=nginx --restart=Never --dry-run -o yaml | kubectl create -f -
show
kubectl delete po nginx
show
# check the livez endpoint
curl -k https://localhost:6443/livez?verbose
# or
kubectl get --raw='/livez?verbose'
# check the readyz endpoint
curl -k https://localhost:6443/readyz?verbose
# or
kubectl get --raw='/readyz?verbose'
# check the healthz endpoint
curl -k https://localhost:6443/healthz?verbose
# or
kubectl get --raw='/healthz?verbose'
show
# create a deployment object using this YAML template with the following command
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
replicas: 2
selector:
matchLabels:
run: nginx
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
run: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}
EOF
# create the file deploy.yaml with the content above
vim deploy.yaml
# create the deployment
kubectl apply -f deploy.yaml
# get verbose output of deployment YAML
kubectl get deploy nginx-deployment -o yaml
# add an annotation to the deployment
kubectl annotate deploy nginx mycompany.com/someannotation="chad"
# delete the deployment
kubectl delete deploy nginx
show
kubectl annotate deploy nginx mycompany.com/someannotation="chad"
show
kubectl label pods nginx env=prod
show
kubectl get pods --show-labels
# or get pods with the env label
kubectl get po -L env
show
kubectl get po --field-selector status.phase=Running
show
kubectl get svc --field-selector metadata.namespace=default
show
kubectl api-resources
show
systemctl list-unit-files --type service --all | grep kube
show
systemctl status kubelet
Use the imperative command to create a pod named nginx-pod with the image nginx, but save it to a YAML file named pod.yaml instead of creating it
show
kubectl run nginx --image nginx-pod --dry-run=client -o yaml > pod.yaml
show
kubectl get svc -A