k8s核心组件----kubectl常用命令总结
本帖最后由 林宝宝 于 2019-8-14 14:01 编辑问题导读:
1、kubectl 大量子命令有哪些用处?
2、kubectl patch的各种操作是什么?
3、获取各种信息的指令是什么?该如何使用?
一、k8s集群搭建
可以参考我的另一篇文章搭建一个集群进行演练:k8s核心组件----kubeadm初始化集群(v1.14.2)
二、kubectl简介
kubectl 是 Kubernetes 的命令行工具(CLI),是 Kubernetes 用户和管理员必备的管理工具。
kubectl 提供了大量的子命令,方便管理 Kubernetes 集群中的各种功能。以下是查看子命令的帮助命令:
kubectl -h:查看子命令列表
kubectl options:查看全局选项
kubectl --help:查看子命令的帮助
kubectl -o=:设置输出格式(如 json、yaml、jsonpath 等)
kubectl explain :查看资源的定义
三、命令行自动补全配置
为了方便练习k8s常用命令使用,配置命令行自动补全
Centos
yum install -y bash-completion
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
Ubuntu
curl -LO https://storage.googleapis.com/k ... linux/amd64/kubectl
chmod +x kubectl
mv kubectl/usr/local/bin/
sudo mv kubectl/usr/local/bin/
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
四、kubectl常用命令总结
1、kubectl version
输出 client 和 server 的版本信息
# kubectl version
Client Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.2", GitCommit:"66049e3b21efe110454d67df4fa62b08ea79a19b", GitTreeState:"clean", BuildDate:"2019-05-16T16:23:09Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.2", GitCommit:"66049e3b21efe110454d67df4fa62b08ea79a19b", GitTreeState:"clean", BuildDate:"2019-05-16T16:14:56Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}
2、kubectl run
用法:kubectl run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas] [--dry-run=bool] [--overrides=inline-json] [--command] --
2.1、创建一个镜像为nginx、副本为2、暴露端口为80的nginx容器
# kubectl run nginx --image=nginx --replicas=2 --port=80
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx created
3、kubectl create
用法:kubectl create -f FILENAME ,创建资源
3.1、创建dashboard通过yaml文件
可以参考我的另一篇文章:k8s UI组件----Kubernetes Dashboard
# kubectl create -f kubernetes-dashboard.yaml
secret/kubernetes-dashboard-certs created
serviceaccount/kubernetes-dashboard created
role.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
deployment.apps/kubernetes-dashboard created
service/kubernetes-dashboard created
3.2、kubectl create namespace:创建namespace
用法:kubectl create namespace NAME [–dry-run]
# kubectl create namespace cyf
namespace/cyf created
3.3、kubectl create secret:创建secret
用法:kubectl create secret
3.3.1、创建一个TLS Secret
# kubectl create secret tls tomcat-ingress-sercret --cert=tls.crt --key=tls.key
secret/tomcat-ingress-sercret created
# kubectl get secrets
NAME TYPE DATA AGE
default-token-g5zk6 kubernetes.io/service-account-token 3 5d14h
tomcat-ingress-sercret kubernetes.io/tls 2 11s
# kubectl describe secrets tomcat-ingress-sercret
Name: tomcat-ingress-sercret
Namespace: default
Labels: <none>
Annotations:<none>
Type:kubernetes.io/tls
Data
====
tls.crt:1294 bytes
tls.key:1675 bytes
3.3.2、创建一个generic Secret
# kubectl create secret generic mysql-root-password --from-literal=password=Myp@ss123
secret/mysql-root-password created
# kubectl get secrets
NAME TYPE DATA AGE
default-token-g5zk6 kubernetes.io/service-account-token 3 9d
mysql-root-password Opaque 1 10s #Opaque:模糊类型
tomcat-ingress-secret kubernetes.io/tls 2 4d7h
3.4、kubectl create configmap:创建configmap
用法:kubectl create configmap NAME [--from-file=source] [--from-literal=key1=value1] [--dry-run]
# kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=myapp.cyf.com
configmap/nginx-config created
3.5、kubectl create serviceaccount:创建serviceaccount
# kubectl create serviceaccount admin
serviceaccount/admin created
# kubectl create serviceaccount mysa -o yaml --dry-run
apiVersion: v1
kind: ServiceAccount
metadata:
creationTimestamp: null
name: mysa
4、kubectl get
用法:kubectl get [(-o|--output=)json|yaml|wide|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=...] (TYPE[.VERSION][.GROUP] | TYPE[.VERSION][.GROUP]/NAME ...)
4.1、kubectl get nodes
4.1.1、获取节点信息
# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 16h v1.14.2
node1 Ready worker 16h v1.14.2
4.1.2、获取节点详细信息
# kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
master Ready master 16h v1.14.2 192.168.10.48 <none> CentOS Linux 7 (Core) 3.10.0-957.el7.x86_64 docker://19.3.1
node1 Ready worker 16h v1.14.2 192.168.10.148 <none> CentOS Linux 7 (Core) 3.10.0-957.el7.x86_64 docker://19.3.1
4.1.3、获取节点标签
# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
master Ready master 2d v1.14.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master,kubernetes.io/os=linux,node-role.kubernetes.io/master=
node1 Ready worker 2d v1.14.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node1,kubernetes.io/os=linux,node-role.kubernetes.io/worker=worker
4.1.4、给节点打标签
# kubectl label nodes node1 disktype=ssd
node/node1 labeled
4.2、kubectl get pods
4.2.1、获取指定namespace的Pod信息(不加-n默认获取default)
# kubectl get pods -n default
NAME READY STATUS RESTARTS AGE
nginx-755464dd6c-b56mr 1/1 Running 0 52s
nginx-755464dd6c-ggwzv 1/1 Running 0 52s
4.2.2、获取所有namespace的Pod信息
# kubectl get pods --all-namespaces
# kubectl get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE
default nginx-755464dd6c-b56mr 1/1 Running 0 3m12s
default nginx-755464dd6c-ggwzv 1/1 Running 0 3m12s
kube-system coredns-fb8b8dccf-b9cwb 1/1 Running 1 17h
kube-system coredns-fb8b8dccf-qcdv2 1/1 Running 1 17h
kube-system etcd-master 1/1 Running 1 17h
kube-system kube-apiserver-master 1/1 Running 1 17h
kube-system kube-controller-manager-master 1/1 Running 1 17h
kube-system kube-flannel-ds-amd64-4kn7q 1/1 Running 1 17h
kube-system kube-flannel-ds-amd64-xpd8f 1/1 Running 1 17h
kube-system kube-proxy-htdpc 1/1 Running 1 17h
kube-system kube-proxy-q49ff 1/1 Running 1 17h
kube-system kube-scheduler-master 1/1 Running 1 17h
kube-system kubernetes-dashboard-6c98b7f57c-q6zp7 1/1 Running 1 16h
4.2.3、获取Pod的详细信息
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-755464dd6c-b56mr 1/1 Running 0 5m31s 10.244.1.11 node1 <none> <none>
nginx-755464dd6c-ggwzv 1/1 Running 0 5m31s 10.244.1.10 node1 <none> <none>
# kubectl get pods -A -o wide
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
default nginx-755464dd6c-b56mr 1/1 Running 0 6m4s 10.244.1.11 node1 <none> <none>
default nginx-755464dd6c-ggwzv 1/1 Running 0 6m4s 10.244.1.10 node1 <none> <none>
kube-system coredns-fb8b8dccf-b9cwb 1/1 Running 1 17h 10.244.1.9 node1 <none> <none>
kube-system coredns-fb8b8dccf-qcdv2 1/1 Running 1 17h 10.244.1.8 node1 <none> <none>
......
4.2.4、获取Pod的labels
# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-755464dd6c-b56mr 1/1 Running 0 27h pod-template-hash=755464dd6c,run=nginx
nginx-755464dd6c-ggwzv 1/1 Running 0 27h pod-template-hash=755464dd6c,run=nginx
pod-demo 2/2 Running 0 58s apps=myapp,tier=frontend
4.2.5、查看所有pod对于指定标签的key是否有value,有则显示,没有则空白
# kubectl get pods -L apps,run
NAME READY STATUS RESTARTS AGE APPS RUN
nginx-755464dd6c-b56mr 1/1 Running 0 27h nginx
nginx-755464dd6c-ggwzv 1/1 Running 0 27h nginx
pod-demo 2/2 Running 0 15m myapp
4.2.6、获取含有指定标签key的pod
# kubectl get pods -l apps --show-labels
NAME READY STATUS RESTARTS AGE LABELS
pod-demo 2/2 Running 0 112s apps=myapp,tier=frontend
4.2.7、获取含有指定标签key=value的pod
等值关系:=和!=
# kubectl get pods -l release=stable --show-labels
NAME READY STATUS RESTARTS AGE LABELS
pod-demo 2/2 Running 0 39m apps=myapp,release=stable,tier=frontend
4.2.8、获取标签key的value在(不在)集合中的pod(in表示在集合,notin表示不在集合)
集合关系:
key in (value1,value2...)
key notin (value1,value2...)
key
!key
# kubectl get pods -l "release in (canary,beta,alpha)"
NAME READY STATUS RESTARTS AGE
nginx-755464dd6c-ggwzv 1/1 Running 0 28h
# kubectl get pods -l "release notin (canary,beta,alpha)"
NAME READY STATUS RESTARTS AGE
nginx-755464dd6c-b56mr 1/1 Running 0 28h
pod-demo 2/2 Running 0 53m
4.2.7、获取podIP
kubectl get pod pod-name -o jsonpath=’{.status.podIP}’
# kubectl get pods nginx-755464dd6c-b56mr -o jsonpath='{.status.podIP}'
10.244.1.11
4.2.8、导出指定pod的yaml配置
# kubectl get pods myapp-deploy-74fb9bdf9b-2rt79 -o yaml --export
Flag --export has been deprecated, This flag is deprecated and will be removed in future.
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
generateName: myapp-deploy-74fb9bdf9b-
labels:
app: myapp
pod-template-hash: 74fb9bdf9b
release: canary
ownerReferences:
.....
4.3、kubectl get namespaces:获取namespace信息
4.3.1、获取所有namespace信息
# kubectl get ns
NAME STATUS AGE
default Active 18h
kube-node-lease Active 18h
kube-public Active 18h
kube-system Active 18h
# kubectl get namespaces
NAME STATUS AGE
default Active 18h
kube-node-lease Active 18h
kube-public Active 18h
kube-system Active 18h
4.3.2、获取指定namespace信息
# kubectl get namespaces kube-system
NAME STATUS AGE
kube-system Active 18h
4.3.3、获取指定namespace的yaml格式信息
# kubectl get namespaces kube-system -o yaml
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: "2019-07-29T09:28:58Z"
name: kube-system
resourceVersion: "10"
selfLink: /api/v1/namespaces/kube-system
uid: 4ad779da-b1e3-11e9-ac67-0800273a2eb7
spec:
finalizers:
- kubernetes
status:
phase: Active
4.3.4、获取指定namespace的json格式信息
# kubectl get namespaces kube-system -o json
{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
"creationTimestamp": "2019-07-29T09:28:58Z",
"name": "kube-system",
"resourceVersion": "10",
"selfLink": "/api/v1/namespaces/kube-system",
"uid": "4ad779da-b1e3-11e9-ac67-0800273a2eb7"
},
"spec": {
"finalizers": [
"kubernetes"
]
},
"status": {
"phase": "Active"
}
}
4.4、kubectl get rs:获取ReplicaSet控制器信息
# kubectl get rs
NAME DESIRED CURRENT READY AGE
myapp 2 2 2 24s
4.5、kubectl get deploy && kubectl get deployment
4.5.1、获取Deployment控制器信息
# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
myapp-deploy 2/2 2 2 22s
nginx 1/1 1 1 19h
# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
myapp-deploy 2/2 2 2 22s
nginx 1/1 1 1 19h
4.5.2、与ReplicaSet的联系。rs名称:myapp-deploy-74fb9bdf9b,deploy名称:myapp-deploy,pod名称:myapp-deploy-74fb9bdf9b-rd4nf
# kubectl get rs
NAME DESIRED CURRENT READY AGE
myapp-deploy-74fb9bdf9b 2 2 2 48s
nginx-755464dd6c 1 1 1 19h
# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-deploy-74fb9bdf9b-rd4nf 1/1 Running 0 7m42s
myapp-deploy-74fb9bdf9b-slfst 1/1 Running 0 7m42s
nginx-755464dd6c-2pgbs 1/1 Running 0 19h
4.5.3、滚动更新
修改版本号后的滚动更新过程
# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
myapp-deploy-74fb9bdf9b-rd4nf 1/1 Running 0 33h
myapp-deploy-74fb9bdf9b-slfst 1/1 Running 0 33h
nginx-755464dd6c-2pgbs 1/1 Running 0 2d4h
myapp-deploy-74fb9bdf9b-lqm77 0/1 Pending 0 33h
myapp-deploy-74fb9bdf9b-lqm77 0/1 Pending 0 33h
myapp-deploy-74fb9bdf9b-lqm77 0/1 ContainerCreating 0 33h
myapp-deploy-74fb9bdf9b-lqm77 1/1 Running 0 33h
myapp-deploy-66cfdc5675-g8dgm 0/1 Pending 0 33h
myapp-deploy-66cfdc5675-g8dgm 0/1 Pending 0 33h
myapp-deploy-66cfdc5675-g8dgm 0/1 ContainerCreating 0 33h
myapp-deploy-66cfdc5675-g8dgm 1/1 Running 0 33h
myapp-deploy-74fb9bdf9b-lqm77 1/1 Terminating 0 33h
myapp-deploy-66cfdc5675-c7fxx 0/1 Pending 0 33h
myapp-deploy-66cfdc5675-c7fxx 0/1 Pending 0 33h
myapp-deploy-66cfdc5675-c7fxx 0/1 ContainerCreating 0 33h
myapp-deploy-66cfdc5675-c7fxx 1/1 Running 0 33h
myapp-deploy-74fb9bdf9b-lqm77 0/1 Terminating 0 33h
myapp-deploy-74fb9bdf9b-rd4nf 1/1 Terminating 0 33h
myapp-deploy-66cfdc5675-xj5qq 0/1 Pending 0 33h
myapp-deploy-66cfdc5675-xj5qq 0/1 Pending 0 33h
myapp-deploy-66cfdc5675-xj5qq 0/1 ContainerCreating 0 33h
myapp-deploy-74fb9bdf9b-lqm77 0/1 Terminating 0 33h
myapp-deploy-74fb9bdf9b-lqm77 0/1 Terminating 0 33h
myapp-deploy-74fb9bdf9b-rd4nf 0/1 Terminating 0 33h
myapp-deploy-66cfdc5675-xj5qq 1/1 Running 0 33h
myapp-deploy-74fb9bdf9b-slfst 1/1 Terminating 0 33h
myapp-deploy-74fb9bdf9b-rd4nf 0/1 Terminating 0 33h
myapp-deploy-74fb9bdf9b-rd4nf 0/1 Terminating 0 33h
myapp-deploy-74fb9bdf9b-slfst 0/1 Terminating 0 33h
myapp-deploy-74fb9bdf9b-slfst 0/1 Terminating 0 33h
myapp-deploy-74fb9bdf9b-slfst 0/1 Terminating 0 33h
查看滚动更新后历史版本和现在运行版本
# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myapp-deploy-66cfdc5675 3 3 3 100s myapp ikubernetes/myapp:v2 app=myapp,pod-template-hash=66cfdc5675,release=canary
myapp-deploy-74fb9bdf9b 0 0 0 16m myapp ikubernetes/myapp:v1 app=myapp,pod-template-hash=74fb9bdf9b,release=canary
4.6、kubectl get svc:获取service信息
4.6.1、获取默认命名空间的service
# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d5h
4.6.2、获取指定命名空间的service
# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default-http-backend ClusterIP 10.109.114.81 <none> 80/TCP 45m
ingress-nginx NodePort 10.106.125.159 <none> 80:30080/TCP,443:30443/TCP 73s
4.7、kubectl get secrets:获取secrets信息
# kubectl get secrets
NAME TYPE DATA AGE
default-token-g5zk6 kubernetes.io/service-account-token 3 5d14h
tomcat-ingress-sercret kubernetes.io/tls 2 11s
4.8、kubectl get pv:获取pv信息
# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv001 1Gi RWO,RWX Retain Available 10s
pv002 1Gi RWX Retain Available 10s
pv003 1Gi RWO,RWX Retain Available 10s
pv004 1Gi RWO,RWX Retain Available 10s
pv005 1Gi RWO,RWX Retain Available 10s
4.9、kubectl get pvc:获取pvc信息
# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mypvc Bound pv002 1Gi RWX 43s
4.10、kubectl get configmap && kubectl get cm:获取configMap信息
# kubectl get cm
NAME DATA AGE
nginx-config 2 4m11s
4.11、kubectl get sts:获取statefulSet控制器信息
# kubectl get sts
NAME READY AGE
myapp 3/3 30s
4.12、kubectl get serviceaccounts && kubectl get sa:获取serviceaccount信息
# kubectl get serviceaccounts && kubectl get sa
NAME SECRETS AGE
admin 1 3m49s
default 1 10d
NAME SECRETS AGE
admin 1 3m50s
default 1 10d
5、kubectl label
用法:kubectl label [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--resource-version=version],资源标签设定
5.1、给一个pod打一个没有存在的标签key=value,
# kubectl label pods pod-demo release=canary
pod/pod-demo labeled
# kubectl get pods pod-demo --show-labels
NAME READY STATUS RESTARTS AGE LABELS
pod-demo 2/2 Running 0 33m apps=myapp,release=canary,tier=frontend
5.2、给一个pod已经存在的标签重新赋值
# kubectl label pods pod-demo release=stable
error: 'release' already has a value (canary), and --overwrite is false
# kubectl label pods pod-demo release=stable --overwrite
pod/pod-demo labeled
5.3、通过key删除标签
# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
master Ready master 8d v1.14.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master,kubernetes.io/os=linux,node-role.kubernetes.io/master=
node1 Ready worker 8d v1.14.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,hostname=node1,kubernetes.io/arch=amd64,kubernetes.io/hostname=node1,kubernetes.io/os=linux,node-role.kubernetes.io/worker=worker
node2 Ready worker 5d6h v1.14.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,hostname=node2,kubernetes.io/arch=amd64,kubernetes.io/hostname=node2,kubernetes.io/os=linux,node-role.kubernetes.io/worker=worker
# kubectl label nodes node1 disktype-
node/node1 labeled
6、kubectl describe
用法:kubectl describe (-f FILENAME | TYPE | TYPE/NAME) ,显示资源的详细描述
6.1、查看某个pod的详细信息
# kubectl describe pods pod-demo
Name: pod-demo
Namespace: default
Priority: 0
PriorityClassName:<none>
Node: node1/192.168.10.148
Start Time: Tue, 30 Jul 2019 18:17:38 +0800
Labels: apps=myapp
tier=frontend
Annotations: <none>
Status: Running
IP: 10.244.1.14
6.2、查看创建的serviceaccount的详细信息
# kubectl describe sa admin
Name: admin
Namespace: default
Labels: <none>
Annotations: <none>
Image pull secrets:<none> #设置镜像拉取的镜像仓库,用sa替代imagePullSecrets
Mountable secrets: admin-token-f6h28
Tokens: admin-token-f6h28
Events: <none>
7、kubectl delete
用法:kubectl delete ([-f FILENAME] | [-k DIRECTORY] | TYPE [(NAME | -l label | --all)]) ,删除资源
7.1、删除pods资源
# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-l9pbb 1/1 Running 0 7m1s
myapp-nlw6m 1/1 Running 0 10m
myapp-ppktd 1/1 Running 0 10m
myapp-wmbnc 1/1 Running 0 28m
myapp-wpfpn 1/1 Running 0 10m
# kubectl delete pods myapp-l9pbb
pod "myapp-l9pbb" deleted
#
7.2、删除service
# kubectl delete svc redis
service "redis" deleted
7.3、删除namespace
# kubectl delete ns cyf
namespace "cyf" deleted
7.4、删除Secret
# kubectl delete secrets tomcat-ingress-sercret
secret "tomcat-ingress-sercret" deleted
7.5、删除configmap
# kubectl delete configmaps nginx
configmap "nginx" deleted
8、kubectl edit
用法:kubectl edit (RESOURCE/NAME | -f FILENAME) ,编辑资源清单
8.1、修改ReplicaSet控制器的yaml清单内容
注意:修改容器版本,不会自动更新(手动删除pod后自动更新:灰度发布)
# kubectl edit rs myapp
replicaset.extensions/myapp edited
9、kubectl exec
用法:kubectl exec POD [-c CONTAINER] -- COMMAND ,类似docker exec,进入容器
9.1、进入pod内的容器
# kubectl exec -it myapp-lmj45 -- /bin/sh
/ # ls
bin dev etc home lib mediamnt proc root run sbin srv sys tmp usr var
/ # exit
# kubectl exec -it myapp-lmj45 sh
/ # ls
bin dev etc home lib mediamnt proc root run sbin srv sys tmp usr var
/ #
9.2、进入pod内指定容器
用法:kubectl exec -it pod-name -c container-name -- /bin/sh
# kubectl exec -it myapp-lmj45 -c myapp-container sh
/ # ls
bin dev etc home lib mediamnt proc root run sbin srv sys tmp usr var
/ # exit
# kubectl exec -it myapp-lmj45 -c myapp-container -- /bin/sh
/ # ls
bin dev etc home lib mediamnt proc root run sbin srv sys tmp usr var
/ #
10、kubectl logs
用法:kubectl logs [-f] [-p] (POD | TYPE/NAME) [-c CONTAINER] ,类似于docker logs,查看容器日志
10.1、查看pod内的容器日志
# kubectl logs myapp-lmj45 --tail 300 -f
10.244.0.0 - - "GET / HTTP/1.1" 200 65 "-" "curl/7.29.0" "-"
10.244.0.0 - - "GET / HTTP/1.1" 200 65 "-" "curl/7.29.0" "-"
10.244.0.0 - - "GET / HTTP/1.1" 200 65 "-" "curl/7.29.0" "-"
10.244.0.0 - - "GET / HTTP/1.1" 200 65 "-" "curl/7.29.0" "-"
10.244.0.0 - - "GET / HTTP/1.1" 200 65 "-" "curl/7.29.0" "-"
10.2、查看pod内指定容器的日志
# kubectl logs myapp-lmj45 -c myapp-container --tail 300 -f
10.244.0.0 - - "GET / HTTP/1.1" 200 65 "-" "curl/7.29.0" "-"
10.244.0.0 - - "GET / HTTP/1.1" 200 65 "-" "curl/7.29.0" "-"
10.244.0.0 - - "GET / HTTP/1.1" 200 65 "-" "curl/7.29.0" "-"
10.244.0.0 - - "GET / HTTP/1.1" 200 65 "-" "curl/7.29.0" "-"
10.244.0.0 - - "GET / HTTP/1.1" 200 65 "-" "curl/7.29.0" "-"
11、kubectl explain
用法:kubectl explain RESOURCE ,显示各种资源清单的字段解释
11.1、查看pod资源清单字段
# kubectl explain pod
KIND: Pod
VERSION:v1
DESCRIPTION:
Pod is a collection of containers that can run on a host. This resource is
created by clients and scheduled onto hosts.
FIELDS:
apiVersion <string>
.....
11.2、查看pod资源清单理metadata字段下的字段(以此类推)
# kubectl explain pod.metadata
KIND: Pod
VERSION:v1
RESOURCE: metadata <Object>
DESCRIPTION:
Standard object's metadata. More info:
https://git.k8s.io/community/con ... entions.md#metadata
ObjectMeta is metadata that all persisted resources must have, which
includes all objects users must create.
FIELDS:
annotations <mapstring>
.....
11.3、查看ReplicaSet资源清单字段
# kubectl explain rs
KIND: ReplicaSet
VERSION:extensions/v1beta1
DESCRIPTION:
DEPRECATED - This group version of ReplicaSet is deprecated by
apps/v1beta2/ReplicaSet. See the release notes for more information.
ReplicaSet ensures that a specified number of pod replicas are running at
any given time.
FIELDS:
apiVersion <string>
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
.....
12、kubectl rollout
用法:kubectl rollout SUBCOMMAND ,滚动更新
类型:
deployments
daemonsets
statefulsets
12.1、查看deployment滚动更新历史版本
# kubectl rollout history deployment myapp-deploy
deployment.extensions/myapp-deploy
REVISIONCHANGE-CAUSE
1 <none>
2 <none>
12.2、更新一个pod后暂停更新
# kubectl rollout pause deployment myapp-deploy
deployment.extensions/myapp-deploy paused
# kubectl get pods -l app=myapp -w
NAME READY STATUS RESTARTS AGE
myapp-deploy-66cfdc5675-cnm4m 1/1 Running 0 12s
myapp-deploy-66cfdc5675-jzghg 1/1 Running 0 12s
myapp-deploy-66cfdc5675-zkc94 1/1 Running 0 12s
myapp-deploy-668ff4dc58-fq6hp 0/1 Pending 0 0s
myapp-deploy-668ff4dc58-fq6hp 0/1 Pending 0 0s
myapp-deploy-668ff4dc58-fq6hp 0/1 ContainerCreating 0 0s
myapp-deploy-668ff4dc58-fq6hp 1/1 Running 0 9s
12.3、解除暂停状态,继续更新
# kubectl rollout resume deployment myapp-deploy
deployment.extensions/myapp-deploy resumed
# kubectl rollout status deployment myapp-deploy
deployment "myapp-deploy" successfully rolled out
12.4、回滚到指定版本
# kubectl rollout history deployment myapp-deploy
deployment.extensions/myapp-deploy
REVISIONCHANGE-CAUSE
1 <none>
2 <none>
3 <none>
# kubectl rollout undo deployment myapp-deploy --to-revision=1
deployment.extensions/myapp-deploy rolled back
# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myapp-deploy-668ff4dc58 0 0 0 4m myapp ikubernetes/myapp:v3 app=myapp,pod-template-hash=668ff4dc58,release=canary
myapp-deploy-66cfdc5675 0 0 0 4m23s myapp ikubernetes/myapp:v2 app=myapp,pod-template-hash=66cfdc5675,release=canary
myapp-deploy-74fb9bdf9b 5 5 5 5m8s myapp ikubernetes/myapp:v1 app=myapp,pod-template-hash=74fb9bdf9b,release=canary
# kubectl rollout history deployment myapp-deploy
deployment.extensions/myapp-deploy
REVISIONCHANGE-CAUSE
2 <none>
3 <none>
4 <none>
12.5、监控deployment更新状态
# kubectl rollout status deployment myapp-deploy
Waiting for deployment "myapp-deploy" rollout to finish: 1 out of 3 new replicas have been updated...
13、kubectl scale
Deployment, ReplicaSet, Replication Controller, or StatefulSet,pod副本的扩容和缩容
用法:kubectl scale [--resource-version=version] [--current-replicas=count] --replicas=COUNT (-f FILENAME | TYPE NAME)
13.1、扩容和缩容# kubectl scale sts myapp --replicas=2
statefulset.apps/myapp scaled
# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-0 1/1 Running 0 14m
myapp-1 1/1 Running 0 14m
14、kubectl patch
用法:kubectl patch (-f FILENAME | TYPE NAME) -p PATCH
14.1、修改deployment副本数
# kubectl patch deployment myapp-deploy -p '{"spec":{"replicas":5}}'
deployment.extensions/myapp-deploy patched
14.2、修改deployment滚动更新策略
# kubectl patch deployment myapp-deploy -p '{"spec":{"strategy":{"rollingUpdate":{"maxSurge":1,"maxUnavailable":0}}}}'
deployment.extensions/myapp-deploy patched
# kubectl describe deployments myapp-deploy
Name: myapp-deploy
Namespace: default
CreationTimestamp: Fri, 02 Aug 2019 01:33:47 +0800
Labels: app=myapp
release=canary
Annotations: deployment.kubernetes.io/revision: 2
kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"name":"myapp-deploy","namespace":"default"},"spec":{"replicas":3...
Selector: app=myapp,release=canary
Replicas: 5 desired | 5 updated | 5 total | 5 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy:0 max unavailable, 1 max surge #已经修改
.....
15、kubectl set
用法:kubectl set SUBCOMMAND
15.1、kubectl set image
用法:kubectl set image (-f FILENAME | TYPE NAME) CONTAINER_NAME_1=CONTAINER_IMAGE_1 ... CONTAINER_NAME_N=CONTAINER_IMAGE_N
15.1.1、修改deployment的镜像版本
# kubectl set image deployment myapp-deploy myapp=ikubernetes/myapp:v3
deployment.extensions/myapp-deploy image updated
15.1.2、修改daemonset的镜像版本
# kubectl set image daemonsets filebeat-ds filebeat=ikubernetes/filebeat:5.6.6-alpine
daemonset.extensions/filebeat-ds image updated
16、kubectl config
用法:kubectl config SUBCOMMAND
16.1、kubectl config view:显示合并的 kubeconfig 配置或一个指定的 kubeconfig 文件
# kubectl config view
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: DATA+OMITTED
server: https://192.168.10.48:6443
name: kubernetes
contexts:
- context:
cluster: kubernetes
user: kubernetes-admin
name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
user:
client-certificate-data: REDACTED
client-key-data: REDACTED
17、kubectl cp:类似docker cp,区别在于拷贝目录下的文件到本地
# kubectl cp -n ingress-nginx nginx-ingress-controller-8bddb774f-jxzk2:/etc/nginx/ .
tar: Removing leading `/' from member names
# ls
fastcgi.conf fastcgi_params geoip koi-winmime.types modsecuritynginx.conf opentracing.json scgi_params template uwsgi_params.default
fastcgi.conf.defaultfastcgi_params.defaultkoi-utflua mime.types.defaultmodules nginx.conf.defaultowasp-modsecurity-crsscgi_params.defaultuwsgi_paramswin-utf
kubectl cordon:标记节点为不可调度节点
# kubectl cordon node2
node/node2 cordoned
# kubectl apply -f deploy-demo.yaml
deployment.apps/myapp-deploy created
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
eureka-deploy-579bf579dc-7gzfh 1/1 Running 0 111m 10.244.2.104 node2 <none> <none>
eureka-deploy-579bf579dc-9vvk6 1/1 Running 0 111m 10.244.1.167 node1 <none> <none>
myapp-deploy-74fb9bdf9b-8cg5l 1/1 Running 0 7s 10.244.1.169 node1 <none> <none>
myapp-deploy-74fb9bdf9b-fmxjr 1/1 Running 0 7s 10.244.1.172 node1 <none> <none>
myapp-deploy-74fb9bdf9b-gk4q2 1/1 Running 0 7s 10.244.1.170 node1 <none> <none>
myapp-deploy-74fb9bdf9b-hxchc 1/1 Running 0 7s 10.244.1.168 node1 <none> <none>
myapp-deploy-74fb9bdf9b-jk4mj 1/1 Running 0 7s 10.244.1.171 node1 <none> <none>
kubectl uncordon:标记节点为可调度节点
# kubectl uncordon node2
node/node2 uncordoned
kubectl cluster-info:查看集群信息
# kubectl cluster-info
Kubernetes master is running at https://192.168.10.48:6443
KubeDNS is running at https://192.168.10.48:6443/api/v ... /kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
kubectl api-versions:查询 Kubernetes API 支持的 API 版本
# kubectl api-versions
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
apps/v1beta1
apps/v1beta2
.....
kubectl api-resources:查看k8s支持的资源类型
# kubectl api-resources
NAME SHORTNAMES APIGROUP NAMESPACED KIND
bindings true Binding
componentstatuses cs false ComponentStatus
configmaps cm true ConfigMap
endpoints ep true Endpoints
events ev true Event
limitranges limits true LimitRange
namespaces ns false Namespace
nodes no false Node
查看指定群组支持的资源类型
# kubectl api-resources --api-group=storage.k8s.io
NAME SHORTNAMES APIGROUP NAMESPACED KIND
csidrivers storage.k8s.io false CSIDriver
csinodes storage.k8s.io false CSINode
storageclasses sc storage.k8s.io false StorageClass
volumeattachments storage.k8s.io false VolumeAttachment
---------------------
版权声明:本文为CSDN博主「cyfblog」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/cyfblog/article/details/97639312
最新经典文章,欢迎关注公众号http://www.aboutyun.com/data/attachment/forum/201907/20/110231gzd1ckizdv384iv3.jpg
感谢分享
页:
[1]