k8s常用命令

查看日志

root@bob-k8s3:/home/bob/CICD-document/jira# kubectl get pods
NAME                    READY     STATUS     RESTARTS   AGE
jira-7d659bbc4d-v6n85   0/1       Init:0/1   0          20m
pgset-0                 1/1       Running    0          1h
pgset-1                 1/1       Running    1          1h

root@bob-k8s3:/home/bob/CICD-document/jira# kubectl get pods --namespace=development
root@bob-k8s3:/home/bob/CICD-document/jira# kubectl logs jira-7d659bbc4d-v6n85
Error from server (BadRequest): container "jira" in pod "jira-7d659bbc4d-v6n85" is waiting to start: PodInitializing
root@bob-k8s3:/home/bob/CICD-document/jira# kubectl get events
root@bob-k8s3:/home/bob/CICD-document/jira# journalctl -f -u kubelet.service
root@bob-k8s3:/home/bob/CICD-document/jira# journalctl -u kubelet
root@bob-k8s3:/home/bob/CICD-document/jira# journalctl -xe

进入pod

root@bob-k8s3:/home/bob/CICD-document/jira# kubectl get pods
NAME                    READY     STATUS     RESTARTS   AGE
pgset-0                 1/1       Running    0          1h
pgset-1                 1/1       Running    1          1h
root@bob-k8s3:/home/bob/CICD-document/jira# kubectl exec -it pgset-0 sh

发布,根据 yaml 创建资源, apply 可以重复执行,create 不行.
Those are two different approaches. kubectl create is what we call Imperative Management. On this approach you tell the Kubernetes API what you want to create, replace or delete, not how you want your K8s cluster world to look like.

kubectl apply is part of the Declarative Management approach, where changes that you may have applied to a live object (i.e. through scale) are maintained even if you apply other changes to the object.

You can read more about imperative and declarative management in the Kubernetes Object Managementdocumentation.

root@bob-k8s3:/home/bob/CICD-document/jira# kubectl apply -f jira.yml
service "jira" created
deployment "jira" created

root@bob-k8s3:/home/bob/CICD-document/jira# kubectl create -f jira.yml
service "jira" created
deployment "jira" created

删除service, deployment


root@bob-k8s3:/home/bob/CICD-document/jira# kubectl delete -f jira.yml
service "jira" deleted
deployment "jira" deleted

其他

# 查看所有 pod 列表,  -n 后跟 namespace, 查看指定的命名空间
kubectl get pod
kubectl get pod -n kube  


# 查看 RC 和 service 列表, -o wide 查看详细信息
kubectl get rc,svc
kubectl get pod,svc -o wide  
kubectl get pod  -o yaml


# 显示 Node 的详细信息
kubectl describe node 192.168.0.212

# 显示 Pod 的详细信息, 特别是查看 pod 无法创建的时候的日志
kubectl describe pod 
eg:
kubectl describe pod redis-master-tqds9

# 基于 pod.yaml 定义的名称删除 pod 
kubectl delete -f pod.yaml 

# 删除所有包含某个 label 的pod 和 service
kubectl delete pod,svc -l name=


# 删除所有 Pod
kubectl delete pod --all
#强制删除pod
kubectl delete pods  --grace-period=0 --force

# 查看 endpoint 列表
kubectl get endpoints


# 执行 pod 的 date 命令
kubectl exec  -- date
kubectl exec  -- bash
kubectl exec  -- ping 10.24.51.9

# 通过bash获得 pod 中某个容器的TTY,相当于登录容器
kubectl exec -it  -c  -- bash
eg:
kubectl exec -it redis-master-cln81 -- bash

# 查看容器的日志
kubectl logs 
kubectl logs -f  # 实时查看日志

# Edit the service named 'docker-registry':
  kubectl edit svc/docker-registry

  # Use an alternative editor
  KUBE_EDITOR="nano" kubectl edit svc/docker-registry

  # Edit the job 'myjob' in JSON using the v1 API format:
  kubectl edit job.v1.batch/myjob -o json

  # Edit the deployment 'mydeployment' in YAML and save the modified config in its annotation:
  kubectl edit deployment/mydeployment -o yaml --save-config

#以Yaml格式导出系统中已有资源描述 To get the yaml for a deployment (service, pod, secret, etc)
kubectl get deployment mysql --export -o yaml > mysql.yaml

#o yaml --dry-run flags with kubectl run or kubectl create 
kubectl run my-cool-app —-image=me/my-cool-app:v1 -o yaml --dry-run > my-cool-app.yaml
kubectl create secret generic my-secret --from-literal=foo=bar -o yaml --dry-run > my-secret.yaml
 
 

Kubernetes等待部署完成 kubectl wait rollout

#等待部署完成不能使用kubectl wait, 该命令只能判断deployment是否available,不能用来判断rollout,即available状态的deployment,很可能老的pod还在terminating,新的pod还没创建好。
kubectl rollout status -n qa -w deployment/pilot-resource-api-server

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