docker常见操作命令(基础服务搭建)

文章目录

网络环境设置

html 复制代码
VirtualBox搭建Ubuntu中的网络配置 https://blog.csdn.net/technologyleader/article/details/125394914

SecureCRT 登录Ubuntu

html 复制代码
https://blog.csdn.net/u011186256/article/details/79963544

查看内存、CPU、磁盘等

html 复制代码
#内存显示(-h是增加可读性)
free -h 

# 按下shfit + M 可以进行排序
top

df -h

docker 公共命令

镜像列表

html 复制代码
sudo docker images

MySQL

docker中安装mysql 5.7

html 复制代码
docker pull mysql:<版本号>
    
例如docker pull mysql:5.7

docker中启动mysql 5.7

html 复制代码
docker run -d --name mysql57 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=your_password mysql:5.7   

这个命令会在后台运行一个名为mysql57的MySQL 5.7容器,将它的3306端口映射到主机的3306端口,并设置ROOT用户的密码为"your_password"。
你可以根据需要修改密码。 

启动是否成功:
docker ps   
如果看到mysql57容器正在运行,则说明安装成功。

docker中停止mysql 5.7

html 复制代码
docker stop mysql57

执行命令:docker ps命令
从列表中找到容器名称NAMES

docker中进入MySQL命令行:

html 复制代码
docker exec -it mysql57 bash

mysql -uroot -p
然后输入密码root

Zookeeper

docker中安装zookeeper 3.9.1

html 复制代码
docker pull zeekeeper:3.9.1

docker中启动zookeeper 3.9.1

html 复制代码
docker run -p 2181:2181 --privileged=false --name zookeeper -d zookeeper:3.9.1

docker中进入zookeeper bash

html 复制代码
docker exec -it zookeeper bash

-- 进入zk命令目录
ll
cd bin
-- 登录到ZK服务器
zkCli.sh -server localhost:2181

-- 查看目录下有什么内容
ls \ 

Kafka

docker中安装kafka 5.4.3

html 复制代码
docker pull confluentinc/cp-kafka:5.4.3

docker中启动kafka 5.4.3

html 复制代码
   sudo docker run -d \
     --name kafka \
     --net=host \
     -e KAFKA_ZOOKEEPER_CONNECT=localhost:2181 \
     -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092 \
     confluentinc/cp-kafka:5.4.3

启动是否成功:看日志
docker logs kafka

docker中停止kafka

html 复制代码
docker stop kafka

docker中删除kafka

html 复制代码
docker rm kafaka

docker中进入kafka bash

html 复制代码
进入bash
sudo docker exec -it kafka bash

创建主题
1. 先找到kafka-topics。 
   1.1 命令:find / -name 'kafka*'
2. /usr/bin/kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test_topic

向主题生产消息
>/usr/bin/kafka-console-producer --broker-list localhost:9092 --topic test_topic
>MessageA
>MessageB
>...

从主题中消费消息
>/usr/bin/kafka-console-consumer --bootstrap-server localhost:9092 --topic test_topic --from-beginnig
>MessageA
>MessageB
>...

备注:
问题:报错Number of alive brokers 1 does not meet the required replication factor 3
解决方案:增加broker数量到3个即可

   docker run -d \
     --name kafka \
     --net=host \
     -e KAFKA_ZOOKEEPER_CONNECT=localhost:2181 \
     -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9093 \
     confluentinc/cp-kafka:5.4.3

   docker run -d \
     --name kafka \
     --net=host \
     -e KAFKA_ZOOKEEPER_CONNECT=localhost:2181 \
     -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9094 \
     confluentinc/cp-kafka:5.4.3

写入数据
kafka日志 /etc/kafka

kafka-topics --create --zookeeper localhost:2181/test --replication-factor 1 --partitions  1 --topic  test
kafka-console-producer --broker-list localhost:9093 --topic test
kafka-console-consumer --bootstrp-server localhost:9092 --topic my-topic --from-beginning


看日志
cat /etc/kafka/kafka.properties
/var/lib/kafka/data

重新选举首领副本
kafka-leader-election --bootstrap-server localhost:9092  --election-type preferred  --all-topic-partitions

ubuntu防火墙关闭

bash 复制代码
#查看防火墙状态
sudo ufw status 
#关闭防火墙
sudo ufw disable
#启用防火墙
sudo ufw enable

Ubuntu系统 防火墙的使用和开放端口 https://www.cnblogs.com/ZainWilson/p/16037183.html

文件查看

html 复制代码
-l:以长格式显示目录内容,包括文件的权限,所属用户、所属组、文件大小、创建时间等信息。
-h:以易读的方式显示文件大小,例如K、M等。
-S:按文件大小排序,显示文件列表。
-r:反向排序,文件名倒序显示。

以上命令将按文件大小排序,以易读的方式显示文件大小,显示目录下的所有文件和目录。
ls -lhS /path/to/directory

kafka常见管理命令

主题管理
创建主题
bash 复制代码
# kafka-topics \
 --zookeeper 192.168.56.101:2181 \
 --create --topic  test-topic  --if-not-exists \
 --replication-factor 2 \
 --partitions 4     

Created topic test-topic.
增加分区
bash 复制代码
# kafka-topics \
--zookeeper 192.168.56.101:2181 \
--alter --topic test-topic --partitions 8 

WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected
Adding partitions succeeded!
删除主题
bash 复制代码
# kafka-topics \
--zookeeper 192.168.56.101:2181 \
--delete --topic --if-not-exists

Topic --if-not-exists is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.
列出集群里的所有主题
bash 复制代码
# kafka-topics \
--zookeeper 192.168.56.101:2181 \
--list

test
test-topic
列出主题详细信息
bash 复制代码
# kafka-topics \
--zookeeper 192.168.56.101:2181 \
--describe

Topic: test     PartitionCount: 1       ReplicationFactor: 1    Configs: 
        Topic: test     Partition: 0    Leader: 1003    Replicas: 1003  Isr: 1003
Topic: test-topic       PartitionCount: 8       ReplicationFactor: 2    Configs: 
        Topic: test-topic       Partition: 0    Leader: 1005    Replicas: 1005,1004     Isr: 1005,1004
        Topic: test-topic       Partition: 1    Leader: 1003    Replicas: 1003,1005     Isr: 1003,1005
        Topic: test-topic       Partition: 2    Leader: 1004    Replicas: 1004,1003     Isr: 1004,1003
        Topic: test-topic       Partition: 3    Leader: 1005    Replicas: 1005,1003     Isr: 1005,1003
        Topic: test-topic       Partition: 4    Leader: 1003    Replicas: 1003,1004     Isr: 1003,1004
        Topic: test-topic       Partition: 5    Leader: 1004    Replicas: 1004,1005     Isr: 1004,1005
        Topic: test-topic       Partition: 6    Leader: 1005    Replicas: 1005,1004     Isr: 1005,1004
        Topic: test-topic       Partition: 7    Leader: 1003    Replicas: 1003,1005     Isr: 1003,1005
        
# kafka-topics \
--zookeeper 192.168.56.101:2181 \
--describe \ 
--under-replicated-partitions #离线的分区详细信息


docker exec <kafka-container-id-or-name> kafka-topics.sh --list --zookeeper <zookeeper-host>:<zookeeper-port>
docker exec dockerrela_kafka_1 kafka-topics.sh --list --zookeeper localhost:2181
列出消费者群组
bash 复制代码
# kafka-consumer-groups \
--bootstrap-server localhost:9092 \
--list

my-consumer-group
列出消费者群组详细信息
bash 复制代码
# kafka-consumer-groups \
--bootstrap-server 192.168.56.101:9092 \
--describe \
--group my-consumer-group-forTestTopic #或者 --all-groups

# 消费者离线的情况
Consumer group 'my-consumer-group' has no active members.

GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
my-consumer-group test  0    2            6         4        -       -       -

# 消费者在线的情况
GROUP    TOPIC     PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG              CONSUMER-ID    HOST          CLIENT-ID
xxxGroup xxxTopict      0          0               0           0               client1-xxx /192.168.56.1   client1
xxxGroup xxxTopict      1          0               0           0               client1-xxx /192.168.56.1   client1
xxxGroup xxxTopict      2          0               0           0               client1-xxx /192.168.56.1   client1
xxxGroup xxxTopict      3          0               0           0               client1-xxx /192.168.56.1   client1
xxxGroup xxxTopict      4          0               0           0               client2-xxx /192.168.56.1   client2
xxxGroup xxxTopict      5          0               0           0               client2-xxx /192.168.56.1   client2
xxxGroup xxxTopict      6          0               0           0               client2-xxx /192.168.56.1   client2
xxxGroup xxxTopict      7          0               0           0               client2-xxx /192.168.56.1   client2
其中:
PARTITION 正在被读取的分区
CURRENT-OFFSET 消费者群组最近提交的偏移量,即消费者在分区里读取的当前偏移量
LOG-END-OFFSET 当前高水位偏移量,即最近一个呗读取消息的偏移量,同时也是最近一个提交到集群的偏移量
LAG 消费者CURRENT-OFFSET和broker的LOG-END-OFFSET 之间的差距
删除消费者群组
bash 复制代码
# kafka-consumer-groups \
--bootstrap-server localhost:9092 \
--delete --group my-consumer-group 

Deletion of requested consumer groups ('my-consumer-group') was successful.

Redis

html 复制代码
下载镜像
docker pull redis

创建并启动redis容器(将名称弄成redis)
docker run --name redis -d redis:lastest 
注意:如果已经存在可以先删掉再来 sudo docker rm redis

启动、停止redis
docker start redis
docker stop redis

进入redis容器
docker exec -it redis redis-cli

其它

查看是否开放了某个端口

复制代码
netstat -tuln

开放端口9092
sudo ufw allow 9092/tcp


sudo docker-compose up -d
sudo docker logs kafka1|tail -f -n 100

sudo docker-compose down
sudo docker ps
相关推荐
简单点了9 小时前
全栈编程基础知识7
运维·服务器·网络
眷蓝天9 小时前
Docker 镜像瘦身:从 GB 到 MB 的优化实践
运维·docker·容器
实心儿儿9 小时前
Linux —— 进程控制 - mini shell
linux·运维·服务器
程序员黄老师10 小时前
Windows文件移动到Linux上的坑
linux·运维·服务器
shizhan_cloud10 小时前
自动化部署Kubernetes集群
运维·kubernetes
mounter62510 小时前
【内核前沿】Linux IPC 迎来大变局?POSIX 消息队列增强、io_uring IPC 与 Bus1 十年回归
linux·运维·服务器·kernel·ipc·io_uring
wzl2026121310 小时前
企业微信定时群发技术实现与实操指南(原生接口+工具落地)
java·运维·前端·企业微信
2401_8955213411 小时前
Linux下安装Redis
linux·运维·redis
网络小白不怕黑11 小时前
2.1VMware部署Windows_server_2008_R2
运维·服务器