namespace
mysql-ns.yaml
apiVersion: v1
kind: Namespace
metadata:
labels:
kubernetes.io/metadata.name: wgs-mysql
name: wgs-mysql
创建ns
# kubectl apply -f mysql-ns.yaml
namespace/wgs-mysql created
查看ns
# kubectl get ns wgs-mysql -o yaml
apiVersion: v1
kind: Namespace
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","kind":"Namespace","metadata":{"annotations":{},"labels":{"kubernetes.io/metadata.name":"wgs-mysql"},"name":"wgs-mysql"}}
creationTimestamp: "2023-01-03T08:59:29Z"
labels:
kubernetes.io/metadata.name: wgs-mysql
name: wgs-mysql
resourceVersion: "1094074"
uid: 54afc0a0-9070-4946-b74f-a8741479dbd0
spec:
finalizers:
- kubernetes
status:
phase: Active
secret
mysql-secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: mysql-auth
namespace: wgs-mysql
type: Opaque
data:
username: cm9vdA== # echo -n 'root' | base64
password: MTIzNDU2 # echo -n '123456' | base64
创建secret
# kubectl apply -f mysql-secret.yaml
secret/mysql-auth created
查看secret
# kubectl get secret -n wgs-mysql
NAME TYPE DATA AGE
mysql-auth Opaque 2 24s
pv
mysql-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv-volume
namespace: wgs-mysql
labels:
type: nfs
spec:
storageClassName: manual
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
nfs:
path: "/data/k8s-data/mysql"
server: 192.168.174.102
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
namespace: wgs-mysql
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
创建pv
# kubectl apply -f mysql-pv.yaml
persistentvolume/mysql-pv-volume created
persistentvolumeclaim/mysql-pv-claim created
查看pv
# kubectl get pv -n wgs-mysql
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
mysql-pv-volume 5Gi RWO Retain Bound wgs-mysql/mysql-pv-claim manual 108s
# kubectl get pvc -n wgs-mysql
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mysql-pv-claim Bound mysql-pv-volume 5Gi RWO manual 112s
configMap
mysql-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql
namespace: wgs-mysql
labels:
app: mysql
data:
master.cnf: |
# Apply this config only on the master.
[mysqld]
#log-bin
#log_bin_trust_function_creators=1
lower_case_table_names=1
server-id=100
slave.cnf: |
# Apply this config only on slaves.
[mysqld]
super-read-only
log_bin_trust_function_creators=1
创建configmap资源
# kubectl apply -f mysql-configmap.yaml
configmap/mysql created
查看configmap资源
# kubectl describe configmap mysql -n wgs-mysql
Name: mysql
Namespace: wgs-mysql
Labels: app=mysql
Annotations: <none>
Data
====
master.cnf:
----
# Apply this config only on the master.
[mysqld]
#log-bin
#log_bin_trust_function_creators=1
lower_case_table_names=1
server-id=100
slave.cnf:
----
# Apply this config only on slaves.
[mysqld]
super-read-only
log_bin_trust_function_creators=1
BinaryData
====
Events: <none>
Deployment
mysql-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
namespace: wgs-mysql
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.7
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
- name: config-map
mountPath: /etc/mysql/conf.d
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
- name: config-map
configMap:
name: mysql
items:
- key: master.cnf
path: master.cnf
mode: 0644
---
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: wgs-mysql
spec:
#type: NodePort
ports:
- port: 3306
# targetPort: 3306
# nodePort: 33306
selector:
app: mysql
clusterIP: None
创建mysql资源
# kubectl apply -f mysql-deployment.yaml
deployment.apps/mysql created
service/mysql created
查看mysql资源
# kubectl get pod -n wgs-mysql -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-6757c9d8dc-vvrxx 1/1 Running 0 35s 172.20.154.231 192.168.174.106 <none> <none>
# kubectl get svc -n wgs-mysql
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql ClusterIP None <none> 3306/TCP 51s
验证mysql
查看mysql环境变量
# kubectl exec mysql-6757c9d8dc-vvrxx -n wgs-mysql -- env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=mysql-6757c9d8dc-vvrxx
GOSU_VERSION=1.14
MYSQL_MAJOR=5.7
MYSQL_VERSION=5.7.40-1.el7
MYSQL_SHELL_VERSION=8.0.31-1.el7
MYSQL_ROOT_PASSWORD=123456
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT=tcp://10.100.0.1:443
KUBERNETES_PORT_443_TCP=tcp://10.100.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_ADDR=10.100.0.1
KUBERNETES_SERVICE_HOST=10.100.0.1
HOME=/root
查看mysql数据库
# kubectl run -it --rm --image=mysql:5.7 -n wgs-mysql --restart=Never mysql-client -- mysql -h mysql -p123456 -e "show databases"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
pod "mysql-client" deleted
查看mysql配置文件
# kubectl exec mysql-6757c9d8dc-vvrxx -n wgs-mysql -- ls /etc/mysql/conf.d/
master.cnf
# kubectl exec mysql-6757c9d8dc-vvrxx -n wgs-mysql -- cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
# kubectl exec mysql-6757c9d8dc-vvrxx -n wgs-mysql -- cat /etc/mysql/conf.d/master.cnf
# Apply this config only on the master.
[mysqld]
#log-bin
#log_bin_trust_function_creators=1
lower_case_table_names=1
server-id=100
查看配置是否生效
# kubectl run -it --rm --image=mysql:5.7 -n wgs-mysql --restart=Never mysql-client -- mysql -h mysql -p123456 -e 'select @@server_id'
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
| 100 |
+-------------+
pod "mysql-client" deleted
查看pv数据
# ls -l mysql/
total 188476
-rw-r----- 1 999 999 56 Jan 3 19:08 auto.cnf
-rw------- 1 999 999 1680 Jan 3 19:08 ca-key.pem
-rw-r--r-- 1 999 999 1112 Jan 3 19:08 ca.pem
-rw-r--r-- 1 999 999 1112 Jan 3 19:08 client-cert.pem
-rw------- 1 999 999 1676 Jan 3 19:08 client-key.pem
-rw-r----- 1 999 999 1318 Jan 3 19:08 ib_buffer_pool
-rw-r----- 1 999 999 50331648 Jan 3 19:09 ib_logfile0
-rw-r----- 1 999 999 50331648 Jan 3 19:08 ib_logfile1
-rw-r----- 1 999 999 79691776 Jan 3 19:09 ibdata1
-rw-r----- 1 999 999 12582912 Jan 3 19:09 ibtmp1
drwxr-x--- 2 999 999 4096 Jan 3 19:08 mysql
lrwxrwxrwx 1 999 999 27 Jan 3 19:08 mysql.sock -> /var/run/mysqld/mysqld.sock
drwxr-x--- 2 999 999 4096 Jan 3 19:08 performance_schema
-rw------- 1 999 999 1680 Jan 3 19:08 private_key.pem
-rw-r--r-- 1 999 999 452 Jan 3 19:08 public_key.pem
-rw-r--r-- 1 999 999 1112 Jan 3 19:08 server-cert.pem
-rw------- 1 999 999 1680 Jan 3 19:08 server-key.pem
drwxr-x--- 2 999 999 12288 Jan 3 19:08 sys