一、K8s部署单机mysql
1.1 说明
- 定制配置数据存放在configMap
- mysql数据放在/opt/mysql目录下(/opt/mysql目录需要事先创建)
- root账号密码使用环境变量env
- 服务暴露方式为nodePort,端口30336
1.2 不足
- 这里为了简单,PV使用了hostPath,所以需要将pod固定在一个node上。可考虑使用nfs。
mysql57_deploy.yml文章来源地址https://www.toymoban.com/news/detail-681137.html
## single mysql deployment
apiVersion: v1
kind: Namespace
metadata:
name: mysql-test
---
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-config
namespace: mysql-test
data:
my.cnf: |-
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
max_connections=2000
default-time_zone='+8:00'
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 10Gi
storageClassName: mysql-local
hostPath:
path: /opt/mysql
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
namespace: mysql-test
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
limits:
storage: 10Gi
storageClassName: mysql-local
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deployment
namespace: mysql-test
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql57
env:
- name: MYSQL_ROOT_PASSWORD
value: Nssg1234*!
image: docker.io/library/mysql:5.7.42
imagePullPolicy: IfNotPresent
resources:
requests:
cpu: 500m
memory: 512Mi
limits:
cpu: 1000m
memory: 1Gi
livenessProbe:
exec:
command: ["mysqladmin", "-uroot", "-p${MYSQL_ROOT_PASSWORD}", "ping"]
initialDelaySeconds: 20
readinessProbe:
exec:
command: ["mysqladmin", "-uroot", "-p${MYSQL_ROOT_PASSWORD}", "ping"]
initialDelaySeconds: 10
volumeMounts:
- name: data
mountPath: /var/lib/mysql
- name: config
mountPath: /etc/mysql/conf.d/my.cnf
subPath: my.cnf
- name: localtime
mountPath: /etc/localtime
volumes:
- name: data
persistentVolumeClaim:
claimName:
mysql-pvc
- name: config
configMap:
name: mysql-config
- name: localtime
hostPath:
path: /etc/localtime
---
apiVersion: v1
kind: Service
metadata:
name: mysql-svc
namespace: mysql-test
spec:
selector:
app: mysql
type: NodePort
ports:
- name: mysql
port: 3306
targetPort: 3306
nodePort: 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
文章来源:https://www.toymoban.com/news/detail-681137.html
到了这里,关于K8s部署单机mysql的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!