As I introduced to you before, the development environment I use is containerised, but I will not use K8 in the first two years. Most of them are Docker or Docker-compose. With more than one year of study and use of K8 in work, I have always had the idea to use K8 to make a set of portable development environment, and then change the computer without worrying about the installation of database, cache, queue and other basic software. I’ll be able to get a new office computer next month, and I can’t put it off any longer with the “just use it” excuse.

If you are asked, “What is the basic software that your application relies on most?” “, I guess most people would answer: “MySQL and Redis”, after all, everything is added, deleted, changed, and checked, how can we do curds all day long without them.

The preparatory work

Tool selection

Since it is to build a development environment on the local Kubernetes, the computer must have a Kubernetes cluster first. Currently, tools that can run Kubernetes clusters locally are: Minikube, Kind and K3d, our MySQL and Redis all rely on the first write resource definition YAML file, and then through Kubectl to Kubernetes cluster execution, so these three tools can be used either.

My own local use is Minikube, this is Kubernetes official tool, to tell the truth, after running up the computer has a point card, Minikube installation steps can refer to my previous article “Minikube- Kubernetes cluster running on the laptop”. The other two, Kind and K3d, are lightweight clusters that support multi-node deployments. Among them, I recommend K3d, especially those who use THE M1 MacBook chip. Currently, they can only use K3d to install Kubernetes cluster.

K3d uses docker containers to run k3S clusters locally. K3s is a lightweight Kubernetes open-source by Rancher Lab. K3d perfectly inherits the advantages of k3S, such as simplicity, speed and less occupation of resources. The image size is only more than 100 M, the startup speed is fast, and the multi-node cluster is supported. While K3S is a lightweight tailoring of Kubernetes, it offers complete functionality that allows complex cloud-native applications like Istio to run smoothly on K3S.

In addition to fast startup speed and less resource consumption, K3d also has a good performance in the field of edge computing and embedded. Since K3S itself is mainly used on the edge, it supports many devices and architectures, such as ARM64 and ARMv7 processors. Many old PCS and devices like raspberry PI can be turned into K3S clusters that burn out for local r&d testing.

Preliminary knowledge

Kubernetes: Kubernetes: Kubernetes: Kubernetes: Kubernetes: Kubernetes: Kubernetes: Kubernetes: Kubernetes: Kubernetes These are all things we need to build our development environment.

Kubernetes Pod Starter Guide

Deployment of the orchestration tool

Combine learning and practice to quickly master Kubernetes Service

Manage configuration files in an object-oriented manner

Understand StatefulSet in depth and orchestrate stateful applications with Kubernetes

MySQL installation

Before we start installing MySQL and Redis on Kubernetes, let me explain how to install these two basic software services.

  1. First of all, because it’s a development environment, we don’t go for high availability, we try to be as lean as possible. Later in this article I will provide links to tutorials on installing master slave and clustered databases for your reference.

  2. The idea behind installing MySQL and Redis is the same. Each service consists of the following major components:

    ① A single copy Pod as a carrier to run MySQL or Redis.

    (2) A Deployment controller for scheduling PODS. Since there is only one Pod in the service and the build order does not need to be maintained, StatefulSet is not required to be used as the controller of Pod.

    (3) A ConfigMap object, which contains configuration items required in MySQL or Redis configuration files, will be mounted to the application container when creating pods.

    (4) A Service object, using Pod as its back-end endpoint, exposes the Service with the same NodeId:NodePort.

The diagram below illustrates the collaboration between these four parts.

After explaining our idea of setting up MySQL and Redis development environment on Kubernetes, we can enter the practical link, I prepared for you can be directly used to use YAML resource definition file.

Create MySQL configuration

We will first create a ConfigMap named mysql-db-config and later mount these configurations as my.cnf configuration files into the mysql application Pod container.

### file name mysql-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-db-config
  namespace: default
  labels:
    app: mysql-db-config
data:
  my.cnf: | [client] default-character-set=utf8mb4 [mysql] default-character-set=utf8mb4 [mysqld] character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci init_connect='SET NAMES utf8mb4' skip-character-set-client-handshake = true Max_connections =2000 secure_file_priv=/var/lib/mysql datadir=/var/lib/mysql bind-address=0.0.0.0 symbolic-links=0Copy the code

Kubectl = Kubernetes; kubectl = Kubernetes; kubectl = Kubernetes; kubectl = Kubernetes

kubectl apply -f mysql-singleton/mysql-configmap.yaml
Copy the code

Create MySQL container and Service

With ConfigMap for MySQL configuration, we can mount it as a configuration file when we create a container running MySQL:

### file name deployment-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  type: NodePort
  ports:
    - port: 3306
      nodePort: 30306
      targetPort: mysql
  selector:
    app: mysql
---
apiVersion:
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - image: Mysql: 5.7
          name: mysql
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: root
          ports:
            - containerPort: 3306
              name: mysql
          volumeMounts:
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
            - name: mysql-config
              mountPath: /etc/mysql/conf.d/my.cnf
              subPath: my.cnf
      volumes:
        - name: mysql-persistent-storage
          emptyDir: {}
        - name: mysql-config
          configMap:
            name: mysql-db-config
            items:
              - key: my.cnf
                path: my.cnf
Copy the code

Similarly, after submitting YAML to Kubernetes using Kubectl, our development environment MySQL will be set up when the resources are created

kubectl apply -f mysql-singleton/deployment-service.yaml
Copy the code

through

In the YAML file above, there are three points that need to be elaborated:

  1. Using mysql db – config. This ConfigMap my CNF. This configuration items to my CNF file name mounted in the container, but because after mount in covered containers conf. D the contents of the directory. With volumemounts.subpath, you can mount key-values in ConfigMap to the container without overwriting existing files in the container.
  2. NodePort is used to connect to the Kubernetes database created by Minikube. NodePort is used to connect to the Kubernetes database created by Minikube. You can run the minikube IP command to query the NodeIP.
  3. The value of the emptyDir data volume should be synchronized with the Pod when the Pod is deleted by Kubectl DELETE. The value should be released after a Pod crash or a Shotdown cluster. Data will not be lost.

After creating MySQL on Kubernetes, we can connect to MySQL service through any client or MySQL command line.

mysql -uroot -proot -h {minikube-ip} -P 30306
Copy the code

Install Redis

Talk clearly how to create single node using Kubernetes MySQL, to create a singleton Redis believe everyone to roughly the process also is clear, only few is defined Redis service these YAML files. I have helped you step on the pit, the following YAML is I have been offline debugging for a period of time, and can correctly complete Redis data persistence.

Because the ConfigMap for Redis is too long, I will put the YAML files required to install Redis on GitHub. You can read the original document or go to github.com/kevinyan815… Visit download.

In this repository I provide detailed installation steps for MySQL and Redis, as well as YAML definition files for various resources, including the previous tutorial on installing ETCD clusters.

For details about the installation procedure, see Setting up an ETCD cluster and WebUI using Kubernetes

Please contact me in the comments if you have any problems during the installation process. If you want to see other basic software installation tutorials on Kubernetes, please also let me know.

conclusion

This article sorted out the installation of MySQL and Redis on Kubernetes, the two basic software we commonly use the operation steps, because the purpose is to use in the local development environment, so strive to resource definition as simple as possible, can do data persistence on the line, high availability is not discussed here. If you are interested in creating MySQL clusters on Kubernetes, please refer to the links I have provided below.

[MySQL Operator FOR Kubernetnes](: medium.com/oracledevs/…)

That’s all for today’s article, please give me a thumbs up if you like my article, AND I will share what I have learned and seen and first-hand experience through technical articles every week. Thank you for your support. Wechat search concerns the public number “network management talk BI talk” every week to teach you an advanced knowledge.