资源对象文件
annotate
#-----------------------------------------#
[root@master k8s]# kubectl apply -f mypod.yaml --record
[root@master k8s]# kubectl get pod mypod -o custom-columns=podName:.metadata.name,annotations:.metadata.annotations."kubernetes\.io/change-cause"
mypod kubectl apply --filename=mypod.yaml --record=true
[root@master k8s]# kubectl annotate pods mypod kubernetes.io/change-cause='my description'
[root@master k8s]# kubectl get pod mypod -o custom-columns=podName:.metadata.name,annotations:.metadata.annotations."kubernetes\.io/change-cause"
api-resources
#-----------------------------------------#
[root@master k8s]# kubectl api-resources -o wide
NAME SHORTNAMES APIVERSION NAMESPACED KIND VERBS
pods po v1 true Pod [get list patch ...]
namespaces ns v1 false Namespace [create get ...]
api-versions
#-----------------------------------------#
[root@master k8s]# kubectl api-versions
admissionregistration.k8s.io/v1
apply
#-----------------------------------------#
[root@master k8s]# kubectl apply -f mypod.yaml
[root@master k8s]# sed 's,mypod,myweb,g' mypod.yaml |kubectl apply -f -
[root@master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
attach
If you don't see a command prompt, try pressing enter.
10.244.219.64:44372: response:200
auth
autoscale
#-----------------------------------------#
[root@master k8s]# kubectl apply -f myDeploy.yaml
deployment.apps/myweb created
[root@master k8s]# kubectl autoscale deployment myweb --min=1 --max=10 --cpu-percent=80
horizontalpodautoscaler.autoscaling/myweb autoscaled
[root@master k8s]# kubectl get horizontalpodautoscalers.autoscaling
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
myweb Deployment/myweb 10%/80% 1 10 1 27m
#-----------------------------------------#
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
targetCPUUtilizationPercentage: 80
certificate
NAME AGE REQUESTOR CONDITION
csr-wsfz7 8s system:node:master Pending
NAME AGE REQUESTOR CONDITION
csr-wsfz7 86s system:node:master Approved,Issued
cluster-info
#-----------------------------------------#
[root@master k8s]# kubectl cluster-info
Kubernetes control plane is running at https://192.168.1.10:6443
CoreDNS is running at https://192.168.1.10:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
completion
# 根据已经给出的 Shell 输出 <Tab> 补全代码
#-----------------------------------------#
[root@master k8s]# source <(kubectl completion bash|tee /etc/bash_completion.d/kubectl)
config
# 创建普通认证用户中的 CN 代表用户名,O 代表组名称
#-----------------------------------------#
[root@master k8s]# openssl genrsa -out luck.key 2048
[root@master k8s]# openssl req -new -key luck.key -out luck.csr -subj "/CN=luck/O=tedu"
[root@master k8s]# mycsr=$(base64 luck.csr |tr -d '\n')
[root@master k8s]# cat <<EOF |kubectl apply -f -
apiVersion: certificates.k8s.io/v1
kind: CertificateSigningRequest
signerName: kubernetes.io/kube-apiserver-client
[root@master k8s]# kubectl get certificatesigningrequests.certificates.k8s.io
NAME AGE SIGNERNAME ... CONDITION
luck-token 12s kubernetes.io/kube-apiserver-client ... Pending
[root@master k8s]# kubectl certificate approve luck-token
[root@master k8s]# kubectl get certificatesigningrequests.certificates.k8s.io
NAME AGE SIGNERNAME ... CONDITION
luck-token 33s kubernetes.io/kube-apiserver-client ... Approved,Issued
[root@master k8s]# kubectl get certificatesigningrequests.certificates.k8s.io luck-token -o jsonpath='{.status.certificate}'| base64 -d >luck.crt
[root@master k8s]# kubectl config --kubeconfig=auth.conf set-cluster k8s-cluster --server=https://192.168.1.10:6443 --certificate-authority=/etc/kubernetes/pki/ca.crt --embed-certs=true
[root@master k8s]# kubectl config --kubeconfig=auth.conf set-credentials adminuser-luck --client-certificate=luck.crt --client-key=luck.key --embed-certs=true
[root@master k8s]# kubectl config --kubeconfig=auth.conf set-context node-cluster --cluster=k8s-cluster --user=adminuser-luck --namespace=default
[root@master k8s]# kubectl config --kubeconfig=auth.conf use-context node-cluster
[root@master k8s]# kubectl create clusterrolebinding luckrole --clusterrole=cluster-admin --user=luck
[root@master k8s]# kubectl config --kubeconfig=auth.conf get-clusters
[root@master k8s]# kubectl config --kubeconfig=auth.conf get-users
[root@master k8s]# kubectl config --kubeconfig=auth.conf get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* node-cluster k8s-cluster adminuser-luck default

convert
#-----------------------------------------#
[root@master k8s]# kubectl convert -f myPod.yaml
cordon
#-----------------------------------------#
[root@master k8s]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 15h v1.22.5
node-0001 Ready node 15h v1.22.5
[root@master k8s]# kubectl cordon node-0001
[root@master k8s]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 15h v1.22.5
node-0001 Ready,SchedulingDisabled node 15h v1.22.5
cp
#-----------------------------------------#
[root@master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myweb-759ffdd494-9956m 1/1 Running 0 5h13m
[root@master k8s]# kubectl cp myweb-759ffdd494-9956m:/var/www/html/index.html ./index.html
tar: Removing leading `/' from member names
[root@master k8s]# ls index.html
[root@master k8s]# echo "hello world" > index.html
[root@master k8s]# kubectl cp index.html myweb-759ffdd494-9956m:/var/www/html/index.html
[root@master k8s]# curl http://10.244.21.168
create
debug
#-----------------------------------------#
[root@master k8s]# kubectl run myapp --image=registry:5000/k8s/pause:3.5 --restart=Never
[root@master k8s]# kubectl debug myapp -it --image=registry:5000/busybox:latest --share-processes --copy-to=debugger
#-----------------------------------------#
[root@master k8s]# vim /etc/kubernetes/manifests/kube-apiserver.yaml
- --feature-gates=EphemeralContainers=true
[root@master k8s]# vim /var/lib/kubelet/config.yaml
EphemeralContainers: true
[root@master k8s]# systemctl restart kubelet
[root@master k8s]# kubectl run myapp --image=registry:5000/k8s/pause:3.5 --restart=Never
[root@master k8s]# kubectl debug -it myapp --image=registry:5000/busybox:latest --target=myapp
dev etc pause proc sys var
delete
#-----------------------------------------#
[root@master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
debugger 2/2 Running 1 (3s ago) 6s
[root@master k8s]# kubectl delete pod debugger
[root@master k8s]# kubectl delete -f mypod.yaml
[root@master k8s]# kubectl get pods
No resources found in default namespace.
describe
Node: node-0001/192.168.1.11
diff(显示目前版本与将要应用的版本之间的差异)
#-----------------------------------------#
[root@master k8s]# kubectl diff -f deploy.yaml
diff -u -N /tmp/LIVE-242154721/apps.v1.Deployment.default.myweb /tmp/MERGED-718616268/apps.v1.Deployment.default.myweb
--- /tmp/LIVE-242154721/apps.v1.Deployment.default.myweb 2021-11-07 15:52:02.711915439 +0800
+++ /tmp/MERGED-718616268/apps.v1.Deployment.default.myweb 2021-11-07 15:52:02.711915439 +0800
deployment.kubernetes.io/revision: "1"
- kubernetes.io/change-cause: httpd.v1
+ kubernetes.io/change-cause: httpd.v2
creationTimestamp: "2021-11-07T07:51:49Z"
drain(清空节点,节点资源被删除也不能被调度)
#-----------------------------------------#
[root@master k8s]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 36h v1.22.5
node-0001 Ready node 36h v1.22.5
[root@master k8s]# kubectl drain node-0001 --delete-emptydir-data --ignore-daemonsets --force
[root@master k8s]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 36h v1.22.5
node-0001 Ready,SchedulingDisabled node 36h v1.22.5
#-----------------------------------------#
[root@master k8s]# kubectl edit pod mypod
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
exec
#-----------------------------------------#
[root@master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myweb-759ffdd494-9956m 1/1 Running 0 27m
[root@master k8s]# kubectl exec -it myweb-759ffdd494-9956m -c httpd -- /bin/bash
[root@myweb-759ffdd494-9956m html]# ls
index.html info.html info.php
explain
#-----------------------------------------#
[root@master k8s]# kubectl explain pod.spec
Specification of the desired behavior of the pod. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
PodSpec is a description of a pod.
expose
#-----------------------------------------#
[root@master k8s]# [root@master k8s]# kubectl expose deployment myweb --port=80 --protocol=TCP --target-port=80 --name=webservice --type ClusterIP
#-----------------------------------------#
#-----------------------------------------#
[root@master k8s]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.254.0.1 <none> 443/TCP 36h
webservice ClusterIP 10.254.17.185 <none> 80/TCP 2s
[root@master k8s]# curl http://10.254.17.185
get
#-----------------------------------------#
[root@master k8s]# kubectl get nodes
ku NAME STATUS ROLES AGE VERSION
master Ready master 36h v1.22.5
node-0001 Ready node 36h v1.22.5
[root@master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
[root@master k8s]# kubectl get pod mypod -o wide
NAME READY STATUS RESTARTS IP ...
mypod 1/1 Running 0 10.244.21.154 ...
[root@master k8s]# kubectl get pod mypod -o yaml
help
Create and run a particular image in a pod.
kubectl run nginx --image=nginx
kustomize
# Kustomize 是一个独立的工具,用来通过 kustomization 文件定制 Kubernetes 对象
# 参考文档: https://cloud.tencent.com/developer/article/1745189
#-----------------------------------------#
[root@master k8s]# vim kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
[root@master k8s]# vim password.txt
[root@master k8s]# kubectl kustomize ./
password.txt: dXNlcm5hbWU9YWRtaW4KcGFzc3dvcmQ9c2VjcmV0Cg==
name: mysecret-2kdd8ckcc7
[root@master k8s]# kubectl apply -k ./
secret/mysecret-2kdd8ckcc7 created
[root@master k8s]# kubectl get secrets
default-token-m7vbm kubernetes.io/service-account-token 3 73d
mysecret-2kdd8ckcc7 Opaque 1 4s
[root@master k8s]# kubectl delete -k ./
secret "mysecret-2kdd8ckcc7" deleted
label
#-----------------------------------------#
[root@master k8s]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
mypod 1/1 Running 0 7m22s <none>
[root@master k8s]# kubectl label pod mypod app=webapp
[root@master k8s]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
mypod 1/1 Running 0 7m41s app=webapp
[root@master k8s]# kubectl label pod mypod app-
[root@master k8s]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
mypod 1/1 Running 0 68m <none>
logs
#-----------------------------------------#
[root@master k8s]# kubectl get pod
NAME READY STATUS RESTARTS AGE
[root@master k8s]# kubectl logs mypod -c linux
10.244.219.64:34666: response:200
options
#-----------------------------------------#
[root@master k8s]# kubectl options
The following options can be passed to any command:
--insecure-skip-tls-verify=false: If true, the server's certificate will not be checked for validity. This will
patch
#-----------------------------------------#
persistentVolumeReclaimPolicy: Recycle
#-----------------------------------------#
[root@master k8s]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS AGE
mypv 5Gi RWO Recycle Available 5s
[root@master k8s]# kubectl patch pv mypv -p '{"spec":{"capacity":{"storage":"8Gi"}}}'
persistentvolume/mypv patched
[root@master k8s]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS AGE
mypv 8Gi RWO Recycle Available 67s
# 插件在 ${PATH} 下,是一个独立的可执行文件,名称以 kubectl- 开头
#-----------------------------------------#
[root@master k8s]# vim /usr/local/bin/kubectl-gettaint
/usr/bin/kubectl get nodes -o custom-columns=NodeName:.metadata.name,Taints:.spec.taints
[root@master k8s]# chmod 755 /usr/local/bin/kubectl-gettaint
[root@master k8s]# kubectl plugin list
The following compatible plugins are available:
/usr/local/bin/kubectl-gettaint
[root@master k8s]# kubectl gettaint
master [map[effect:NoSchedule key:node-role.kubernetes.io/master]]

port-forward
#-----------------------------------------#
[root@master k8s]# kubectl port-forward --address 0.0.0.0 pod/mypod 8080 80
Forwarding from 0.0.0.0:8080 -> 8080
Forwarding from 0.0.0.0:80 -> 80
#-----------------------------------------#
[root@master local]# curl http://master:8080
proxy
# 运行一个 kubernetes API 服务器代理
#-----------------------------------------#
[root@master k8s]# kubectl proxy --port=80
Starting to serve on 127.0.0.1:80
#-----------------------------------------#
[root@master k8s]# curl http://127.0.0.1/version
"gitCommit": "c92036820499fedefec0f847e2054d824aea6cd1",
"buildDate": "2021-10-27T18:35:25Z",
"platform": "linux/amd64"
replace
#-----------------------------------------#
[root@master k8s]# kubectl replace --force -f mypod.yaml
rollout
set
# 为资源对象设置功能特性
#-----------------------------------------#
[root@master k8s]# kubectl set env pods --all --list
# Pod mypod, container linux
[root@master k8s]# kubectl set env deployment/myweb myEnv=prod
deployment.apps/myweb env updated
[root@master k8s]# kubectl exec -i -t myweb-6c645646c9-pqjc7 -- sh -c 'echo ${myEnv}'
prod
[root@master k8s]# kubectl get deployments.apps myweb -o wide
NAME READY AGE CONTAINERS IMAGES SELECTOR
myweb 1/1 25s httpd registry:5000/myos:httpd app=apache
[root@master k8s]# kubectl set image deployment/myweb httpd=registry:5000/myos:nginx
deployment.apps/myweb image updated
[root@master k8s]# kubectl get deployments.apps myweb -o wide
NAME READY AGE CONTAINERS IMAGES SELECTOR
myweb 1/1 45s httpd registry:5000/myos:nginx app=apache
taint
# 在一个或者多个节点上更新污点配置
#-----------------------------------------#
[root@master k8s]# kubectl get nodes -o custom-columns=NodeName:.metadata.name,Taints:.spec.taints
NodeName Taints
master [map[effect:NoSchedule key:node-role.kubernetes.io/master]]
node-0001
[root@master k8s]# kubectl taint node node-0001 k=v:PreferNoSchedule
node/node-0001 tainted
[root@master k8s]# kubectl get nodes -o custom-columns=NodeName:.metadata.name,Taints:.spec.taints
NodeName Taints
master [map[effect:NoSchedule key:node-role.kubernetes.io/master]]
node-0001 [map[effect:PreferNoSchedule key:k value:v]]
[root@master k8s]# kubectl taint node node-0001 k-
node/node-0001 untainted
[root@master k8s]# kubectl get nodes -o custom-columns=NodeName:.metadata.name,Taints:.spec.taints
NodeName Taints
master [map[effect:NoSchedule key:node-role.kubernetes.io/master]]
#-----------------------------------------#
[root@master k8s]# kubectl rollout history deployment
[root@master k8s]# kubectl rollout undo deployment myweb --to-revision=1
deployment.apps/myweb rolled back
[root@master k8s]# kubectl rollout history deployment
#-----------------------------------------#
[root@master k8s]# kubectl run mypod --image=registry:5000/myos:httpd
#-----------------------------------------#
- image: registry:5000/myos:httpd
#-----------------------------------------#
[root@master k8s]# kubectl apply -f myDeploy.yaml
deployment.apps/myweb created
[root@master ~]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
[root@master ~]# kubectl scale deployment myweb --replicas=3
deployment.apps/myweb scaled
[root@master ~]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE

top
#-----------------------------------------#
[root@master k8s]# kubectl top nodes
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
node-0001 45m 2% 931Mi 11%
[root@master k8s]# kubectl top pods
NAME CPU(cores) MEMORY(bytes)
uncordon
# 解除(cordon、drain)资源不可调度标记
#-----------------------------------------#
[root@master k8s]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 15h v1.22.5
node-0001 Ready,SchedulingDisabled node 15h v1.22.5
[root@master k8s]# kubectl uncordon node-0001
node/node-0001 uncordoned
[root@master k8s]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 15h v1.22.5
node-0001 Ready node 15h v1.22.5
version
#-----------------------------------------#
[root@master k8s]# kubectl version -o yaml
buildDate: "2021-10-27T18:41:28Z"
gitCommit: c92036820499fedefec0f847e2054d824aea6cd1
buildDate: "2021-10-27T18:35:25Z"
gitCommit: c92036820499fedefec0f847e2054d824aea6cd1
#-----------------------------------------#
terminationGracePeriodSeconds: 0
image: registry:5000/busybox:latest
imagePullPolicy: IfNotPresent
image: registry:5000/busybox:latest
imagePullPolicy: IfNotPresent
echo "hello world !!!" >/var/www/index.html
httpd -v -f -p 0.0.0.0:80 -h /var/www
#-----------------------------------------#
[root@master k8s]# kubectl replace --force -f mypod.yaml
[root@master k8s]# time kubectl wait --for=condition=Ready pod/mypod
