k8s部署单点的mysql实例
记录一下根据kubernetes.io的范本部署一个mysql实例的过程
准备一个k8s集群,配置好存储类,我这里使用的是rook-ceph的cephfs
mysql.yaml
# get from https://kubernetes.io/docs/tasks/run-application/run-single-instance-stateful-application/
apiVersion: v1
kind: Namespace
metadata:
name: daxinxindahaohao-mysql
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
namespace: daxinxindahaohao-mysql
spec:
storageClassName: rook-cephfs
accessModes:
- ReadWriteMany
resources:
requests:
storage: 50G
---
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: daxinxindahaohao-mysql
spec:
type: ClusterIP
ports:
- name: mysql-3306
protocol: TCP
port: 3306
targetPort: 3306
selector:
app: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
namespace: oneops-mysql
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
# 指定现在使用的特定版本
- image: docker.io/library/mysql:5.7.36
name: mysql
# 解决pvc使用ceph的rbd格式引入的lost+found文件导致无法正常启动的问题
args: ["--ignore-db-dir=lost+found"]
env:
# 设定root用户的账号密码
- name: MYSQL_ROOT_PASSWORD
value: "123456"
- name: TZ
value: Asia/Shanghai
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
# 增加探针
readinessProbe:
tcpSocket:
port: 3306
initialDelaySeconds: 1
periodSeconds: 30
successThreshold: 2
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
- name: TZ
value: Asia/Shanghai
- name: MYSQL_ROOT_PASSWORD
value: "123456"
解决报错信息:[ERROR] --initialize specified but the data directory has files in it. Aborting.
args: ["--ignore-db-dir=lost+found"]
# 增加探针
readinessProbe:
tcpSocket:
port: 3306
initialDelaySeconds: 1
periodSeconds: 30
successThreshold: 2
这里增加探针的原因是因为pod状态变为RUNNING之后,内部的mariadb服务会有一个初始化重启过程,只有当初始化重启完毕之后,3306端口才会被监听起来。
简单的说就是mariadb服务的生命周期是:第一次启动–》自动初始化–》重启服务进行初始化–》初始化完成,二次启动。只有当二次启动之后,服务才是正常对外提供服务的。
但是如果是设置数据库初始化脚本的话,在第一次启动的时候就载入数据库表结构文件就会出现中断的错误,影响初始化脚本的成功运行。
下面付上初始化启动的日志
2023-09-06 16:18:13+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.36-1debian10 started.
2023-09-06 16:18:14+08:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2023-09-06 16:18:14+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.36-1debian10 started.
2023-09-06 16:18:15+08:00 [Note] [Entrypoint]: Initializing database files
2023-09-06T08:18:15.068741Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-09-06T08:18:53.008582Z 0 [Warning] InnoDB: New log files created, LSN=45790
2023-09-06T08:18:55.351193Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2023-09-06T08:18:56.020897Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 05ba0787-4c8e-11ee-b619-fecb0056b728.
2023-09-06T08:18:56.139729Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2023-09-06T08:18:56.709872Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2023-09-06T08:18:56.709901Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2023-09-06T08:18:56.710623Z 0 [Warning] CA certificate ca.pem is self signed.
2023-09-06T08:18:56.913185Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2023-09-06 16:20:26+08:00 [Note] [Entrypoint]: Database files initialized
2023-09-06 16:20:26+08:00 [Note] [Entrypoint]: Starting temporary server
2023-09-06 16:20:26+08:00 [Note] [Entrypoint]: Waiting for server startup
2023-09-06T08:20:26.418510Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-09-06T08:20:26.423936Z 0 [Note] mysqld (mysqld 5.7.36) starting as process 108 ...
2023-09-06T08:20:26.430518Z 0 [Note] InnoDB: PUNCH HOLE support available
2023-09-06T08:20:26.430547Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2023-09-06T08:20:26.430551Z 0 [Note] InnoDB: Uses event mutexes
2023-09-06T08:20:26.430554Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2023-09-06T08:20:26.430557Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2023-09-06T08:20:26.430562Z 0 [Note] InnoDB: Using Linux native AIO
2023-09-06T08:20:26.430958Z 0 [Note] InnoDB: Number of pools: 1
2023-09-06T08:20:26.431158Z 0 [Note] InnoDB: Using CPU crc32 instructions
2023-09-06T08:20:26.434067Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2023-09-06T08:20:26.446992Z 0 [Note] InnoDB: Completed initialization of buffer pool
2023-09-06T08:20:26.450888Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2023-09-06T08:20:26.471519Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2023-09-06T08:20:26.700633Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2023-09-06T08:20:26.701971Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2023-09-06T08:20:29.172209Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2023-09-06T08:20:29.177887Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2023-09-06T08:20:29.177925Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2023-09-06T08:20:29.180270Z 0 [Note] InnoDB: 5.7.36 started; log sequence number 2751302
2023-09-06T08:20:29.180687Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2023-09-06T08:20:29.181526Z 0 [Note] Plugin 'FEDERATED' is disabled.
2023-09-06T08:20:29.192825Z 0 [Note] InnoDB: Buffer pool(s) load completed at 230906 16:20:29
2023-09-06T08:20:29.199171Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2023-09-06T08:20:29.199208Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
2023-09-06T08:20:29.199215Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2023-09-06T08:20:29.199220Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2023-09-06T08:20:29.200335Z 0 [Warning] CA certificate ca.pem is self signed.
2023-09-06T08:20:29.200416Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
2023-09-06T08:20:29.210472Z 0 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2023-09-06T08:20:29.283645Z 0 [Note] Event Scheduler: Loaded 0 events
2023-09-06T08:20:29.284159Z 0 [Note] mysqld: ready for connections.
Version: '5.7.36' socket: '/var/run/mysqld/mysqld.sock' port: 0 MySQL Community Server (GPL)
2023-09-06 16:20:30+08:00 [Note] [Entrypoint]: Temporary server started.
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
2023-09-06T08:21:17.708990Z 0 [Note] InnoDB: page_cleaner: 1000ms intended loop took 14928ms. The settings might not be optimal. (flushed=3 and evicted=0, during the time.)
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
2023-09-06 16:21:22+08:00 [Note] [Entrypoint]: Stopping temporary server
2023-09-06T08:21:22.927973Z 0 [Note] Giving 0 client threads a chance to die gracefully
2023-09-06T08:21:22.928036Z 0 [Note] Shutting down slave threads
2023-09-06T08:21:22.928052Z 0 [Note] Forcefully disconnecting 0 remaining clients
2023-09-06T08:21:22.928063Z 0 [Note] Event Scheduler: Purging the queue. 0 events
2023-09-06T08:21:22.928161Z 0 [Note] Binlog end
2023-09-06T08:21:22.929679Z 0 [Note] Shutting down plugin 'ngram'
2023-09-06T08:21:22.929707Z 0 [Note] Shutting down plugin 'partition'
2023-09-06T08:21:22.929713Z 0 [Note] Shutting down plugin 'BLACKHOLE'
2023-09-06T08:21:22.929719Z 0 [Note] Shutting down plugin 'ARCHIVE'
2023-09-06T08:21:22.929722Z 0 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
2023-09-06T08:21:22.929787Z 0 [Note] Shutting down plugin 'MRG_MYISAM'
2023-09-06T08:21:22.929797Z 0 [Note] Shutting down plugin 'MyISAM'
2023-09-06T08:21:22.929821Z 0 [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL'
2023-09-06T08:21:22.929827Z 0 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
2023-09-06T08:21:22.929831Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
2023-09-06T08:21:22.929834Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
2023-09-06T08:21:22.929837Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
2023-09-06T08:21:22.929842Z 0 [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
2023-09-06T08:21:22.929845Z 0 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
2023-09-06T08:21:22.929849Z 0 [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
2023-09-06T08:21:22.929852Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
2023-09-06T08:21:22.929856Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLES'
2023-09-06T08:21:22.929862Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
2023-09-06T08:21:22.929865Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
2023-09-06T08:21:22.929869Z 0 [Note] Shutting down plugin 'INNODB_FT_CONFIG'
2023-09-06T08:21:22.929873Z 0 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
2023-09-06T08:21:22.929876Z 0 [Note] Shutting down plugin 'INNODB_FT_DELETED'
2023-09-06T08:21:22.929879Z 0 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
2023-09-06T08:21:22.929883Z 0 [Note] Shutting down plugin 'INNODB_METRICS'
2023-09-06T08:21:22.929887Z 0 [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INFO'
2023-09-06T08:21:22.929890Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
2023-09-06T08:21:22.929895Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
2023-09-06T08:21:22.929899Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
2023-09-06T08:21:22.929903Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
2023-09-06T08:21:22.929907Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
2023-09-06T08:21:22.929910Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
2023-09-06T08:21:22.929914Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM'
2023-09-06T08:21:22.929917Z 0 [Note] Shutting down plugin 'INNODB_CMP_RESET'
2023-09-06T08:21:22.929921Z 0 [Note] Shutting down plugin 'INNODB_CMP'
2023-09-06T08:21:22.929925Z 0 [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
2023-09-06T08:21:22.929928Z 0 [Note] Shutting down plugin 'INNODB_LOCKS'
2023-09-06T08:21:22.929932Z 0 [Note] Shutting down plugin 'INNODB_TRX'
2023-09-06T08:21:22.929936Z 0 [Note] Shutting down plugin 'InnoDB'
2023-09-06T08:21:22.930018Z 0 [Note] InnoDB: FTS optimize thread exiting.
2023-09-06T08:21:22.930342Z 0 [Note] InnoDB: Starting shutdown...
2023-09-06T08:21:23.030855Z 0 [Note] InnoDB: Dumping buffer pool(s) to /var/lib/mysql/ib_buffer_pool
2023-09-06T08:21:23.034210Z 0 [Note] InnoDB: Buffer pool(s) dump completed at 230906 16:21:23
2023-09-06T08:21:35.754523Z 0 [Note] InnoDB: Shutdown completed; log sequence number 12661989
2023-09-06T08:21:36.550125Z 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
2023-09-06T08:21:36.550239Z 0 [Note] Shutting down plugin 'MEMORY'
2023-09-06T08:21:36.550268Z 0 [Note] Shutting down plugin 'CSV'
2023-09-06T08:21:36.550281Z 0 [Note] Shutting down plugin 'sha256_password'
2023-09-06T08:21:36.550287Z 0 [Note] Shutting down plugin 'mysql_native_password'
2023-09-06T08:21:36.550653Z 0 [Note] Shutting down plugin 'binlog'
2023-09-06T08:21:36.556583Z 0 [Note] mysqld: Shutdown complete
2023-09-06 16:21:36+08:00 [Note] [Entrypoint]: Temporary server stopped
2023-09-06 16:21:36+08:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
2023-09-06T08:21:37.264717Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-09-06T08:21:37.271432Z 0 [Note] mysqld (mysqld 5.7.36) starting as process 1 ...
2023-09-06T08:21:37.279621Z 0 [Note] InnoDB: PUNCH HOLE support available
2023-09-06T08:21:37.279661Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2023-09-06T08:21:37.279669Z 0 [Note] InnoDB: Uses event mutexes
2023-09-06T08:21:37.279677Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2023-09-06T08:21:37.279684Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2023-09-06T08:21:37.279695Z 0 [Note] InnoDB: Using Linux native AIO
2023-09-06T08:21:37.280444Z 0 [Note] InnoDB: Number of pools: 1
2023-09-06T08:21:37.280779Z 0 [Note] InnoDB: Using CPU crc32 instructions
2023-09-06T08:21:37.285025Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2023-09-06T08:21:37.311721Z 0 [Note] InnoDB: Completed initialization of buffer pool
2023-09-06T08:21:37.317736Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2023-09-06T08:21:37.338335Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2023-09-06T08:21:38.398840Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2023-09-06T08:21:38.401005Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2023-09-06T08:21:40.329229Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2023-09-06T08:21:40.334360Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2023-09-06T08:21:40.334393Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2023-09-06T08:21:40.335344Z 0 [Note] InnoDB: Waiting for purge to start
2023-09-06T08:21:40.385739Z 0 [Note] InnoDB: 5.7.36 started; log sequence number 12661989
2023-09-06T08:21:40.386093Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2023-09-06T08:21:40.386668Z 0 [Note] Plugin 'FEDERATED' is disabled.
2023-09-06T08:21:40.401632Z 0 [Note] InnoDB: Buffer pool(s) load completed at 230906 16:21:40
2023-09-06T08:21:40.409594Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2023-09-06T08:21:40.409615Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
2023-09-06T08:21:40.409621Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2023-09-06T08:21:40.409625Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2023-09-06T08:21:40.410398Z 0 [Warning] CA certificate ca.pem is self signed.
2023-09-06T08:21:40.410457Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
2023-09-06T08:21:40.411310Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2023-09-06T08:21:40.411396Z 0 [Note] IPv6 is available.
2023-09-06T08:21:40.411422Z 0 [Note] - '::' resolves to '::';
2023-09-06T08:21:40.411460Z 0 [Note] Server socket created on IP: '::'.
2023-09-06T08:21:40.421034Z 0 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2023-09-06T08:21:40.480176Z 0 [Note] Event Scheduler: Loaded 0 events
2023-09-06T08:21:40.480637Z 0 [Note] mysqld: ready for connections.
Version: '5.7.36' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
2023-09-06T08:25:16.406841Z 2 [Note] Access denied for user 'root'@'localhost' (using password: NO)
只是单纯部署使用的话用不到探针的配置。我这里选择使用是因为有个一键式部署和初始化的功能需求,加上探针的话就只需要判断pod状态就可以了,比较简单。