注意k8s各版本的引用:
1.extensions/v1beta1是用于kubernetes版本在1.6之前,资源申明
2.apps/v1beta1是用于1.6-1.9版本之间
3.apps/v1是1.9版本以后使用
目前使用的是1.16.3版本可以正常使用apps/v1

建立命名空间:

kubectl create namespace mysqlspace

建立PVC

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql
  namespace: mysqlspace
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: "50Gi"
  volumeName: 
  storageClassName: nfs

建立configmap配置

apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-config
  namespace: mysqlspace
data:
  mysqld.cnf: |
        [mysqld]
        pid-file        = /var/run/mysqld/mysqld.pid
        socket          = /var/run/mysqld/mysqld.sock
        datadir         = /var/lib/mysql
        log-error      = /var/log/mysql/error.log
        bind-address   = 0.0.0.0
        # Disabling symbolic-links is recommended to prevent assorted security risks
        symbolic-links=0
        max_connections=1000
        default_storage_engine=innodb
        skip_external_locking
        lower_case_table_names=1
        skip_host_cache
        skip_name_resolve
        character_set_server=utf8
        sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

建立depployment及svc配置:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: mysql1
  name: mysql1
  namespace: mysqlspace
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql1
  serviceName: ""
  template:
    metadata:
      labels:
        app: mysql1
    spec:
      containers:
      - env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"
        image: mysql:5.7
        imagePullPolicy: Always
        args:
        - --default_authentication_plugin=mysql_native_password
        - --character-set-server=utf8mb4
        - --collation-server=utf8mb4_unicode_ci
        name: mysql1
#        resources:
#          limits:
#            cpu: 800m
#            memory: 2Gi
#          requests:
#            cpu: 500m
#            memory: 512Mi
#        securityContext:
#          privileged: false
        ports:
        - name: mysql
          containerPort: 3306 # 容器应用监听的端口号

        volumeMounts:
        - name: db-data
          mountPath: /var/lib/mysql
        - name: mysql-config
          mountPath: /etc/mysql/conf.d/
      volumes:
      - name: db-data
        persistentVolumeClaim:
          claimName: mysql
      - name: mysql-config
        configMap: 
          name: mysql-config
---
apiVersion: v1
kind: Service
metadata:
  name: mysql1
  namespace: mysqlspace
spec:
  type: NodePort
  selector:
    app: mysql1
  ports:
  - name: 3306-3306-tcp
    nodePort: 31306
    port: 3306
    protocol: TCP
    targetPort: 3306

建立ingress.yaml配置

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-nginx
  namespace: mysqlspace
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: mysql.onboot.cn   #指定一个域名
    http:
      paths:
      - path: /    #此处可以指定跳转的url比如 /serviceA
        backend:
          serviceName: mysql1    #之前启动的集群服务名称
          servicePort: 31306
最后修改日期:2021年4月16日

作者

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。