一 Deployment相对于RC的优势

RS与Deployment主要用于替代RC。RS的全称为Replica Set。相对于RC,RS与Deployment的优势如下:

  • RC只支持基于等式的selector,如env=dev或者environment!=qa。但在RS中,还支持新的基于集合的selector,如version in (v1.0,v2.0)或者env not in (dev,qa)。这给复杂的运维管理带来方便
  • 使用Deployment升级Pod只需要定义Pod的最终状态,k8s会为你执行必要的操作。虽然使用kubectl rolling-update也可以完成滚动升级,但它是在客户端与服务端多次交互控制RC完成的,所以REST API中并没有rolling-update的接口,这为定制自己的管理系统带来了一些麻烦。Deployment拥有更加灵活的升级、回滚功能。

Replica Set目前与RC的区别只是支持的selector不同,后续会加入更多的功能。Deployment使用了Replica Set,是更高一层的概念。除非需要自定义升级功能或者根本不需要升级Pod,否则还是建议使用Deployment而不直接使用Replica Set。

二 Deployment是简单使用

2.1 同时安装三个nginx pod

[root@docker-server1 manifests]# cd /yamls/

[root@docker-server1 yamls]# mkdir deployment

[root@docker-server1 yamls]# cd deployment

[root@docker-server1 deployment]# vim nginx-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-deployment
namespace: default
spec:
replicas:
selector:
matchLabels:
name: hello-deployment
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: %
maxUnavailable:
template:
metadata:
labels:
name: hello-deployment
spec:
containers:
- name: webserver
image: nginx:1.4
ports:
- containerPort:

[root@docker-server1 deployment]# kubectl apply -f nginx-deployment.yaml

2.2 查看

[root@docker-server1 deployment]# kubectl get pods

NAME                               READY   STATUS         RESTARTS   AGE
goproxy / Running 126m
hello-deployment-c6fb6d858-9g5pq / ErrImagePull 11s
hello-deployment-c6fb6d858-qnnld / ErrImagePull 11s
hello-deployment-c6fb6d858-v549m / ErrImagePull 11s
init-demo / Running 68m
nginx / Running 4h13m
nginx-volume / Running 88m

[root@docker-server1 deployment]# kubectl get pods -o wide

NAME                               READY   STATUS             RESTARTS   AGE     IP            NODE              NOMINATED NODE   READINESS GATES
goproxy / Running 126m 10.244.1.8 192.168.132.132 <none> <none>
hello-deployment-c6fb6d858-9g5pq / ImagePullBackOff 21s 10.244.2.11 192.168.132.133 <none> <none>
hello-deployment-c6fb6d858-qnnld / ImagePullBackOff 21s 10.244.1.12 192.168.132.132 <none> <none>
hello-deployment-c6fb6d858-v549m / ImagePullBackOff 21s 10.244.1.13 192.168.132.132 <none> <none>
init-demo / Running 68m 10.244.1.10 192.168.132.132 <none> <none>
nginx / Running 4h14m 10.244.2.10 192.168.132.133 <none> <none>
nginx-volume / Running 89m 10.244.1.9 192.168.132.132 <none> <none>

[root@docker-server1 deployment]# kubectl describle pods  hello-deployment-c6fb6d858-v549m

Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled <unknown> default-scheduler Successfully assigned default/hello-deployment-c6fb6d858-v549m to 192.168.132.132
Normal Pulling 4m36s (x4 over 6m20s) kubelet, 192.168.132.132 Pulling image "nginx:1.4"
Warning Failed 4m32s (x4 over 6m12s) kubelet, 192.168.132.132 Failed to pull image "nginx:1.4": rpc error: code = Unknown desc = Error response from daemon: manifest for nginx:1.4 not found: manifest unknown: manifest unknown
Warning Failed 4m32s (x4 over 6m12s) kubelet, 192.168.132.132 Error: ErrImagePull
Warning Failed 4m8s (x7 over 6m11s) kubelet, 192.168.132.132 Error: ImagePullBackOff
Normal BackOff 74s (x19 over 6m11s) kubelet, 192.168.132.132 Back-off pulling image "nginx:1.4"

是nginx:1.4没有这个镜像

2.3 滚动更新

修改为1.14版本,就会自己更新

滚动更新策略

1 先删后起

2 先起后删

[root@docker-server1 deployment]#  kubectl apply -f nginx-deployment.yaml

[root@docker-server1 deployment]# kubectl get pods -o wide

hello-deployment-5fdb46d67c-jvvjr   /     ContainerCreating   0          4s      <none>        192.168.132.132   <none>           <none>
hello-deployment-c6fb6d858-9g5pq / ImagePullBackOff 8m15s 10.244.2.11 192.168.132.133 <none> <none>
hello-deployment-c6fb6d858-qnnld / ImagePullBackOff 8m15s 10.244.1.12 192.168.132.132 <none> <none>
hello-deployment-c6fb6d858-v549m / ImagePullBackOff 8m15s 10.244.1.13 192.168.132.132 <none> <none>

[root@docker-server1 deployment]# kubectl get pods -o wide

hello-deployment-5fdb46d67c-7ct2z   /     ContainerCreating             9s      <none>        192.168.132.133   <none>           <none>
hello-deployment-5fdb46d67c-jvvjr / Running 34s 10.244.1.14 192.168.132.132 <none> <none>
hello-deployment-c6fb6d858-9g5pq / ImagePullBackOff 8m45s 10.244.2.11 192.168.132.133 <none> <none>
hello-deployment-c6fb6d858-qnnld / ImagePullBackOff 8m45s 10.244.1.12 192.168.132.132 <none> <none>

已经运行成功更新一个

[root@docker-server1 deployment]# kubectl get pods -o wide

hello-deployment-5fdb46d67c-7ct2z   /     Running             118s    10.244.2.12   192.168.132.133   <none>           <none>
hello-deployment-5fdb46d67c-jvvjr / Running 2m23s 10.244.1.14 192.168.132.132 <none> <none>
hello-deployment-5fdb46d67c-qfjht / Running 90s 10.244.1.15 192.168.132.132 <none> <none

全部更新

二 部署一个word press网站

2.1 部署mysql

[root@docker-server1 deployment]# vim mysql-deployment.yaml

apiVersion:  apps/v1
kind: Deployment
metadata:
name: mysql
namespace: default
spec:
replicas:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
ports:
- containerPort:
env:
- name: MYSQL_ROOT_PASSWORD
value: RedHat123
- name: MYSQL_DATABASE
value: wordpress

[root@docker-server1 deployment]# kubectl apply -f mysql-deployment.yaml

[root@docker-server1 deployment]# kubectl get pods

mysql-5d4695cd5-kq9cl               0/1     ContainerCreating   0          8s

[root@docker-server1 deployment]# yum -y install mysql

[root@docker-server1 deployment]# kubectl get pods

NAME                                READY   STATUS    RESTARTS   AGE
goproxy 1/1 Running 0 149m
hello-deployment-5fdb46d67c-7ct2z 1/1 Running 0 15m
hello-deployment-5fdb46d67c-jvvjr 1/1 Running 0 15m
hello-deployment-5fdb46d67c-qfjht 1/1 Running 0 14m
init-demo 1/1 Running 0 92m
mysql-5d4695cd5-kq9cl 1/1 Running 0 102s
nginx 2/2 Running 4 4h37m
nginx-volume 1/1 Running 0 112m

[root@docker-server1 deployment]# kubectl get pods -o wide

mysql-5d4695cd5-kq9cl               /     Running             3m22s   10.244.1.16   192.168.132.132

[root@docker-server1 deployment]# mysql -uroot -pRedHat123 -h10.244.1.16

2.2 部署wordpress

[root@docker-server1 deployment]# vi wordpress-deployment.yaml

apiVersion:  apps/v1
kind: Deployment
metadata:
name: wordpress
namespace: default
spec:
replicas:
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
nodeName: 192.168.1.233
containers:
- name: wordpress
image: wordpress:5.3.-php7.-apache
ports:
- containerPort:
env:
- name: WORDPRESS_DB_PASSWORD
value: RedHat123
- name: WORDPRESS_DB_NAME
value: wordpress
- name: WORDPRESS_DB_HOST
value: 10.244.1.16
- name: WORDPRESS_DB_USER
value: root

[root@docker-server1 deployment]# kubectl apply -f wordpress-deployment.yaml

[root@docker-server1 deployment]# kubectl get pods -o wide

NAME                         READY   STATUS              RESTARTS   AGE     IP            NODE              NOMINATED NODE   READINESS GATES
goproxy / Running 164m 10.244.1.8 192.168.132.132 <none> <none>
init-demo / Running 106m 10.244.1.10 192.168.132.132 <none> <none>
mysql-5d4695cd5-kq9cl / Running 16m 10.244.1.16 192.168.132.132 <none> <none>
nginx / Running 4h51m 10.244.2.10 192.168.132.133 <none> <none>
nginx-volume / Running 127m 10.244.1.9 192.168.132.132 <none> <none>
wordpress-86cbfc4456-zp985 / ContainerCreating 2s <none> 192.168.132.131 <none> <none>

[root@docker-server1 deployment]# kubectl describe pods wordpress-86cbfc4456-zp985

Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Pulling 3m5s kubelet, 192.168.132.131 Pulling image "wordpress:5.3.2-php7.2-apache"
Normal Pulled 97s kubelet, 192.168.132.131 Successfully pulled image "wordpress:5.3.2-php7.2-apache"
Normal Created 94s kubelet, 192.168.132.131 Created container wordpress
Normal Started 94s kubelet, 192.168.132.131 Started container wordpress

[root@docker-server1 deployment]# kubectl get pods -o wide

NAME                         READY   STATUS    RESTARTS   AGE     IP            NODE              NOMINATED NODE   READINESS GATES
goproxy / Running 167m 10.244.1.8 192.168.132.132 <none> <none>
init-demo / Running 109m 10.244.1.10 192.168.132.132 <none> <none>
mysql-5d4695cd5-kq9cl / Running 19m 10.244.1.16 192.168.132.132 <none> <none>
nginx / Running 4h55m 10.244.2.10 192.168.132.133 <none> <none>
nginx-volume / Running 130m 10.244.1.9 192.168.132.132 <none> <none>
wordpress-86cbfc4456-zp985 / Running 3m9s 10.244.0.4 192.168.132.131 <none> <none>

2.3 service资源对象创建MySQL的转发

负载均衡器对象,通过创建 service资源对象,会在ku即e- proxy上为这一类资源创建一个負载均衡器,并为其生成一个集群内部可访问的不变的VIP

[root@docker-server1 deployment]# mkdir /yamls/svcs

[root@docker-server1 deployment]# cd /yamls/svcs

[root@docker-server1 svcs]# vi mysql-svc.yaml

apiVersion: v1
kind: Service
metadata:
name: mysql-svc
spec:
selector: #匹配label
app: mysql
type: ClusterIP
ports:
- port: 3306
targetPort:

[root@docker-server1 svcs]# kubectl apply -f mysql-svc.yaml

[root@docker-server1 svcs]# kubectl get svc

NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes ClusterIP 10.96.0.1 <none> /TCP 10h
mysql-svc ClusterIP 10.96.240.104 <none> /TCP 18s

[root@docker-server1 svcs]# cat mysql-svc.yam

apiVersion: v1
kind: Service
metadata:
name: mysql-svc
spec:
selector:
app: mysql
type: ClusterIP
clusterIP: 10.96.240.104
ports:
- port:
targetPort:

[root@docker-server1 svcs]# kubectl delete -f mysql-svc.yaml

[root@docker-server1 svcs]# kubectl apply -f mysql-svc.yaml

[root@docker-server1 svcs]# kubectl get svc

NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes ClusterIP 10.96.0.1 <none> /TCP 10h
mysql-svc ClusterIP 10.96.240.104 <none> /TCP 7s

[root@docker-server1 svcs]# mysql -uroot -pRedHat123 -h10.96.240.104

在这里连接很慢

[root@docker-server1 deployment]# kubectl get svc --all-namespaces

NAMESPACE              NAME                        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                  AGE
default kubernetes ClusterIP 10.96.0.1 <none> /TCP 11h
default mysql-svc ClusterIP 10.96.240.104 <none> /TCP 9m37s
kube-system kube-dns ClusterIP 10.96.0.10 <none> /UDP,/TCP,/TCP 11h
kubernetes-dashboard dashboard-metrics-scraper ClusterIP 10.96.93.119 <none> /TCP 10h
kubernetes-dashboard kubernetes-dashboard NodePort 10.96.87.98 <none> :/TCP 10h

2.4 修改wordspress

apiVersion:  apps/v1
kind: Deployment
metadata:
name: wordpress
namespace: default
spec:
replicas:
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
nodeName: 192.168.132.131
containers:
- name: wordpress
image: wordpress:5.3.-php7.-apache
ports:
- containerPort:
env:
- name: WORDPRESS_DB_PASSWORD
value: RedHat123
- name: WORDPRESS_DB_NAME
value: wordpress
- name: WORDPRESS_DB_HOST
value: 10.96.240.104 #修改这个IP,为serviceIP
- name: WORDPRESS_DB_USER
value: root

2.5 外部访问wordpress

[root@docker-server1 svcs]# vi wordpress-svc.yaml

apiVersion: v1
kind: Service
metadata:
name: wordpress
spec:
selector:
app: wordpress
type: NodePort #为node节点配置端口映射
ports:
- port:
targetPort:
nodePort:

[root@docker-server1 svcs]# kubectl apply -f wordpress-svc.yaml

[root@docker-server1 svcs]# kubectl get svc

NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes ClusterIP 10.96.0.1 <none> /TCP 11h
mysql-svc ClusterIP 10.96.240.104 <none> /TCP 16m
wordpress NodePort 10.96.72.115 <none> :/TCP 2s

[root@docker-server1 svcs]# netstat -ntlp|grep 32080

2.6 查看详细信息

[root@docker-server1 ~]# kubectl describe  svc mysql-svc

Name:              mysql-svc
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=mysql
Type: ClusterIP
IP: 10.96.240.104
Port: <unset> /TCP
TargetPort: /TCP
Endpoints: 10.244.1.22:
Session Affinity: None
Events: <none>

[root@docker-server1 ~]# kubectl describe  svc wordpress

Name:                     wordpress
Namespace: default
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"wordpress","namespace":"default"},"spec":{"ports":[{"nodePort":...
Selector: app=wordpress
Type: NodePort
IP: 10.96.72.115
Port: <unset> /TCP
TargetPort: /TCP
NodePort: <unset> /TCP
Endpoints: 10.244.0.6:
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>

2.7 查看iptables

[root@docker-server1 ~]# iptables -t nat -L|grep 32080

KUBE-MARK-MASQ  tcp  --  anywhere             anywhere             /* default/wordpress: */ tcp dpt:
KUBE-SVC-2T26TNVTRAU2MSW5 tcp -- anywhere anywhere /* default/wordpress: */ tcp dpt:

[root@docker-server1 ~]# iptables -t nat -L

KUBE-MARK-MASQ  tcp  -- !docker-server1/    10.96.240.104        /* default/mysql-svc: cluster IP */ tcp dpt:mysql
KUBE-SVC-USVAXDL4J4VMVCQU tcp -- anywhere 10.96.240.104 /* default/mysql-svc: cluster IP */ tcp dpt:mysql
KUBE-MARK-MASQ tcp -- !docker-server1/ 10.96.72.115 /* default/wordpress: cluster IP */ tcp dpt:http
KUBE-SVC-2T26TNVTRAU2MSW5 tcp -- anywhere 10.96.72.115 /* default/wordpress: cluster IP */ tcp dpt:http
KUBE-MARK-MASQ udp -- !docker-server1/ 10.96.0.10 /* kube-system/kube-dns:dns cluster IP */ udp dpt:domain
KUBE-SVC-TCOU7JCQXEZGVUNU udp -- anywhere 10.96.0.10 /* kube-system/kube-dns:dns cluster IP */ udp dpt:domain
KUBE-MARK-MASQ tcp -- !docker-server1/ 10.96.0.10 /* kube-system/kube-dns:dns-tcp cluster IP */ tcp dpt:domain
KUBE-SVC-ERIFXISQEP7F7OF4 tcp -- anywhere 10.96.0.10 /* kube-system/kube-dns:dns-tcp cluster IP */ tcp dpt:domain
KUBE-MARK-MASQ tcp -- !docker-server1/ 10.96.0.10 /* kube-system/kube-dns:metrics cluster IP */ tcp dpt:
KUBE-SVC-JD5MR3NA4I4DYORP tcp -- anywhere 10.96.0.10 /* kube-system/kube-dns:metrics cluster IP */ tcp dpt:
KUBE-MARK-MASQ tcp -- !docker-server1/ 10.96.93.119 /* kubernetes-dashboard/dashboard-metrics-scraper: cluster IP */ tcp dpt:irdmi
KUBE-SVC-NDSMHFCKXJRPU4FV tcp -- anywhere 10.96.93.119 /* kubernetes-dashboard/dashboard-metrics-scraper: cluster IP */ tcp dpt:irdmi
KUBE-MARK-MASQ tcp -- !docker-server1/ 10.96.87.98 /* kubernetes-dashboard/kubernetes-dashboard: cluster IP */ tcp dpt:https
KUBE-SVC-4CRUJHTV5RT5YMFY tcp -- anywhere 10.96.87.98 /* kubernetes-dashboard/kubernetes-dashboard: cluster IP */ tcp dpt:https
KUBE-MARK-MASQ tcp -- !docker-server1/ 10.96.0.1 /* default/kubernetes:https cluster IP */ tcp dpt:https 

2.8 外部配置wordpress

访问:http://192.168.132.131:32080

选择简体中文

配置邮件

登录

输入用户名密码

最终界面

配置完成


博主声明:本文的内容来源主要来自誉天教育晏威老师,由本人实验完成操作验证,需要的博友请联系誉天教育(http://www.yutianedu.com/),获得官方同意或者晏老师(https://www.cnblogs.com/breezey/)本人同意即可转载,谢谢!

006.kubernets之Deployment简单部署的更多相关文章

  1. kubernets之Deployment资源

    一  声明式的升级应用 1.1  回顾一下kubernets集群里面部署一个应用的形态应该是什么样子的,通过一副简单的图来描述一下 通过RC或者RS里面的模板创建了三个pod,之后通过一个servci ...

  2. XenApp简单部署

    作者:MR.Yangwj 目录 XenApp简单部署... 1 一.         XenApp安装... 1 (一)      服务器配置任务... 9 1)     许可证服务器配置... 9 ...

  3. mongodb 简单部署方案及实例

    mongodb 简单部署方案及实例 转载:http://my.oschina.net/zhuzhu0129/blog/53290 第一节 准备工作 一 安装mongodb  我这里选用rehl 5.6 ...

  4. Glue4Net简单部署基于win服务的Socket程序

    smark 专注于高并发网络和大型网站架规划设计,提供.NET平台下高吞吐的网络通讯应用技术咨询和支持 Glue4Net简单部署基于win服务的Socket程序 在写一些服务应用的时候经常把要它部署到 ...

  5. SQL Server中的变更捕获技术--简单部署

    ------准备------ CREATE DATABASE db_test_cdc ,) ,name )); INSERT INTO t1(name)VALUES('test') ------开始- ...

  6. Tomcat_记一次tomcatwar包应用简单部署过程

    记一次tomcat war包应用简单部署过程 by:授客 QQ:1033553122 1.  实践环境 Linux apache-tomcat-7.0.73 2.  实践步骤 # 解压tomcat压缩 ...

  7. Ceph分布式存储-原理介绍及简单部署

    1)Ceph简单概述Ceph是一个分布式存储系统,诞生于2004年,最早致力于开发下一代高性能分布式文件系统的项目.Ceph源码下载:http://ceph.com/download/.随着云计算的发 ...

  8. IntelliJ IDEA 14.1.4导入项目启动报错:Error during artifact deployment.[组件部署期间出错]

    1.问题描述:Error during artifact deployment.[组件部署期间出错] 2.删除Artifacts 3.刷新 4.重新生成Artifacts 5.重新选择 再重新启动项目 ...

  9. WebStorm 简单部署服务器进行测试操作

    WebStorm 简单部署服务器对外发布接口 第一步: 查看webstorm防火墙是否允许链接,控制面板-->防火墙-->高级设置 入站规则-->webstrom是否允许链接 ,双击 ...

随机推荐

  1. 浅谈使用spring security中的BCryptPasswordEncoder方法对密码进行加密与密码匹配

    浅谈使用springsecurity中的BCryptPasswordEncoder方法对密码进行加密(encode)与密码匹配(matches) spring security中的BCryptPass ...

  2. linux平台依赖性

    每个电脑平台有其自己的特点, 内核设计者可以自由使用所有的特性来获得更好的性能. in the target object file ??? 不象应用程序开发者, 他们必须和预编译的库一起连接他们的代 ...

  3. vue中 js获取图片尺寸 设置不同样式

    1.JS: 请求到后端数据后 判断图片尺寸 2.HTML代码 根据设置的类型,给图片添加不同的样式 3.CSS代码 添加不同尺寸的样式

  4. jQuery 工具类函数-检测对象是否为原始对象

    调用名为$.isPlainObject的工具函数,能检测对象是否为通过{}或new Object()关键字创建的原始对象,如果是,返回true,否则,返回false值,调用格式为: $.isPlain ...

  5. java.util.Date和jdk1.8新时间API比拼

    旧的时间和日期的API的缺陷 Java 的 java.util.Date 和 java.util.Calendar 类易用性差,不支持时区,而且都不是线程安全的. Date如果不格式化,打印出的日期可 ...

  6. Linux 内核 启动时间

    为见到 PCI 如何工作的, 我们从系统启动开始, 因为那是设备被配置的时候. 当一个 PCI 设备上电时, 硬件保持非激活. 换句话说, 设备只响应配置交易. 在上电时, 设备没有内存并且没有 I/ ...

  7. vue 插件大全

    UI组件 element - 饿了么出品的Vue2的web UI工具套件 Vux - 基于Vue和WeUI的组件库 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开 ...

  8. How to output the target message in dotnet build command line

    How can I output my target message when I using dotnet build in command line. I use command line to ...

  9. kafka性能测试代码

    bin/kafka-producer-perf-test.sh --num-records 5000000 --record-size 5000 \ --topic kafkatopic2 \ --b ...

  10. javascript 闭包的理解(一)

    过很多谈如何理解闭包的方法,但大多数文章,都是照抄或者解释<Javascript高级程序设计(第三版)>对于闭包的讲解,甚至例程都不约而同的引用高程三181页‘闭包与变量’一节的那个“返回 ...