1 kubernetes安装

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
APP_NAME='mysql'
STORAGE_CLASS='rook-cephfs'
PVC_VOLUME_MODE='Filesystem'
PVC_RESOURCES_REQUESTS='20Gi'
PVC_ACCESS_MODES='ReadWriteOnce'
NAMESPACE='middleware'
REPLICAS=1
IMAGE="harbor.example.com/library/mysql:8.0.33"
RESOURCES_REQUESTS_CPU='500m'
RESOURCES_LIMITS_CPU='1000m'
RESOURCES_REQUESTS_MEMORY='512Mi'
RESOURCES_LIMITS_MEMORY='1Gi'
CONTAINER_PORT=3306
PORT_NAME='mysql'
PORT_PROTOCOL='TCP'
SERVICE_PORT=3306
DATA_VOLUMES_NAME='data'
DATA_VOLUMES_MOUNTPATH='/var/lib/mysql'
CONFIG_VOLUMES_NAME='config'
CONFIG_VOLUMES_MOUNTPATH='/etc/mysql/conf.d/config-file.cnf'
CONFIG_VOLUMES_SUBPATH='config-file.cnf'
SERVICE_PORT_NAME='mysql'
MYSQL_ROOT_PWD=`echo -n 'Password' | base64`

mkdir -p ~/deploy/${APP_NAME}
cd ~/deploy/${APP_NAME}

cat <<EOF | tee ~/deploy/${APP_NAME}/namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: ${NAMESPACE}
EOF

kubectl apply -f ~/deploy/${APP_NAME}/namespace.yaml

cat <<EOF | tee ~/deploy/${APP_NAME}/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: ${APP_NAME}-config
  namespace: ${NAMESPACE}
data:
  ${CONFIG_VOLUMES_SUBPATH}: |-
    [mysqld]
    default_authentication_plugin=mysql_native_password
    explicit_defaults_for_timestamp
    character-set-server=UTF8MB4
    collation-server=utf8mb4_general_ci
    max_connections=1000
    lower_case_table_names=1
    max_connect_errors=1000
EOF

kubectl apply -f ~/deploy/${APP_NAME}/configmap.yaml

cat <<EOF | tee ~/deploy/${APP_NAME}/secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: ${APP_NAME}-secret
  namespace: ${NAMESPACE}
data:
  mysql-root-password: ${MYSQL_ROOT_PWD}
EOF

kubectl apply -f ~/deploy/${APP_NAME}/secret.yaml

cat <<EOF | tee ~/deploy/${APP_NAME}/pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: ${APP_NAME}-pvc
  namespace: ${NAMESPACE}
spec:
  storageClassName: ${STORAGE_CLASS}
  volumeMode: ${PVC_VOLUME_MODE}
  resources:
    requests:
      storage: ${PVC_RESOURCES_REQUESTS}
  accessModes:
    - ${PVC_ACCESS_MODES}
EOF

kubectl apply -f ~/deploy/${APP_NAME}/pvc.yaml

cat <<EOF | tee ~/deploy/${APP_NAME}/service-headless.yaml
apiVersion: v1
kind: Service
metadata:
  name: ${APP_NAME}-svc-headless
  namespace: ${NAMESPACE}
  labels:
    app: ${APP_NAME}
spec:
  ports:
    - name: ${SERVICE_PORT_NAME}
      port: ${SERVICE_PORT}
      protocol: ${PORT_PROTOCOL}
      targetPort: ${CONTAINER_PORT}
  selector:
    app: ${APP_NAME}
  type: ClusterIP
  clusterIP: None
EOF

cat <<EOF | tee ~/deploy/${APP_NAME}/service.yaml
kind: Service
apiVersion: v1
metadata:
  name: ${APP_NAME}-svc
  namespace: ${NAMESPACE}
  labels:
    app: ${APP_NAME}
spec:
  ports:
    - name: ${APP_NAME}
      protocol: TCP
      port: ${SERVICE_PORT}
      targetPort: ${CONTAINER_PORT}
      nodePort: 30306
  selector:
    app: ${APP_NAME}
  type: NodePort
EOF

kubectl apply -f ~/deploy/${APP_NAME}/service-headless.yaml

cat <<EOF | tee ~/deploy/${APP_NAME}/statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: ${NAMESPACE}
  name: ${APP_NAME}-statefulset
spec:
  selector:
    matchLabels:
      app: ${APP_NAME}
  serviceName: ${APP_NAME}-svc-headless
  replicas: ${REPLICAS}
  volumeClaimTemplates:
    - metadata:
        name: ${APP_NAME}-pvc
      spec:
        accessModes: 
          - ${PVC_ACCESS_MODES}
        storageClassName: ${STORAGE_CLASS}
        resources:
          requests:
            storage: ${PVC_RESOURCES_REQUESTS}
  template:
    metadata:
      labels:
        app: ${APP_NAME}
    spec:
      volumes:
        - name: ${DATA_VOLUMES_NAME}
          persistentVolumeClaim:
            claimName: ${APP_NAME}-pvc
        - name: ${CONFIG_VOLUMES_NAME}
          configMap:
            name: ${APP_NAME}-config
      containers:
        - name: ${APP_NAME}
          image: ${IMAGE}
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  key: mysql-root-password
                  name: ${APP_NAME}-secret
            - name: TZ
              value: Asia/Shanghai
          ports:
            - containerPort: ${CONTAINER_PORT}
              name: ${PORT_NAME}
              protocol: ${PORT_PROTOCOL}
          resources:
            requests:
              cpu: ${RESOURCES_REQUESTS_CPU}
              memory: ${RESOURCES_REQUESTS_MEMORY}
            limits:
              cpu: ${RESOURCES_LIMITS_CPU}
              memory: ${RESOURCES_LIMITS_MEMORY}
          volumeMounts:
            - name: ${DATA_VOLUMES_NAME}
              mountPath: ${DATA_VOLUMES_MOUNTPATH}
            - name: ${CONFIG_VOLUMES_NAME}
              mountPath: ${CONFIG_VOLUMES_MOUNTPATH}
              subPath: ${CONFIG_VOLUMES_SUBPATH}
          readinessProbe:
            exec:
              command:
                - /bin/bash
                - '-ec'
                - |
                  password_aux="\${MYSQL_ROOT_PASSWORD:-}"
                  if [[ -f "\${MYSQL_ROOT_PASSWORD_FILE:-}" ]]; then
                      password_aux=\$(cat "\$MYSQL_ROOT_PASSWORD_FILE")
                  fi
                  mysqladmin status -uroot -p"\${password_aux}"
            failureThreshold: 5
            initialDelaySeconds: 10
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 3
          livenessProbe:
            exec:
              command:
                - /bin/bash
                - '-ec'
                - |
                  password_aux="\${MYSQL_ROOT_PASSWORD:-}"
                  if [[ -f "\${MYSQL_ROOT_PASSWORD_FILE:-}" ]]; then
                      password_aux=\$(cat "\$MYSQL_ROOT_PASSWORD_FILE")
                  fi
                  mysqladmin status -uroot -p"\${password_aux}"
            failureThreshold: 5
            initialDelaySeconds: 10
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 3
          startupProbe:
            exec:
              command:
                - /bin/bash
                - '-ec'
                - |
                  password_aux="\${MYSQL_ROOT_PASSWORD:-}"
                  if [[ -f "\${MYSQL_ROOT_PASSWORD_FILE:-}" ]]; then
                      password_aux=\$(cat "\$MYSQL_ROOT_PASSWORD_FILE")
                  fi
                  mysqladmin status -uroot -p"\${password_aux}"
            failureThreshold: 10
            initialDelaySeconds: 30
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 3
EOF

kubectl apply -f ~/deploy/${APP_NAME}/statefulset.yaml

kubectl -n middleware exec -it mysql-statefulset-0 -- /bin/bash

mysql -uroot -p'Password'


CREATE DATABASE IF NOT EXISTS jumpserver DEFAULT CHARACTER SET UTF8MB4 COLLATE utf8mb4_general_ci;
CREATE USER 'jumpserver'@'%' IDENTIFIED WITH mysql_native_password BY 'TestPassword';
GRANT ALL ON jumpserver.* TO 'jumpserver'@'%';
FLUSH PRIVILEGES;

# 本地
mysql -ujumpserver -p'TestPassword'
# 线上
mysql -ujumpserver -p'TestPassword' -h mysql-svc-headless -P 3306

2 docker安装

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
docker volume create --driver local mysql-data

docker run -d --name mysql \
-p 3306:3306 \
-e TZ=Asia/Shanghai \
-e MYSQL_ROOT_PASSWORD=password \
-e MYSQL_DATABASE=test \
-e MYSQL_USER=test \
-e MYSQL_PASSWORD=password \
-v mysql-data:/var/lib/mysql \
mysql:8.2.0 \
--default-authentication-plugin=caching_sha2_password \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_unicode_ci \
--max-connections=699 \
--max-connect-errors=599 \
--lower-case-table-names=1 \
--host-cache-size=0 \
--log-timestamps=SYSTEM

docker exec -it mysql mysql -uroot -ppassword
docker exec -it mysql mysql -utest -ppassword test