🔖 🐳 Docker & Kubernetes Cheat Sheet
2025-01-22 08:19:30    2k 字   
This post is also available in English and alternative languages.

1. Cheat Sheet

1.1. Docker

👇👇需要科学上网👇👇

👉👉👉 点击跳转 - 需要科学上网


1.2. Docker Compose

👇👇需要科学上网👇👇

👉👉👉 点击跳转 - 需要科学上网


1.3. Kubernetes

👇👇需要科学上网👇👇

👉👉👉 点击跳转 - 需要科学上网

含义命令
查看 Kubernetes 集群中的 namespacekubectl get namespaces
获取 Pod 列表kubectl get pods -n <namespace>
进入指定的 Pod 和容器kubectl exec -it <pod-name> -n <namespace> -- /bin/bash
kubectl exec -it <pod-name> -n <namespace> -- /bin/sh
下载文件kubectl cp <namespace>/<pod-name>:/path/to/dmp-file /local/path
示例:kubectl cp my-namespace/my-pod:/app/dump-file.dmp /home/user/dump-file.dmp

2. Docker 服务实例

docker-tutorial

通过 docker、docker-compose 创建部署 postgresql、redis、elasticsearch 等服务。

‼️ 注意:以下案例是为了让测试实例快速、简单运行,不要用于生产环境部署。


2.1. docker

2.1.1. MongoDB

MongoDB Docker Installation: Everything You Need to Know


1
2
3
4
5
6
7
8
9
docker run --name mongo_database \
-e MONGO_INITDB_ROOT_USERNAME={username} \
-e MONGO_INITDB_ROOT_PASSWORD={password} \
-p 27017:27017 \
-d mongo:latest

# 连接 mongodb
docker exec -it mongo_database mongo -u {username} -p {password}
docker exec -it mongo_database mongosh -u {username} -p {password}

创建 database 后赋予权限:

1
2
3
4
5
6
7
use test_database

db.createUser({
user: "{user}",
pwd: "{password}",
roles: [{ role: "readWrite", db: "test_database" }]
})

2.1.2. MySQL

2.1.2.1. install - 1

Installing MySQL with Docker

1
2
3
4
5
6
7
docker run --name mysql-latest \
-p 3306:3306 -p 33060:33060 \
-e MYSQL_ROOT_HOST='%' -e MYSQL_ROOT_PASSWORD='strongpassword' \
-d mysql/mysql-server:latest

# 连接 mysql
docker exec -it mysql-latest mysql -uroot -pstrongpassword

2.1.2.2. install - 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1. 拉取 mysql 镜像

2. 运行容器
docker run --name mysql \
-p 3306:3306 \
-v $HOME/_docker/mysql/conf.d:/etc/mysql/conf.d \
-v $HOME/_docker/mysql/data:/var/lib/mysql \
-v /etc/localtime:/etc/localtime:ro \
-e MYSQL_ROOT_PASSWORD=123456 \

# -d mysql:5.7.23
# --name mysql:容器名字为 mysql
# -p 3306:3306:将容器的 3306 端口映射到主机的 3306 端口
# -v $HOME/_docker/mysql/conf.d:将主机当前目录下的 ~/_docker/mysql/conf.d 挂载到容器的 /etc/mysql/conf.d,这个是挂载配置目录
# -v $HOME/_docker/mysql/data:将主机当前目录下的 data 目录挂载到容器的 /var/lib/mysqs,为数据文件存放路径
# -e MYSQL_ROOT_PASSWORD=123456:初始化root用户的密码

3. 进入容器内部
docker exec -it mysql bash

2.1.3. RabbitMQ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 启动 RabbitMQ,并启用 MQTT 插件
docker run -d \
--name rabbitmq \
-e RABBITMQ_DEFAULT_USER=admin \
-e RABBITMQ_DEFAULT_PASS=admin \
-p 1883:1883 \
-p 4369:4369 \
-p 5671:5671 \
-p 5672:5672 \
-p 15671:15671 \
-p 15672:15672 \
-p 15691-15692:15691-15692 \
-p 25672:25672 \
rabbitmq:3-management \
/bin/bash -c "rabbitmq-plugins enable rabbitmq_mqtt rabbitmq_web_mqtt && rabbitmq-server"

2.1.4. Nginx

1
2
3
4
5
6
7
8
9
10
11
# 在本地创建以下目录结构:
/Users/{your.dir}/docker/nginx/
├── nginx.conf
├── conf.d/
│ └── default.conf
├── logs/
└── html/
└── index.html

# 创建挂载的目录
mkdir -p /Users/{your.dir}/docker/nginx/{conf.d,logs,html}
1
2
3
4
5
6
7
8
9
10
# 默认配置文件在下面
docker run \
-p 80:80 \
--name nginx \
-v /Users/{your.dir}/docker/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /Users/{your.dir}/docker/nginx/conf.d:/etc/nginx/conf.d \
-v /Users/{your.dir}/docker/nginx/logs:/var/log/nginx \
-v /Users/{your.dir}/docker/nginx/html:/usr/share/nginx/html \
-e TZ=Asia/Shanghai \
-d nginx:latest

2.1.4.1. default.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;

# 访问日志
access_log /var/log/nginx/host.access.log main;

# 根目录配置
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

# 错误页面配置
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# 禁止访问 .htaccess 文件
location ~ /\.ht {
deny all;
}
}

2.1.4.2. nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# /etc/nginx/nginx.conf
user nginx;
worker_processes auto;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
keepalive_timeout 65;

# 包含额外的配置文件
include /etc/nginx/conf.d/*.conf;
}

2.1.4.3. index.html

1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello, Nginx!</title>
</head>
<body>
<h1>Hello, Nginx!</h1>
<p>This is a test page.</p>
</body>
</html>

2.2. docker-compose

2.2.1. docker-compose 命令

「docker-compose」是用于定义和运行多容器 Docker 应用程序的编排工具。使用「docker-compose」后不再需要逐一创建和启动容器。使用 YML 文件来配置应用程序需要的所有服务,然后使用一个命令,就可以从 YML 文件配置中创建并启动所有服务。

命令含义&备忘
docker-compose up启动所有编排容器服务
docker-compose up -d后台启动所有编排容器服务
docker-compose ps查看正在运行中的容器

2.2.2. postgresql

通过 docker compose 搭建;默认使用目录名称

目录名:./docker-compose/postgresql/
文件名:docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
version: '3'
services:
postgres:
image: arm64v8/postgres:12.6
container_name: postgresql
hostname: postgresql
#restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 123456
POSTGRES_DB: demo
TZ: Asia/Shanghai
PGTZ: Asia/Shanghai
ports:
- '5432:5432'
volumes:
- pg-data:/var/lib/postgresql/data
postgres-exporter:
image: prometheuscommunity/postgres-exporter
container_name: postgres-exporter
hostname: postgres-exporter
#restart: always
environment:
DATA_SOURCE_NAME: postgresql://postgres:123456@postgresql:5432/postgres?sslmode=disable
ports:
- 9187:9187
pgadmin:
image: dpage/pgadmin4:7.6
container_name: pgadmin
hostname: pgadmin
#restart: always
environment:
PGADMIN_DEFAULT_EMAIL: xxx@163.com
PGADMIN_DEFAULT_PASSWORD: 123456
ports:
- "5433:80"
volumes:
pg-data:

2.2.3. redis_cluster

通过 docker compose 搭建;默认使用目录名称

目录名:./docker-compose/redis_cluster/
文件名:docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
version: '3'
services:
redis-node-0:
image: docker.io/bitnami/redis-cluster:7.2
container_name: redis-node-0
ports:
- "6379:6379"
volumes:
- redis-cluster_data-0:/bitnami/redis/data
environment:
- 'REDIS_PASSWORD=bitnami'
- 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'

redis-node-1:
image: docker.io/bitnami/redis-cluster:7.2
container_name: redis-node-1
ports:
- "6380:6379"
volumes:
- redis-cluster_data-1:/bitnami/redis/data
environment:
- 'REDIS_PASSWORD=bitnami'
- 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'

redis-node-2:
image: docker.io/bitnami/redis-cluster:7.2
container_name: redis-node-2
ports:
- "6381:6379"
volumes:
- redis-cluster_data-2:/bitnami/redis/data
environment:
- 'REDIS_PASSWORD=bitnami'
- 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'

redis-node-3:
image: docker.io/bitnami/redis-cluster:7.2
container_name: redis-node-3
ports:
- "6382:6379"
volumes:
- redis-cluster_data-3:/bitnami/redis/data
environment:
- 'REDIS_PASSWORD=bitnami'
- 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'

redis-node-4:
image: docker.io/bitnami/redis-cluster:7.2
container_name: redis-node-4
ports:
- "6383:6379"
volumes:
- redis-cluster_data-4:/bitnami/redis/data
environment:
- 'REDIS_PASSWORD=bitnami'
- 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'

redis-node-5:
image: docker.io/bitnami/redis-cluster:7.2
container_name: redis-node-5
ports:
- "6384:6379"
depends_on:
- redis-node-0
- redis-node-1
- redis-node-2
- redis-node-3
- redis-node-4
environment:
- 'REDIS_PASSWORD=bitnami'
- 'REDISCLI_AUTH=bitnami'
- 'REDIS_CLUSTER_REPLICAS=1'
- 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
- 'REDIS_CLUSTER_CREATOR=yes'

volumes:
redis-cluster_data-0:
driver: local
redis-cluster_data-1:
driver: local
redis-cluster_data-2:
driver: local
redis-cluster_data-3:
driver: local
redis-cluster_data-4:
driver: local
redis-cluster_data-5:
driver: local

2.2.4. elasticsearch_cluster

Install Elasticsearch with Docker | Elasticsearch Guide [8.3] | Elastic


2.2.5. kafka_cluster

通过 docker compose 搭建;默认使用目录名称

目录名:./docker-compose/kafka_cluster/
文件名:docker-compose.yml
注意:需要将yml中127.0.0.1替换成宿主机ip

kafkaManager:http://localhost:39000/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
version: '3'
services:
zookeeper:
image: wurstmeister/zookeeper
container_name: zookeeper
ports:
- "2181:2181"

kafka1:
image: wurstmeister/kafka
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: 127.0.0.1
KAFKA_CREATE_TOPICS: TestComposeTopic:4:3
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_BROKER_ID: 1
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://127.0.0.1:9092
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
container_name: kafka01

kafka2:
image: wurstmeister/kafka
ports:
- "9093:9093"
environment:
KAFKA_ADVERTISED_HOST_NAME: 127.0.0.1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_BROKER_ID: 2
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://127.0.0.1:9093
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9093
container_name: kafka02

kafka3:
image: wurstmeister/kafka
ports:
- "9094:9094"
environment:
KAFKA_ADVERTISED_HOST_NAME: 127.0.0.1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_BROKER_ID: 3
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://127.0.0.1:9094
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9094
container_name: kafka03

kafka-manager:
image: sheepkiller/kafka-manager
environment:
ZK_HOSTS: 127.0.0.1
ports:
- "39000:9000"
expose:
- "9000"

2.2.6. Rabbit MQ

  1. 安装镜像:docker pull arm64v8/rabbitmq:3.13.6-management
  2. 运行:docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 arm64v8/rabbitmq:3.13.6-management
  3. 控制台界面:http://localhost:15672/#/,账号密码:guest

如果控制台界面无法打开,尝试以下方法:

1
2
3
4
5
# 进入容器
docker exec -it {容器id} /bin/bash

# 执行此命令:
rabbitmq-plugins enable rabbitmq_management
RabbitMQ无法进入管理界面