k8s 常见命令

#kubernetes命令自动补全(只针对当前bash)
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
​
#将get信息到处为yaml格式
kubectl get deploy kube-dns -n kube-system -o yaml > kube-dns.yaml
​
#更新yaml文件并更新服务(1.8版本开始改为apply)
kubectl update -f kube-dns.yaml 
​
#编辑当前应用的配置
kubectl edit deployment nginx
​
#查看当前集群中的daemoset及详细信息
kubectl get ds --all-namespaces
kubectl describe daemonset kube-flannel-ds -n kube-system
​
#新建namespace
kubectl create namespace morata-namespace
​
#删除namespace
kubectl delete namespace morata-namespace
​
#设置节点为维护模式(即节点不可被调度,但不影响已有pod的运行)
 kubectl cordon k8s-slave
#关闭维护模式
 kubectl uncordon k8s-slave
 
创建deployment并查看信息


#创建nginx,并且设置为2份
 kubectl run nginx --image=nginx:1.7.9 --replicas=2
 
#删除deployment
 kubectl delete deployment nginx
 
#查看所有deployment
 kubectl get deployment 
​
#查看nignx deployment的详细信息
 kubectl describe deployment nginx 
​
#查看pod
 kubectl get pod
 kubectl get pod nginx-6f8cf9fbc4-lvsqj -o wide
​
#查看pod详细信息
 kubectl describe pod nginx-6f8cf9fbc4-lvsqj 
​
#在pod中运行命令
 kubectl exec nginx-6f8cf9fbc4-lvsqj ls
# 默认在pod 123456-7890的第一个容器中运行“date”并获取输出
$ kubectl exec 123456-7890 date

# 在pod 123456-7890的容器ruby-container中运行“date”并获取输出
$ kubectl exec 123456-7890 -c ruby-container date

 
#进入pod
 kubectl exec nginx-6f8cf9fbc4-lvsqj -it bash
​
#查看replicaset
 kubectl get rs
​
#查看replicaset的详细信息
 kubectl describe rs nginx-6f8cf9fbc4
​
#创建service
 cat nginx.svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - name: http
    port: 8888
    nodePort: 30001
    targetPort: 80
  selector:
    run: nginx
  type: NodePort
  
 kubectl create -f nginx.svc.yaml
 kubectl expose deployment nginx --type=NodePort --name=nginx-test --port=80
 
# 删除service
 kubectl delete -f nginx.svc.yam
 kubectl delete svc nginx
 
#查看service及详细信息
 kubectl get svc
 kubectl describe svc nginx
 
#查看endpoint
 kubectl get endpoints
 NAME         ENDPOINTS                       AGE
 kubernetes   192.168.8.250:6443              10h
 nginx        10.244.0.13:80,10.244.1.31:80   5m
 nginx-test   10.244.0.13:80,10.244.1.31:80   50s
#deployment扩容缩容
 kubectl scale deployment nginx --replicas=3
 
 kubectl scale deployment nginx --replicas=2
 #查看ep、rs、pod都发生扩容/缩容
 kubectl get endpoints 
​
 kubectl get rs 
​
 kubectl get pod
 
#滚动升级/回滚
 #升级nginx至1.9.1
 kubectl set image deploy nginx nginx=nginx:1.9.1
 kubectl edit deployment/nginx-deployment
或者kubectl apply -f nginx-deployment.yaml
#实时观察发布状态:kubectl rollout status deployment/nginx-deployment
#查看deployment历史修订版本
kubectl rollout history deployment/nginx-deploymentkubectl rollout history deployment/nginx-deployment --revision=3
# 回滚到以前版本
kubectl rollout undo deployment/nginx-deploymentkubectl rollout undo deployment/nginx-deployment --to-revision=3
# 扩容deployment的Pod副本数量kubectl scale deployment nginx-deployment --replicas=10
# 设置启动扩容/缩容
kubectl autoscale deployment nginx-deployment --min=10 --max=15 --cpu-percent=80
 #查看升级状态
 kubectl rollout status deployment nginx 
​
  #查看升级历史
  kubectl rollout history deployment nginx 
​
 #查看历史版本详情
 kubectl rollout history deployment nginx --revision=2
​
 #可以查看rs、deployemnt、svc、pod查看升级中的变化
 kubectl get rs 
 kubectl get svc 
 kubectl get pod
 kubectl get deployment 
 
 #查看deployment升级事件详情
 kubectl describe deployment nginx
​
 #升级失败回滚
 kubectl set image deploy nginx nginx=nginx:1.95
 
  #查看升级状态一直处于等待状态
  kubectl rollout status deployment nginx 
    Waiting for rollout to finish: 1 old replicas are pending termination...
  
  #可以查看升级历史、svc、rs、pod等详情可以得知错误原因
  
  #回滚操作
  kubectl rollout undo deployment nginx
  
  #查看deployment回滚详情
  kubectl describe deployment nginx

  

 
posted @ 2022-12-02 13:53  淡忘的时间  阅读(21)  评论(0编辑  收藏  举报