K8S集群系列二:来个DEMO

本文档详细介绍了在K8S集群中部署MySQL服务的步骤,包括选择镜像、创建ReplicationController和Service,以及如何从外部访问MySQL服务。在成功部署后,还展示了从其他计算机连接到MySQL服务的过程。
摘要由CSDN通过智能技术生成

在上一节K8S集群系列一:K8S集群环境搭建中,已经完成K8S集群的搭建,那么接下来弄个DEMO服务

本DEMO选择MySQL服务作为示例,以备不时之需。需要注意的是,部署服务都需要通过master进行,master将会按照部署文件描述yaml进行自动部署在某一个或多个节点中的pod。

1:查找MySQL镜像

[root@master ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   8226                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   2806                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   613                                     [OK]
percona                           Percona Server is a fork of the MySQL relati…   436                 [OK]                
centurylink/mysql                 Image containing mysql. Optimized to be link…   60                                      [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   53                                      
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   45                                      
deitch/mysql-backup               Automated and scheduled mysql database dumps…   36                                      [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   32                                      
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   28                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      27                                      [OK]
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   20                                      
prom/mysqld-exporter                                                              18                                      [OK]
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   13                                      
mysql/mysql-router                MySQL Router provides transparent routing be…   11                                      
circleci/mysql                    MySQL is a widely used, open-source relation…   11                                      
arey/mysql-client                 Run a MySQL client from a docker container      9                                       [OK]
yloeffler/mysql-backup            This image runs mysqldump to backup data usi…   6                                       [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6                                       
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   4                                       [OK]
jelastic/mysql                    An image of the MySQL database server mainta…   1                                       
monasca/mysql-init                A minimal decoupled init container for mysql    0                                       
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   0                                       [OK]
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                0                                       [OK]
astronomerio/mysql-sink           MySQL sink                                      0                                       [OK]

2:选定其中一个镜像,然后拉取。

[root@master ~]# docker pull mysql

不加版本号,那么将会自动获取最新版本。稍事等待,输出如下内容。若出现异常,请重试第2步。

Using default tag: latest
latest: Pulling from library/mysql
743f2d6c1f65: Pull complete 
3f0c413ee255: Pull complete 
aef1ef8f1aac: Pull complete 
f9ee573e34cb: Pull complete 
3f237e01f153: Pull complete 
f9da32e8682a: Pull complete 
4b8da52fb357: Pull complete 
3416ca8f6890: Pull complete 
786698c2d5de: Pull complete 
4ddf84d07bd1: Pull complete 
cd3aa23461b6: Pull complete 
9f287a2a95ad: Pull complete 
Digest: sha256:711df5b93720801b3a727864aba18c2ae46c07f9fe33d5ce9c1f5cbc2c035101
Status: Downloaded newer image for mysql:latest

3:想查看一下当前镜像有哪些?

[root@master ~]# docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
weaveworks/weave-kube             2.5.2               f04a043bb67a        2 weeks ago         148MB
weaveworks/weave-npc              2.5.2               5ce48e0d813c        2 weeks ago         49.6MB
mysql                             latest              990386cbd5c0        3 weeks ago         443MB
dustise/kube-proxy                v1.14.0             5cd54e388aba        2 months ago        82.1MB
dustise/kube-apiserver            v1.14.0             ecf910f40d6e        2 months ago        210MB
dustise/kube-scheduler            v1.14.0             00638a24688b        2 months ago        81.6MB
dustise/kube-controller-manager   v1.14.0             b95b1efa0436        2 months ago        158MB
dustise/coredns                   1.3.1               eb516548c180        4 months ago        40.3MB
dustise/etcd                      3.3.10              2c4adeb21b4f        6 months ago        258MB
dustise/pause                     3.1                 da86e6ba6ca1        17 months ago       742kB

其中,mysql镜像是刚才拉取下来的。

4:创建ReplicationController文件

[root@master ~]# vi mysql-rc.yaml
apiVersion: v1
kind: ReplicationController                            #副本控制器RC
metadata:
  name: mysql                                          #RC的名称,全局唯一
spec:
  replicas: 1                                          #Pod副本的期待数量
  selector:
    app: mysql                                         #符合目标的Pod拥有此标签
  template:                                            #根据此模板创建Pod的副本(实例)
    metadata:
      labels:
        app: mysql                                     #Pod副本拥有的标签,对应RC的Selector
    spec:
      containers:                                      #Pod内容器的定义部分
      - name: mysql                                    #容器的名称
        image: mysql              #容器对应的Docker image
        ports:
        - containerPort: 3306                          #容器应用监听的端口号
        env:                                           #注入容器内的环境变量
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"                              #MYSQl的ROOT登录密码
 

5:使用kubectl命令创建RC

[root@master ~]# kubectl create -f mysql-rc.yaml 
replicationcontroller/mysql created

6:查看当前pod

[root@master ~]# kubectl get pod
NAME          READY   STATUS              RESTARTS   AGE
mysql-p5bbl   0/1     ContainerCreating   0          12s

可以看到容器正在创建中,若容器创建成功,则会显示如下信息:

[root@master ~]# kubectl get pod
NAME          READY   STATUS    RESTARTS   AGE
mysql-p5bbl   1/1     Running   0          3m55s

7:创建与之对应的service描述文件

[root@master ~]# vi mysql-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: mysql   #service拥有的全局唯一名称
spec:
  type: NodePort
  ports:
  - port: 3306  #service提供服务的端口号
    nodePort: 30306    #节点访问的端口号,限制范围(30000~32767)
  selector:
    app: mysql  #service对于pod拥有这里定义的标签

其中:type: NodePort的意思是,需要通过节点服务器的端口进行访问,节点端口是nodePort。

8:使用kubectl创建service

[root@master ~]# kubectl create -f  mysql-svc.yaml
service/mysql created

9:如果要修改配置重新创建service的话,需要先删除他

[root@master ~]# kubectl delete -f  mysql-svc.yaml
service "mysql" deleted

10:查看当前服务service(30306是node节点端口,其他计算机可以通过此端口访问)

[root@master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP          6h26m
mysql        NodePort    10.96.240.35   <none>        3306:30306/TCP   64m

ClusterIP是Kubelet虚拟的IP地址,在集群环境中,可以使用此IP地址进行访问。但是在节点或其他计算机中,无法访问。

11:登录node,登录mysql

11.1 查看node上运行的容器

[root@node1 ~]# docker ps -a
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS               NAMES
0e6c020d4632        mysql                   "docker-entrypoint.s…"   30 minutes ago      Up 30 minutes                           k8s_mysql_mysql-p5bbl_default_b3d65262-86a9-11e9-9e4f-080027b45424_0
b7bc1f460b78        dustise/pause:3.1       "/pause"                 30 minutes ago      Up 30 minutes                           k8s_POD_mysql-p5bbl_default_b3d65262-86a9-11e9-9e4f-080027b45424_0
6e6ca297f853        dustise/coredns         "/coredns -conf /etc…"   3 hours ago         Up 3 hours                              k8s_coredns_coredns-6897bd7b5-qxjdc_kube-system_439579a5-8682-11e9-9e4f-080027b45424_0
1d68e9580eeb        dustise/coredns         "/coredns -conf /etc…"   3 hours ago         Up 3 hours                              k8s_coredns_coredns-6897bd7b5-bszk4_kube-system_43964369-8682-11e9-9e4f-080027b45424_0
a7fcf744ac5d        weaveworks/weave-npc    "/usr/bin/weave-npc"     3 hours ago         Up 3 hours                              k8s_weave-npc_weave-net-wr7pb_kube-system_29a6bdb8-8693-11e9-9e4f-080027b45424_0
c02d9104953f        dustise/pause:3.1       "/pause"                 3 hours ago         Up 3 hours                              k8s_POD_coredns-6897bd7b5-bszk4_kube-system_43964369-8682-11e9-9e4f-080027b45424_0
82561632858f        dustise/pause:3.1       "/pause"                 3 hours ago         Up 3 hours                              k8s_POD_coredns-6897bd7b5-qxjdc_kube-system_439579a5-8682-11e9-9e4f-080027b45424_0
d3aae3df25b8        weaveworks/weave-kube   "/home/weave/launch.…"   3 hours ago         Up 3 hours                              k8s_weave_weave-net-wr7pb_kube-system_29a6bdb8-8693-11e9-9e4f-080027b45424_0
5993b46250d2        dustise/pause:3.1       "/pause"                 3 hours ago         Up 3 hours                              k8s_POD_weave-net-wr7pb_kube-system_29a6bdb8-8693-11e9-9e4f-080027b45424_0
eb5b4fa199bd        dustise/kube-proxy      "/usr/local/bin/kube…"   3 hours ago         Up 3 hours                              k8s_kube-proxy_kube-proxy-9chv4_kube-system_dee0eb7b-8691-11e9-9e4f-080027b45424_0
73dc8af08d64        dustise/pause:3.1       "/pause"                 3 hours ago         Up 3 hours                              k8s_POD_kube-proxy-9chv4_kube-system_dee0eb7b-8691-11e9-9e4f-080027b45424_0

11.2 登录mysql容器

[root@node1 ~]# docker exec -it 0e6c020d4632 /bin/bash

11.3 登录mysql服务(密码为mysql-rc.yaml文件中的root密码,可修改)

root@mysql-p5bbl:/# mysql -h127.0.0.1 -uroot -p123456
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 9
Server version: 8.0.16 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.

11.4 执行mysql命令

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

11.5 设置mysql可以其他服务器访问

mysql> alter user 'root'@'%' identified with mysql_native_password by'123456';
Query OK, 0 rows affected (0.01 sec)

mysql> alter  user 'root'@'%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

12:在其他计算机上,登录mysql服务

demo服务完成。

下一节K8S集群系列三:部署自己写的SpringBoot程序将讲述如何发布自己所写程序到docker中,敬请期待。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值