k8s安装MySQL
给node03 设置标签 app=mysql
在node03上设置PV,并给该节点打上app=mysql标签,在deploy中nodeSelector选择该节点,使其调度在这个节点上
kubectl label node node03 app=mysql
# 查看标签
kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
master01 Ready control-plane,master 58d v1.20.9 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master01,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=
node01 Ready <none> 57d v1.20.9 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node01,kubernetes.io/os=linux
node02 Ready <none> 56d v1.20.9 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node02,kubernetes.io/os=linux
node03 Ready <none> 56d v1.20.9 app=mysql,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node03,kubernetes.io/os=linux
创建namespace
kind: Namespace
apiVersion: v1
metadata:
name: mysql-dev
创建config map
kind: ConfigMap
apiVersion: v1
metadata:
name: my.cnf
namespace: default
data:
my.cnf:
[client]
default-character-set=utf8
port=3306
[mysqld]
socket=/var/run/mysqld/mysqld.sock
pid-file = /var/run/mysqld/cpep.pid
datadir = /var/lib/mysql
character-set-server=utf8
init_connect=SET NAMES utf8
创建PV和PVC持久化
# 在node03上创建目录
mkdir -p /data/k8s/mysql-dev/mysql-xu/
pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: dev-mysql-xu
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 20Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/data/k8s/mysql-dev/mysql-xu"
pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: dev-mysql-xu-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
创建deploy和service 并指定配置集
deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: dev-mysql
spec:
replicas: 1
selector:
matchLabels:
app: dev-mysql
template:
metadata:
labels:
app: dev-mysql
spec:
containers:
- name: mysql
image: mysql:5.7
imagePullPolicy: IfNotPresent
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
ports:
- name: mysql
containerPort: 3306
volumeMounts:
- name: mysql-volume
mountPath: /var/lib/mysql
- name: config-my.cnf
mountPath: "/etc/mysql/mysql.conf.d/"
volumes:
- name: mysql-volume
persistentVolumeClaim:
claimName: dev-mysql-xu-claim
- name: config-my.cnf
configMap:
name: my.cnf
nodeSelector:
app: mysql
service.yaml (NodePort)
apiVersion: v1
kind: Service
metadata:
name: dev-mysql
spec:
type: NodePort
ports:
- port: 3306
targetPort: 3306
nodePort: 30306
selector:
app: dev-mysql