当前位置:网站首页>Notes series k8s orchestration MySQL container - stateful container creation process
Notes series k8s orchestration MySQL container - stateful container creation process
2022-07-27 05:24:00 【Master yuan】
List of articles
0. The goal is
Use k8s Create a personal definition mysql Environment , Create a table by default phone, And can store data persistently .
1. Environmental preparation
- docker 19.03
- k8s 1.18.13
- minikube v1.14
- mysql Mirror image
2. Database table file
The file named :create_phone.sql
CREATE DATABASE IF NOT EXISTS `test`;
USE `test`;
DROP TABLE IF EXISTS `phone`;
CREATE TABLE `phone` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`brand` VARCHAR(50) NOT NULL ,
`name` VARCHAR(50) NOT NULL,
`price` INT(10) NOT NULL,
PRIMARY KEY (`id`) USING BTREE
)ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4;
3. Create installation script
The file named :install_data.sh
#!/bin/bash
mysql -uroot -p$MYSQL_ROOT_PASSWORD <<EOF
source $WORK_PATH/$FILE_0;
4. Create custom mysql Mirror image
establish Dockerfile file
FROM mysql:latest
MAINTAINER [email protected]
ENV WORK_PATH /usr/yy/test/work
ENV AUTO_RUN_DIR /docker-entrypoint-initdb.d
ENV FILE_0 create_phone.sql
ENV INSTALL_DATA_SHELL install_data.sh
RUN mkdir -p $WORK_PATH
COPY ./$FILE_0 $WORK_PATH
COPY ./$INSTALL_DATA_SHELL $AUTO_RUN_DIR/
RUN chmod a+x $AUTO_RUN_DIR/$INSTALL_DATA_SHELL
After creation, use the following instructions to package the image
docker build -t yy-msql:0.0.8 .
Pay attention to the last point and don't fall .
5. establish persitentVolume file
First create a msql-pv.yaml file
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-mysql
spec:
accessModes:
- ReadWriteMany
capacity:
storage: 5Gi
claimRef:
name: pvc-mysql
namespace: default
hostPath: # NOTE The mariadb requires the ownership of this directory, hence set the owner to uid:gid as 999:999
path: /home/yayuan/yy-k8s/db/mysql-data/
persistentVolumeReclaimPolicy: Retain
#storageClassName: slow
After creation, use the following instructions to declare PV
kubectl apply -f mysql-pv.yaml
6. establish pvc
Create a declaration file mysql-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-mysql
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
volumeName: pv-mysql
Be careful : there volumeName That's what I said pv Name .
Then also use the following instructions to declare :
kubectl apply -f mysql-pvc.yaml
7. establish deployment And service
The file name is :deployment-service.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
type: NodePort
ports:
- port: 3306
nodePort: 30306
targetPort: mysql
selector:
app: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: yy-mysql:0.0.8
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: root
- name: MYSQL_USER
value: user
- name: MYSQL_PASSWORD
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
- name: mysql-config
mountPath: /etc/mysql/conf.d/my.cnf
subPath: my.cnf
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: pvc-mysql
- name: mysql-config
configMap:
name: mysql-db-config
items:
- key: my.cnf
path: my.cnf
still
kubectl apply -f deployment-service.yaml
9. Look at the results
By this time, it should have been created , Let's see the effect
[email protected]:~/yy-k8s/k8s$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv-mysql 5Gi RWX Retain Terminating default/pvc-mysql 16h
[email protected]:~/yy-k8s/k8s$ kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc-mysql Bound pv-mysql 5Gi RWX standard 16h
[email protected]:~/yy-k8s/k8s$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 29d
mysql NodePort 10.103.78.144 <none> 3306:30306/TCP 20h
[email protected]:~/yy-k8s/k8s$ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
mysql 1/1 1 1 20h
[email protected]:~/yy-k8s/k8s$ kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-54ddf6cf-lghfj 1/1 Running 0 56m 172.17.0.7 host-192-168-0-26 <none> <none>
[email protected]:~/yy-k8s/k8s$ kubectl exec -it mysql-54ddf6cf-lghfj bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
[email protected]:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.23 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| phone |
+----------------+
1 row in set (0.00 sec)
mysql> show create phone;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'phone' at line 1
mysql> show create table phone;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| phone | CREATE TABLE `phone` (
`id` int NOT NULL AUTO_INCREMENT,
`brand` varchar(50) NOT NULL,
`name` varchar(50) NOT NULL,
`price` int NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
After inserting data , instant pod End of life cycle , Data is still stored on disk .
10. summary
There is still something missing here , Including but not limited to :
- configMap
- secret
- storageClass
The digging goes on and on ! If someone reads it, fill it out , It's my own notes that no one reads , Fill in according to your mood .
边栏推荐
- [optical flow] - data format analysis, flowwarp visualization
- JVM上篇:内存与垃圾回收篇十一--执行引擎
- Rolling Division
- Detailed description of polymorphism
- Basic operation of vim
- LeetCode刷题之322 Coin Change
- JVM上篇:内存与垃圾回收篇十二--StringTable
- How does the TCP server handle multiple client connections on one port (one-to-one or one to many)
- pytorch 数据类型 和 numpy 数据 相互转化
- Explore the mysteries of the security, intelligence and performance of the universal altek platform!
猜你喜欢

35.滚动 scroll

JVM上篇:内存与垃圾回收篇十二--StringTable

JVM Part 1: memory and garbage collection part 6 -- runtime data area local method & local method stack

Explore the mysteries of the security, intelligence and performance of the universal altek platform!

JVM Part 1: memory and garbage collection part 8 - runtime data area - Method area

MQ set expiration time, priority, dead letter queue, delay queue

Integrate SSM

Use of collection framework

The project connects with Alipay payment, and the intranet penetration realizes the monitoring of asynchronous callback notification of successful payment of Alipay

JVM上篇:内存与垃圾回收篇三--运行时数据区-概述及线程
随机推荐
The interface can automatically generate E and other asynchronous access or restart,
Constraints of MySQL table
Detailed description of polymorphism
[optical flow] - data format analysis, flowwarp visualization
2021 OWASP top 6-10 collection
Bean的生命周期&&依赖注入*依赖自动装配
B1024 科学计数法
秒杀系统设计
JVM Part 1: memory and garbage collection part 9 - runtime data area - object instantiation, memory layout and access location
Summary of knowledge points (I)
MQ set expiration time, priority, dead letter queue, delay queue
Use of collection framework
笔记系列之docker安装Postgresql 14
数据库设计——关系数据理论(超详细)
I've heard the most self disciplined sentence: those I can't say are silent
DBUtils
What is the future development direction of software testing engineers?
稀疏数组→五子棋的存盘续盘等操作
知识点总结(一)
B1029 old keyboard