kubectl常用命令

kubectl常用命令


#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
 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 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


你可能感兴趣的:(kubectl常用命令)