kubernetes命令大全 K8S



状态查询

# 查看集群信息

kubectl cluster-info

systemctl status kube-apiserver

systemctl status kubelet

systemctl status kube-proxy

systemctl status kube-scheduler

systemctl status kube-controller-manager

systemctl status docker


node相关

# 查看namespaces

kubectl get namespaces

# 为节点增加lable

kubectl label nodes 10.126.72.31 points=test

# 查看节点和lable

kubectl get nodes --show-labels

# 查看状态

kubectl get componentstatuses

# Node的隔离与恢复

## 隔离

kubectl cordon k8s-node1

## 恢复

kubectl uncordon k8s-node1


查询

# 查看nodes节点

kubectl get nodes

# 通过yaml文件查询

kubectl get -f xxx-yaml/

# 查询资源

kubectl get resourcequota

# endpoints端

kubectl get endpoints

# 查看pods

# 查看指定空间`kube-system`的pods

kubectl get po -n kube-system

# 查看所有空间的

kubectl get pods -o wide --all-namespaces

# 其他的写法

kubectl get pod -o wide --namespace=kube-system

# 获取svc

kubectl get svc --all-namespaces

# 其他写法

kubectl get services --all-namespaces

# 通过lable查询

kubectl get pods -l app=nginx -o yaml|grep podIP

# 当我们发现一个pod迟迟无法创建时,描述一个pods

kubectl describe pod xxx


删除所有pod

# 删除所有pods

kubectl delete pods --all

# 删除所有包含某个lable的pod和serivce

kubectl delete pods,services -l name=<lable-name>

# 删除ui server,然后重建

kubectl delete deployments kubernetes-dashboard --namespace=kube-system

kubectl delete services kubernetes-dashboard --namespace=kube-system

# 强制删除部署

kubectl delete deployment kafka-1

# 删除rc

kubectl delete rs --all && kubectl delete rc --all

## 强制删除Terminating状态的pod

kubectl delete deployment kafka-1 --grace-period=0 --force


滚动

# 升级

kubectl apply -f xxx.yaml --record

# 回滚

kubectl rollout undo deployment javademo

# 查看滚动升级记录

kubectl rollout history deployment {名称}


查看日志

# 查看指定镜像的日志

kubectl logs -f kube-dns-699984412-vz1q6 -n kube-system

kubectl logs --tail=10 nginx  

#指定其中一个查看日志

kubectl logs kube-dns-699984412-n5zkz -c kubedns --namespace=kube-system

kubectl logs kube-dns-699984412-vz1q6 -c dnsmasq --namespace=kube-system

kubectl logs kube-dns-699984412-mqb14 -c sidecar --namespace=kube-system

# 看日志

journalctl -f


扩展

# 扩展副本

kubectl scale rc xxxx --replicas=3

kubectl scale rc mysql --replicas=1

kubectl scale --replicas=3 -f foo.yaml


执行

# 启动

nohup kubectl proxy --address='10.1.70.247' --port=8001 --accept-hosts='^*$' >/dev/null 2>&1 &

# 进入镜像

kubectl exec kube-dns-699984412-vz1q6 -n kube-system -c kubedns ifconfig

kubectl exec kube-dns-699984412-vz1q6 -n kube-system -c kubedns ifconfig /bin/bash

# 执行镜像内命令

kubectl exec kube-dns-4140740281-pfjhr -c etcd --namespace=kube-system etcdctl get /skydns/local/cluster/default/redis-master


无限循环命令

while true; do sleep 1; done


资源管理

# 暂停资源更新(资源变更不会生效)

kubectl rollout pause deployment xxx

# 恢复资源更新

kubectl rollout resume deployment xxx

# 设置内存、cpu限制

kubectl set resources deployment xxx -c=xxx --limits=cpu=200m,memory=512Mi --requests=cpu=1m,memory=1Mi

# 设置storageclass为默认

kubectl patch storageclass <your-class-name> -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'


其他

# 创建一个 Deployment 对象,以运行一个 nginx 实例:

kubectl run nginx --image nginx

---下面的命令完成了相同的任务,但是命令格式不同:

kubectl create deployment nginx --image nginx

# 创建和删除

kubectl create -f dashboard-controller.yaml

kubectl delete -f dashboard-dashboard.yaml

#直接使用配置文件中的对象定义,替换Kubernetes中对应的对象:

kubectl replace -f dashboard-dashboard.yaml

# 查看指定pods的环境变量

kubectl exec xxx env

# 判断dns是否通

kubectl exec busybox -- nslookup kube-dns.kube-system

# kube-proxy状态

systemctl status kube-proxy -l

# token的

kubectl get serviceaccount/kube-dns --namespace=kube-system -o yaml|grep token


#Kubernetes对象

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2 # 运行 2 个容器化应用程序副本
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

使用 kube apply 命令可以创建该 .yaml 文件中的 Deployment 对象:

kubectl apply -f https://kuboard.cn/statics/learning/obj/deployment.yaml

---输出结果如下所示:

deployment.apps/nginx-deployment created

---使用 kubectl delete 命令可以删除该 .yaml 文件中的 Deployment 对象:

kubectl delete -f https://kuboard.cn/statics/learning/obj/deployment.yaml


-必填字段

在上述的 .yaml 文件中,如下字段是必须填写的:

apiVersion: 用来创建对象时所使用的Kubernetes API版本

kind: 被创建对象的类型

metadata: 用于唯一确定该对象的元数据:包括 name 和 namespace,如果 namespace 为空,则默认值为 default

spec: 描述您对该对象的期望状态

不同类型的 Kubernetes,其 spec 对象的格式不同(含有不同的内嵌字段),通过 API 手册 (opens new window) 可以查看 Kubernetes 对象的字段和描述。例如,假设您想了解 Pod 的 spec 定义,可以在 这里 (opens new window)找到,Deployment 的 spec 定义可以在 这里 (opens new window) 找到。


# 查看集群中的名称空间列表:

kubectl get namespaces

# 删除名称空间:

kubectl delete namespaces <名称空间的名字>


# Spring Cloud on Kubernetes



签名:这个人很懒,什么也没有留下!
最新回复 (0)
返回