Command 的子部分
Kubectl CheatSheet
Switch Context
- use different config
kubectl --kubeconfig /root/.kube/config_ack get pod
Resource
create resource
Resource Fromkubectl create -n <$namespace> -f <$file_url>
helm install <$resource_id> <$resource_id> \ --namespace <$namespace> \ --create-namespace \ --version <$version> \ --repo <$repo_url> \ --values resource.values.yaml \ --atomic
debug resource
kubectl -n <$namespace> describe <$resource_id>
- logging resource
kubectl -n <$namespace> logs -f <$resource_id>
- port forwarding resource
kubectl -n <$namespace> port-forward <$resource_id> --address 0.0.0.0 8080:80 # local:pod
- delete all resource under specific namespace
kubectl delete all --all -n <$namespace>
- delete error pods
kubectl -n <$namespace> delete pods --field-selector status.phase=Failed
- force delete
kubectl -n <$namespace> delete pod <$resource_id> --force --grace-period=0
- opening a Bash Shell inside a Pod
kubectl -n <$namespace> exec -it <$resource_id> -- bash
- copy secret to another namespace
kubectl -n <$namespaceA> get secret <$secret_name> -o json \
| jq 'del(.metadata["namespace","creationTimestamp","resourceVersion","selfLink","uid"])' \
| kubectl -n <$namespaceB> apply -f -
- copy secret to another name
kubectl -n <$namespace> get secret <$old_secret_name> -o json | \
jq 'del(.metadata["namespace","creationTimestamp","resourceVersion","selfLink","uid","ownerReferences","annotations","labels"]) | .metadata.name = "<$new_secret_name>"' | \
kubectl apply -n <$namespace> -f -
- delete all completed job
kubectl delete jobs -n <$namespace> --field-selector status.successful=1
Nodes
- add taint
kubectl taint nodes <$node_ip> <key:value>
- remove taint
kubectl remove taint
- show info extract by json path
kubectl get nodes -o jsonpath='{.items[*].spec.podCIDR}'
Deploy
- rollout show rollout history
kubectl -n <$namespace> rollout history deploy/<$deploy_resource_id>
undo rollout
kubectl -n <$namespace> rollout undo deploy <$deploy_resource_id> --to-revision=1