Kubernetes(K8S) 常用命令
Docker 常用命令
# 查看API版本
[root@k8smaster ~]# kubectl api-versions
# 重启 K8S
[root@k8smaster ~]# systemctl restart kubelet
# 查看 kubelet 状态
[root@k8smaster ~]# systemctl status kubelet
# 重启 POD kubectl rollout restart deployment <deployment-name> -n <namespace>
[root@k8smaster ~]# kubectl rollout restart deployment javademo1 -n default
# 查看节点资源,需要安装metrics-server 【https://www.cnblogs.com/vipsoft/p/16896510.html】
[root@k8smaster ~]# kubectl top node
# 查看 Pod 在节点上的资源分配情况
[root@k8smaster ~]# kubectl describe node
# 查看 Pod 资源使用情况
[root@k8smaster ~]# kubectl top pod -A
# 查看 POD 在哪个节点上运行
[root@k8smaster ~]# kubectl get pod -A -o wide
# 查看 所有 secret
[root@k8smaster ~]# kubectl get secret -A
# 查看 secret 明细
[root@k8smaster ~]# kubectl describe secret -A
# 创建 secret
[root@k8smaster ~]# kubectl create secret docker-registry registry-vipsoft-shanghai \
--namespace=default \
--docker-server=registry.cn-shanghai.aliyuncs.com \
--docker-username=vipsoft \
--docker-password=123456 \
--docker-email=xxx@xxx.com
# 删除 secret
[root@k8smaster ~]# kubectl delete secret registry-vipsoft-shanghai -n namespace
# 弹性伸缩 创建 3 个副本
[root@k8smaster ~]# kubectl scale deployment javademo1 --replicas=3
# 查看所有命名空间
[root@k8smaster ~]# kubectl get namespace
# 查看 node3的详情
[root@k8smaster ~]# kubectl describe nodes k8snode3
# 给node打标签
[root@k8smaster ~]# kubectl label node k8snode1 env_role=dev
# 查看 node 标签
[root@k8smaster ~]# kubectl get nodes k8snode1 --show-labels
# 查看当前节点的污点
[root@k8smaster ~]# kubectl describe node k8snode3 | grep Taint
# 添加污点 kubectl taint node 节点名称 key=value:污点值-
[root@k8smaster ~]# kubectl taint node k8snode1 key=value:NoSchedule
# 删除污点 kubectl taint node 节点名称 key=value:污点值-
[root@k8smaster ~]# kubectl taint node k8snode1 env_role:NoSchedule-
# 删除 svc
[root@k8smaster ~]# kubectl delete svc nginx
# 删除 svc
[root@k8smaster ~]# kubectl delete statefulset --all
# 创建命名空间
[root@k8smaster ~]# kubectl create ns vipsoft-dev
# 看所有Pod都在哪些节点上运行
[root@k8smaster ~]# kubectl get pod -A -o yaml |grep '^ n'|grep -v nodeSelector|awk 'NR%3==1{print ++n"\n"$0;next}1'
创建 Deployment
# 创建 POD
[root@k8smaster ~]# kubectl create -f xx.yaml
[root@k8smaster ~]# kubectl apply -f xx.yaml
# 如果yaml文件中的kind值为deployment,那么上面这两个命令都可以创建一个deployment,生成相应数量的pod
区别:
kubectl create:
(1)kubectl create命令,是先删除所有现有的东西,重新根据yaml文件生成新的。所以要求yaml文件中的配置必须是完整的
(2)kubectl create命令,用同一个yaml 文件执行替换replace命令,将会不成功,fail掉。
kubectl apply:
kubectl apply 命令,根据配置文件里面列出来的内容,升级现有的。所以yaml文件的内容可以只写需要升级的属性
# 删除 Pod nfs-nginx.yaml
[root@k8smaster ~]# kubectl delete -f nfs-nginx.yaml
# 查看 pod 的日志 并 持续打印
[root@k8smaster ~]# kubectl logs -f --tail=200 pods/podname -n namespace
# 进到容器
[root@k8smaster ~]# kubectl exec -it nginx-pvc-58b7bf955f-5clzg bash
# 查看 K8S 配置
[root@k8smaster ~]# cat $HOME/.kube/config
# 查看 pods 情况
[root@k8smaster ~]# kubectl get pods -n kube-system
# 查看 nodes 情况
[root@k8smaster ~]# kubectl get nodes
# 删除 nodes
[root@k8smaster ~]# kubectl drain centos-7-56 --delete-local-data --force --ignore-daemonsets node/centos-7-56 cordoned
# 查看default 命名空间下的 pod 状态
[root@k8smaster ~]# kubectl get pods
# 查看所有命名空间下的 pod
[root@k8smaster ~]# kubectl get pods -A
# 查看 pod,排除 default 命名空间
[root@k8smaster ~]# kubectl get pod -A |grep -v default
# 查看所有 javademo1 的 pod 的详情
[root@k8smaster ~]# kubectl describe pod javademo1
# 对外暴露 80 端口
[root@k8smaster ~]# kubectl expose deployment nginx --port=80 --type=NodePort
[root@k8smaster ~]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/javademo1-d7856c75c-czv2g 1/1 Running 0 152m
pod/javademo1-d7856c75c-n28rs 1/1 Running 0 151m
pod/javademo1-d7856c75c-xzqjc 1/1 Running 0 151m
pod/nginx-f89759699-5hkdw 1/1 Running 0 26d
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/javademo1 NodePort 10.106.43.46 <none> 8111:31452/TCP 20d
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 26d
service/nginx NodePort 10.103.87.81 <none> 80:30339/TCP 26d
# -o wide 显示资源的额外信息, 可以显示 pod 在哪个节点上运行
[root@k8smaster ~]# kubectl get pods -n ingress-nginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-ingress-controller-5cb8688798-gspq4 1/1 Running 0 18h 10.244.2.39 k8snode2 <none> <none>
[root@k8smaster ~]# kubectl describe svc javademo1
Name: javademo1
Namespace: default
Labels: app=javademo1
Annotations: <none>
Selector: app=javademo1
Type: NodePort
IP: 10.106.43.46
Port: <unset> 8111/TCP
TargetPort: 8111/TCP
NodePort: <unset> 31452/TCP
Endpoints: 10.244.1.31:8111,10.244.1.32:8111,10.244.2.38:8111
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@k8smaster ~]#
# 查看创建的 Deployment 对象
[root@k8smaster ~]# kubectl get deployments
# 查看 Deployeement 详情
[root@k8smaster ~]# kubectl describe deployment/javademo1
# 查看 Deployment 上线状态
[root@k8smaster ~]# kubectl rollout status deployment/javademo1
# 查看 Deployment 对象创建的 ReplicaSet:
[root@k8smaster ~]# kubectl get rs
# 查看 Deployment 对象操作 ReplicaSet 创建的 Pod,并显示生成的标签:
[root@k8smaster ~]# kubectl get pods --show-labels
#查看当前运行的 Pod
[root@k8smaster ~]# kubectl get pods
# 查看当前证书到期时间
[root@k8smaster ~]# kubeadm alpha certs check-expiration
# 初次创建,生成 yaml 文件 --dry-run,减少编写量,避免出错
[root@k8smaster ~]# kubectl create deployment javademo1 --image=registry.cn-shanghai.aliyuncs.com/vipsoft/vipsoft:1.0.0 --dry-run -o yaml > javademo1.yaml
# 已部署的,导出 yaml 文件
[root@k8smaster ~]# kubectl get deployment javademo1 -o yaml > javademo2.yaml
升级
# kubectl describe deployment/javademo1
# 查看 Deployeement 详情 里可以看到 Containers 内容
# Containers:
# vipsoft:
# Image: registry.cn-shanghai.aliyuncs.com/vipsoft/vipsoft:1.0
# 设置vipsoft容器中的镜像,镜像版本变了,就会触发重新拉取动作
[root@k8smaster ~]# kubectl set image deployment/javademo1 vipsoft=registry.cn-shanghai.aliyuncs.com/vipsoft/vipsoft:2.0
deployment.apps/javademo1 image updated
# 查看状态
[root@k8smaster ~]# kubectl rollout status deployment/javademo1
deployment "javademo1" successfully rolled out
# 修改 yaml 文件,直接更新版本号
[root@k8smaster ~]# kubectl edit deployment/javademo1
回滚
# 查看 Deployment 部署历史, CHANGE-CAUSE 里没有值,需要在创建 Deployment 时,使用 --record 参数,就可以在 CHANGE-CAUSE 列看到每个版本使用的命令了
[root@k8smaster ~]# kubectl rollout history deployment/javademo1
# 查看版本 2 的信息:
[root@k8smaster ~]# kubectl rollout history deployment/javademo1 --revision=2
# 这边没加参加,回滚到上一个版本
[root@k8smaster ~]# kubectl rollout undo deployment/javademo1
# 加上参数,回滚到指定版本
[root@k8smaster ~]# kubeetl rollout undo deployment/javademo1 --to-revision=2
# 查看 Deployeement 详情
[root@k8smaster ~]# kubectl describe deployment/javademo1
本文来自博客园,作者:VipSoft 转载请注明原文链接:https://www.cnblogs.com/vipsoft/p/16868545.html