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

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

1.下载mysql镜像

[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 imagefor mysql:5.7[root@k8s-node1 ~]# docer images-bash: docer: 未找到命令

[root@k8s-node1 ~]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

mytomcat v8 f1332ae3f57012days ago 463MB

mytomcat v9 f1332ae3f57012days ago 463MB

tomcat8 f1332ae3f570 12days ago 463MB

mysql5.7 98455b9624a9 2weeks 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 2weeks 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 2weeks 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 4months ago 258MB

k8s.gcr.io/pause 3.1 da86e6ba6ca1 15months ago 742kB

[root@k8s-node1 ~]#

2.启动,-v

[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.787b7410ea379136b3743e194bb312b1bb6fe02abe73e706e04c68856aecc507a

[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 connectionid is 2Server version:5.7.25MySQL 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 clearthe 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 connectionid is 12Server version:5.7.25MySQL Community Server (GPL)

Copyright (c)2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type'help;' or '\h' for help. Type '\c' to clearthe 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: 3306protocol: 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 ClusterIP10.96.0.1 443/TCP 8d mysql-out-svc ClusterIP 10.111.241.185 3306/TCP 18s service-hello NodePort 10.98.166.242 80:31111/TCP 8d run=hello

[root@k8s-master ~]# kubectl get endpoints -o wide

NAME ENDPOINTS AGE

kubernetes192.168.111.130:64438d

mysql-out-svc 192.168.111.131:330637s

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 hello-5cd4456b66-sb5px 1/1 Running 1 7d17h 10.244.1.37 k8s-node1

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 connectionid is 13Server version:5.7.25MySQL Community Server (GPL)

Copyright (c)2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MySQL [(none)]>

到此,k8s上安装msyql完成

coreos 安装 mysql_kubernetes下安装mysql相关推荐

  1. opencv python安装 centos_centos下安装opencv

    根据项目需要,安装opencv并提供给开发使用,并且使用opencv提供python3的API接口.虽然不知道是个啥,还是简单了解下. opencv是什么? OpenCV的全称是Open Source ...

  2. linux mysql5.7.11安装_Linux下安装MySQL 5.7.11

    拷贝mysql5.7安装包 bash-4.1# cp /mnt/hgfs/Downloads/mysql-5.7.11-1.el6.x86_64.rpm-bundle.tar /home 解压安装包 ...

  3. linux上离线安装mysql_Linux下安装mysql(离线安装和在线安装)

    一:在线安装mysql 1.首先检测一下,mysql之前有没有被安装 命令:rpm -qa | grep mysql 2.删除mysql的命令: rpm -e --nodeps `rpm -qa | ...

  4. nginx mysql 安装_CentOS7下安装Nginx+MySQL教程

    首先我们需要安装nginx的yum源 警告:/var/tmp/rpm-tmp.aRXWtf: 头V4 RSA/SHA1 Signature, 密钥 ID 7bd9bf62: NOKEY 准备中...  ...

  5. mysql gz 安装_Linux下安装mysql 5.7.17.tar.gz的教程详解

    1.创建MySQL组和mysql用户 groupadd mysql useradd -r -g mysql mysql 2.拷贝: cp -R mysql-5.7.16-linux-glibc2.5- ...

  6. linux mysql安装_Linux下安装mysql服务(超详细)

    Mysql数据库的安装对于开发者来说,是我们必然会面对的问题,它的安装过程其实并不复杂,并且网络上的安装教程也非常多,但是对于新手来说,各种不同形式的安装教程,又给新手们带来了要选择哪种方式进行安装的 ...

  7. windows mysql4.1安装_Windows下,MySql的安装及配置(Install and Config of MySQL in Windows)...

    电脑重新换了系统,需要安装MySQL.以前用MySQL的时候,从来没有详细配置过,这次从网上找了些资料,好好的配置了一下MySQL.发现以前见表的时候不能存储中文字段需要各处改的问题,其实在初始配置的 ...

  8. centos命令行安装mysql_Centos下安装mysql 总结

    一.MySQL安装 二.MySQL的几个重要目录 MySQL安装完成后不象SQL Server默认安装在一个目录,它的数据库文件.配置文件和命令文件分别在不同的目录,了解这些目录非常重要,尤其对于Li ...

  9. 服务器centos安装mysql_centos下安装mysql服务器的方法

    项目需要就在现有的服务器上面重新安装了个mysql服务器,还挺费劲儿呢,因为之前都是在我的笔记本上面试验的,它的系统是Ubuntu的,什么路径啊,启动方式啊.都不一样所以这次还是让我纠结了一把: 下面 ...

最新文章

  1. Tomcat版本不同,功能区别也是很大!
  2. 简单的VC 操作XML 文件的的方法
  3. HDU2553 N皇后问题
  4. Conway#39;s law(康威定律)
  5. 华为堡垒机_运维堡垒机----Gateone
  6. 调侃面向对象编程的23种设计模式
  7. git merge 步骤
  8. 刮刮乐html5效果擦除,HTML5实现刮刮卡的效果
  9. 通过web的方式动态查看tomcat的catalina.out的日志(web.py)
  10. screenfull.js跨浏览器使用JavaScript Fullscreen API
  11. cdrx8如何批量导出jpg_Coreldraw/CDR X8 存低版本打开问题 – 数码打印破图 – Coreldraw/CDR软件崩溃 – 渐变导位图角度变了...
  12. 钢琴音源 Native Instruments Definitive Piano Collection
  13. 【帆软FR】新增自定义字体(以LED字体为例)
  14. XSS Filter Evasion Cheat Sheet 中文版
  15. html 在线测试 鱼缸,新做的草缸教程,看1遍你也会做,鱼缸造景其实很简单
  16. Linux 下的IP/子网计算器:ipcalc
  17. CRI shim:kubelet怎么与容器运行时交互
  18. java 最短遍历路径_凯文培根游戏的最短路径图遍历
  19. 不看好叮咚买菜的N个理
  20. 软件测试bug分析定位技巧

热门文章

  1. 别看程序员只是表面风光
  2. 计算机集数控技术文献,数控技术
  3. oracle宣传视频下载,1300首 Audiomachine 背景音乐电影宣传预告片配乐合辑(23集)...
  4. java.io下载_java io 文件下载
  5. linux中怎么测试i o,[linux]linuxI/O测试的方法之dd
  6. 蜜汁问题?差异基因分析谁比谁有差别吗?
  7. Cell子刊:盗梦空间成真,科学家成功进入他人梦境,并与之“交流对话”
  8. PDF Expert使用教程:如何在Mac上使用PDF Expert编辑PDF
  9. 第十二届蓝桥杯青少年python组 第1-3题 C++实现
  10. P2651 添加括号III(python3实现)