日常工作中最常用的 kubernetes 命令,做下总结
kubctl 常用命令说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 [root@ceph0 ~]# kubectl kubectl controls the Kubernetes cluster manager. Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/ Basic Commands (Beginner): create Create a resource from a file or from stdin. expose 使用 replication controller, service, deployment 或者 pod 并暴露它作为一个 新的 Kubernetes Service run 在集群中运行一个指定的镜像 set 为 objects 设置一个指定的特征 Basic Commands (Intermediate): explain 查看资源的文档 get 显示一个或更多 resources edit 在服务器上编辑一个资源 delete Delete resources by filenames, stdin, resources and names, or by resources and label selector Deploy Commands: rollout Manage the rollout of a resource scale 为 Deployment, ReplicaSet, Replication Controller 或者 Job 设置一个新的副本数量 autoscale 自动调整一个 Deployment, ReplicaSet, 或者 ReplicationController 的副本数量 Cluster Management Commands: certificate 修改 certificate 资源. cluster-info 显示集群信息 top Display Resource (CPU/Memory/Storage) usage. cordon 标记 node 为 unschedulable uncordon 标记 node 为 schedulable drain Drain node in preparation for maintenance taint 更新一个或者多个 node 上的 taints Troubleshooting and Debugging Commands: describe 显示一个指定 resource 或者 group 的 resources 详情 logs 输出容器在 pod 中的日志 attach Attach 到一个运行中的 container exec 在一个 container 中执行一个命令 port-forward Forward one or more local ports to a pod proxy 运行一个 proxy 到 Kubernetes API server cp 复制 files 和 directories 到 containers 和从容器中复制 files 和 directories. auth Inspect authorization Advanced Commands: apply 通过文件名或标准输入流(stdin)对资源进行配置 patch 使用 strategic merge patch 更新一个资源的 field(s) replace 通过 filename 或者 stdin替换一个资源 wait Experimental: Wait for one condition on one or many resources convert 在不同的 API versions 转换配置文件 Settings Commands: label 更新在这个资源上的 labels annotate 更新一个资源的注解 completion Output shell completion code for the specified shell (bash or zsh) Other Commands: alpha Commands for features in alpha api-resources Print the supported API resources on the server api-versions Print the supported API versions on the server, in the form of "group/version" config 修改 kubeconfig 文件 plugin Runs a command-line plugin version 输出 client 和 server 的版本信息 Usage: kubectl [flags] [options] Use "kubectl <command> --help" for more information about a given command. Use "kubectl options" for a list of global command-line options (applies to all commands).
查看所有 namespace 下的 pod
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 [root@ceph0 ~]# kubectl get pods --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE cluster infra-etcd-cluster-0 1/1 Running 0 23d cluster infra-etcd-cluster-1 1/1 Running 0 23d cluster infra-etcd-cluster-2 1/1 Running 0 23d cluster kube-apiserver-5d76df98fc-8vxlb 1/1 Running 0 23d cluster kube-apiserver-5d76df98fc-ds5q7 1/1 Running 1 23d cluster kube-apiserver-5d76df98fc-xvbxn 1/1 Running 0 23d cluster kube-controller-manager-c47df946f-4rvlr 1/1 Running 22 23d cluster kube-controller-manager-c47df946f-j4sw8 1/1 Running 25 23d cluster kube-controller-manager-c47df946f-pkhpt 1/1 Running 25 23d cluster kube-scheduler-688bddcddc-dkw6g 1/1 Running 21 23d cluster kube-scheduler-688bddcddc-ll2hx 1/1 Running 18 23d cluster kube-scheduler-688bddcddc-lpbx9 1/1 Running 22 23d kube-system etcd-192.168.10.186 1/1 Running 1 23d kube-system etcd-192.168.10.187 1/1 Running 0 23d kube-system etcd-192.168.10.25 1/1 Running 1 23d kube-system kube-proxy-4zkgl 1/1 Running 0 23d kube-system kube-proxy-f56fz 1/1 Running 0 23d kube-system kube-proxy-m6k8b 1/1 Running 0 23d kube-system milky-ctrl-9cc54bcbc-8ktvb 1/1 Running 11 23d kube-system milky-ctrl-9cc54bcbc-9mmbm 1/1 Running 2 23d kube-system milky-ctrl-9cc54bcbc-mwrgl 1/1 Running 1 23d
参数解释
属性
说明
NAMESPACE
所在的namespace
NAME
pod 名称
READY
当前准备就绪的pod 1/1 表示当前1和就绪1个 如果是pod 异常了 这个值会发生变化
STATUS
容器的生命周期
RESTARTS
pod 重启的次数
AGE
pod 运行的时间
查看所有 namespace 下的 deployments
get deployment 包括namespace 名称 deployment 的name,以及预期副本数 和当前副本数 最新的副本数以及可用的副本数和运行时间
1 2 3 4 5 6 [root@ceph0 ~]# kubectl get deployments --all-namespaces NAMESPACE NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE cluster kube-apiserver 3 3 3 3 23d cluster kube-controller-manager 3 3 3 3 23d cluster kube-scheduler 3 3 3 3 23d kube-system milky-ctrl 3 3 3 3 23d
查看pod 的 IP 和node 地址
1 2 3 4 5 6 7 8 9 10 11 12 13 14 [root@ceph0 ~]# kubectl get pods --all-namespaces -o wide NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE cluster infra-etcd-cluster-0 1/1 Running 0 23d 192.168.10.187 192.168.10.187 cluster infra-etcd-cluster-1 1/1 Running 0 23d 192.168.10.186 192.168.10.186 cluster infra-etcd-cluster-2 1/1 Running 0 23d 192.168.10.25 192.168.10.25 cluster kube-apiserver-5d76df98fc-8vxlb 1/1 Running 0 23d 192.168.10.187 192.168.10.187 cluster kube-apiserver-5d76df98fc-ds5q7 1/1 Running 1 23d 192.168.10.25 192.168.10.25 cluster kube-apiserver-5d76df98fc-xvbxn 1/1 Running 0 23d 192.168.10.186 192.168.10.186 cluster kube-controller-manager-c47df946f-4rvlr 1/1 Running 22 23d 192.168.10.186 192.168.10.186 cluster kube-controller-manager-c47df946f-j4sw8 1/1 Running 25 23d 192.168.10.187 192.168.10.187 cluster kube-controller-manager-c47df946f-pkhpt 1/1 Running 25 23d 192.168.10.25 192.168.10.25 cluster kube-scheduler-688bddcddc-dkw6g 1/1 Running 21 23d 192.168.10.187 192.168.10.187 cluster kube-scheduler-688bddcddc-ll2hx 1/1 Running 18 23d 192.168.10.186 192.168.10.186 cluster kube-scheduler-688bddcddc-lpbx9 1/1 Running 22 23d 192.168.10.25 192.168.10.25
查看deployment 配置
describe 用来显示一个指定 resource 或者 group 的 resources 详情。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 [root@ceph0 ~]# kubectl describe deployment kube-apiserver -n cluster Name: kube-apiserver Namespace: cluster CreationTimestamp: Fri, 15 Feb 2019 17:20:43 +0800 Labels: <none> Annotations: deployment.kubernetes.io/revision=1 Selector: component=kube-apiserver Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 25% max unavailable, 25% max surge Pod Template: Labels: component=kube-apiserver Containers: kube-apiserver: Image: harbor-inner.env1.qingzhou.com/library/kube-apiserver-amd64:v1.11.1 Port: <none> Host Port: <none> Command: kube-apiserver --bind-address=$(K8S_KUBE_ADVERTISE_ADDRESS) --insecure-bind-address=127.0.0.1 --insecure-port=28080 --secure-port=26443 --authorization-mode=Node,RBAC --allow-privileged=true --kubelet-https=true --apiserver-count=3 --kubelet-client-certificate=/etc/kubernetes/pki/kubernetes.pem --kubelet-client-key=/etc/kubernetes/pki/kubernetes-key.pem --basic-auth-file=/etc/kubernetes/pki/basic-auth.csv --enable-bootstrap-token-auth=true --token-auth-file=/etc/kubernetes/pki/token.csv --client-ca-file=/etc/kubernetes/pki/ca.pem --tls-cert-file=/etc/kubernetes/pki/kubernetes.pem --tls-private-key-file=/etc/kubernetes/pki/kubernetes-key.pem --service-account-key-file=/etc/kubernetes/pki/ca-key.pem --logtostderr=true --log-dir=/var/log/kubernetes --requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-ca.pem --requestheader-allowed-names=kubernetes --requestheader-extra-headers-prefix=X-Remote-Extra- --requestheader-group-headers=X-Remote-Group --requestheader-username-headers=X-Remote-User --proxy-client-cert-file=/etc/kubernetes/pki/front-proxy-client.pem --proxy-client-key-file=/etc/kubernetes/pki/front-proxy-client-key.pem --runtime-config=api/all=true --enable-aggregator-routing=true --admission-control=Initializers,NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,NodeRestriction,ResourceQuota --service-cluster-ip-range=10.88.0.0/16 --etcd-servers=http://192.168.10.186:14389,http://192.168.10.25:14389,http://192.168.10.187:14389 Requests: cpu: 250m Environment: K8S_KUBE_ADVERTISE_ADDRESS: (v1:spec.nodeName) Mounts: /etc/kubernetes/pki/ from k8spki (ro) /etc/localtime from localtime (rw) /var/log/kubernetes/ from datalog (rw) Volumes: k8spki: Type: HostPath (bare host directory volume) Path: /data/user-k8s/cluster/etc/pki HostPathType: DirectoryOrCreate datalog: Type: HostPath (bare host directory volume) Path: /data/user-k8s/cluster/log/kubernetes HostPathType: DirectoryOrCreate localtime: Type: HostPath (bare host directory volume) Path: /etc/localtime HostPathType: Conditions: Type Status Reason ---- ------ ------ Progressing True NewReplicaSetAvailable Available True MinimumReplicasAvailable OldReplicaSets: kube-apiserver-5d76df98fc (3/3 replicas created) NewReplicaSet: <none> Events: <none>
查看pod 的 logs
1 kubectl logs -f pod-name -n namspaces
喜欢新事物,关注云计算行业,对新的技术有追求,喜欢写作和coding