kubernetes 部署 mysql 服务

1、使用如下yaml文件部署mysql服务
1.1、创建deployment

cat mysql-deplpy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    k8s.kuboard.cn/name: mysql
  name: mysql
  namespace: default
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s.kuboard.cn/name: mysql
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        k8s.kuboard.cn/name: mysql
    spec:
      containers:
        - env:
            - name: MYSQL_ROOT_PASSWORD
              value: '123456'
          image: 'mysql:5.7'
          imagePullPolicy: IfNotPresent
          name: mysql
		  lifecycle: 
            preStop:          #优雅的停止服务
              exec: 
                command: 
				  [
				  "/bin/sh"
				  "-c"
				  "echo this pod is stopping. > /stop.log && sleep 30s"    #优雅停止服务时间
				  ]       
          ports:
            - containerPort: 3306
              name: mysql-port
              protocol: TCP
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      terminationGracePeriodSeconds: 40      #优雅的停止服务,此值必须大于preStop的sleep值

1.2、创建service

cat mysql-svc.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    k8s.kuboard.cn/name: mysql
  name: mysql
  namespace: default
spec:
  ports:
    - name: mysql-port
      nodePort: 30295
      port: 3306
      protocol: TCP
      targetPort: 3306
  selector:
    k8s.kuboard.cn/name: mysql
  type: NodePort

2、应用yaml文件创建服务

kubectl apply -f mysql-deplpy.yaml
kubectl apply -f mysql-svc.yaml

3、查看是否创建成功

[root@master ~]# kubectl get pod,svc 
NAME                            READY   STATUS    RESTARTS   AGE
pod/mysql-6c9b459c75-lk6dq      1/1     Running   0          1h

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
service/mysql        NodePort    10.106.41.29     <none>        3306:30295/TCP   1h

4、进入容器

[root@master ~]# kubectl exec -it mysql-6c9b459c75-lk6dq  bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@mysql-6c9b459c75-lk6dq:/# 
root@mysql-6c9b459c75-lk6dq:/# 
root@mysql-6c9b459c75-lk6dq:/# 
root@mysql-6c9b459c75-lk6dq:/# 
root@mysql-6c9b459c75-lk6dq:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sonar              |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

5、mysql服务部署完成,可以使用集群内地址或者nodeport方式进行mysql数据库连接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维那些事~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值