• kubernetes下安装mysql


    参考文档:https://blog.csdn.net/sealir/article/details/81177747

    注:有mysql安装在k8s集群内,集群外且通过k8s service endpoint代理外部mysql服务供内k8s内部集群访问两种方式,本文为第二种

    一,先在k8s-node上docker安装mysql,并远程连接可用

    1.下载mysql镜像

    如果很慢请参考docker pull centos慢问题的解决方案

    [root@k8s-node1 ~]# docker pull mysql:5.7
    5.7: Pulling from library/mysql
    27833a3ba0a5: Pull complete 
    864c283b3c4b: Pull complete 
    cea281b2278b: Pull complete 
    8f856c14f5af: Pull complete 
    9c4f38c23b6f: Pull complete 
    1b810e1751b3: Pull complete 
    5479aaef3d30: Pull complete 
    1d924ec3d520: Pull complete 
    1ab7ae63ac60: Pull complete 
    08aa5f3680e9: Pull complete 
    a832d0a0972a: Pull complete 
    Digest: sha256:dba5fed182e64064b688ccd22b2f9cad4ee88608c82f8cff21e17bab8da72b81
    Status: Downloaded newer image for mysql:5.7
    [root@k8s-node1 ~]# docer images
    -bash: docer: 未找到命令
    [root@k8s-node1 ~]# docker images
    REPOSITORY                           TAG                 IMAGE ID            CREATED             SIZE
    mytomcat                             v8                  f1332ae3f570        12 days ago         463MB
    mytomcat                             v9                  f1332ae3f570        12 days ago         463MB
    tomcat                               8                   f1332ae3f570        12 days ago         463MB
    mysql                                5.7                 98455b9624a9        2 weeks ago         372MB
    k8s.gcr.io/kube-proxy                v1.14.0             5cd54e388aba        2 weeks ago         82.1MB
    k8s.gcr.io/kube-controller-manager   v1.14.0             b95b1efa0436        2 weeks ago         158MB
    k8s.gcr.io/kube-scheduler            v1.14.0             00638a24688b        2 weeks ago         81.6MB
    k8s.gcr.io/kube-apiserver            v1.14.0             ecf910f40d6e        2 weeks ago         210MB
    quay.io/coreos/flannel               v0.11.0-amd64       ff281650a721        2 months ago        52.6MB
    k8s.gcr.io/coredns                   1.3.1               eb516548c180        2 months ago        40.3MB
    k8s.gcr.io/etcd                      3.3.10              2c4adeb21b4f        4 months ago        258MB
    k8s.gcr.io/pause                     3.1                 da86e6ba6ca1        15 months ago       742kB
    [root@k8s-node1 ~]# 


    2.启动,-v <dir>:/var/lib/mysql,将宿主机目录dir挂载到容器中

    [root@k8s-node1 mysql]# docker run -d -p 3306:3306 -v /var/local/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mysql --name docker-mysql mysql:5.7
    87b7410ea379136b3743e194bb312b1bb6fe02abe73e706e04c68856aecc507a
    [root@k8s-node1 mysql]# 

    进入mysql container

    [root@k8s-node1 mysql]# docker ps -a
    CONTAINER ID        IMAGE                  COMMAND                  CREATED              STATUS                     PORTS                               NAMES
    87b7410ea379        mysql:5.7              "docker-entrypoint.s…"   About a minute ago   Up About a minute          0.0.0.0:3306->3306/tcp, 33060/tcp   docker-mysql
    [root@k8s-node1 mysql]# docker exec -it 87b7410ea379 /bin/bash
    root@87b7410ea379:/# mysql -uroot -pmysql
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 2
    Server version: 5.7.25 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
    
    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> exit
    Bye
    root@87b7410ea379:/# exit
    exit
    [root@k8s-node1 mysql]#

     3.远程连接

    二,到k8s master创建server,endpoint代理访问

    1.到k8s-master上验证是否可以连接,前提是已安装mysql客户端(如果没有centos7安装mysql客户端

    [root@k8s-master master]# mysql -h192.168.111.131 -P3306 -uroot -pmysql 
    Welcome to the MariaDB monitor.  Commands end with ; or g.
    Your MySQL connection id is 12
    Server version: 5.7.25 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
    
    MySQL [(none)]> exit
    Bye
    [root@k8s-master master]# 

    2.创建mysql-out.svc.yaml文件

    [root@k8s-master ~]# cat mysql-out-svc.yaml
    apiVersion: v1
    kind: Service
    metadata:
      name: mysql-out-svc
    spec:
      ports:
        - port: 3306
          protocol: TCP
          targetPort: 3306
     
    ---
    apiVersion: v1
    kind: Endpoints
    metadata:
      name: mysql-out-svc
    subsets:
      - addresses:
          - ip: "192.168.111.131"
        ports:
          - port: 3306
    [root@k8s-master ~]# 

    在service中,各项配置意义

    spec:
      type: NodePort      #这里代表是NodePort类型的,另外还有ingress,LoadBalancer
      ports:
      - port: 80          #这里的端口和clusterIP(kubectl describe service service-hello中的IP的port)对应,即在集群中所有机器上curl 10.98.166.242:80可访问发布的应用服务
        targetPort: 8080  #端口一定要和container暴露出来的端口对应,nodejs暴露出来的端口是8081,所以这里也应是8081
        protocol: TCP
        nodePort: 31111   # 所有的节点都会开放此端口,此端口供外部调用。
      selector:
        run: hello         #这里选择器一定要选择容器的标签,之前写name:kube-node是错的。

    3.启动service

    [root@k8s-master ~]# kubectl create -f mysql-out-svc.yaml
    service/mysql-out-svc created
    endpoints/mysql-out-svc created
    [root@k8s-master ~]# kubectl get services -o wide
    NAME            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE   SELECTOR
    kubernetes      ClusterIP   10.96.0.1        <none>        443/TCP        8d    <none>
    mysql-out-svc   ClusterIP   10.111.241.185   <none>        3306/TCP       18s   <none>
    service-hello   NodePort    10.98.166.242    <none>        80:31111/TCP   8d    run=hello
    [root@k8s-master ~]# kubectl get endpoints -o wide
    NAME            ENDPOINTS                           AGE
    kubernetes      192.168.111.130:6443                8d
    mysql-out-svc   192.168.111.131:3306                37s
    service-hello   10.244.1.36:8080,10.244.1.37:8080   8d

    因为使用的是k8s集群外部mysql服务,然后用service代理,让k8s集群内的其它service(如service-hello)可通过 cluster-ip:port(10.111.241.185:3306)来访问外部的mysql服务
    所以此外并没有生成mysql 的pod

    [root@k8s-master ~]# kubectl get pods -o wide
    NAME                     READY   STATUS    RESTARTS   AGE     IP            NODE        NOMINATED NODE   READINESS GATES
    hello-5cd4456b66-gstq6   1/1     Running   1          7d17h   10.244.1.36   k8s-node1   <none>           <none>
    hello-5cd4456b66-sb5px   1/1     Running   1          7d17h   10.244.1.37   k8s-node1   <none>           <none>

    4.在k8s-master上也可以通过 cluster-ip:port(10.111.241.185:3306)来访问外部的mysql服务

    [root@k8s-master ~]# mysql -h10.111.241.185 -P3306 -uroot -pmysql 
    Welcome to the MariaDB monitor.  Commands end with ; or g.
    Your MySQL connection id is 13
    Server version: 5.7.25 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
    
    MySQL [(none)]> 

     到此,k8s上安装msyql完成

  • 相关阅读:
    VBOX虚拟化工具做VPA学习都很方便硬件信息完全实现真实模拟
    Dynamics CRM2016 使用web api来创建注释时的注意事项
    Dynamics CRM build numbers
    域控制器的角色转移
    辅域控制器的安装方法
    利用QrCode.Net生成二维码 asp.net mvc c#
    给现有的word和pdf加水印
    利用LogParser将IIS日志插入到数据库
    短文本情感分析
    Dynamics CRM Ribbon WorkBench 当ValueRule的值为空时的设置
  • 原文地址:https://www.cnblogs.com/pu20065226/p/10688444.html
Copyright © 2020-2023  润新知