1、基于命令行创建基础deployment

[root@vms10 chap6-deployment]# kubectl create deployment web1 --image=nginx  --dry-run=client -o yaml>web.yaml

[root@vms10 chap6-deployment]# cat web.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web1
  name: web1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web1
    spec:
      containers:
      - image: nginx
        name: nginx
        imagePullPolicy: IfNotPresent
        resources: {}
status: {}

[root@vms10 chap6-deployment]# kubectl apply -f web.yaml
deployment.apps/web1 created

[root@vms10 chap6-deployment]# kubectl get pod
NAME                    READY   STATUS    RESTARTS   AGE
web1-5bfb6d8dcc-22qg9   1/1     Running   0          5m
[root@vms10 chap6-deployment]# kubectl get deployment
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
web1   1/1     1            1           5m3s


# 由deployment部署的pod即使被删除了,也会重新创建。
# 删除了deployment,pod也会被删除

[root@vms10 chap6-deployment]# kubectl delete deployment wed1

2、deployment创建pod流程

  1. 由deployment创建relicaSet
  2. 由relicaSet创建pod
[root@vms10 chap6-deployment]# kubectl get rs
NAME              DESIRED   CURRENT   READY   AGE
web1-5bfb6d8dcc   1         1         1       12m

[root@vms10 chap6-deployment]# kubectl get pod
NAME                    READY   STATUS    RESTARTS   AGE
web1-5bfb6d8dcc-dll2t   1/1     Running   0          6m29s

3、修改副本数

命令行方式修改--建议

[root@vms10 chap6-deployment]# kubectl get deploy
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
web1   3/3     3            3           46m
[root@vms10 chap6-deployment]# kubectl scale deploy web1 --replicas=5
deployment.apps/web1 scaled

在线修改,立即生效

[root@vms10 chap6-deployment]# kubectl edit deploy web1
deployment.apps/web1 edited

直接修改yaml文件,重新应用生效

[root@vms10 chap6-deployment]# kubectl apply -f web.yaml

4、HPA水平自动伸缩

通过检测pod CPU的负载,解决deployment里某pod负载太重(默认80%),动态伸缩pod的数量来负载均衡。

1、安装好metric server
[root@vms10 ~]# kubectl top nodes
NAME            CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
vms10.rhce.cc   231m         11%    1287Mi          68%
vms20.rhce.cc   66m          6%     692Mi           72%
vms30.rhce.cc   50m          5%     535Mi           61%


2、在resource中定义资源
[root@vms10 chap6-deployment]# cat web.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web1
  name: web1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web1
    spec:
      containers:
      - image: nginx
        name: nginx
        imagePullPolicy: IfNotPresent
        resources:
          requests:
            cpu: 400m
status: {}
[root@vms10 chap6-deployment]# kubectl apply -f web.yaml
deployment.apps/web1 created
[root@vms10 chap6-deployment]# kubectl get pods
NAME                    READY   STATUS              RESTARTS   AGE
web1-65bd67cbf8-4wbhr   0/1     ContainerCreating   0          2s


3、创建svc
[root@vms10 ~]# kubectl expose --name=svc1 deploy web1 --port=80 --target-port=80
service/svc1 exposed
[root@vms10 ~]# kubectl get svc
NAME   TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
svc1   ClusterIP   10.109.50.82   <none>        80/TCP    16s

4、配置HPA
[root@vms10 ~]# kubectl autoscale deploy web1 --max=5 --cpu-percent=80
horizontalpodautoscaler.autoscaling/web1 autoscaled
[root@vms10 chap6-deployment]# kubectl get hpa
NAME   REFERENCE         TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
web1   Deployment/web1   0%/80%    1         5         1          27m

5、测试
[root@vms10 chap6-deployment]# kubectl get pod
NAME                    READY   STATUS    RESTARTS   AGE
web1-65bd67cbf8-4wbhr   1/1     Running   1          14h

[root@vms30 ~]# yum -y install httpd-tools
[root@vms30 ~]# ab -t 600 -n 1000000 -c 1000 http://10.109.50.82:80/index.html
-n在测试会话中所执行的请求个数。默认时,仅执行一个请求。 
-c一次产生的请求个数。默认是一次一个。 
-t测试所进行的最大秒数

# 查看hpa负载超过80%
[root@vms10 chap6-deployment]# kubectl get hpa
NAME   REFERENCE         TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
web1   Deployment/web1   87%/80%   1         5         2          31m

# 通过hpa自动添加一个pod降低负载
[root@vms10 chap6-deployment]# kubectl get pod
NAME                    READY   STATUS    RESTARTS   AGE
web1-65bd67cbf8-4wbhr   1/1     Running   1          14h
web1-65bd67cbf8-bp79q   1/1     Running   0          69s

# 一段时间后负载降低,恢复至1个pod

5、deployment健壮性测试

vms30.rhce.cc关机,等一段时间就会发现,pod都会在vms20.rhce.cc上运行

vms30.rhce.cc 重新启动,pod并不会返回到vms30.rhce.cc 上运行

6、deployment切换镜像

# kubectl set image deployment web1 容器名=镜像名
[root@vms10 chap6-deployment]# kubectl set image deploy web1 nginx=nginx:1.9
deployment.apps/web1 image updated

[root@vms10 chap6-deployment]# kubectl get deployment -owide
NAME   READY   UP-TO-DATE   AVAILABLE   AGE    CONTAINERS   IMAGES      SELECTOR
web1   1/1     1            1           127m   nginx        nginx:1.9   app=web1

# 查看deployment历史操作
[root@vms10 chap6-deployment]# kubectl rollout history deploy web1
deployment.apps/web1
REVISION  CHANGE-CAUSE
5         kubectl set image deploy web1 nginx=nginx:1.9 --record=true

 7、滚动升级

[root@vms10 chap6-deployment]# kubectl edit deploy web1
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate


maxSurge 一次性升级25%
maxUnavailable 一次性删除多少个旧pod

8、其他控制器daemonSet

在每一个节点上都创建一个该pod,并且当新增一个节点时,自动创建一个pod

[root@vms10 chap7-daemonSet]# cat ds.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  creationTimestamp: null
  labels:
    app: web1
  name: web1
spec:
  selector:
    matchLabels:
      app: web1
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web1
    spec:
      containers:
      - image: nginx
        name: nginx
        imagePullPolicy: IfNotPresent

[root@vms10 chap7-daemonSet]# kubectl apply -f ds.yaml
daemonset.apps/web1 created

[root@vms10 chap7-daemonSet]# kubectl get pod
NAME         READY   STATUS    RESTARTS   AGE
web1-92wz6   1/1     Running   0          5s
web1-gn9ls   1/1     Running   0          5s

[root@vms10 chap7-daemonSet]# kubectl get ds
NAME   DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
web1   2         2         2       2            2           <none>          23s

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐