1、为什么K8s要引入deployment资源。
答:deployment也是保证pod高可用的一种方式,明明已经有RC(Replication Controller),为什么还要引入deployment呢?是因为deployment解决了RC(Replication Controller)的一个痛点。是因为RC在进行版本升级的时候,会造成一个问题,就是它的svc(Service)短时间内访问不了了,deployment就是解决这个问题的。
2、创建一个deployment的配置文件。
声明api的版本。
apiVersion: extensions/v1beta1
kind代表资源的类型,资源是Deployment。
kind: Deployment
资源叫什么名字,是在其属性metadata里面的。
metadata:
第一个属性name的值是myweb,即Service的名字就叫做myweb。
name: nginx-deployment
spec是详细,详细里面定义了一个容器。
spec:
副本的数量,比RC少了一个selector。
replicas: 3
模板,都是使用模板来启动Pod的。
template:
资源叫什么名字,是在其属性metadata里面的。
metadata:
# 给Pod贴上了一个标签,标签是app: nginx,标签是有一定的作用的。
labels:
app: nginx
# spec是详细,详细里面定义了一个容器。
spec:
# 定义一个容器,可以声明多个容器的。
containers:
# 容器的名称叫做nginx
- name: nginx
# 使用了什么镜像,可以使用官方公有的,也可以使用私有的。
image: 192.168.110.133/nginx:1.13
# ports定义容器的端口
ports:
# 容器的端口是80,如果容器有多个端口,可以在后面接着写一行即可。
- containerPort: 80
具体操作,如下所示:
[root@k8s-master ~]# cd k8s/
[root@k8s-master k8s]# ls
pod rc svc
[root@k8s-master k8s]# mkdir deploy
[root@k8s-master k8s]# cd deploy/
[root@k8s-master deploy]# ls
[root@k8s-master deploy]# kubectl get rc
NAME DESIRED CURRENT READY AGE
myweb 3 3 3 4d
[root@k8s-master deploy]# kubectl delete rc myweb
replicationcontroller "myweb" deleted
[root@k8s-master deploy]# kubectl get rc
No resources found.
[root@k8s-master deploy]# kubectl get svc
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.254.0.1 <none> 443/TCP 12d
myweb 10.254.210.104 <nodes> 80:30000/TCP 2d
[root@k8s-master deploy]# kubectl delete svc myweb
service "myweb" deleted
[root@k8s-master deploy]# kubectl get svc
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.254.0.1 <none> 443/TCP 12d
[root@k8s-master deploy]# vim nginx-deploy.yaml
[root@k8s-master deploy]#
然后开始创建deployment,如下所示:
1 [root@k8s-master deploy]# kubectl create -f nginx-deploy.yaml
2 deployment "nginx-deployment" created
然后创建deployment资源完毕之后,可以查看创建的资源,如下所示:
[root@k8s-master deploy]# kubectl get all -o wide
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy/nginx-deployment 3 3 3 0 2m
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
svc/kubernetes 10.254.0.1 <none> 443/TCP 12d <none>
NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR
rs/nginx-deployment-3699032092 3 3 0 2m nginx 192.168.110.133/nginx:1.13 app=nginx,pod-template-hash=3699032092
NAME READY STATUS RESTARTS AGE IP NODE
po/nginx-deployment-3699032092-3jnk6 0/1 ImagePullBackOff 0 2m 172.16.85.2 k8s-master
po/nginx-deployment-3699032092-97bfm 0/1 ImagePullBackOff 0 2m 172.16.38.2 k8s-node3
po/nginx-deployment-3699032092-fmqkx 0/1 ImagePullBackOff 0 2m 172.16.5.2 k8s-node2
[root@k8s-master deploy]#
我这里明显没有启动起来,说镜像拉取错误,仔细检查是nginx-deploy.yaml配置文件,在配置image的地方写错了,漏写了端口号导致的。
再使用命令创建一遍,就发现创建好了。deployment并不像RC直接去启动Pod,它们之间的区别是deployment会启动一个RS,RS拥有RC的百分之九十的功能,比RC的功能还多一些,由RC再去启动三个POD。
[root@k8s-master deploy]# kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-deployment 3 3 3 0 5m
[root@k8s-master deploy]# kubectl delete deployment nginx-deployment
deployment "nginx-deployment" deleted
[root@k8s-master deploy]# kubectl get deployment
No resources found.
[root@k8s-master deploy]# kubectl get all -o wide
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
svc/kubernetes 10.254.0.1 <none> 443/TCP 12d <none>
[root@k8s-master deploy]# kubectl create -f nginx-deploy.yaml
deployment "nginx-deployment" created
[root@k8s-master deploy]# kubectl get all -o wide
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy/nginx-deployment 3 3 3 3 7s
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
svc/kubernetes 10.254.0.1 <none> 443/TCP 12d <none>
NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR
rs/nginx-deployment-3018768667 3 3 3 7s nginx 192.168.110.133:5000/nginx:1.13 app=nginx,pod-template-hash=3018768667
NAME READY STATUS RESTARTS AGE IP NODE
po/nginx-deployment-3018768667-1c6lm 1/1 Running 0 7s 172.16.38.2 k8s-node3
po/nginx-deployment-3018768667-cvvjc 1/1 Running 0 7s 172.16.85.2 k8s-master
po/nginx-deployment-3018768667-grvlh 1/1 Running 0 7s 172.16.5.2 k8s-node2
[root@k8s-master deploy]#
此时,已经有了一个deployment,那么如何让Pod里面的服务让外界访问呢,我们也是需要创建一个service的,除了使用配置文件进行创建,还可以通过命令进行关联。
[root@k8s-master ~]# kubectl expose deployment nginx-deployment --port=80 --type=NodePort
service "nginx-deployment" exposed
[root@k8s-master ~]# kubectl get all -o wide
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy/nginx-deployment 3 3 3 3 1h
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
svc/kubernetes 10.254.0.1 <none> 443/TCP 12d <none>
svc/nginx-deployment 10.254.95.103 <nodes> 80:36848/TCP 23s app=nginx
NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR
rs/nginx-deployment-3018768667 3 3 3 1h nginx 192.168.110.133:5000/nginx:1.13 app=nginx,pod-template-hash=3018768667
NAME READY STATUS RESTARTS AGE IP NODE
po/nginx-deployment-3018768667-1c6lm 1/1 Running 0 1h 172.16.38.2 k8s-node3
po/nginx-deployment-3018768667-cvvjc 1/1 Running 0 1h 172.16.85.2 k8s-master
po/nginx-deployment-3018768667-grvlh 1/1 Running 0 1h 172.16.5.2 k8s-node2
[root@k8s-master ~]# curl -I 192.168.110.133:36848
HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Wed, 17 Jun 2020 03:05:29 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT
Connection: keep-alive
ETag: "5acb8e45-264"
Accept-Ranges: bytes
[root@k8s-master ~]#
下面可以对nginx进行版本升级,不用创建配置文件,这里可以直接修改配置文件即可。
1 [root@k8s-master ~]# kubectl edit deployment nginx-deployment
具体修改内容,如下所示:
修改,保存之后再使用curl命令进行查看,发现版本已经进行了升级了的。
[root@k8s-master ~]# curl -I 192.168.110.133:36848
HTTP/1.1 200 OK
Server: nginx/1.15.12
Date: Wed, 17 Jun 2020 03:09:26 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT
Connection: keep-alive
ETag: "5cb5d3c3-264"
Accept-Ranges: bytes
[root@k8s-master ~]#
可以通过命令查看升级之后三个Pod节点的情况,这里可以看到多了一个rs/nginx-deployment-3181297949,之前的那个rs/nginx-deployment-3018768667都被设置为了0。
注意:这里我也出现了升级之后无法将Node2节点的Pod启动成功的情况,我重启了三台机器,三个节点的Pod都有了,这里可能是因为从私有仓库下载的地方没有配置好,重启之前Node2节点使用docker images命令是无法查看到私有仓库的nginx:1.15版本的,重启之后才可以看到。
[root@k8s-master ~]# kubectl get all -o wide
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy/nginx-deployment 3 3 3 3 1h
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
svc/kubernetes 10.254.0.1 <none> 443/TCP 12d <none>
svc/nginx-deployment 10.254.95.103 <nodes> 80:36848/TCP 14m app=nginx
NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR
rs/nginx-deployment-3018768667 0 0 0 1h nginx 192.168.110.133:5000/nginx:1.13 app=nginx,pod-template-hash=3018768667
rs/nginx-deployment-3181297949 3 3 3 10m nginx 192.168.110.133:5000/nginx:1.15 app=nginx,pod-template-hash=3181297949
NAME READY STATUS RESTARTS AGE IP NODE
po/nginx-deployment-3181297949-9d3t0 1/1 Running 1 10m 172.16.59.2 k8s-node2
po/nginx-deployment-3181297949-mjtk3 1/1 Running 1 10m 172.16.16.2 k8s-master
po/nginx-deployment-3181297949-vp84j 1/1 Running 1 10m 172.16.32.2 k8s-node3
[root@k8s-master ~]#
来源:oschina
链接:https://my.oschina.net/u/4394698/blog/4814645