Skip to content

Latest commit

 

History

History
138 lines (109 loc) · 2.81 KB

k8s_mysql-test.md

File metadata and controls

138 lines (109 loc) · 2.81 KB

...menustart

...menuend

Create A Persistent Volume

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-mysql-data
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 100Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/opt/data/mysql"

Create a mysql in k8s with root/rootpwd

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-mysql-57
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

---
apiVersion: v1
items:
- apiVersion: apps/v1
  kind: Deployment
  metadata:
    labels:
      k8s-app: mysql-57
    name: mysql-57
  spec:
    replicas: 1
    selector:
      matchLabels:
        k8s-app: mysql-57
    strategy:
      type: RollingUpdate
    template:
      metadata:
        labels:
          k8s-app: mysql-57
      spec:
        containers:
        - env:
          - name: MYSQL_ROOT_PASSWORD
            value: rootpwd
          image: mysql:5.7
          imagePullPolicy: IfNotPresent
          name: mysql-57
          resources:
            limits:
              cpu: "1"
              memory: 1Gi
            requests:
              cpu: 100m
              memory: 128Mi
          volumeMounts: # sub level under containers [optional]
            - mountPath: "/var/lib/mysql"
              name: pvc-mysql-57-storage
        dnsPolicy: ClusterFirst
        restartPolicy: Always
        volumes: # same level as containers [optional]
          - name: pvc-mysql-57-storage
            persistentVolumeClaim:
              claimName: pvc-mysql-57

- apiVersion: v1
  kind: Service
  metadata:
    name: mysql-57
  spec:
    ports:
    - name: 3306-3306-tcp
      port: 3306
      protocol: TCP
      targetPort: 3306
    selector:
      k8s-app: mysql-57
    sessionAffinity: None
    type: ClusterIP

kind: List

Expose Mysql service in k8s

so far you can not access mysql exterannly because its type is ClusterIP.

use portforward , say you have mysql service under namespace mysql-57, you listen on port 33057 on all address, and forward to 3306 in a pod select by the deployment.

// START /B   to run in background
START /B  -n mysql-57  --address 0.0.0.0   port-forward deployment/mysql-57 33057:3306

Now you can connect to mysql from outside

mysql -h <host-name> -P 33057 -u<user> -p<passwd>