k8s常用命令

weixin_33912445發表於2018-10-30

檢視日誌

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