1、为什么K8s要引入deployment资源。
答:deployment也是保证pod高可用的一种方式,明明已经有RC(Replication Controller),为什么还要引入deployment呢?是因为deployment解决了RC(Replication Controller)的一个痛点。是因为RC在进行版本升级的时候,会造成一个问题,就是它的svc(Service)短时间内访问不了了,deployment就是解决这个问题的。
2、创建一个deployment的配置文件。
1 # 声明api的版本。2 apiVersion: extensions/v1beta13 # kind代表资源的类型,资源是Deployment。4 kind: Deployment5 # 资源叫什么名字,是在其属性metadata里面的。6 metadata:7 # 第一个属性name的值是myweb,即Service的名字就叫做myweb。8 name: nginx-deployment9 # 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]# ls3 pod rc svc4 [root@k8s-master k8s]# mkdir deploy5 [root@k8s-master k8s]# cd deploy/6 [root@k8s-master deploy]# ls7 [root@k8s-master deploy]# kubectl get rc 8 NAME DESIRED CURRENT READY AGE9 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 wide2 NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE3 deploy/nginx-deployment 3 3 3 0 2m4 5 NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR6 svc/kubernetes 10.254.0.1 <none> 443/TCP 12d <none>7 8 NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR9 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 AGE3 nginx-deployment 3 3 3 0 5m4 [root@k8s-master deploy]# kubectl delete deployment nginx-deployment5 deployment "nginx-deployment" deleted6 [root@k8s-master deploy]# kubectl get deployment 7 No resources found.8 [root@k8s-master deploy]# kubectl get all -o wide9 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=NodePort2 service "nginx-deployment" exposed3 [root@k8s-master ~]# kubectl get all -o wide4 NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE5 deploy/nginx-deployment 3 3 3 3 1h6 7 NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR8 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:368482 HTTP/1.1 200 OK3 Server: nginx/1.15.124 Date: Wed, 17 Jun 2020 03:09:26 GMT5 Content-Type: text/html6 Content-Length: 6127 Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT8 Connection: keep-alive9 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 wide2 NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE3 deploy/nginx-deployment 3 3 3 3 1h4 5 NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR6 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=nginx8 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:368482 HTTP/1.1 200 OK3 Server: nginx/1.15.124 Date: Wed, 17 Jun 2020 03:23:53 GMT5 Content-Type: text/html6 Content-Length: 6127 Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT8 Connection: keep-alive9 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" created3 [root@k8s-master ~]# kubectl rollout history deployment nginx-deployment 4 deployments "nginx-deployment"5 REVISION CHANGE-CAUSE6 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 nginx2 deployments "nginx"3 REVISION CHANGE-CAUSE4 1 kubectl run nginx --image=192.168.110.133:5000/nginx:1.13 --replicas=3 --record5 2 kubectl set image deploy nginx nginx=192.168.110.133:5000/nginx:1.156 7 [root@k8s-master ~]# kubectl rollout undo deployment nginx --to-revision=18 deployment "nginx" rolled back9 [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]# ls3 deploy pod rc svc4 [root@k8s-master k8s]# clear5 [root@k8s-master k8s]# wget https://www.qstack.com.cn/tomcat_demo.zip6 --2020-06-17 11:49:34-- https://www.qstack.com.cn/tomcat_demo.zip7 Resolving www.qstack.com.cn (www.qstack.com.cn)... 123.125.46.149, 111.202.85.378 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.302 [root@k8s-master rc]# docker push 192.168.110.133:5000/mysql:5.7.303 [root@k8s-master ~]# docker images 4 REPOSITORY TAG IMAGE ID CREATED SIZE5 192.168.110.133:5000/mysql 5.7.30 9cfcce23593a 8 days ago 448 MB6 docker.io/mysql 5.7.30 9cfcce23593a 8 days ago 448 MB7 docker.io/busybox latest 1c35c4412082 2 weeks ago 1.22 MB8 docker.io/registry latest 708bc6af7e5e 4 months ago 25.8 MB9 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: v12 kind: ReplicationController3 metadata:4 name: mysql5 spec:6 replicas: 17 selector:8 app: mysql9 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-xcth23 Namespace: default4 Node: k8s-node3/192.168.110.1355 Start Time: Wed, 17 Jun 2020 21:25:04 +08006 Labels: app=mysql7 Status: Pending8 IP: 172.16.32.29 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 registry2 /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 NAMES6 a27987d97039 registry "/entrypoint.sh /e..." 11 days ago Exited (2) 9 days ago registry7 [root@k8s-master ~]# docker rm a27987d970398 a27987d970399 [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" created3 [root@k8s-master tomcat_demo]# kubectl get rc 4 NAME DESIRED CURRENT READY AGE5 mysql 1 1 1 5s6 [root@k8s-master tomcat_demo]# kubectl get all -o wide7 NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR8 rc/mysql 1 1 1 11s mysql 192.168.110.133:5000/mysql:5.7.30 app=mysql9 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.json2 {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/docker7 # /etc/sysconfig/docker8 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: v13 kind: Service4 metadata:5 name: mysql6 spec:7 ports:8 - port: 33069 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" created3 [root@k8s-master tomcat_demo]# kubectl get svc 4 NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE5 kubernetes 10.254.0.1 <none> 443/TCP 13d6 mysql 10.254.245.194 <none> 3306/TCP 7s7 [root@k8s-master tomcat_demo]# kubectl get all -o wide8 NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR9 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: v12 kind: ReplicationController3 metadata:4 name: myweb5 spec:6 replicas: 17 selector:8 app: myweb9 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/tomcat2 Using default tag: latest3 Trying to pull repository docker.io/library/tomcat ... 4 latest: Pulling from docker.io/library/tomcat5 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版)》 这本书,这本书还是比较不错的,很喜欢。