文章目录
- 一、K8s部署单机mysql
- 1.1 说明
- 1.2 不足
- 二、部署
- 三、检查
一、K8s部署单机mysql
1.1 说明
- 定制配置数据存放在configMap
- mysql数据放在/opt/mysql目录下(/opt/mysql目录需要事先创建)
- root账号密码使用环境变量env
- 服务暴露方式为nodePort,端口30336
1.2 不足
- 这里为了简单,PV使用了hostPath,所以需要将pod固定在一个node上。可考虑使用nfs。
mysql57_deploy.yml
## single mysql deployment
apiVersion: v1
kind: Namespace
metadata:name: mysql-test
---
apiVersion: v1
kind: ConfigMap
metadata:name: mysql-confignamespace: mysql-test
data:my.cnf: |-[client]default-character-set=utf8mb4[mysql]default-character-set=utf8mb4[mysqld] max_connections=2000default-time_zone='+8:00'character-set-server=utf8mb4collation-server=utf8mb4_unicode_ci
---
apiVersion: v1
kind: PersistentVolume
metadata:name: mysql-pv
spec:accessModes:- ReadWriteOncecapacity:storage: 10GistorageClassName: mysql-localhostPath:path: /opt/mysql
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: mysql-pvcnamespace: mysql-test
spec:accessModes:- ReadWriteOnceresources:requests:storage: 1Gilimits:storage: 10GistorageClassName: mysql-local
---
apiVersion: apps/v1
kind: Deployment
metadata:name: mysql-deploymentnamespace: mysql-test
spec:replicas: 1selector:matchLabels:app: mysqltemplate:metadata:labels:app: mysqlspec:containers:- name: mysql57env:- name: MYSQL_ROOT_PASSWORDvalue: Nssg1234*!image: docker.io/library/mysql:5.7.42imagePullPolicy: IfNotPresentresources:requests:cpu: 500mmemory: 512Milimits:cpu: 1000mmemory: 1GilivenessProbe:exec:command: ["mysqladmin", "-uroot", "-p${MYSQL_ROOT_PASSWORD}", "ping"]initialDelaySeconds: 20readinessProbe:exec:command: ["mysqladmin", "-uroot", "-p${MYSQL_ROOT_PASSWORD}", "ping"]initialDelaySeconds: 10volumeMounts:- name: datamountPath: /var/lib/mysql- name: configmountPath: /etc/mysql/conf.d/my.cnfsubPath: my.cnf- name: localtimemountPath: /etc/localtimevolumes:- name: datapersistentVolumeClaim:claimName:mysql-pvc- name: configconfigMap:name: mysql-config- name: localtimehostPath:path: /etc/localtime
---
apiVersion: v1
kind: Service
metadata:name: mysql-svcnamespace: mysql-test
spec:selector:app: mysqltype: NodePortports:- name: mysqlport: 3306targetPort: 3306nodePort: 30336
二、部署
mkdir /opt/mysql
ctr -n k8s.io i pull docker.io/library/mysql:5.7.42
kubectl apply -f mysql57_deploy.yml
三、检查
kubectl get deployment -n mysql-test
kubectl get pods -n mysql-test
kubectl exec -it [pod_name] -n mysql-test -- /bin/sh