• Kubernetes(k8s)的deployment资源


    1、为什么K8s要引入deployment资源。

    答:deployment也是保证pod高可用的一种方式,明明已经有RC(Replication Controller),为什么还要引入deployment呢?是因为deployment解决了RC(Replication Controller)的一个痛点。是因为RC在进行版本升级的时候,会造成一个问题,就是它的svc(Service)短时间内访问不了了,deployment就是解决这个问题的。

    2、创建一个deployment的配置文件。

     1 # 声明api的版本。
     2 apiVersion: extensions/v1beta1
     3 # kind代表资源的类型,资源是Deployment。
     4 kind: Deployment
     5 # 资源叫什么名字,是在其属性metadata里面的。
     6 metadata:
     7   # 第一个属性name的值是myweb,即Service的名字就叫做myweb。
     8   name: nginx-deployment
     9 # spec是详细,详细里面定义了一个容器。
    10 spec:
    11   # 副本的数量,比RC少了一个selector。
    12   replicas: 3
    13   # 模板,都是使用模板来启动Pod的。
    14   template:
    15     # 资源叫什么名字,是在其属性metadata里面的。
    16     metadata:
    17       # 给Pod贴上了一个标签,标签是app: nginx,标签是有一定的作用的。
    18       labels:
    19         app: nginx
    20     # spec是详细,详细里面定义了一个容器。
    21     spec:
    22       # 定义一个容器,可以声明多个容器的。
    23       containers:
    24         # 容器的名称叫做nginx
    25         - name: nginx
    26         # 使用了什么镜像,可以使用官方公有的,也可以使用私有的。
    27           image: 192.168.110.133/nginx:1.13
    28         # ports定义容器的端口
    29           ports:
    30             #  容器的端口是80,如果容器有多个端口,可以在后面接着写一行即可。
    31             - containerPort: 80

    具体操作,如下所示:

     1 [root@k8s-master ~]# cd k8s/
     2 [root@k8s-master k8s]# ls
     3 pod  rc  svc
     4 [root@k8s-master k8s]# mkdir deploy
     5 [root@k8s-master k8s]# cd deploy/
     6 [root@k8s-master deploy]# ls
     7 [root@k8s-master deploy]# kubectl get rc 
     8 NAME      DESIRED   CURRENT   READY     AGE
     9 myweb     3         3         3         4d
    10 [root@k8s-master deploy]# kubectl delete rc myweb 
    11 replicationcontroller "myweb" deleted
    12 [root@k8s-master deploy]# kubectl get rc 
    13 No resources found.
    14 [root@k8s-master deploy]# kubectl get svc 
    15 NAME         CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
    16 kubernetes   10.254.0.1       <none>        443/TCP        12d
    17 myweb        10.254.210.104   <nodes>       80:30000/TCP   2d
    18 [root@k8s-master deploy]# kubectl delete svc myweb 
    19 service "myweb" deleted
    20 [root@k8s-master deploy]# kubectl get svc 
    21 NAME         CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
    22 kubernetes   10.254.0.1   <none>        443/TCP   12d
    23 [root@k8s-master deploy]# vim nginx-deploy.yaml
    24 [root@k8s-master deploy]# 

    然后开始创建deployment,如下所示:

    1 [root@k8s-master deploy]# kubectl create -f nginx-deploy.yaml 
    2 deployment "nginx-deployment" created

    然后创建deployment资源完毕之后,可以查看创建的资源,如下所示:

     1 [root@k8s-master deploy]# kubectl get all -o wide
     2 NAME                      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
     3 deploy/nginx-deployment   3         3         3            0           2m
     4 
     5 NAME             CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE       SELECTOR
     6 svc/kubernetes   10.254.0.1   <none>        443/TCP   12d       <none>
     7 
     8 NAME                             DESIRED   CURRENT   READY     AGE       CONTAINER(S)   IMAGE(S)                     SELECTOR
     9 rs/nginx-deployment-3699032092   3         3         0         2m        nginx          192.168.110.133/nginx:1.13   app=nginx,pod-template-hash=3699032092
    10 
    11 NAME                                   READY     STATUS             RESTARTS   AGE       IP            NODE
    12 po/nginx-deployment-3699032092-3jnk6   0/1       ImagePullBackOff   0          2m        172.16.85.2   k8s-master
    13 po/nginx-deployment-3699032092-97bfm   0/1       ImagePullBackOff   0          2m        172.16.38.2   k8s-node3
    14 po/nginx-deployment-3699032092-fmqkx   0/1       ImagePullBackOff   0          2m        172.16.5.2    k8s-node2
    15 [root@k8s-master deploy]# 

    我这里明显没有启动起来,说镜像拉取错误,仔细检查是nginx-deploy.yaml配置文件,在配置image的地方写错了,漏写了端口号导致的。

    再使用命令创建一遍,就发现创建好了。deployment并不像RC直接去启动Pod,它们之间的区别是deployment会启动一个RS,RS拥有RC的百分之九十的功能,比RC的功能还多一些,由RC再去启动三个POD。

     1 [root@k8s-master deploy]# kubectl get deployment 
     2 NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
     3 nginx-deployment   3         3         3            0           5m
     4 [root@k8s-master deploy]# kubectl delete deployment nginx-deployment
     5 deployment "nginx-deployment" deleted
     6 [root@k8s-master deploy]# kubectl get deployment 
     7 No resources found.
     8 [root@k8s-master deploy]# kubectl get all -o wide
     9 NAME             CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE       SELECTOR
    10 svc/kubernetes   10.254.0.1   <none>        443/TCP   12d       <none>
    11 [root@k8s-master deploy]# kubectl create -f nginx-deploy.yaml 
    12 deployment "nginx-deployment" created
    13 [root@k8s-master deploy]# kubectl get all -o wide
    14 NAME                      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    15 deploy/nginx-deployment   3         3         3            3           7s
    16 
    17 NAME             CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE       SELECTOR
    18 svc/kubernetes   10.254.0.1   <none>        443/TCP   12d       <none>
    19 
    20 NAME                             DESIRED   CURRENT   READY     AGE       CONTAINER(S)   IMAGE(S)                          SELECTOR
    21 rs/nginx-deployment-3018768667   3         3         3         7s        nginx          192.168.110.133:5000/nginx:1.13   app=nginx,pod-template-hash=3018768667
    22 
    23 NAME                                   READY     STATUS    RESTARTS   AGE       IP            NODE
    24 po/nginx-deployment-3018768667-1c6lm   1/1       Running   0          7s        172.16.38.2   k8s-node3
    25 po/nginx-deployment-3018768667-cvvjc   1/1       Running   0          7s        172.16.85.2   k8s-master
    26 po/nginx-deployment-3018768667-grvlh   1/1       Running   0          7s        172.16.5.2    k8s-node2
    27 [root@k8s-master deploy]# 

    此时,已经有了一个deployment,那么如何让Pod里面的服务让外界访问呢,我们也是需要创建一个service的,除了使用配置文件进行创建,还可以通过命令进行关联。

     1 [root@k8s-master ~]# kubectl expose deployment nginx-deployment --port=80 --type=NodePort
     2 service "nginx-deployment" exposed
     3 [root@k8s-master ~]# kubectl get all -o wide
     4 NAME                      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
     5 deploy/nginx-deployment   3         3         3            3           1h
     6 
     7 NAME                   CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE       SELECTOR
     8 svc/kubernetes         10.254.0.1      <none>        443/TCP        12d       <none>
     9 svc/nginx-deployment   10.254.95.103   <nodes>       80:36848/TCP   23s       app=nginx
    10 
    11 NAME                             DESIRED   CURRENT   READY     AGE       CONTAINER(S)   IMAGE(S)                          SELECTOR
    12 rs/nginx-deployment-3018768667   3         3         3         1h        nginx          192.168.110.133:5000/nginx:1.13   app=nginx,pod-template-hash=3018768667
    13 
    14 NAME                                   READY     STATUS    RESTARTS   AGE       IP            NODE
    15 po/nginx-deployment-3018768667-1c6lm   1/1       Running   0          1h        172.16.38.2   k8s-node3
    16 po/nginx-deployment-3018768667-cvvjc   1/1       Running   0          1h        172.16.85.2   k8s-master
    17 po/nginx-deployment-3018768667-grvlh   1/1       Running   0          1h        172.16.5.2    k8s-node2
    18 [root@k8s-master ~]# curl -I 192.168.110.133:36848
    19 HTTP/1.1 200 OK
    20 Server: nginx/1.13.12
    21 Date: Wed, 17 Jun 2020 03:05:29 GMT
    22 Content-Type: text/html
    23 Content-Length: 612
    24 Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT
    25 Connection: keep-alive
    26 ETag: "5acb8e45-264"
    27 Accept-Ranges: bytes
    28 
    29 [root@k8s-master ~]# 

    下面可以对nginx进行版本升级,不用创建配置文件,这里可以直接修改配置文件即可。

    1 [root@k8s-master ~]# kubectl edit deployment nginx-deployment

    具体修改内容,如下所示:

    修改,保存之后再使用curl命令进行查看,发现版本已经进行了升级了的。

     1 [root@k8s-master ~]# curl -I 192.168.110.133:36848
     2 HTTP/1.1 200 OK
     3 Server: nginx/1.15.12
     4 Date: Wed, 17 Jun 2020 03:09:26 GMT
     5 Content-Type: text/html
     6 Content-Length: 612
     7 Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT
     8 Connection: keep-alive
     9 ETag: "5cb5d3c3-264"
    10 Accept-Ranges: bytes
    11 
    12 [root@k8s-master ~]# 

    可以通过命令查看升级之后三个Pod节点的情况,这里可以看到多了一个rs/nginx-deployment-3181297949,之前的那个rs/nginx-deployment-3018768667都被设置为了0。

    注意:这里我也出现了升级之后无法将Node2节点的Pod启动成功的情况,我重启了三台机器,三个节点的Pod都有了,这里可能是因为从私有仓库下载的地方没有配置好,重启之前Node2节点使用docker images命令是无法查看到私有仓库的nginx:1.15版本的,重启之后才可以看到。

     1 [root@k8s-master ~]# kubectl get all -o wide
     2 NAME                      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
     3 deploy/nginx-deployment   3         3         3            3           1h
     4 
     5 NAME                   CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE       SELECTOR
     6 svc/kubernetes         10.254.0.1      <none>        443/TCP        12d       <none>
     7 svc/nginx-deployment   10.254.95.103   <nodes>       80:36848/TCP   14m       app=nginx
     8 
     9 NAME                             DESIRED   CURRENT   READY     AGE       CONTAINER(S)   IMAGE(S)                          SELECTOR
    10 rs/nginx-deployment-3018768667   0         0         0         1h        nginx          192.168.110.133:5000/nginx:1.13   app=nginx,pod-template-hash=3018768667
    11 rs/nginx-deployment-3181297949   3         3         3         10m       nginx          192.168.110.133:5000/nginx:1.15   app=nginx,pod-template-hash=3181297949
    12 
    13 NAME                                   READY     STATUS    RESTARTS   AGE       IP            NODE
    14 po/nginx-deployment-3181297949-9d3t0   1/1       Running   1          10m       172.16.59.2   k8s-node2
    15 po/nginx-deployment-3181297949-mjtk3   1/1       Running   1          10m       172.16.16.2   k8s-master
    16 po/nginx-deployment-3181297949-vp84j   1/1       Running   1          10m       172.16.32.2   k8s-node3
    17 [root@k8s-master ~]# 

    deployment的回滚操作,如下所示:

    [root@k8s-master ~]# kubectl rollout undo deployment nginx-deployment

     1 [root@k8s-master ~]# curl -I 192.168.110.133:36848
     2 HTTP/1.1 200 OK
     3 Server: nginx/1.15.12
     4 Date: Wed, 17 Jun 2020 03:23:53 GMT
     5 Content-Type: text/html
     6 Content-Length: 612
     7 Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT
     8 Connection: keep-alive
     9 ETag: "5cb5d3c3-264"
    10 Accept-Ranges: bytes
    11 
    12 [root@k8s-master ~]# kubectl roll
    13 rolling-update  rollout         
    14 [root@k8s-master ~]# kubectl rollout undo deployment nginx-deployment
    15 deployment "nginx-deployment" rolled back
    16 [root@k8s-master ~]# curl -I 192.168.110.133:36848
    17 HTTP/1.1 200 OK
    18 Server: nginx/1.13.12
    19 Date: Wed, 17 Jun 2020 03:25:22 GMT
    20 Content-Type: text/html
    21 Content-Length: 612
    22 Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT
    23 Connection: keep-alive
    24 ETag: "5acb8e45-264"
    25 Accept-Ranges: bytes
    26 
    27 [root@k8s-master ~]# 

    可以查看deployment存在那些历史版本,如下所示:

    1 [root@k8s-master ~]# kubectl rollout history deployment nginx-deployment 
    2 deployments "nginx-deployment"
    3 REVISION    CHANGE-CAUSE
    4 2        <none>
    5 3        <none>
    6 
    7 [root@k8s-master ~]# 

    如果想要查看到历史版本中的改变记录、镜像版本,需要使用新的方式进行发布。

    1 版本发布
    2 kubectl run nginx --image=192.168.110.133:5000/nginx:1.13 --replicas=3
    3 
    4 版本升级
    5 kubectl set image deploy nginx nginx=192.168.110.133:5000/nginx:1.15
    6 
    7 历史版本查询
    8 [root@k8s-master ~]# kubectl rollout history deployment nginx

    具体操作,如下所示:

     1 [root@k8s-master ~]# kubectl run nginx --image=192.168.110.133:5000/nginx:1.13 --replicas=3 --record 
     2 deployment "nginx" created
     3 [root@k8s-master ~]# kubectl rollout history deployment nginx-deployment 
     4 deployments "nginx-deployment"
     5 REVISION    CHANGE-CAUSE
     6 2        <none>
     7 3        <none>
     8 
     9 [root@k8s-master ~]# kubectl rollout history deployment nginx
    10 deployments "nginx"
    11 REVISION    CHANGE-CAUSE
    12 1        kubectl run nginx --image=192.168.110.133:5000/nginx:1.13 --replicas=3 --record
    13 
    14 [root@k8s-master ~]# kubectl set image deploy nginx nginx=192.168.110.133:5000/nginx:1.15
    15 deployment "nginx" image updated
    16 [root@k8s-master ~]# kubectl rollout history deployment nginx
    17 deployments "nginx"
    18 REVISION    CHANGE-CAUSE
    19 1        kubectl run nginx --image=192.168.110.133:5000/nginx:1.13 --replicas=3 --record
    20 2        kubectl set image deploy nginx nginx=192.168.110.133:5000/nginx:1.15
    21 
    22 [root@k8s-master ~]# kubectl get all -o wide
    23 NAME                      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    24 deploy/nginx              3         3         3            3           1m
    25 deploy/nginx-deployment   3         3         3            3           1h
    26 
    27 NAME                   CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE       SELECTOR
    28 svc/kubernetes         10.254.0.1      <none>        443/TCP        12d       <none>
    29 svc/nginx-deployment   10.254.95.103   <nodes>       80:36848/TCP   32m       app=nginx
    30 
    31 NAME                             DESIRED   CURRENT   READY     AGE       CONTAINER(S)   IMAGE(S)                          SELECTOR
    32 rs/nginx-3525615529              0         0         0         1m        nginx          192.168.110.133:5000/nginx:1.13   pod-template-hash=3525615529,run=nginx
    33 rs/nginx-3675430827              3         3         3         23s       nginx          192.168.110.133:5000/nginx:1.15   pod-template-hash=3675430827,run=nginx
    34 rs/nginx-deployment-3018768667   3         3         3         1h        nginx          192.168.110.133:5000/nginx:1.13   app=nginx,pod-template-hash=3018768667
    35 rs/nginx-deployment-3181297949   0         0         0         27m       nginx          192.168.110.133:5000/nginx:1.15   app=nginx,pod-template-hash=3181297949
    36 
    37 NAME                                   READY     STATUS    RESTARTS   AGE       IP            NODE
    38 po/nginx-3675430827-3gdxl              1/1       Running   0          23s       172.16.59.4   k8s-node2
    39 po/nginx-3675430827-8t88l              1/1       Running   0          22s       172.16.16.2   k8s-master
    40 po/nginx-3675430827-tbvf9              1/1       Running   0          23s       172.16.32.4   k8s-node3
    41 po/nginx-deployment-3018768667-56jw4   1/1       Running   0          11m       172.16.59.3   k8s-node2
    42 po/nginx-deployment-3018768667-gl0s6   1/1       Running   0          11m       172.16.16.3   k8s-master
    43 po/nginx-deployment-3018768667-vbhp1   1/1       Running   0          11m       172.16.32.3   k8s-node3
    44 [root@k8s-master ~]# 

    回滚到指定的版本,undo是回滚到上一个版本,操作,如下所示:

     1 [root@k8s-master ~]# kubectl rollout history deployment nginx
     2 deployments "nginx"
     3 REVISION    CHANGE-CAUSE
     4 1        kubectl run nginx --image=192.168.110.133:5000/nginx:1.13 --replicas=3 --record
     5 2        kubectl set image deploy nginx nginx=192.168.110.133:5000/nginx:1.15
     6 
     7 [root@k8s-master ~]# kubectl rollout undo deployment nginx --to-revision=1
     8 deployment "nginx" rolled back
     9 [root@k8s-master ~]# kubectl rollout history deployment nginx
    10 deployments "nginx"
    11 REVISION    CHANGE-CAUSE
    12 2        kubectl set image deploy nginx nginx=192.168.110.133:5000/nginx:1.15
    13 3        kubectl run nginx --image=192.168.110.133:5000/nginx:1.13 --replicas=3 --record
    14 
    15 [root@k8s-master ~]# 

    总结:deployment比RC更好使哦,deployment不依赖于配置文件,会成为主流的哦,重点使用。

    3、k8s中不同类型的pod之间如何相互访问?例如,web服务如何访问db服务呢?

    答:在k8s里面,服务与服务之间需要相互访问的时候,需要借助于VIP,因为每个Pod的ip地址是不固定的。可以固定的是VIP(可以理解为是VIP的地址ClusterIp),在k8s中,Pod与Pod之间相互访问就是通过VIP来进行访问的。比如,web服务想要访问db服务,web服务就要知道db服务的VIP(可以理解为是VIP的地址ClusterIp),不然无法进行访问。

     1 [root@k8s-master ~]# cd k8s/
     2 [root@k8s-master k8s]# ls
     3 deploy  pod  rc  svc
     4 [root@k8s-master k8s]# clear
     5 [root@k8s-master k8s]# wget https://www.qstack.com.cn/tomcat_demo.zip
     6 --2020-06-17 11:49:34--  https://www.qstack.com.cn/tomcat_demo.zip
     7 Resolving www.qstack.com.cn (www.qstack.com.cn)... 123.125.46.149, 111.202.85.37
     8 Connecting to www.qstack.com.cn (www.qstack.com.cn)|123.125.46.149|:443... connected.
     9 HTTP request sent, awaiting response... 200 OK
    10 Length: 1486 (1.5K) [application/zip]
    11 Saving to: ‘tomcat_demo.zip’
    12 
    13 100%[======================================================================================>] 1,486       --.-K/s   in 0.01s   
    14 
    15 2020-06-17 11:49:34 (106 KB/s) - ‘tomcat_demo.zip’ saved [1486/1486]
    16 
    17 [root@k8s-master k8s]# ls
    18 deploy  pod  rc  svc  tomcat_demo.zip
    19 [root@k8s-master k8s]# unzip tomcat_demo.zip 
    20 Archive:  tomcat_demo.zip
    21    creating: tomcat_demo/
    22   inflating: tomcat_demo/mysql-rc.yml  
    23   inflating: tomcat_demo/mysql-svc.yml  
    24   inflating: tomcat_demo/tomcat-rc.yml  
    25   inflating: tomcat_demo/tomcat-svc.yml  
    26 [root@k8s-master k8s]# cd tomcat_demo/
    27 [root@k8s-master tomcat_demo]# ls
    28 mysql-rc.yml  mysql-svc.yml  tomcat-rc.yml  tomcat-svc.yml
    29 [root@k8s-master tomcat_demo]# 

    mysql-rc.yml 这个RC保证mysql的Pod高可用。mysql-svc.yml 这个SVC保证tomcat被访问。tomcat-rc.yml 这个RC保证tomcat是高可用的。tomcat-svc.yml 这个svc保证tomcat被外界所访问。

    如果是自己向服务器传包,那么可以直接使用docker的命令进行操作即可。如果想从docker的仓库里面下载,需要修改一下这个配置,不然下载不动。

    [root@k8s-master ~]# vim /etc/docker/daemon.json

    1 {
    2   "insecure-registries": ["192.168.110.133:5000"],
    3   "registry-mirrors" : ["https://registry.docker-cn.com"]
    4 }

    使用国内的docker的镜像加速,可以不用配置自己的私有仓库的哈。

     1 [root@k8s-master rc]# docker tag docker.io/mysql:5.7.30 192.168.110.133:5000/mysql:5.7.30
     2 [root@k8s-master rc]# docker push 192.168.110.133:5000/mysql:5.7.30
     3 [root@k8s-master ~]# docker images 
     4 REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE
     5 192.168.110.133:5000/mysql                            5.7.30              9cfcce23593a        8 days ago          448 MB
     6 docker.io/mysql                                       5.7.30              9cfcce23593a        8 days ago          448 MB
     7 docker.io/busybox                                     latest              1c35c4412082        2 weeks ago         1.22 MB
     8 docker.io/registry                                    latest              708bc6af7e5e        4 months ago        25.8 MB
     9 docker.io/nginx                                       1.15                53f3fd8007f7        13 months ago       109 MB
    10 192.168.110.133:5000/nginx                            1.15                53f3fd8007f7        13 months ago       109 MB
    11 192.168.110.133:5000/nginx                            1.13                ae513a47849c        2 years ago         109 MB
    12 docker.io/nginx                                       1.13                ae513a47849c        2 years ago         109 MB
    13 registry.access.redhat.com/rhel7/pod-infrastructure   latest              99965fb98423        2 years ago         209 MB
    14 192.168.110.133:5000/pod-infrastructure               latest              34d3450d733b        3 years ago         205 MB
    15 [root@k8s-master ~]# 

    创建mysql的RC(Replication Controller)副本控制器。

    [root@k8s-master tomcat_demo]# vim mysql-rc.yml

     1 apiVersion: v1
     2 kind: ReplicationController
     3 metadata:
     4   name: mysql
     5 spec:
     6   replicas: 1
     7   selector:
     8     app: mysql
     9   template:
    10     metadata:
    11       labels:
    12         app: mysql
    13     spec:
    14       containers:
    15         - name: mysql
    16           image: 192.168.110.133:5000/mysql:5.7.30
    17           ports:
    18           - containerPort: 3306
    19           env:
    20           - name: MYSQL_ROOT_PASSWORD   # 数据库的环境变量,设置数据库的密码为123456
    21             value: '123456'

    操作,如下所示:

    执行创建命令,创建 mysql的RC(Replication Controller)副本控制器。

    [root@k8s-master tomcat_demo]# kubectl create -f mysql-rc.yml

    4、插播一个问题,具体就是我三台centos7机器,docker的版本是1.13.1,然后搭了私有仓库,但是呢,每次下载都不好使,除了第一次搭建好,后来都是讲机器挂起了,直接使用的,前几篇都遇到这个问题了,原因是私有仓库的docker进程都没有了。

     1 [root@k8s-master tomcat_demo]# kubectl describe pod mysql-xcth2 
     2 Name:        mysql-xcth2
     3 Namespace:    default
     4 Node:        k8s-node3/192.168.110.135
     5 Start Time:    Wed, 17 Jun 2020 21:25:04 +0800
     6 Labels:        app=mysql
     7 Status:        Pending
     8 IP:        172.16.32.2
     9 Controllers:    ReplicationController/mysql
    10 Containers:
    11   mysql:
    12     Container ID:    
    13     Image:        192.168.110.133:5000/mysql:5.7.30
    14     Image ID:        
    15     Port:        3306/TCP
    16     State:        Waiting
    17       Reason:        ErrImagePull
    18     Ready:        False
    19     Restart Count:    0
    20     Volume Mounts:    <none>
    21     Environment Variables:
    22       MYSQL_ROOT_PASSWORD:    123456
    23 Conditions:
    24   Type        Status
    25   Initialized     True 
    26   Ready     False 
    27   PodScheduled     True 
    28 No volumes.
    29 QoS Class:    BestEffort
    30 Tolerations:    <none>
    31 Events:
    32   FirstSeen    LastSeen    Count    From            SubObjectPath        Type        Reason            Message
    33   ---------    --------    -----    ----            -------------        --------    ------            -------
    34   34s        34s        1    {kubelet k8s-node3}                Warning        MissingClusterDNS    kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.
    35   33s        33s        1    {default-scheduler }                Normal        Scheduled        Successfully assigned mysql-xcth2 to k8s-node3
    36   33s        19s        2    {kubelet k8s-node3}    spec.containers{mysql}    Normal        Pulling            pulling image "192.168.110.133:5000/mysql:5.7.30"
    37   33s        19s        2    {kubelet k8s-node3}    spec.containers{mysql}    Warning        Failed            Failed to pull image "192.168.110.133:5000/mysql:5.7.30": Error while pulling image: Get http://192.168.110.133:5000/v1/repositories/mysql/images: dial tcp 192.168.110.133:5000: connect: connection refused
    38   33s        19s        2    {kubelet k8s-node3}                Warning        FailedSync        Error syncing pod, skipping: failed to "StartContainer" for "mysql" with ErrImagePull: "Error while pulling image: Get http://192.168.110.133:5000/v1/repositories/mysql/images: dial tcp 192.168.110.133:5000: connect: connection refused"
    39 
    40   32s    6s    2    {kubelet k8s-node3}    spec.containers{mysql}    Normal    BackOff        Back-off pulling image "192.168.110.133:5000/mysql:5.7.30"
    41   32s    6s    2    {kubelet k8s-node3}                Warning    FailedSync    Error syncing pod, skipping: failed to "StartContainer" for "mysql" with ImagePullBackOff: "Back-off pulling image "192.168.110.133:5000/mysql:5.7.30""

    可以使用docker ps查看是否还运行着私有仓库registry。

    注意:报错的原因,如下所示:

    1 /usr/bin/docker-current: Error response from daemon: Conflict. The container name "/registry" is already in use by container a27987d97039c8596ad2a2150cee9e3fbe7580c8131e9f258aea8a922c22a237. You have to remove (or rename) that container to be able to reuse that name..

    大致的意思是:来自守护进程的错误响应:冲突。容器名称“/runoob-nginx-test”为已在容器“a27987d97039c8596ad2a2150cee9e3fbe7580c8131e9f258aea8a922c22a237”中使用。您必须删除(或重命名)该容器才能重用该名称。

     1 [root@k8s-master ~]# docker run -d -p 5000:5000 --restart=always --name registry -v /opt/myregistry:/var/lib/registry registry
     2 /usr/bin/docker-current: Error response from daemon: Conflict. The container name "/registry" is already in use by container a27987d97039c8596ad2a2150cee9e3fbe7580c8131e9f258aea8a922c22a237. You have to remove (or rename) that container to be able to reuse that name..
     3 See '/usr/bin/docker-current run --help'.
     4 [root@k8s-master ~]#  docker ps -l 
     5 CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                  PORTS               NAMES
     6 a27987d97039        registry            "/entrypoint.sh /e..."   11 days ago         Exited (2) 9 days ago                       registry
     7 [root@k8s-master ~]# docker rm a27987d97039
     8 a27987d97039
     9 [root@k8s-master ~]#  docker ps -l 
    10 CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
    11 ee95778bd5d9        busybox             "sh"                12 days ago         Exited (127) 12 days ago                       friendly_payne
    12 [root@k8s-master ~]# docker run -d -p 5000:5000 --restart=always --name registry -v /opt/myregistry:/var/lib/registry registry
    13 5e72b0961647003c757fd41ffe824027c857d1f3938b9a53535645f4f372a40a
    14 [root@k8s-master ~]# docker ps 
    15 CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
    16 5e72b0961647        registry            "/entrypoint.sh /e..."   4 seconds ago       Up 3 seconds        0.0.0.0:5000->5000/tcp   registry
    17 [root@k8s-master ~]# 

    解决的方法:使用docker ps 时查看发现没有运行,使用 docker ps -l , 可以看到有一个nginx容器 ( -l 是显示最新创建的容器包括所有状态) 。删除容器重名的容器再次运行就行。使用docker ps只能查看正在运行的容器,因为nginx这个容器已经给被停止了所以看不到。

    然后将你的镜像上传到私有仓库里面,如下所示:

     1 [root@k8s-master tomcat_demo]# docker tag docker.io/mysql:5.7.30 192.168.110.133:5000/mysql:5.7.30 
     2 [root@k8s-master tomcat_demo]# docker push 192.168.110.133:5000/mysql:5.7.30 
     3 The push refers to a repository [192.168.110.133:5000/mysql]
     4 c90a34afcab0: Pushed 
     5 ac7657905788: Pushed 
     6 8f0182ef7c8c: Pushed 
     7 91ae264962fb: Pushed 
     8 3a2464d8e0c0: Pushed 
     9 44853bb67274: Pushed 
    10 61cbb8ea6481: Pushed 
    11 66c45123fd43: Pushed 
    12 c3f46b20a0d3: Pushed 
    13 365386a39e0e: Pushed 
    14 13cb14c2acd3: Pushed 
    15 5.7.30: digest: sha256:0563b36ec2d1a262f79e1d8562e61f642a0f64f93306d8a709047cdea0444d0a size: 2621
    16 [root@k8s-master tomcat_demo]# 

    然后可以创建mysql的RC,此时就可以顺利创建成功了。

     1 [root@k8s-master tomcat_demo]# kubectl create -f mysql-rc.yml 
     2 replicationcontroller "mysql" created
     3 [root@k8s-master tomcat_demo]# kubectl get rc 
     4 NAME      DESIRED   CURRENT   READY     AGE
     5 mysql     1         1         1         5s
     6 [root@k8s-master tomcat_demo]# kubectl get all -o wide
     7 NAME       DESIRED   CURRENT   READY     AGE       CONTAINER(S)   IMAGE(S)                            SELECTOR
     8 rc/mysql   1         1         1         11s       mysql          192.168.110.133:5000/mysql:5.7.30   app=mysql
     9 
    10 NAME             CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE       SELECTOR
    11 svc/kubernetes   10.254.0.1   <none>        443/TCP   13d       <none>
    12 
    13 NAME             READY     STATUS        RESTARTS   AGE       IP            NODE
    14 po/mysql-537xf   1/1       Running       0          11s       172.16.16.3   k8s-master
    15 po/mysql-gnws2   1/1       Terminating   0          3m        172.16.32.3   k8s-node3
    16 [root@k8s-master tomcat_demo]# 

    由于前几篇的文章,没有发现注册中心已经挂了,所以这里再贴一下我的docker加速和私有仓库的配置。

     1 [root@k8s-master tomcat_demo]# cat /etc/docker/daemon.json
     2 {
     3   "insecure-registries": ["192.168.110.133:5000"],
     4   "registry-mirrors" : ["https://tnxkcso1.mirror.aliyuncs.com"]    
     5 }
     6 [root@k8s-master tomcat_demo]# cat /etc/sysconfig/docker
     7 # /etc/sysconfig/docker
     8 
     9 # Modify these options if you want to change the way the docker daemon runs
    10 OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false'
    11 # 信任私有仓库,镜像加速
    12 # OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false
    13 # --registry-mirror=https://registry.docker-cn.com --insecure-registry=192.168.110.133:5000'
    14 
    15 
    16 
    17 if [ -z "${DOCKER_CERT_PATH}" ]; then
    18     DOCKER_CERT_PATH=/etc/docker
    19 fi
    20 
    21 # Do not add registries in this file anymore. Use /etc/containers/registries.conf
    22 # instead. For more information reference the registries.conf(5) man page.
    23 
    24 # Location used for temporary files, such as those created by
    25 # docker load and build operations. Default is /var/lib/docker/tmp
    26 # Can be overriden by setting the following environment variable.
    27 # DOCKER_TMPDIR=/var/tmp
    28 
    29 # Controls the /etc/cron.daily/docker-logrotate cron job status.
    30 # To disable, uncomment the line below.
    31 # LOGROTATE=false
    32 
    33 # docker-latest daemon can be used by starting the docker-latest unitfile.
    34 # To use docker-latest client, uncomment below lines
    35 #DOCKERBINARY=/usr/bin/docker-latest
    36 #DOCKERDBINARY=/usr/bin/dockerd-latest
    37 #DOCKER_CONTAINERD_BINARY=/usr/bin/docker-containerd-latest
    38 #DOCKER_CONTAINERD_SHIM_BINARY=/usr/bin/docker-containerd-shim-latest
    39 [root@k8s-master tomcat_demo]# 

    mysql的RC创建成功之后,开始创建svc,如下所示:

     1 [root@k8s-master tomcat_demo]# cat mysql-svc.yml 
     2 apiVersion: v1
     3 kind: Service
     4 metadata:
     5   name: mysql
     6 spec:
     7   ports:
     8     - port: 3306
     9       targetPort: 3306
    10   selector:
    11     app: mysql
    12 [root@k8s-master tomcat_demo]# 

    创建命令和查看是否创建成功的,命令如下所示:

     1 [root@k8s-master tomcat_demo]# kubectl create -f mysql-svc.yml 
     2 service "mysql" created
     3 [root@k8s-master tomcat_demo]# kubectl get svc 
     4 NAME         CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
     5 kubernetes   10.254.0.1       <none>        443/TCP    13d
     6 mysql        10.254.245.194   <none>        3306/TCP   7s
     7 [root@k8s-master tomcat_demo]# kubectl get all -o wide
     8 NAME       DESIRED   CURRENT   READY     AGE       CONTAINER(S)   IMAGE(S)                            SELECTOR
     9 rc/mysql   1         1         1         31m       mysql          192.168.110.133:5000/mysql:5.7.30   app=mysql
    10 
    11 NAME             CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE       SELECTOR
    12 svc/kubernetes   10.254.0.1       <none>        443/TCP    13d       <none>
    13 svc/mysql        10.254.245.194   <none>        3306/TCP   28s       app=mysql
    14 
    15 NAME             READY     STATUS    RESTARTS   AGE       IP            NODE
    16 po/mysql-537xf   1/1       Running   0          31m       172.16.16.3   k8s-master
    17 [root@k8s-master tomcat_demo]# 

    可以看到RC、SVC是通过selector进行关联起来的,而且pod也已经运行起来了。

    mysql是已经创建完毕了,那么tomcat是如何和mysql连接起来的呢,这里还是通过环境变量来做的。服务与服务之间是通过VIP来进行连接的,tomcat要连接mysql就需要配置此环境变量MYSQL_SERVICE_HOST

    注意:如果你的项目已经开发好了,并且已经搞到了tomcat里面了,将这个tomcat打成镜像,然后上传到私有仓库,可以创建Pod这些。我的由于没有项目,这里就不进行tomcat的创建了,和mysql的类似。

     1 apiVersion: v1
     2 kind: ReplicationController
     3 metadata:
     4   name: myweb
     5 spec:
     6   replicas: 1
     7   selector:
     8     app: myweb
     9   template:
    10     metadata:
    11       labels:
    12         app: myweb
    13     spec:
    14       containers:
    15         - name: myweb
    16           image: 192.168.110.133:5000/tomcat-app:v2
    17           ports:
    18           - containerPort: 8080
    19           env:
    20           - name: MYSQL_SERVICE_HOST
    21             value: '10.254.245.194'
    22           - name: MYSQL_SERVICE_PORT
    23             value: '3306'

    创建tomcat的RC命令,如下所示:

    首先,需要将镜像下载下来,然后上传到私有仓库才可以的哦。

     1 [root@k8s-master ~]# docker pull docker.io/tomcat
     2 Using default tag: latest
     3 Trying to pull repository docker.io/library/tomcat ... 
     4 latest: Pulling from docker.io/library/tomcat
     5 e9afc4f90ab0: Pull complete 
     6 989e6b19a265: Pull complete 
     7 af14b6c2f878: Pull complete 
     8 5573c4b30949: Pull complete 
     9 fb1a405f128d: Pull complete 
    10 612a9f566fdc: Pull complete 
    11 cf63ebed1142: Pull complete 
    12 fbb20561cd50: Pull complete 
    13 e99c920870d7: Pull complete 
    14 b7f793f2be47: Pull complete 
    15 Digest: sha256:81c2a95e5b1b5867229d75255abe54928d505deb81c8ff8949b61fde1a5d30a1
    16 Status: Downloaded newer image for docker.io/tomcat:latest
    17 [root@k8s-master ~]# docker images 
    18 REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE
    19 docker.io/tomcat                                      latest              2eb5a120304e        7 days ago          647 MB
    20 192.168.110.133:5000/mysql                            5.7.30              9cfcce23593a        8 days ago          448 MB
    21 docker.io/mysql                                       5.7.30              9cfcce23593a        8 days ago          448 MB
    22 docker.io/busybox                                     latest              1c35c4412082        2 weeks ago         1.22 MB
    23 docker.io/registry                                    latest              708bc6af7e5e        4 months ago        25.8 MB
    24 192.168.110.133:5000/nginx                            1.15                53f3fd8007f7        13 months ago       109 MB
    25 docker.io/nginx                                       1.15                53f3fd8007f7        13 months ago       109 MB
    26 192.168.110.133:5000/nginx                            1.13                ae513a47849c        2 years ago         109 MB
    27 docker.io/nginx                                       1.13                ae513a47849c        2 years ago         109 MB
    28 registry.access.redhat.com/rhel7/pod-infrastructure   latest              99965fb98423        2 years ago         209 MB
    29 192.168.110.133:5000/pod-infrastructure               latest              34d3450d733b        3 years ago         205 MB
    30 [root@k8s-master ~]# docker tag docker.io/tomcat:latest 192.168.110.133:5000/tomcat:latest
    31 [root@k8s-master ~]# docker push 192.168.110.133:5000/tomcat:latest 
    32 The push refers to a repository [192.168.110.133:5000/tomcat]
    33 123a7175f991: Pushed 
    34 68b9387df273: Pushed 
    35 a1c4399f9b22: Pushed 
    36 4f866e977815: Pushed 
    37 f73b2345c404: Pushed 
    38 f5181c7ef902: Pushed 
    39 2e5b4ca91984: Pushed 
    40 527ade4639e0: Pushed 
    41 c2c789d2d3c5: Pushed 
    42 8803ef42039d: Pushed 
    43 latest: digest: sha256:5e96c003ec31d89fa4b241463ee05a66ddc2cc58976dd1ecb54af313394cd19e size: 2421
    44 [root@k8s-master ~]# 

    这里就不搞了,具体内容可以看下《Kubernetes权威指南:从Docker到Kubernetes实践全接触(第4版)》 这本书,这本书还是比较不错的,很喜欢。


    2020-06-2310:33:37

    1、Docker制作自己的镜像,第一个Java Web应用。

    答:Dockerfile就是用来告诉Docker我要怎么样的来制作镜像,制作镜像的每一步操作分别是什么。

    dokcer build命令就是用来执行Dockerfile里面所描述的每一件事情,最终会把docker镜像给我们构建出来。

    2、JPress,一个专业的建站神器,是WordPress的Java版本。去下载war包,https://gitee.com/fuhai/jpress/releases

    首先,将war包上传上之后,编写一个Dockerfile文件,如下所示:

     1 [root@slaver1 package]# ls
     2 cerebro-0.7.2.tgz           erlang-solutions-1.0-1.noarch.rpm   jpress-v3.2.5.war                 logstash-6.7.0.tar.gz  rabbitmq-server-3.5.1-1.noarch.rpm
     3 elasticsearch-6.7.0.tar.gz  filebeat-6.7.0-linux-x86_64.tar.gz  kibana-6.7.0-linux-x86_64.tar.gz  materials
     4 [root@slaver1 package]# docker images 
     5 REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
     6 docker.io/nginx             latest              2622e6cca7eb        13 days ago         132 MB
     7 docker.io/jpressio/jpress   latest              e329d5688bad        3 weeks ago         241 MB
     8 docker.io/hello-world       latest              bf756fb1ae65        5 months ago        13.3 kB
     9 docker.io/nginx             1.15                53f3fd8007f7        13 months ago       109 MB
    10 docker.io/nginx             1.13                ae513a47849c        2 years ago         109 MB
    11 [root@slaver1 package]# vim Dockerfile

    怎么创建java web端的镜像呢,首先镜像要运行在tomcat里面的,创建镜像的第一步,说明继承自那个基础镜像,因为程序是Java web程序,肯定是需要用到tomcat的,先将tomcat的镜像下载下来。

     1 [root@slaver1 ~]# docker images 
     2 REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
     3 docker.io/nginx             latest              2622e6cca7eb        13 days ago         132 MB
     4 docker.io/jpressio/jpress   latest              e329d5688bad        3 weeks ago         241 MB
     5 docker.io/hello-world       latest              bf756fb1ae65        5 months ago        13.3 kB
     6 docker.io/nginx             1.15                53f3fd8007f7        13 months ago       109 MB
     7 docker.io/nginx             1.13                ae513a47849c        2 years ago         109 MB
     8 [root@slaver1 ~]# docker search tomcat
     9 INDEX       NAME                                    DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
    10 docker.io   docker.io/tomcat                        Apache Tomcat is an open source implementa...   2758      [OK]       
    11 docker.io   docker.io/tomee                         Apache TomEE is an all-Apache Java EE cert...   79        [OK]       
    12 docker.io   docker.io/dordoka/tomcat                Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 ba...   54                   [OK]
    13 docker.io   docker.io/bitnami/tomcat                Bitnami Tomcat Docker Image                     35                   [OK]
    14 docker.io   docker.io/kubeguide/tomcat-app          Tomcat image for Chapter 1                      28                   
    15 docker.io   docker.io/consol/tomcat-7.0             Tomcat 7.0.57, 8080, "admin/admin"              17                   [OK]
    16 docker.io   docker.io/cloudesire/tomcat             Tomcat server, 6/7/8                            15                   [OK]
    17 docker.io   docker.io/aallam/tomcat-mysql           Debian, Oracle JDK, Tomcat & MySQL              13                   [OK]
    18 docker.io   docker.io/arm32v7/tomcat                Apache Tomcat is an open source implementa...   10                   
    19 docker.io   docker.io/rightctrl/tomcat              CentOS , Oracle Java, tomcat application s...   6                    [OK]
    20 docker.io   docker.io/maluuba/tomcat7-java8         Tomcat7 with java8.                             5                    
    21 docker.io   docker.io/unidata/tomcat-docker         Security-hardened Tomcat Docker container.      4                    [OK]
    22 docker.io   docker.io/amd64/tomcat                  Apache Tomcat is an open source implementa...   2                    
    23 docker.io   docker.io/arm64v8/tomcat                Apache Tomcat is an open source implementa...   2                    
    24 docker.io   docker.io/jelastic/tomcat               An image of the Tomcat Java application se...   2                    
    25 docker.io   docker.io/99taxis/tomcat7               Tomcat7                                         1                    [OK]
    26 docker.io   docker.io/camptocamp/tomcat-logback     Docker image for tomcat with logback integ...   1                    [OK]
    27 docker.io   docker.io/i386/tomcat                   Apache Tomcat is an open source implementa...   1                    
    28 docker.io   docker.io/oobsri/tomcat8                Testing CI Jobs with different names.           1                    
    29 docker.io   docker.io/ppc64le/tomcat                Apache Tomcat is an open source implementa...   1                    
    30 docker.io   docker.io/appsvc/tomcat                                                                 0                    
    31 docker.io   docker.io/cfje/tomcat-resource          Tomcat Concourse Resource                       0                    
    32 docker.io   docker.io/picoded/tomcat7               tomcat7 with jre8 and MANAGER_USER / MANAG...   0                    [OK]
    33 docker.io   docker.io/s390x/tomcat                  Apache Tomcat is an open source implementa...   0                    
    34 docker.io   docker.io/secoresearch/tomcat-varnish   Tomcat and Varnish 5.0                          0                    [OK]
    35 [root@slaver1 ~]# docker pull docker.io/tomcat
    36 Using default tag: latest
    37 Trying to pull repository docker.io/library/tomcat ... 
    38 latest: Pulling from docker.io/library/tomcat
    39 e9afc4f90ab0: Pull complete 
    40 989e6b19a265: Pull complete 
    41 af14b6c2f878: Pull complete 
    42 5573c4b30949: Pull complete 
    43 fb1a405f128d: Downloading [================>                                  ] 1.752 MB/5.285 MB
    44 612a9f566fdc: Download complete 
    45 cf63ebed1142: Downloading [=======================>                           ] 91.59 MB/196.2 MB
    46 fbb20561cd50: Download complete 
    47 e99c920870d7: Download complete 
    48 b7f793f2be47: Download complete 

    tomcat的运行需要用到java环境的,可以肯定的是tomcat的镜像是包括jdk的,所以不用担心是否有jdk。

    1 # 第一步,说明继承那个基础镜像,tomcat作为基础镜像。此处告诉Docker我要做自己的镜像,以tomcat镜像为起点。
    2 from docker.io/tomcat
    3 
    4 # 第二步,描述镜像的所有者,可以省略。
    5 MAINTAINER biehl biehl@qq.com
    6 
    7 # 第三步,将自己的web应用放到镜像里面,将本地的war包拷贝到tomcat的webapps目录下面。可以看看程序是否可以随着tomcat启动起来。
    8 COPY jpress-v3.2.5.war /usr/local/tomcat/webapps

    dokcer build命令就是用来执行Dockerfile里面所描述的每一件事情,最终会把docker镜像给我们构建出来。

     1 [root@slaver1 package]# docker build .
     2 Sending build context to Docker daemon 657.4 MB
     3 Step 1/3 : FROM docker.io/tomcat
     4  ---> 2eb5a120304e
     5 Step 2/3 : MAINTAINER biehl biehl@qq.com
     6  ---> Using cache
     7  ---> 8b53c62d5360
     8 Step 3/3 : COPY jpress-v3.2.5.war /usr/local/tomcat/webapps
     9  ---> d8e02860520e
    10 Removing intermediate container 0b42ee524e6f
    11 Successfully built d8e02860520e
    12 [root@slaver1 package]# 

    可以使用docker images命令进行查看,但是名称和版本是none的。

     1 [root@slaver1 package]# docker images 
     2 REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
     3 <none>                      <none>              d8e02860520e        18 seconds ago      718 MB
     4 docker.io/tomcat            latest              2eb5a120304e        12 days ago         647 MB
     5 docker.io/nginx             latest              2622e6cca7eb        13 days ago         132 MB
     6 docker.io/jpressio/jpress   latest              e329d5688bad        3 weeks ago         241 MB
     7 docker.io/hello-world       latest              bf756fb1ae65        5 months ago        13.3 kB
     8 docker.io/nginx             1.15                53f3fd8007f7        13 months ago       109 MB
     9 docker.io/nginx             1.13                ae513a47849c        2 years ago         109 MB
    10 [root@slaver1 package]# 

    使用下面的命令可以创建有名称的镜像,如下所示:

     1 [root@slaver1 package]# ls
     2 cerebro-0.7.2.tgz  elasticsearch-6.7.0.tar.gz         filebeat-6.7.0-linux-x86_64.tar.gz  kibana-6.7.0-linux-x86_64.tar.gz  materials
     3 Dockerfile         erlang-solutions-1.0-1.noarch.rpm  jpress-v3.2.5.war                   logstash-6.7.0.tar.gz             rabbitmq-server-3.5.1-1.noarch.rpm
     4 [root@slaver1 package]# docker build -t jpress:v3.2.5 .
     5 Sending build context to Docker daemon 657.4 MB
     6 Step 1/3 : FROM docker.io/tomcat
     7  ---> 2eb5a120304e
     8 Step 2/3 : MAINTAINER biehl biehl@qq.com
     9  ---> Using cache
    10  ---> 8b53c62d5360
    11 Step 3/3 : COPY jpress-v3.2.5.war /usr/local/tomcat/webapps
    12  ---> Using cache
    13  ---> d8e02860520e
    14 Successfully built d8e02860520e
    15 [root@slaver1 package]# docker images 
    16 REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
    17 jpress                      v3.2.5              d8e02860520e        3 days ago          718 MB
    18 docker.io/tomcat            latest              2eb5a120304e        2 weeks ago         647 MB
    19 docker.io/nginx             latest              2622e6cca7eb        2 weeks ago         132 MB
    20 docker.io/jpressio/jpress   latest              e329d5688bad        3 weeks ago         241 MB
    21 docker.io/hello-world       latest              bf756fb1ae65        5 months ago        13.3 kB
    22 docker.io/nginx             1.15                53f3fd8007f7        13 months ago       109 MB
    23 docker.io/nginx             1.13                ae513a47849c        2 years ago         109 MB
    24 [root@slaver1 package]# 

    运行自己的容器,如下所示:

     1 [root@slaver1 package]# docker images 
     2 REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
     3 jpress                      v3.2.5              d8e02860520e        3 days ago          718 MB
     4 docker.io/tomcat            latest              2eb5a120304e        2 weeks ago         647 MB
     5 docker.io/nginx             latest              2622e6cca7eb        2 weeks ago         132 MB
     6 docker.io/jpressio/jpress   latest              e329d5688bad        3 weeks ago         241 MB
     7 docker.io/hello-world       latest              bf756fb1ae65        5 months ago        13.3 kB
     8 docker.io/nginx             1.15                53f3fd8007f7        13 months ago       109 MB
     9 docker.io/nginx             1.13                ae513a47849c        2 years ago         109 MB
    10 [root@slaver1 package]# docker run -d -p 8888:8080 jpress
    11 Unable to find image 'jpress:latest' locally
    12 Trying to pull repository docker.io/library/jpress ... 
    13 /usr/bin/docker-current: repository docker.io/jpress not found: does not exist or no pull access.
    14 See '/usr/bin/docker-current run --help'.
    15 [root@slaver1 package]# docker run -d -p 8888:8080 jpress:v3.2.5
    16 91eb59a60d06944a45326c4a00a62c0454a3ae22b9ec894081f6238d857e1758
    17 [root@slaver1 package]# 

    查看容器是否正常运行了,如下所示:

    1 [root@slaver1 package]# docker ps
    2 CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS                    NAMES
    3 91eb59a60d06        jpress:v3.2.5       "catalina.sh run"   About a minute ago   Up About a minute   0.0.0.0:8888->8080/tcp   musing_carson
    4 [root@slaver1 package]# 

    检查端口是否存在了,如下所示:

    1 [root@slaver1 package]# netstat -na | grep 8888
    2 tcp6       0      0 :::8888                 :::*                    LISTEN     
    3 [root@slaver1 package]# 

    访问地址,http://192.168.110.133:8888/,我此时已经关闭了防火墙,因为是本地测试的,随便了一点,找不到主页,其实此时我的tomcat已经启动了,此时我的tomcat的版本是Apache Tomcat/9.0.36版本的,此时你也可以换成低版本,从上面的步骤再走一遍,我这里修改了一些配置。

    将webapps.dist里面的内容全部拷贝到webapps目录下面,即可访问,不用重启容器。

     1 [root@slaver1 ~]# docker ps 
     2 CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
     3 91eb59a60d06        jpress:v3.2.5       "catalina.sh run"   20 minutes ago      Up 20 minutes       0.0.0.0:8888->8080/tcp   musing_carson
     4 [root@slaver1 ~]# docker exec -it 91eb59a60d06 /bin/bash
     5 root@91eb59a60d06:/usr/local/tomcat# ls
     6 BUILDING.txt  CONTRIBUTING.md  LICENSE    NOTICE    README.md  RELEASE-NOTES  RUNNING.txt  bin  conf  lib  logs  native-jni-lib  temp  webapps  webapps.dist  work
     7 root@91eb59a60d06:/usr/local/tomcat# cd webapps.dist/
     8 root@91eb59a60d06:/usr/local/tomcat/webapps.dist# ls
     9 ROOT  docs  examples  host-manager  manager
    10 root@91eb59a60d06:/usr/local/tomcat/webapps.dist# cp -r * ../webapps
    11 root@91eb59a60d06:/usr/local/tomcat/webapps.dist# ls
    12 ROOT  docs  examples  host-manager  manager
    13 root@91eb59a60d06:/usr/local/tomcat/webapps.dist# cd ../webapps
    14 root@91eb59a60d06:/usr/local/tomcat/webapps# ls
    15 ROOT  docs  examples  host-manager  jpress-v3.2.5  jpress-v3.2.5.war  manager
    16 root@91eb59a60d06:/usr/local/tomcat/webapps# 

    jpress界面404可能是打包出了问题,官网现在不提供war包下载,可以从这里获得https://gitee.com/GalaIO/jpress/blob/master/wars/jpress-web-newest.war

    访问地址http://192.168.110.133:8888/jpress-v3.2.5/,抱404,我这里选择使用上面的链接下载包,重新上传,安装,部署操作。

    首先停止这个容器,如下所示:

    1 [root@slaver1 package]# docker stop 91eb59a60d06
    2 91eb59a60d06
    3 [root@slaver1 package]# docker ps
    4 CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    5 [root@slaver1 package]# netstat -na | grep 8888
    6 [root@slaver1 package]# 

    修改Dockerfile配置文件,然后执行它,如下所示:

     1 [root@slaver1 package]# docker images 
     2 REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
     3 <none>                  <none>              dabfdefb7d8c        57 minutes ago      680 MB
     4 jpress                  v3.2.5              d8e02860520e        4 days ago          718 MB
     5 docker.io/tomcat        latest              2eb5a120304e        2 weeks ago         647 MB
     6 docker.io/nginx         latest              2622e6cca7eb        2 weeks ago         132 MB
     7 docker.io/hello-world   latest              bf756fb1ae65        5 months ago        13.3 kB
     8 docker.io/nginx         1.15                53f3fd8007f7        13 months ago       109 MB
     9 docker.io/nginx         1.13                ae513a47849c        2 years ago         109 MB
    10 [root@slaver1 package]# docker build -t jpress .
    11 Sending build context to Docker daemon 705.3 MB
    12 Step 1/3 : FROM docker.io/tomcat
    13  ---> 2eb5a120304e
    14 Step 2/3 : MAINTAINER biehl biehl@qq.com
    15  ---> Using cache
    16  ---> 8b53c62d5360
    17 Step 3/3 : COPY jpress-web-newest.war /usr/local/tomcat/webapps
    18  ---> 023489e699f5
    19 Removing intermediate container 4dfb0add6ea6
    20 Successfully built 023489e699f5
    21 [root@slaver1 package]# docker images 
    22 REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
    23 jpress                  latest              023489e699f5        15 seconds ago      668 MB
    24 <none>                  <none>              dabfdefb7d8c        58 minutes ago      680 MB
    25 jpress                  v3.2.5              d8e02860520e        4 days ago          718 MB
    26 docker.io/tomcat        latest              2eb5a120304e        2 weeks ago         647 MB
    27 docker.io/nginx         latest              2622e6cca7eb        2 weeks ago         132 MB
    28 docker.io/hello-world   latest              bf756fb1ae65        5 months ago        13.3 kB
    29 docker.io/nginx         1.15                53f3fd8007f7        13 months ago       109 MB
    30 docker.io/nginx         1.13                ae513a47849c        2 years ago         109 MB
    31 [root@slaver1 package]# docker run -d -p 8888:8080 jpress
    32 ec6ceeaf4d2129658ff67b330eef20691bfee1a9df669b15bb12116a6935fe2d
    33 [root@slaver1 package]# docker ps
    34 CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
    35 ec6ceeaf4d21        jpress              "catalina.sh run"   8 seconds ago       Up 3 seconds        0.0.0.0:8888->8080/tcp   wizardly_aryabhata
    36 [root@slaver1 package]# netstat -na | grep 8888
    37 tcp6       0      0 :::8888                 :::*                    LISTEN     
    38 [root@slaver1 package]# docker exec -it ec6ceeaf4d21 /bin/bash
    39 root@ec6ceeaf4d21:/usr/local/tomcat# ls
    40 BUILDING.txt  CONTRIBUTING.md  LICENSE    NOTICE    README.md  RELEASE-NOTES  RUNNING.txt  bin  conf  lib  logs  native-jni-lib  temp  webapps  webapps.dist  work
    41 root@ec6ceeaf4d21:/usr/local/tomcat# cd webapps.dist/
    42 root@ec6ceeaf4d21:/usr/local/tomcat/webapps.dist# ls
    43 ROOT  docs  examples  host-manager  manager
    44 root@ec6ceeaf4d21:/usr/local/tomcat/webapps.dist# cp -r * ../webapps
    45 root@ec6ceeaf4d21:/usr/local/tomcat/webapps.dist# cd ..
    46 root@ec6ceeaf4d21:/usr/local/tomcat# ls
    47 BUILDING.txt  CONTRIBUTING.md  LICENSE    NOTICE    README.md  RELEASE-NOTES  RUNNING.txt  bin  conf  lib  logs  native-jni-lib  temp  webapps  webapps.dist  work
    48 root@ec6ceeaf4d21:/usr/local/tomcat# cd webapps
    49 root@ec6ceeaf4d21:/usr/local/tomcat/webapps# ls
    50 ROOT  docs  examples  host-manager  jpress-web-newest  jpress-web-newest.war  manager
    51 root@ec6ceeaf4d21:/usr/local/tomcat/webapps# 

    运行效果http://192.168.110.133:8888/jpress-web-newest/install,如下所示:

    现在,使用docker安装一下mysql,这样就可以连一下jpress了。

     1 [root@slaver1 package]# docker pull mysql:5.7.26
     2 Trying to pull repository docker.io/library/mysql ... 
     3 5.7.26: Pulling from docker.io/library/mysql
     4 0a4690c5d889: Pull complete 
     5 98aa2fc6cbeb: Pull complete 
     6 0777e6eb0e6f: Pull complete 
     7 2464189c041c: Pull complete 
     8 b45df9dc827d: Pull complete 
     9 b42b00086160: Pull complete 
    10 bb93567627c7: Pull complete 
    11 48acc32b4863: Pull complete 
    12 6257d2da4815: Pull complete 
    13 1cd5ed3b2653: Pull complete 
    14 f4ba7ff24ae9: Pull complete 
    15 Digest: sha256:bdee7a98276ccf377d2c00b8ceaa9f65455a9376481467bbcc3d1e6b662dac5d
    16 Status: Downloaded newer image for docker.io/mysql:5.7.26
    17 [root@slaver1 package]# docker images 
    18 REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
    19 jpress                              latest              023489e699f5        About an hour ago   668 MB
    20 <none>                              <none>              dabfdefb7d8c        2 hours ago         680 MB
    21 jpress                              v3.2.5              d8e02860520e        4 days ago          718 MB
    22 docker.io/tomcat                    latest              2eb5a120304e        2 weeks ago         647 MB
    23 docker.io/nginx                     latest              2622e6cca7eb        2 weeks ago         132 MB
    24 docker.io/hello-world               latest              bf756fb1ae65        5 months ago        13.3 kB
    25 docker.io/centos/mysql-57-centos7   latest              f83a2938370c        8 months ago        452 MB
    26 docker.io/mysql                     5.7.26              e9c354083de7        11 months ago       373 MB
    27 docker.io/nginx                     1.15                53f3fd8007f7        13 months ago       109 MB
    28 docker.io/nginx                     1.13                ae513a47849c        2 years ago         109 MB
    29 [root@slaver1 package]# docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=jpress docker.io/mysql:5.7.26 
    30 df4e1235ec72e06d515d1dfc79ee373baafdd81284c8bf2af577d0beea53fc12
    31 [root@slaver1 package]# docker ps
    32 CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                               NAMES
    33 df4e1235ec72        docker.io/mysql:5.7.26   "docker-entrypoint..."   17 seconds ago      Up 16 seconds       0.0.0.0:3306->3306/tcp, 33060/tcp   agitated_hopper
    34 ec6ceeaf4d21        jpress                   "catalina.sh run"        About an hour ago   Up About an hour    0.0.0.0:8888->8080/tcp              wizardly_aryabhata
    35 [root@slaver1 package]# netstat -na | grep 3306
    36 tcp6       0      0 :::3306                 :::*                    LISTEN     
    37 [root@slaver1 package]# 

    开始安装jpress,如下所示:

    现在重启一下web容器,如下所示:

     1 [root@slaver1 package]# docker ps
     2 CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                               NAMES
     3 df4e1235ec72        docker.io/mysql:5.7.26   "docker-entrypoint..."   4 minutes ago       Up 4 minutes        0.0.0.0:3306->3306/tcp, 33060/tcp   agitated_hopper
     4 ec6ceeaf4d21        jpress                   "catalina.sh run"        2 hours ago         Up 2 hours          0.0.0.0:8888->8080/tcp              wizardly_aryabhata
     5 [root@slaver1 package]# docker restart ec6ceeaf4d21
     6 ec6ceeaf4d21
     7 [root@slaver1 package]# docker ps
     8 CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                               NAMES
     9 df4e1235ec72        docker.io/mysql:5.7.26   "docker-entrypoint..."   4 minutes ago       Up 4 minutes        0.0.0.0:3306->3306/tcp, 33060/tcp   agitated_hopper
    10 ec6ceeaf4d21        jpress                   "catalina.sh run"        2 hours ago         Up 6 seconds        0.0.0.0:8888->8080/tcp              wizardly_aryabhata

    http://192.168.110.133:8888/jpress-web-newest/效果,如下所示:


    2020-06-27  14:56:55

    1、搞一下自己做的小项目,它不香嘛?它没有成就感嘛,说干就干!!!将自己的项目打个war包,然后上传到服务器上哦,记得自己的项目链接数据库的信息是自己服务器mysql的ip地址、账号密码,如下所示:

    1 drivername=com.mysql.jdbc.Driver
    2 url=jdbc:mysql://192.168.110.133:3306/book?useUnicode=true&amp;characterEncoding=UTF-8
    3 user=root
    4 password=123456

    编写一个Dockfile文件,如下所示:

    1 [root@slaver1 book]# ls
    2 book-master.war  Dockerfile
    3 [root@slaver1 book]# vim Dockerfile

    配置内容,如下所示:

    1 # 第一步,说明继承那个基础镜像,tomcat作为基础镜像。此处告诉Docker我要做自己的镜像,以tomcat镜像为起点。
    2 from docker.io/tomcat
    3 
    4 # 第二步,描述镜像的所有者,可以省略。
    5 MAINTAINER biehl biehl@qq.com
    6 
    7 # 第三步,将自己的web应用放到镜像里面,将本地的war包拷贝到tomcat的webapps目录下面。可以看看程序是否可以随着tomcat启动起来。
    8 COPY book-master.war /usr/local/tomcat/webapps

    编写完毕Dockerfile配置文件,然后执行它,如下所示:

     1 [root@slaver1 book]# docker build -t book .
     2 Sending build context to Docker daemon 21.39 MB
     3 Step 1/3 : FROM docker.io/tomcat
     4  ---> 2eb5a120304e
     5 Step 2/3 : MAINTAINER biehl biehl@qq.com
     6  ---> Using cache
     7  ---> 8b53c62d5360
     8 Step 3/3 : COPY book-master.war /usr/local/tomcat/webapps
     9  ---> 8c2657940ce3
    10 Removing intermediate container f9fffe3ee453
    11 Successfully built 8c2657940ce3
    12 [root@slaver1 book]# docker images 
    13 REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
    14 book                                latest              8c2657940ce3        8 seconds ago       669 MB
    15 jpress                              latest              023489e699f5        2 hours ago         668 MB
    16 <none>                              <none>              dabfdefb7d8c        3 hours ago         680 MB
    17 jpress                              v3.2.5              d8e02860520e        4 days ago          718 MB
    18 docker.io/tomcat                    latest              2eb5a120304e        2 weeks ago         647 MB
    19 docker.io/nginx                     latest              2622e6cca7eb        2 weeks ago         132 MB
    20 docker.io/hello-world               latest              bf756fb1ae65        5 months ago        13.3 kB
    21 docker.io/centos/mysql-57-centos7   latest              f83a2938370c        8 months ago        452 MB
    22 docker.io/mysql                     5.7.26              e9c354083de7        11 months ago       373 MB
    23 docker.io/nginx                     1.15                53f3fd8007f7        13 months ago       109 MB
    24 docker.io/nginx                     1.13                ae513a47849c        2 years ago         109 MB
    25 [root@slaver1 book]# docker run -d -p 8888:8080 book
    26 4d52cd016170c07dfec13236624c165adca7f103579c130192b33a74a3d87025
    27 [root@slaver1 book]# docker ps
    28 CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                               NAMES
    29 4d52cd016170        book                     "catalina.sh run"        6 seconds ago       Up 4 seconds        0.0.0.0:8888->8080/tcp              gallant_bohr
    30 df4e1235ec72        docker.io/mysql:5.7.26   "docker-entrypoint..."   22 minutes ago      Up 22 minutes       0.0.0.0:3306->3306/tcp, 33060/tcp   agitated_hopper
    31 [root@slaver1 book]# netstat -na | grep 8888
    32 tcp6       0      0 :::8888                 :::*                    LISTEN     
    33 [root@slaver1 book]# docker exec -it 4d52cd016170 /bin/bash
    34 root@4d52cd016170:/usr/local/tomcat# ls
    35 BUILDING.txt  CONTRIBUTING.md  LICENSE    NOTICE    README.md  RELEASE-NOTES  RUNNING.txt  bin  conf  lib  logs  native-jni-lib  temp  webapps  webapps.dist  work
    36 root@4d52cd016170:/usr/local/tomcat# cat webapps
    37 cat: webapps: Is a directory
    38 root@4d52cd016170:/usr/local/tomcat# ls webapps
    39 book-master  book-master.war
    40 root@4d52cd016170:/usr/local/tomcat# cd webapps.dist/
    41 root@4d52cd016170:/usr/local/tomcat/webapps.dist# ls
    42 ROOT  docs  examples  host-manager  manager
    43 root@4d52cd016170:/usr/local/tomcat/webapps.dist# cp -r * ../webapps
    44 root@4d52cd016170:/usr/local/tomcat/webapps.dist# cd ..
    45 root@4d52cd016170:/usr/local/tomcat# cd webapps
    46 root@4d52cd016170:/usr/local/tomcat/webapps# ls
    47 ROOT  book-master  book-master.war  docs  examples  host-manager  manager
    48 root@4d52cd016170:/usr/local/tomcat/webapps# 

     如果访问tomcat报404还是将文件信息从webapps.dist拷贝到webapps里面,就行了。然后访问http://192.168.110.133:8888/可以正常访问。

    然后开始访问你的项目吧,http://192.168.110.133:8888/book-master/,需要注意的是,使用navicat连上你的服务器数据库,执行sql文件,这样不香嘛?

    Linux服务器docker 部署java web项目乱码问题。我是可以正常登陆的,但是插入和修改是乱码的,这里解决一下这个问题,首先我的项目,数据库都是utf8格式的。发现使用docker创建的Mysql的默认参数有的不是utf8格式的,这里可以进行修改一下,如下所示:

    根据自己的需求,设计参数为utf8即可。

    这种方式并没有用,也没有解决乱码问题。进入容器,修改mysql的字符集编码,如下所示:

     1 [root@slaver1 book]# docker ps
     2 CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                               NAMES
     3 4d52cd016170        book                     "catalina.sh run"        25 minutes ago      Up 25 minutes       0.0.0.0:8888->8080/tcp              gallant_bohr
     4 df4e1235ec72        docker.io/mysql:5.7.26   "docker-entrypoint..."   47 minutes ago      Up 7 minutes        0.0.0.0:3306->3306/tcp, 33060/tcp   agitated_hopper
     5 [root@slaver1 book]# docker exec -it df4e1235ec72 /bin/bash     
     6 root@df4e1235ec72:/# mysql -u root -p
     7 Enter password: 
     8 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
     9 root@df4e1235ec72:/# mysql -u root -p
    10 Enter password: 
    11 Welcome to the MySQL monitor.  Commands end with ; or g.
    12 Your MySQL connection id is 44
    13 Server version: 5.7.26 MySQL Community Server (GPL)
    14 
    15 Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
    16 
    17 Oracle is a registered trademark of Oracle Corporation and/or its
    18 affiliates. Other names may be trademarks of their respective
    19 owners.
    20 
    21 Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
    22 
    23 mysql> 

    现在使用root账号登陆,密码是自己指定的。然后查看mysql的字符集,如下所示:

     1 root@df4e1235ec72:/# mysql -u root -p
     2 Enter password: 
     3 Welcome to the MySQL monitor.  Commands end with ; or g.
     4 Your MySQL connection id is 44
     5 Server version: 5.7.26 MySQL Community Server (GPL)
     6 
     7 Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
     8 
     9 Oracle is a registered trademark of Oracle Corporation and/or its
    10 affiliates. Other names may be trademarks of their respective
    11 owners.
    12 
    13 Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
    14 
    15 mysql> SHOW VARIABLES LIKE 'character_set_%';
    16 +--------------------------+----------------------------+
    17 | Variable_name            | Value                      |
    18 +--------------------------+----------------------------+
    19 | character_set_client     | latin1                     |
    20 | character_set_connection | latin1                     |
    21 | character_set_database   | latin1                     |
    22 | character_set_filesystem | binary                     |
    23 | character_set_results    | latin1                     |
    24 | character_set_server     | latin1                     |
    25 | character_set_system     | utf8                       |
    26 | character_sets_dir       | /usr/share/mysql/charsets/ |
    27 +--------------------------+----------------------------+
    28 8 rows in set (0.00 sec)
    29 
    30 mysql> SHOW VARIABLES LIKE 'collation_%';
    31 +----------------------+-------------------+
    32 | Variable_name        | Value             |
    33 +----------------------+-------------------+
    34 | collation_connection | latin1_swedish_ci |
    35 | collation_database   | latin1_swedish_ci |
    36 | collation_server     | latin1_swedish_ci |
    37 +----------------------+-------------------+
    38 3 rows in set (0.01 sec)

    设置外部访问Mysql的编码,如下所示;

     1 mysql> SET NAMES 'utf8';
     2 Query OK, 0 rows affected (0.01 sec)
     3 
     4 mysql> SHOW VARIABLES LIKE 'character_set_%';
     5 +--------------------------+----------------------------+
     6 | Variable_name            | Value                      |
     7 +--------------------------+----------------------------+
     8 | character_set_client     | utf8                       |
     9 | character_set_connection | utf8                       |
    10 | character_set_database   | latin1                     |
    11 | character_set_filesystem | binary                     |
    12 | character_set_results    | utf8                       |
    13 | character_set_server     | latin1                     |
    14 | character_set_system     | utf8                       |
    15 | character_sets_dir       | /usr/share/mysql/charsets/ |
    16 +--------------------------+----------------------------+
    17 8 rows in set (0.00 sec)
    18 
    19 mysql> SHOW VARIABLES LIKE 'collation_%';
    20 +----------------------+-------------------+
    21 | Variable_name        | Value             |
    22 +----------------------+-------------------+
    23 | collation_connection | utf8_general_ci   |
    24 | collation_database   | latin1_swedish_ci |
    25 | collation_server     | latin1_swedish_ci |
    26 +----------------------+-------------------+
    27 3 rows in set (0.00 sec)
    28 
    29 mysql> 

    复制mysqld.cnf文件到/docker文件夹上面(在进行复制前需要先退出mysql的命令行哈)。

    1 root@df4e1235ec72:/# mkdir docker                     
    2 root@df4e1235ec72:/# cp /etc/mysql/mysql.conf.d/mysqld.cnf /docker/mysqld.cnf
    3 root@df4e1235ec72:/# ls /docker
    4 mysqld.cnf
    5 root@df4e1235ec72:/# 

    安装vim,如果不安装vim在使用vim的时候会报找不到,在修改docker中mysql的配置文件,无法使用vi命令。

     1 root@df4e1235ec72:/docker# apt-get update 
     2 Ign:1 http://deb.debian.org/debian stretch InRelease                                                
     3 Get:2 http://repo.mysql.com/apt/debian stretch InRelease [21.6 kB]                                  
     4 Get:3 http://deb.debian.org/debian stretch-updates InRelease [93.6 kB]                       
     5 Get:4 http://security.debian.org/debian-security stretch/updates InRelease [94.3 kB]                              
     6 Get:5 http://repo.mysql.com/apt/debian stretch/mysql-5.7 amd64 Packages [5689 B]                                  
     7 Get:6 http://security.debian.org/debian-security stretch/updates/main amd64 Packages [529 kB]
     8 Get:7 http://deb.debian.org/debian stretch Release [118 kB]                                                                                                                                                                    
     9 Get:8 http://deb.debian.org/debian stretch-updates/main amd64 Packages [28.2 kB]                                                                                                                                               
    10 Get:9 http://deb.debian.org/debian stretch Release.gpg [2410 B]                                                                                                                                                                
    11 Get:10 http://deb.debian.org/debian stretch/main amd64 Packages [7083 kB]                                                                                                                                                      
    12 Fetched 7976 kB in 7min 40s (17.3 kB/s)                                                                                                                                                                                        
    13 Reading package lists... Done
    14 root@df4e1235ec72:/docker# apt-get install vim
    15 Reading package lists... Done
    16 Building dependency tree       
    17 Reading state information... Done
    18 The following additional packages will be installed:
    19   vim-common vim-runtime xxd
    20 Suggested packages:
    21   ctags vim-doc vim-scripts
    22 The following NEW packages will be installed:
    23   vim vim-common vim-runtime xxd
    24 0 upgraded, 4 newly installed, 0 to remove and 26 not upgraded.
    25 Need to get 6734 kB of archives.
    26 After this operation, 31.1 MB of additional disk space will be used.
    27 Do you want to continue? [Y/n] y
    28 Get:1 http://deb.debian.org/debian stretch/main amd64 xxd amd64 2:8.0.0197-4+deb9u3 [132 kB]
    29 Get:2 http://deb.debian.org/debian stretch/main amd64 vim-common all 2:8.0.0197-4+deb9u3 [159 kB]                                                                                                                              
    30 Get:3 http://deb.debian.org/debian stretch/main amd64 vim-runtime all 2:8.0.0197-4+deb9u3 [5409 kB]                                                                                                                            
    31 Get:4 http://deb.debian.org/debian stretch/main amd64 vim amd64 2:8.0.0197-4+deb9u3 [1034 kB]                                                                                                                                  
    32 Fetched 6734 kB in 7min 23s (15.2 kB/s)                                                                                                                                                                                        
    33 debconf: delaying package configuration, since apt-utils is not installed
    34 Selecting previously unselected package xxd.
    35 (Reading database ... 8907 files and directories currently installed.)
    36 Preparing to unpack .../xxd_2%3a8.0.0197-4+deb9u3_amd64.deb ...
    37 Unpacking xxd (2:8.0.0197-4+deb9u3) ...
    38 Selecting previously unselected package vim-common.
    39 Preparing to unpack .../vim-common_2%3a8.0.0197-4+deb9u3_all.deb ...
    40 Unpacking vim-common (2:8.0.0197-4+deb9u3) ...
    41 Selecting previously unselected package vim-runtime.
    42 Preparing to unpack .../vim-runtime_2%3a8.0.0197-4+deb9u3_all.deb ...
    43 Adding 'diversion of /usr/share/vim/vim80/doc/help.txt to /usr/share/vim/vim80/doc/help.txt.vim-tiny by vim-runtime'
    44 Adding 'diversion of /usr/share/vim/vim80/doc/tags to /usr/share/vim/vim80/doc/tags.vim-tiny by vim-runtime'
    45 Unpacking vim-runtime (2:8.0.0197-4+deb9u3) ...
    46 Selecting previously unselected package vim.
    47 Preparing to unpack .../vim_2%3a8.0.0197-4+deb9u3_amd64.deb ...
    48 Unpacking vim (2:8.0.0197-4+deb9u3) ...
    49 Setting up xxd (2:8.0.0197-4+deb9u3) ...
    50 Setting up vim-common (2:8.0.0197-4+deb9u3) ...
    51 Setting up vim-runtime (2:8.0.0197-4+deb9u3) ...
    52 Setting up vim (2:8.0.0197-4+deb9u3) ...
    53 update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in auto mode
    54 update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vimdiff (vimdiff) in auto mode
    55 update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rvim (rvim) in auto mode
    56 update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rview (rview) in auto mode
    57 update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vi (vi) in auto mode
    58 update-alternatives: warning: skip creation of /usr/share/man/fr/man1/vi.1.gz because associated file /usr/share/man/fr/man1/vim.1.gz (of link group vi) doesn't exist
    59 update-alternatives: warning: skip creation of /usr/share/man/it/man1/vi.1.gz because associated file /usr/share/man/it/man1/vim.1.gz (of link group vi) doesn't exist
    60 update-alternatives: warning: skip creation of /usr/share/man/pl/man1/vi.1.gz because associated file /usr/share/man/pl/man1/vim.1.gz (of link group vi) doesn't exist
    61 update-alternatives: warning: skip creation of /usr/share/man/ru/man1/vi.1.gz because associated file /usr/share/man/ru/man1/vim.1.gz (of link group vi) doesn't exist
    62 update-alternatives: warning: skip creation of /usr/share/man/ja/man1/vi.1.gz because associated file /usr/share/man/ja/man1/vim.1.gz (of link group vi) doesn't exist
    63 update-alternatives: warning: skip creation of /usr/share/man/man1/vi.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group vi) doesn't exist
    64 update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/view (view) in auto mode
    65 update-alternatives: warning: skip creation of /usr/share/man/fr/man1/view.1.gz because associated file /usr/share/man/fr/man1/vim.1.gz (of link group view) doesn't exist
    66 update-alternatives: warning: skip creation of /usr/share/man/it/man1/view.1.gz because associated file /usr/share/man/it/man1/vim.1.gz (of link group view) doesn't exist
    67 update-alternatives: warning: skip creation of /usr/share/man/pl/man1/view.1.gz because associated file /usr/share/man/pl/man1/vim.1.gz (of link group view) doesn't exist
    68 update-alternatives: warning: skip creation of /usr/share/man/ru/man1/view.1.gz because associated file /usr/share/man/ru/man1/vim.1.gz (of link group view) doesn't exist
    69 update-alternatives: warning: skip creation of /usr/share/man/ja/man1/view.1.gz because associated file /usr/share/man/ja/man1/vim.1.gz (of link group view) doesn't exist
    70 update-alternatives: warning: skip creation of /usr/share/man/man1/view.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group view) doesn't exist
    71 update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/ex (ex) in auto mode
    72 update-alternatives: warning: skip creation of /usr/share/man/fr/man1/ex.1.gz because associated file /usr/share/man/fr/man1/vim.1.gz (of link group ex) doesn't exist
    73 update-alternatives: warning: skip creation of /usr/share/man/it/man1/ex.1.gz because associated file /usr/share/man/it/man1/vim.1.gz (of link group ex) doesn't exist
    74 update-alternatives: warning: skip creation of /usr/share/man/pl/man1/ex.1.gz because associated file /usr/share/man/pl/man1/vim.1.gz (of link group ex) doesn't exist
    75 update-alternatives: warning: skip creation of /usr/share/man/ru/man1/ex.1.gz because associated file /usr/share/man/ru/man1/vim.1.gz (of link group ex) doesn't exist
    76 update-alternatives: warning: skip creation of /usr/share/man/ja/man1/ex.1.gz because associated file /usr/share/man/ja/man1/vim.1.gz (of link group ex) doesn't exist
    77 update-alternatives: warning: skip creation of /usr/share/man/man1/ex.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group ex) doesn't exist
    78 update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in auto mode
    79 update-alternatives: warning: skip creation of /usr/share/man/fr/man1/editor.1.gz because associated file /usr/share/man/fr/man1/vim.1.gz (of link group editor) doesn't exist
    80 update-alternatives: warning: skip creation of /usr/share/man/it/man1/editor.1.gz because associated file /usr/share/man/it/man1/vim.1.gz (of link group editor) doesn't exist
    81 update-alternatives: warning: skip creation of /usr/share/man/pl/man1/editor.1.gz because associated file /usr/share/man/pl/man1/vim.1.gz (of link group editor) doesn't exist
    82 update-alternatives: warning: skip creation of /usr/share/man/ru/man1/editor.1.gz because associated file /usr/share/man/ru/man1/vim.1.gz (of link group editor) doesn't exist
    83 update-alternatives: warning: skip creation of /usr/share/man/ja/man1/editor.1.gz because associated file /usr/share/man/ja/man1/vim.1.gz (of link group editor) doesn't exist
    84 update-alternatives: warning: skip creation of /usr/share/man/man1/editor.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group editor) doesn't exist
    85 root@df4e1235ec72:/docker# 

    修改my.cnf配置文件,如果你喜欢的话,可以直接修改的,我这里备份出来进行修改,再进行覆盖原有的文件。

    在容器中将刚才修改后的文件覆盖回去,如下所示:

    1 root@df4e1235ec72:/docker# vim mysqld.cnf 
    2 root@df4e1235ec72:/docker# cp /docker/mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf 
    3 root@df4e1235ec72:/docker# 

    退出并重启容器,如下所示:

     1 [root@slaver1 book]# docker ps
     2 CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                               NAMES
     3 531862daae8d        docker.io/mysql:5.7.26   "docker-entrypoint..."   21 minutes ago      Up 21 minutes       0.0.0.0:3306->3306/tcp, 33060/tcp   focused_blackwell
     4 4d52cd016170        book                     "catalina.sh run"        About an hour ago   Up About an hour    0.0.0.0:8888->8080/tcp              gallant_bohr
     5 [root@slaver1 book]# docker restart 531862daae8d
     6 531862daae8d
     7 [root@slaver1 book]# docker ps
     8 CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                               NAMES
     9 531862daae8d        docker.io/mysql:5.7.26   "docker-entrypoint..."   21 minutes ago      Up 2 seconds        0.0.0.0:3306->3306/tcp, 33060/tcp   focused_blackwell
    10 4d52cd016170        book                     "catalina.sh run"        About an hour ago   Up About an hour    0.0.0.0:8888->8080/tcp              gallant_bohr
    11 [root@slaver1 book]# 

    注意,千万别基于刚才的容器创建一个新的镜像,不然,之前的工作又要重新做一遍了的。

    如果虚拟机重新启动,启动docker,然后启动你的mysql容器,docker start CONTAINER ID,停止dcker stop CONTAINER ID ,重启dcker stop CONTAINER ID。 

  • 相关阅读:
    Python模块、包、异常、文件(案例)
    jQuery DataTable 删除数据后重新加载
    Python|面向对象
    python开发的学生管理系统
    使用JDK开发WebServrice案例
    Python入门(案例)
    Spring总结以及在面试中的一些问题
    Web Services简单介绍
    Canvas实现文字粒子化,并且绕轴旋转(完善)
    HTML5 Canvas画数字时钟
  • 原文地址:https://www.cnblogs.com/biehongli/p/13150609.html
Copyright © 2020-2023  润新知