一、K8s部署单机mysql

1.1 说明

  1. 定制配置数据存放在configMap
  2. mysql数据放在/opt/mysql目录下(/opt/mysql目录需要事先创建)
  3. root账号密码使用环境变量env
  4. 服务暴露方式为nodePort,端口30336

1.2 不足

  1. 这里为了简单,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-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
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐