本文记录使用K8S后常用的命令
一、查看linux系统
查看系统版本
[root@localhost ~]# lsb_release -a #查看linux版本,基本适用于所有linux系统。lsb_release系统不自带,需要手动安装
查看服务器资源
[root@localhost ~]# cat /proc/cpuinfo| grep "processor"| wc -l && #CPU虚拟核数、内存、硬盘查询
cat /proc/meminfo | grep MemTotal &&
df -lh
查看硬盘
[root@localhost ~]# lsblk #查看磁盘数量
[root@localhost ~]# df -lh #磁盘使用情况
[root@localhost ~]# du -sh /* |sort -h #查看大文件
[root@localhost /]# du --max-depth=1 -h #查看当前目录每个文件夹的情况
top
[root@localhost /]# top #linux的任务管理器,按c显示详细
[root@localhost /]# top -H -p PID(进程ID)
[root@localhost /]# kill PID(进程ID) #杀掉进程
[root@localhost /]# kill -9 PID(进程ID) #强制杀进程
远程文件传输
[root@localhost home]# scp test.php root@192.168.1.10:/home #将本机test.php传输到192.168.1.10home目录下
[root@localhost home]# scp -r data root@192.168.107.251:/home #传目录文件夹需要带-r,不然报错
端口查询
[root@localhost /]# netstat -lanp #查询所有
[root@localhost /]# netstat -tunlp |grep 80 #查看80端口
查看公网IP
root@localhost /]# curl cip.cc
定时任务
[root@localhost /]# crontab -l #查看
[root@localhost /]# crontab -e #编辑
[root@localhost /]# cat /var/log/cron #查看定时任务执行日志
二、Docker
docker查看和删除
[root@localhost /]# docker ps -a #查看已经创建的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a2940bfed90b wordpress "docker-entrypoint..." 3 hours ago Up About an hour 0.0.0.0:8080->80/tcp wordpress
[root@localhost /]# docker stop a2940bfed90b #停止容器
[root@localhost /]# docker rm a2940bfed90b #删除容器
[root@localhost /]# docker restart a2940bfed90b #重启容器
镜像使用
[root@localhost /]# docker images #查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.20.0 7ab27dbbfbdf 16 months ago 133MB
[root@localhost /]# docker search nginx #搜索镜像
[root@localhost /]# docker pull/push nginx #拉取/推送镜像
[root@localhost /]# docker rmi 7ab27dbbfbdf #删除镜像
[root@localhost /]# docker tag nginx(Old REPOSITORY/IMAGE) nginx:v10(NewR EPOSITORY/IMAGE) #改名(为了改仓库路径)
镜像迁移
[root@localhost /]# docker save [repo:tag] > [file.tar] #源主机打包
[root@localhost /]# docker load --input [file.tar] #目标主机上还原
镜像推送示例
[root@localhost /]# docker pull nginx #拉取官方镜像
[root@localhost /]# docker tag nginx:latest test.harbor.cn/library/nginx:latest #打tag改名
[root@localhost /]# docker push test.harbor.cn/library/nginx:latest #推送
清理没有容器使用的镜像
[root@localhost /]# docker system prune -a
进入容器安装vim
[root@localhost ~]# docker exec -it jira bash #进入容器内
root@a8d5d4274bb2:/var/atlassian/application-data/jira# apt update #更新
root@a8d5d4274bb2:/var/atlassian/application-data/jira# apt-get install vim -y #安装
三、K8S
查看api版本
[root@master01 /]# kubectl api-versions
组件日志查询
[root@master01 home]# journalctl -u kubelet |tail
[root@master01 home]# journalctl -xe #从结尾开始看系统日志
看错报错
kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-7875fc54bb-sln85 0/1 ContainerCreating 0 32m
kubectl describe pods -n kube-system coredns-7875fc54bb-sln85
Pod日志查询
[root@localhost ~]# kubectl get nodes #查看节点
[root@node01 ~]# kubectl get svc --all-namespaces -o wide #查看所有namespaces信息,包含映射端口
[root@localhost ~]# kubectl describe node master01 #显示master01节点详细信息
[root@master01 /]# kubectl get pod -A #查看所有pod
NAMESPACE NAME READY STATUS RESTARTS AGE
prometheus grafana-5676dff8c7-559t6 1/1 Running 0 158m
[root@master01 /]# kubectl get pod -n prometheus #查看pod名称
NAME READY STATUS RESTARTS AGE
grafana-5676dff8c7-559t6 1/1 Running 0 155m
[root@master01 /]# kubectl logs -f -n prometheus grafana-5676dff8c7-559t6 #查询实时日志
[root@master01 /]# kubectl logs --tail 100 -f -n prometheus grafana-5676dff8c7-559t6 #显示最后100行日志
[root@master01 /]# kubectl logs --since 1h -f -n prometheus grafana-5676dff8c7-559t6 #显示最近一个小时及之后的日志
[root@master01 /]# kubectl logs -n prometheus grafana-5676dff8c7-559t6 > test.txt #导出成txt文档
进入容器&文件拷贝
[root@master01 /]# kubectl exec -it -n prometheus grafana-5676dff8c7-559t6 /bin/bash #进入容器
kubectl cp -n [namespace] [NAME]:[容器内文件路径] [主机文件夹路径] #容器内文件拷出到主机
kubectl cp -n [namespace] [主机文件相对路径] [NAME]:[容器内文件夹路径] #将主机文件拷入容器
节点标签
[root@master01 /]# kubectl get nodes --show-labels #查询
NAME STATUS ROLES AGE VERSION LABELS
node03 NotReady <none> 8d v1.16.0 beta.kubernetes.io/arch=amd64,kubernetes.io/os=linux
[root@master01 /]# kubectl label nodes k8s-node2 deploy.type=gpu
[root@master01 /]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
node03 NotReady <none> 8d v1.16.0 beta.kubernetes.io/arch=amd64,deploy.type=gpu,kubernetes.io/os=linux
nodeSelector指定节点/节点组部署
#对节点/节点组打标签后,通过nodeSelector调度部署
spec:
template:
spec:
nodeSelector:
deploy.type: pds
Pod扩容/缩容
kubectl scale deployment nginx-deployment --replicas 5
kubectl scale deployment nginx-deployment --replicasa=1
驱逐Pod到其它节点
kubectl cordon [此节点] #停止调度:将node调为SchedulingDisabled
kubectl delete pod -n [namespace] [pod_name] #删除pod
kubectl uncordon [此节点] #恢复调度
kubectl命令补全工具bash-completion
[root@master01 /]# yum install -y bash-completion
[root@master01 /]# source /usr/share/bash-completion/bash_completion
[root@master01 /]# source <(kubectl completion bash)
[root@master01 /]# echo "source <(kubectl completion bash)" >> ~/.bashrc
[root@master01 /]# kubectl
annotate autoscale cordon drain kustomize port-forward set
api-resources certificate cp edit label proxy taint
api-versions cluster-info create exec logs replace top
apply completion delete explain options rollout uncordon
attach config describe expose patch run version
auth convert diff get plugin scale wait
删除pod
[root@k8s-master01 ~]# kubectl get pod -A #查看namespace和podname
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system calico-kube-controllers-5dc679f767-7mhl5 0/1 Pending 0 30s
[root@k8s-master01 ~]# kubectl delete pod calico-kube-controllers-5dc679f767-7mhl5 -n kube-system #删除pod kubectl get delete pod “podname” -n "namespace"
[root@k8s-master01 ~]# kubectl get deployment -n kube-system
NAME READY UP-TO-DATE AVAILABLE AGE
coredns 0/1 1 0 14h
metrics-server 0/1 1 0 14h
kubectl delete deployment coredns -n kube-system
kubectl delete deployment metrics-server -n kube-system
如果有calico.yaml文件的正确路径和权限
kubectl delete -f calico.yaml
强制删除
[root@k8s-master01 ~]# kubectl get pod -A #查看namespace
NAMESPACE NAME READY STATUS RESTARTS AGE
kubernetes-dashboard dashboard-metrics-scraper-799d786dbf-sm8x6 1/1 Running 0 64s
kubernetes-dashboard kubernetes-dashboard-674846bfb9-m289g 1/1 Running 0 3s
[root@k8s-master01 ~]# kubectl delete namespace kubernetes-dashboard --force --grace-period=0 #强制删除kubernetes-dashboard下的pod
批量删除指定条件的pod
kubectl get pod -n [NAMESPACE] |grep [关键字] | awk '{print $1};' | xargs kubectl delete pod -n [NAMESPACE]
强制删除Terminating状态Pod
kubectl delete pod -n [namespace] [pod] --force --grace-period=0 #删除单个Pod
kubectl get pod -n [namespace] | grep Terminating | awk '{print $1};' | xargs -n kubectl delete pod -n [namespace] --force --grace-period=0 # 批量,某命名空间下处于Terminating状态的Pods
kubectl get pod -A | grep Terminating| awk '{print $1};' | xargs kubectl delete pod --force --grace-period=0 #批量,所有命名空间下
设置默认sc
kubectl patch sc alicloud-nas -p '{"metadata": {"annotations": {"storageclass.beta.kubernetes.io/is-default-class": "true"}}}'
四、数据库、中间件等
Mysql资源查询
# 1. 进入Pod
kubectl exec -it -n [namespace] [pod_name] /bin/bash
# 2. 连接
mysql -h127.0.0.1 -uroot -p[password]
# 3. 显示数据库
show databases;
# 4. 创建数据库
create database school;
# 5. 进入数据库
use school;
# 6. 查看所有表
show tables;
# 7. 创建表
create table teacher
( id int(3) auto_increment not null primary key,
name char(10) not null,
address varchar(50) default '深圳',
year date
);
# 8. 插入字段
insert into teacher values('1','allen','大连一中','1976-10-10');
insert into teacher values('2','jack','大连二中','1975-12-23');
# 9. 查看表结构
desc teacher;
# 10. 查看表中数据
select * from teacher;
# 11 排序,部分展示
select * from homeid_mac order by last_modified_date desc limit 0,10;
# 查询数量
select mac,count(*) from robot_map_manage group by mac
创建用户
# 创建只读用户(分为两行执行执行)
CREATE USER 'read'@'127.0.0.1' IDENTIFIED BY '123456'; # 本地登录
CREATE USER 'read'@'%' IDENTIFIED BY '123456'; # 远程登录
GRANT SELECT ON *.* TO 'read'@'127.0.0.1' WITH GRANT OPTION; # 只读
GRANT SELECT ON *.* TO 'read'@'%' WITH GRANT OPTION;
# 开通只读用户对phpmyadmin库的操作权限
GRANT ALL ON phpmyadmin.* TO 'read'@'%' WITH GRANT OPTION;
# 创建读写账号
grant all ON *.* TO maxuser@'localhost' identified by '123456' with grant option;
grant all ON *.* TO maxuser@'%' identified by '123456' with grant option;
# 创建指定库操作权限用户
grant all ON db01.* TO 'pro1'@'localhost' identified by 'f65b3ce62069931a' with grant option;
grant all ON db01.* TO 'pro1'@'%' identified by 'f65b3ce62069931a' with grant option;
复制部分数据到另一个表中
insert into table1 select * from db2.table1 where id>19745
MySql数据导出与恢复
数据备份
# 1. 进入Pod
kubectl exec -it -n [namespace] [pod_name] /bin/bash
# 2.1 备份单个库
mysqldump -uroot -p admin > admin-bak.sql
mysqldump --single-transaction -uroot -p admin > admin-bak.sql
# 2.2 备份所有的库
mysqldump -h127.0.0.1 -uroot -p -A > 2021-05-25-bak.sql
3. 拷出备份文件
kubectl cp -n [namespace] [pod_name]:bak.sql bak.sql
Mysql修改最大连接数
1. 拷入备份文件
kubectl cp bak.sql -n [namespace] [pod_name]:/
2. 创建数据库(恢复单个库时需要)
create database admin;
use admin;
3. 恢复
source /bak.sql
Mysql锁表解锁
SHOW PROCESSLIST;
KILL [进程]
# 查询未提交的事务
select * from information_schema.innodb_trx
五、Redis
资源查询
# 1.查询r的Pod名称及所在空间
kubectl get pod -A |grep redis
# 2.进入Pod
kubectl exec -it -n [namespace] [pod_name] /bin/bash
# 3.进入redis命令台
redis-cli
# 4.密码认证
auth password
# 5.列出所有信息
INFO
# 6.切换到dattabase
SELECT [database]
# 7.列出所有key
keys *
# 8.查询key的长度(hash为例)
hlen [key]
# 9.列出所有value
hscan [key] 0 count 10000