目的

        基于k8s集群做mysql应用搭建练习。

        以下实验可以基于我其他文档搭建的k8s集群来进行。

参考文档

        服务(Service) | Kubernetes

        运行一个单实例有状态应用 | Kubernetes

检查集群状态

root@kmaster1:~# kubectl get nodes
NAME       STATUS   ROLES           AGE   VERSION
kmaster1   Ready    control-plane   9d    v1.28.0
kmaster2   Ready    control-plane   9d    v1.28.0
kmaster3   Ready    control-plane   9d    v1.28.0
knode1     Ready    <none>          9d    v1.28.0
knode2     Ready    <none>          9d    v1.28.0
knode3     Ready    <none>          9d    v1.28.0
knode4     Ready    <none>          9d    v1.28.0

准备配置文件

# 创建一个目录用来存放应用配置yaml,方便管理

mkdir /root/application/mysql
cd /root/application/mysql


# 创建配置yaml
vim mysql-deployment.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/single-data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  type: NodePort
  selector:
    app: mysql
  ports:
  - port: 3306
    targetPort: 3306
    nodePort: 31313
---
apiVersion: apps/v1
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: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pvc


# yaml介绍
1、创建一个kind: PersistentVolume也就是pv,只要指定大小,挂载在节点的目录等等
2、创建一个kind: PersistentVolumeClaim也就是pvc,指定大小,绑定的pv名 storageClassName: 
manual
3、创建一个kind: Service将pod的mysql端口放到节点上,放到k8s集群外可以访问node:31313对应mysql:3306
4、创建deployment,启动mysql单节点服务
root@kmaster1:~/application/mysql# kubectl apply -f mysql-deployment.yaml 
persistentvolume/mysql-pv created
persistentvolumeclaim/mysql-pvc created
service/mysql-service created
deployment.apps/mysql created
root@kmaster1:~/application/mysql# kubectl get pv
NAME       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM               STORAGECLASS   REASON   AGE
mysql-pv   20Gi       RWO            Retain           Bound    default/mysql-pvc   manual                  16s
root@kmaster1:~/application/mysql# kubectl get pvc
NAME        STATUS   VOLUME     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql-pvc   Bound    mysql-pv   20Gi       RWO            manual         22s
root@kmaster1:~/application/mysql# kubectl get svc
NAME            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes      ClusterIP   10.96.0.1        <none>        443/TCP          9d
mysql-service   NodePort    10.100.220.219   <none>        3306:31313/TCP   27s
root@kmaster1:~/application/mysql# kubectl get svc -owide
NAME            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE   SELECTOR
kubernetes      ClusterIP   10.96.0.1        <none>        443/TCP          9d    <none>
mysql-service   NodePort    10.100.220.219   <none>        3306:31313/TCP   35s   app=mysql
root@kmaster1:~/application/mysql# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
mysql-6fbb5bdc8b-7lvx8   1/1     Running   0          39s
root@kmaster1:~/application/mysql# kubectl get pods -owide
NAME                     READY   STATUS    RESTARTS   AGE   IP             NODE     NOMINATED NODE   READINESS GATES
mysql-6fbb5bdc8b-7lvx8   1/1     Running   0          48s   10.244.3.132   knode4   <none>           <none>

客户端连接测试

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐