一、Replication Controller
Replication Controller(复制控制器,RC),使Pod副本保持在预期值
root@k8s-master \~\]# vim replicationcontroller-nginx.yaml apiVersion: v1 kind: ReplicationController metadata: name: nginx spec: replicas: 3 selector: app: nginx template: metadata: name: nginx labels: app: nginx spec: containers: - name: nginx image: nginx:1.7.9 ports: - containerPort: 80
(1)创建replicationController
root@k8s-master \~\]# ku create -f replicationcontroller-nginx.yaml
replicationcontroller/nginx created
\[root@k8s-master \~\]# ku get pod
NAME READY STATUS RESTARTS AGE
nginx-bmsx8 1/1 Running 0 8s
nginx-brpgz 1/1 Running 0 8s
### (2)删除Pod
\[root@k8s-master \~\]# ku delete pod nginx-bmsx8 -n default
pod "nginx-bmsx8" deleted
\[root@k8s-master \~\]# ku get pod
NAME READY STATUS RESTARTS AGE
nginx-9hbr6 1/1 Running 0 4s
nginx-brpgz 1/1 Running 0 12m
### (3)删除replicationcontroller
\[root@k8s-master \~\]# ku delete -f replicationcontroller-nginx.yaml
replicationcontroller "nginx" deleted
## 二、ReplicaSet
ReplicaSet (复制集,RS)基于集合的标签选择器的下一代Replication Controller,用于Deployment协调创建,删除和更新。ReplicaSet支持标签选择器,建议使用Deployment自动管理ReplicaSet。
标签是用来标识k8s对象的一组附加在其上的一组键值对,通过标签筛选和排除一组对象。
标签选择器用来选择一组对(标签并不能表示唯一一个对象)
标签选择器
标签定义的方式:
基于等式的定义: app=nginx
基于键值对定义方式: app : nginx
基于集合标签定义方式: {key : app,operator:in,values:\[nginx,apache\]}
### (1)编辑ReplicaSet文件
> \[root@k8s-master \~\]# vim replicaset-example.yaml
>
> apiVersion: apps/v1
>
> kind: ReplicaSet
>
> metadata:
>
> name: frontend
>
> labels:
>
> app: guestbook
>
> tier: frontend
>
> spec:
>
> # modify replicas according to your case
>
> replicas: 3
>
> selector:
>
> matchLabels:
>
> tier: frontend
>
> matchExpressions:
>
> - {key: tier, operator: In, values: \[frontend\]}
>
> template:
>
> metadata:
>
> labels:
>
> app: guestbook
>
> tier: frontend
>
> spec:
>
> containers:
>
> - name: php-redis
>
> image: nginx:1.7.9
>
> resources:
> requests:
> cpu: 100m
> memory: 100Mi
>
> env:
>
> - name: GET_HOSTS_FROM
>
> value: dns
>
> # If your cluster config does not include a dns service, then to
>
> # instead access environment variables to find service host
>
> # info, comment out the 'value: dns' line above, and uncomment the
>
> # line below.
>
> # value: env
>
> ports:
>
> - containerPort: 80
备注:
requests:请求资源限制,分配资源必须达到此要求
limits:最多可以请求多少资源
单位m:cpu计量单位是毫核(m)。一个节点cpu核心数量乘以1000,得到总结点cpu数量。如:一个节点有两核,那么该节点cpu总和量为2000m,该容器启动时为100/2000核心(5%)。
### (2)创建RS
\[root@k8s-master \~\]# ku create -f replicaset-example.yaml
replicaset.apps/frontend created
### (3)查看Pod
\[root@k8s-master \~\]# ku get pod
NAME READY STATUS RESTARTS AGE
frontend-8dv56 1/1 Running 0 8s
frontend-d6d82 1/1 Running 0 8s
frontend-dzmgf 1/1 Running 0 8s
### (4)删除RS
\[root@k8s-master \~\]# ku delete -f replicaset-example.yaml
replicaset.apps "frontend" deleted
## 三、无状态应用管理Deployment
无状态服务(stateless service)单次进行请求处理,不依赖其他请求。Deployment用来管理RS,为Pod和RS提供声明性更新,以及其他功能,生产环境中使用Deployment替代RS。
> \[root@k8s-master \~\]# vim nginx-deployment.yaml
>
> apiVersion: apps/v1
>
> kind: Deployment
>
> metadata:
>
> name: nginx-deployment
>
> labels:
>
> name: nginx-deployment
>
> spec:
>
> replicas: 2
>
> selector:
>
> matchLabels:
>
> app: nginx
>
> template:
>
> metadata:
>
> labels:
>
> app: nginx
>
> spec:
>
> containers:
>
> - name: nginx
>
> image: nginx:1.7.9
>
> ports:
>
> - name: nginx
>
> containerPort: 80
### (1)创建Deployment
\[root@k8s-master \~\]# ku create -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
\[root@k8s-master \~\]# ku get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-6cf9b75cdd-56qzm 1/1 Running 0 9s
nginx-deployment-6cf9b75cdd-sxs5t 1/1 Running 0 9s
### (2)更新Deployment
\[root@k8s-master \~\]# ku set image deployment nginx-deployment nginx=nginx:1.9.1 --record
Flag --record has been deprecated, --record will be removed in the future
\[root@k8s-master \~\]# ku set image deployment nginx-deployment nginx=nginx:1.12.1 --record
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/nginx-deployment image updated
### (3)查看更新
\[root@k8s-master \~\]# ku rollout status deployment.v1.apps/nginx-deployment
deployment "nginx-deployment" successfully rolled out
\[root@k8s-master \~\]# ku get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-55bbd8478b 2 2 2 6m50s
nginx-deployment-6cf9b75cdd 0 0 0 11m
nginx-deployment-7569c477b6 0 0 0 7m14s
\[root@k8s-master \~\]# ku describe deploy nginx-deployment (查看详细信息)
### (4)回滚更新
\[root@k8s-master \~\]# ku set image deployment nginx-deployment nginx=dotbalo/canary:v1 --record
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/nginx-deployment image updated
\[root@k8s-master \~\]# ku set image deployment nginx-deployment nginx=dotbalo/canary:v3 --record
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/nginx-deployment image updated
### (5)查看历史版本
\[root@k8s-master \~\]# ku rollout history deployment/nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 \