K8s常用命令操作
一、kubectl命令补全
、master安装命令补全,并临时生效
yum install -y bash-completion
source /usr/share/bash-completion/bash_completion
、永久生效
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
二、启动状态
、master节点
更改配置文件,重新加载
systemctl daemon-reload
启动master相关组件
systemctl start kube-apiserver
systemctl start kube-controller-manager
systemctl start kube-scheduler
停止master相关组件
systemctl stop kube-apiserver
systemctl stop kube-controller-manager
systemctl stop kube-scheduler
重启master相关组件
systemctl restart kube-apiserver
systemctl restart kube-controller-manager
systemctl restart kube-scheduler
查看master相关组件状态
systemctl status kube-apiserver
systemctl status kube-controller-manager
systemctl status kube-scheduler
、etcd服务
更改配置后,重新加载
systemctl daemon-reload
启动etcd服务
systemctl start etcd.service
停止etcd服务
systemctl stop etcd.service
重启etcd服务
systemctl restart etcd.service
查看etcd服务状态
systemctl status etcd.service
、worker节点
更改配置后,重启加载
systemctl daemon-reload
启动worker端相关组件
systemctl start kube-proxy
systemctl start docker
systemctl start kubelet
停止worker端相关组件
systemctl stop kube-proxy
systemctl stop docker
systemctl stop kubelet
重启worker端相关组件
systemctl restart kube-proxy
systemctl restart docker
systemctl restart kubelet
查看worker端相关组件状态
systemctl status kube-proxy
systemctl status docker
systemctl status kubelet
三、kubectl 常用命令操作
、帮助信息
kubectl -h 查看具体操作参数
、kubectl可以操作的资源
Valid resource types include:
* all
* certificatesigningrequests (aka 'csr')
* clusterrolebindings
* clusterroles
* componentstatuses (aka 'cs')
* configmaps (aka 'cm')
* controllerrevisions
* cronjobs
* customresourcedefinition (aka 'crd')
* daemonsets (aka 'ds')
* deployments (aka 'deploy')
* endpoints (aka 'ep')
* events (aka 'ev')
* horizontalpodautoscalers (aka 'hpa')
* ingresses (aka 'ing')
* jobs
* limitranges (aka 'limits')
* namespaces (aka 'ns')
* networkpolicies (aka 'netpol')
* nodes (aka 'no')
* persistentvolumeclaims (aka 'pvc')
* persistentvolumes (aka 'pv')
* poddisruptionbudgets (aka 'pdb')
* podpreset
* pods (aka 'po')
* podsecuritypolicies (aka 'psp')
* podtemplates
* replicasets (aka 'rs')
* replicationcontrollers (aka 'rc')
* resourcequotas (aka 'quota')
* rolebindings
* roles
* secrets
* serviceaccounts (aka 'sa')
* services (aka 'svc')
* statefulsets (aka 'sts')
* storageclasses (aka 'sc')
、获取具体操作的帮助信息
kubectl get
[(-o|--output=)json|yaml|wide|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=...]
(TYPE [NAME | -l label] | TYPE/NAME ...) [flags] [options]
、查看类命令
获取节点相应服务的信息
kubectl get nodes
kubectl get pods
按selector名来查找pod
kubectl get pod --selector name=redis
查看集群信息
kubectl cluster-info
查看各组件信息
kubectl -s http://localhost:8080 get componentstatuses
查看pods所在的运行节点
kubectl get pods -o wide
查看pods定义的详细信息
kubectl get pods -o yaml
查看运行的pod的环境变量
kubectl exec pod名 env
查看指定pod的日志
kubectl logs -f pods/heapster-xxxxx -n kube-system
查看pod
kubectl get pods --namespace kube-system
kubectl -n kube-system get po,svc -l k8s-app=kubernetes-dashboard
查看svc
kubectl get svc -n kube-system
kubectl get services -n kube-system
kubectl get pods -n kube-system
查看 服务分配到了哪个pod上
kubectl get pod -o wide -n kube-system
获取ingress的host名字,
kubectl get ing -n kube-system
在容器外查看k8s容器内部文件
kubectl exec -it jenkins-cd8d886f9-jg7bv cat /var/jenkins_home/secrets/initialAdminPassword
73bbc8f776d141c982da7919006f0785
、操作类命令
创建资源
kubectl create -f 文件名.yaml
重建资源
kubectl replace -f 文件名 [--force]
删除资源
kubectl delete -f 文件名
kubectl delete pod pod名
kubectl delete rc rc名
kubectl delete service service名
kubectl delete pod --all
强制删除
kubectl delete pod coredns-65f84dd9fc-57p42 -n kube-system --grace-period= --force
kubectl delete pod coredns-65f84dd9fc-57p42 -n kube-system --now
kubectl delete rc kube-dns-v20 --force=true --grace-period= --namespace=kube-system
pod无法删除解决
重启这个pod对应的node上的docker
实在不行,重启这三台
重启不管用 就改下delpoment的名字重新创建
四、kubectl进阶命令操作
、kubectl get:获取指定资源的基本信息
kubectl get services kubernetes-dashboard -n kube-system #查看所有service
kubectl get deployment kubernetes-dashboard -n kube-system #查看所有发布
kubectl get pods --all-namespaces #查看所有pod
kubectl get pods -o wide --all-namespaces #查看所有pod的IP及节点
kubectl get pods -n kube-system | grep dashboard
kubectl get nodes -lzone #获取zone的节点
、kubectl describe:查看指定资源详细描述信息
kubectl describe service/kubernetes-dashboard --namespace="kube-system"
kubectl describe pods/kubernetes-dashboard--g6q8c --namespace="kube-system" #指定类型查看
kubectl describe pod nginx-772ai #查看pod详细信息
、kubectl scale:动态伸缩
kubectl scale rc nginx --replicas= # 动态伸缩
kubectl scale deployment redis-slave --replicas= #动态伸缩
kubectl scale --replicas= -f redis-slave-deployment.yaml #动态伸缩
、kubectl exec:进入pod启动的容器
kubectl exec -it redis-master--q47hh -n kube-system /bin/bash #进入容器
、kubectl label:添加label值
kubectl label nodes node1 zone=north #增加节点lable值 spec.nodeSelector: zone: north #指定pod在哪个节点
kubectl label pod redis-master--q47hh role=master #增加lable值 [key]=[value]
kubectl label pod redis-master--q47hh role- #删除lable值
kubectl label pod redis-master--q47hh role=backend --overwrite #修改lable值
、kubectl rolling-update:滚动升级
kubectl rolling-update redis-master -f redis-master-controller-v2.yaml #配置文件滚动升级
kubectl rolling-update redis-master --image=redis-master:2.0 #命令升级
kubectl rolling-update redis-master --image=redis-master:1.0 --rollback #pod版本回滚
五、etcdctl 常用操作
etcdctl cluster-health #检查网络集群健康状态
etcdctl --endpoints=https://192.168.71.221:2379 cluster-health #带有安全认证检查网络集群健康状态
etcdctl member list
etcdctl set /k8s/network/config '{ "Network": "10.1.0.0/16" }'
etcdctl get /k8s/network/config