作者:程序那点事儿 日期:2024/01/30 01:29
route
route -n
kubectl apply(不存在就创建,存在就更新,执行的文件较小)
kubectl apply -f kube-flannel.yml
kubectl apply -f nfs-storage-class.yaml
kubectl apply -f nfs-sc-demo-statefulset.yaml
kubectl create(不存就创建,存在报错,可以执行较大的文件)
kubectl create deployment nginx --image=nginx
kubectl create ns ingress-nginx
kubectl create configmap test-dir-config --from-file=test/ # 基于文件夹创建configmap,会加载该文件夹下的所有文件
kubectl create configmap spring-boot-test-yaml --from-file=application.yaml # 基于文件加载
kubectl create configmap spring-boot-test-alises-yaml --from-file=app.yaml=application.yaml # 对加载的文件重命名
kubectl create configmap test-key-value-config --from-literal=username=root --from-literal=password=admin # 以kv的方式加载
kubectl create configmap test-env-config --from-literal=JAVA_OPT_TEST='-Xmx=512 -Xmx=512' --from-literal=APP_ANAME=spring-boot-env-test
kubectl create secret generic orig-secret --from-literal=username=admin --from-literal=password=mysql123
kubectl create secret docker-registry harbor-secret --docker-username=admin --docker-password=Harbor12345 --docker-email=luliang2099@163.com --docker-server=192.168.43.106:8880
kubectl create cm test-dir-config --from-file=./test/ --dry-run -o yaml | kubectl repace -f- # 将test目录下的文件输出为一个yaml文件,并将这个yaml文件发送给k8s 实现容器的配置更新
kubectl create -f nfs-provisioner-rbac.yaml
kubectl scale
kubectl scale deploy --replicas=3 nginx # 部署3个pod(临时生效)
kubectl scale sts web --replicas=5
kubectl autoscale(HPA自动扩容)
kubectl autoscale deploy nginx-deploy --cpu-percent=20 --min=2 --max=5 # 自动扩容
kubectl expose
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl delete
kubectl delete -f kube-flannel.yml
kubectl delete po kube-flannel-ds-amd64-kl6s6 -n kube-system # 删除pod
kubectl delete deploy nginx # 删除deploy会删除deploy下的所有pod
kubectl delete svc nginx # 删除service
kubectl delete sts web --cascade=false # 非级联删除(默认是级联删除)
kubectl delete secret harbor-secret
kubectl delete sa -n kube-system nfs-client-provisioner
kubectl delete role -n kube-system leader-locking-nfs-client-provisioner
kubectl delete clusterrole -n kube-system nfs-client-provisioner-runner
kubectl delete rolebinding -n kube-system leader-locking-nfs-client-provisioner
kubectl delete clusterrolebinding -n kube-system run-nfs-client-provisioner
kubectl set
kubectl set image deployment.apps/nginx nginx=nginx:1.7.9 # 更新命令
kubectl set image deployment.apps/nginx nginx=nginx:1.7.9 --record # 显示修改原因
kubectl get
kubectl get all
kubectl get ns
kubectl get nodes
kubectl get nodes --show-labels
kubectl get deploy
kubectl get deploy nginx -o yaml # 资源信息通过yaml方式输出
kubectl get replicaset
kubectl get ingress
kubectl get ds
kubectl get pods
kubectl get pods --all-namespaces
kubectl get po,rs,deploy
kubectl get cs # 组件状态 componet status 简写
kubectl get all -o wide # 查看完整信息
kubectl get configmap # configmap信息,configmap 可以简写为 cm
###
kubectl get po -o wide # 容器布署的详细信息
time kubectl get po -w # 持续监听容器状态变化,加上time会打印监听时间
### label
kubectl get po --show-labels # 显示pod的标签(默认名称空间)
kubectl get po --show-labels -n kube-system # 显示pod的标签(指定名称空间)
kubectl get po -n ingress-nginx -o wide
kubectl get po -l version=1.0.0,app=nginx # label多条件筛选,是并且关系
kubectl get po -l 'mylabel in (haha123,hehe,haha)' # 多值筛选,是或者关系
kubectl get po -l app=nginx-deploy -o wide
kubectl get po -l 'mylabel in (haha123,hehe,haha),app=nginx' # 多条件 + 多值
##
kubectl get sts
kubectl get pvc
kubectl get pv
kubectl get hpa
kubectl get ep # endpoints
##
kubectl get role -n ingress-nginx -o yaml
kubectl get clusterrole -n ingress-nginx -o yaml
kubectl describe
kubectl describe po kube-flannel-ds-44kh7 -n kube-system # 查看pod详情
kubectl describe deploy nginx # 可以在deploy看到滚动更新的过程
kubectl describe sts web
kubectl describe svc nginx-svc
kubectl describe ep nginx-svc-external
kubectl describe configmap test-dir-config
kubectl describe configmap/test-env-config
kubectl describe secret harbor-secret
kubectl edit
kubectl edit deploy -n kube-system coredns # 输出yaml详情(可以搜索查找内容)
kubectl edit po nginx-85b98978db-gv9pg # pod 不接受修改
kubectl edit secret harbor-secret
kubectl patch
kubectl patch sts web --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value": "nginx:1.9.1"}]' # 更新方式二
kubectl rollout
kubectl rollout status deployments nginx # 监听deploy更新状态
kubectl rollout history deployment.apps/nginx # 查看历史版本
kubectl rollout history deployment.apps/nginx --revision=3 # 查看指定版本详情
kubectl rollout history sts web --revision=2 # 版本详情
kubectl rollout history sts web
kubectl rollout undo deployment.apps/nginx --to-revision=3 # 回退版本
kubectl rollout pause deploy nginx # 暂停(修改时不更新)
kubectl rollout resume deploy nginx # 恢复
kubectl exec
kubectl exec -it nginx-85b98978db-gv9pg -c nginx -- cat /kubectl exec -it nginx-85b98978db-gv9pg -c nginx -- cat /usr/share/nginx/html/index.html # 到pod的nginx容器中执行一条命令
kubectl exec -it nginx-85b98978db-gv9pg -c nginx -- ls /usr/local # 查看容器中文件或目录
kubectl exec -it dns-test -- sh
kubectl exec -it empty-dir-pd -c nginx-empty-dir1 -- sh # 当一个pod有多个容器时,指定进入某个容器中
kubectl cp
kubectl cp test.txt nginx-85b98978db-gv9pg:/usr/local/ # 将本地 test.txt文件拷贝到nginx容器中
kubectl label
kubectl label po nginx-85b98978db-gv9pg mylabel=haha # 为资源添加标签(临时生效)
kubectl label po nginx-85b98978db-gv9pg mylabel=haha123 --overwrite # 覆盖已有标签
kubectl label no k8s-node1 type=mylabel # 为节点添加标签
kubectl label nodes k8s-master ingress- # 删除节点标签
kubectl run
kubectl run -it --image busybox:1.28.4 dns-test /bin/sh
ping web-0.nginx # ping sts 中的容器
nslookup web-0.nginx # 查看dns映射
kubectl replace
kubectl replace -f nginx-deploy.yaml # nginx-deploy.yaml文件替换到deployment.apps/nginx-deploy
kubectl top
kubectl top -h # 查看cpu和内存的占用情况
kubectl top po nginx-deploy-56696fbb5-mzsgg # 查看pod的资源占用情况,需要安装k8s_安装metrics-server
kubectl taint
kubectl taint no k8s-master node-role.kubernetes.io/master:NoSchedule # 添加污点
kubectl taint no k8s-node2 memory=low:NoSchedule # 添加污点
kubectl taint no k8s-master node-role.kubernetes.io/master:NoSchedule- # 删污点
kubectl logs
kubectl logs -f nginx-deploy-56696fbb5-t2fmh # 查看pod的日志
kubectl logs -f nfs-client-provisioner-845cf4584c-4gzlz # 查看制备器日志
测试脚本
while true; do wget -q -O- http://10.96.167.59 > /dev/null; done # 写一个死循环