k8s部署webstack(storageclass方式)

前言:

部署MySql时无法自动创建webstack数据库 此时需要手动创建

1
2
3
4
# 登录
mysql -uroot -proot_password -h10.96.0.2
CREATE DATABASE webstack;
FLUSH PRIVILEGES;

一、配置文件(webstack.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# mysql配置
---
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-cm
namespace: dev
data:
my.cnf: |
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

[mysqld]
## 同一局域网内注意要唯一
server-id=001
## 开启二进制日志功能,可以随便取(关键)
log-bin=mysql-bin

gtid_mode=on
enforce_gtid_consistency=on

relay-log=relay-log.log
binlog_format=ROW
#MySQL5.7可以不启用此参数,5.7版本使用了gtid_executed表记录同步复制的信息,避免两次写入relay-log和binlog,降低了从库磁盘I/O
#log_slave_updates=true
master_info_repository=TABLE
relay_log_info_repository=TABLE
sync_master_info=1
slave_parallel_workers=2
binlog_checksum=CRC32
master_verify_checksum=1
slave_sql_verify_checksum=1
binlog_rows_query_log_events=1
#replicate_do_db=tt
#MySQL5.7新增加的值,配置基于表的组提交并行复制,默认值为database(基于库进行多线程复制,MySQL5.6是基于库的方式进行多线程方式复制)建议改为logical_clock,基于表的组方式复制,提高复制的效率
slave_parallel_type=logical_clock
#开启通用查询日志
general_log=1
#设置通用日志的输出格式为文件和表
log_output=FILE,TABLE

# mysql
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
namespace: dev
spec:
selector:
matchLabels:
app: mysql # has to match .spec.template.metadata.labels
serviceName: "mysql"
replicas: 1 # by default is 1 # 3副本
# minReadySeconds: 10 # by default is 0
template:
metadata:
labels:
app: mysql # has to match .spec.selector.matchLabels
spec:
terminationGracePeriodSeconds: 10
containers:
- name: mysql
image: registry.cn-hangzhou.aliyuncs.com/zznn/mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: "root_password" # 设置根密码,建议使用 Secrets 存储
- name: MYSQL_DATABASE
value: webstack # 设置根密码,建议使用 Secrets 存储
- name: MYSQL_USER
value: webstack # 设置根密码,建议使用 Secrets 存储
- name: MYSQL_PASSWORD
value: "root_password" # 设置根密码,建议使用 Secrets 存储
args:
- --character-set-server=utf8mb4
# - --collation-server=utf8mb4_unicode_ci
# - "--default-authentication-plugin=mysql_native_password"
volumeMounts:
- mountPath: /var/lib/mysql
name: mysql-storage
- name: config-map
mountPath: "/etc/mysql/my.cnf"
subPath: my.cnf
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: www
mountPath: /var/lib/mysql
volumes:
# 定义 ConfigMap 类型的 Volume
- name: config-map
configMap:
name: mysql-cm # 引用名为 mysql-cm 的 ConfigMap
# # 定义持久化存储 Volume
# - name: mysql-storage
# persistentVolumeClaim:
# claimName: mysql-pvc
volumeClaimTemplates: # 3副本里面都会申请一个存储 此配置就类似pvc了
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ] # 访问模式ReadWriteOnce 允许一个客户进行读写
storageClassName: "managed-nfs-storage" # 指定定义的StorageClass
resources:
requests:
storage: 2Gi # 申请空间大小

# 使mysql ip固定
---
apiVersion: v1
kind: Service
metadata:
name: service-mysql
namespace: dev
spec:
selector:
app: mysql
clusterIP: 10.96.0.2 # service的ip地址,如果不写,默认会生成一个
type: ClusterIP
ports:
- port: 3306 # Service端口
targetPort: 3306 # pod端口

# 构建webstack
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: webstack
namespace: dev
spec:
replicas: 1 # 修改为3副本
selector:
matchLabels:
app: webstack # 必须与 .spec.template.metadata.labels 匹配
template:
metadata:
labels:
app: webstack # 必须与 .spec.selector.matchLabels 匹配
spec:
terminationGracePeriodSeconds: 10
containers:
- name: webstack
image: registry.cn-hangzhou.aliyuncs.com/zznn/mycentos:webstackv-1.2.2
env:
- name: LOGIN_COPTCHA
value: "false"
- name: DB_HOST
value: "10.96.0.2"
- name: DB_DATABASE
value: webstack
- name: DB_USERNAME
value: root
- name: DB_PASSWORD
value: root_password
- name: APP_URL
value: "" # 空值明确写为 ""
ports:
- containerPort: 8000
name: webstack
command: ['/entrypoint.sh','serve']

# 暴露webstack端口
---
apiVersion: v1
kind: Service
metadata:
namespace: dev
name: webstack-service
spec:
selector:
app: webstack
ports:
- protocol: TCP
port: 8000
targetPort: 8000
sessionAffinity: ClientIP
type: NodePort # 将服务暴露为 NodePort 类型

效果(验证mysql持久化)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 查看mysql情况
ll /mnt/data/mysql/dev-www-mysql-0-pvc-2396853f-5aed-4678-bfb7-73ca48e7a7cd/
# total 188488
# drwxrwxrwx 6 systemd-coredump root 4096 Nov 25 16:21 ./
# drwxrwxrwx 5 root root 4096 Nov 25 14:49 ../
# -rw-r----- 1 systemd-coredump systemd-coredump 56 Nov 25 14:49 auto.cnf
# -rw------- 1 systemd-coredump systemd-coredump 1680 Nov 25 14:49 ca-key.pem
# -rw-r--r-- 1 systemd-coredump systemd-coredump 1112 Nov 25 14:49 ca.pem
# -rw-r--r-- 1 systemd-coredump systemd-coredump 1112 Nov 25 14:49 client-cert.pem
# -rw------- 1 systemd-coredump systemd-coredump 1676 Nov 25 14:49 client-key.pem
# -rw-r----- 1 systemd-coredump systemd-coredump 542 Nov 25 16:20 ib_buffer_pool
# -rw-r----- 1 systemd-coredump systemd-coredump 50331648 Nov 25 16:22 ib_logfile0
# -rw-r----- 1 systemd-coredump systemd-coredump 50331648 Nov 25 14:49 ib_logfile1
# -rw-r----- 1 systemd-coredump systemd-coredump 79691776 Nov 25 16:22 ibdata1
# -rw-r----- 1 systemd-coredump systemd-coredump 12582912 Nov 25 16:22 ibtmp1
# drwxr-x--- 2 systemd-coredump systemd-coredump 4096 Nov 25 14:49 mysql/
# lrwxrwxrwx 1 systemd-coredump systemd-coredump 27 Nov 25 16:21 mysql.sock -> /var/run/mysqld/mysqld.sock
# drwxr-x--- 2 systemd-coredump systemd-coredump 4096 Nov 25 14:49 performance_schema/
# -rw------- 1 systemd-coredump systemd-coredump 1680 Nov 25 14:49 private_key.pem
# -rw-r--r-- 1 systemd-coredump systemd-coredump 452 Nov 25 14:49 public_key.pem
# -rw-r--r-- 1 systemd-coredump systemd-coredump 1112 Nov 25 14:49 server-cert.pem
# -rw------- 1 systemd-coredump systemd-coredump 1676 Nov 25 14:49 server-key.pem
# drwxr-x--- 2 systemd-coredump systemd-coredump 12288 Nov 25 14:49 sys/
drwxr-x--- 2 systemd-coredump systemd-coredump 4096 Nov 25 16:21 webstack/

验证:

https://github.com/zznn-cloud/zznn-cloud-blog-images/raw/main/Qexo/24/11/image_8d945e7a4af63ec585953c76f2668914.png