欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

k8s常用命令

程序员文章站 2022-03-12 12:17:24
...

查看日志

[email protected]:/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

[email protected]:/home/bob/CICD-document/jira# kubectl get pods --namespace=development
[email protected]:/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
[email protected]:/home/bob/CICD-document/jira# kubectl get events
[email protected]:/home/bob/CICD-document/jira# journalctl -f -u kubelet.service
[email protected]:/home/bob/CICD-document/jira# journalctl -u kubelet
[email protected]:/home/bob/CICD-document/jira# journalctl -xe

进入pod

[email protected]:/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
[email protected]:/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.

[email protected]:/home/bob/CICD-document/jira# kubectl apply -f jira.yml
service "jira" created
deployment "jira" created

[email protected]:/home/bob/CICD-document/jira# kubectl create -f jira.yml
service "jira" created
deployment "jira" created

删除service, deployment


[email protected]:/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 <pod-name> -o yaml


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

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

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

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


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

# 查看 endpoint 列表
kubectl get endpoints


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

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

# 查看容器的日志
kubectl logs <pod-name>
kubectl logs -f <pod-name> # 实时查看日志

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