文章目录

展开

Kubernetes-Controller 控制器

控制器 Controller

控制器的作用

1.pod类型的资源,删除pod后,不会重建
2.替用户监视并保证相应的节点上始终有用户所期望的副本数量的pod在运行
3.如果所运行的pod副本数超过了用户期望的,那么控制器就会删掉,直到和用户期望的一致
4.如果所运行的pod副本数低于用户期望的,那么控制器就会创建,直到和用户期望的一致

控制器类型

RC: 标准控制器
RS(ReplicaSet): 按用户期望的副本创建Pod,并始终保持相应数量的副本

Deployment: 
1)控制RS来保证Pod始终保持相应的数量副本
2)滚动更新,回滚,回滚默认保留10个版本
3)声明式配置,支持动态修改
4)管理无状态应用最理想的控制器
5)de节点可能会运行0个或多个pod

DaemonSet: 一个节点只运行一个,必须是始终运行的状态(例:kube-proxy,zabbix-agent)

StatefullSet: 有状态的应用

RS 控制器

# rs控制器创建5个nginx的pod示例
[root@master-1 ~]# vim rs-nginx.yml 
apiVersion: apps/v1           ## apiVersion接口除了pod的接口还加上了RS控制器的接口
kind: ReplicaSet              ## kind资源类型指定为要使用的控制器
metadata:                     ## 那这里的metadata就是给这个RS控制器定义内容
  labels:                     ## RS控制器定义标签
    app: nginx-rs
  name: nginx-controller-rs   ## RS控制器的名字 pod的名字就是这个名字+随机数
spec:                         ## 定义资源
  replicas: 5                 ## replicas指定启动的服务的个数
  selector:
    matchLabels:              ## 定义一个pod的标签 下面的Pod指定这个标签 连接并让控制器管理
      nginx: nginx 
  template:                   ## RS控制器通过用template来包含之前pod的配置
    metadata:
      name: nginx-pod-rs
      labels:
        nginx: nginx          ## 这里指定上面的RS控制器的标签
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        imagePullPolicy: IfNotPresent

# 纯净版示例
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  labels:
    app: nginx-rs
  name: nginx-controller-rs
spec:
  replicas: 5
  selector:
    matchLabels:
      nginx: nginx 
  template:
    metadata:
      name: nginx-pod-rs
      labels:
        nginx: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        imagePullPolicy: IfNotPresent

# 运行
[root@master-1 ~]# kubectl apply -f rs-nginx.yml 
replicaset.apps/nginx-controller-rs created

# 查看nginx
[root@master-1 ~]# kubectl get pod
NAME                        READY   STATUS    RESTARTS   AGE
nginx-controller-rs-hf7gr   1/1     Running   0          20s
nginx-controller-rs-thqq9   1/1     Running   0          20s
nginx-controller-rs-w7dhd   1/1     Running   0          20s
nginx-controller-rs-wk9px   1/1     Running   0          20s
nginx-controller-rs-x54kj   1/1     Running   0          20s

# 查看控制器
[root@master-1 ~]# kubectl get replicasets
NAME                  DESIRED   CURRENT   READY   AGE
nginx-565785f75c      1         1         1       3d23h
nginx-controller-rs   5         5         5       5m8s

# 扩缩容(法一:资源清单修改)
[root@master-1 ~]# vim rs-nginx.yml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  labels:
    app: nginx-rs
  name: nginx-controller-rs
spec:
  ## 修改为3个
  replicas: 3
  selector:
    matchLabels:
      nginx: nginx 
  template:
    metadata:
      name: nginx-pod-rs
      labels:
        nginx: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        imagePullPolicy: IfNotPresent
[root@master-1 ~]# kubectl apply -f rs-nginx.yml 
replicaset.apps/nginx-controller-rs configured
[root@master-1 ~]# kubectl get replicasets
NAME                  DESIRED   CURRENT   READY   AGE
nginx-565785f75c      1         1         1       3d23h
nginx-controller-rs   3         3         3       7m5s

# 扩缩容(法二:kube命令修改)
[root@master-1 ~]# kubectl edit rs nginx-controller-rs 
## 扩容为6个
  replicas: 6
[root@master-1 ~]# kubectl get replicasets
NAME                  DESIRED   CURRENT   READY   AGE
nginx-565785f75c      1         1         1       3d23h
nginx-controller-rs   6         6         6       9m33s

# 简单方法的扩缩容(rs)
[root@master-1 ~]# kubectl scale rs nginx-controller-rs --replicas=10
replicaset.apps/nginx-controller-rs scaled
[root@master-1 ~]# kubectl get replicasets
NAME                  DESIRED   CURRENT   READY   AGE
nginx-565785f75c      1         1         1       3d23h
nginx-controller-rs   10        10        10      10m

Deployment 控制器(运用的比较多,实践工作中看需求)

image-20230924203149362

# Deployment控制器创建5个nginx的pod示例
[root@master-1 ~]# vim dp-nginx.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx-dp
  name: nginx-controller-dp
spec:
  replicas: 5
  selector:
    matchLabels:
      nginx: nginx01
  template:
    metadata:
      labels:
        nginx: nginx01
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        imagePullPolicy: IfNotPresent

# 运行
[root@master-1 ~]# kubectl apply -f dp-nginx.yml 
deployment.apps/nginx-controller-dp created

# 查看rs控制器
[root@master-1 ~]# kubectl  get rs
NAME                             DESIRED   CURRENT   READY   AGE
nginx-565785f75c                 1         1         1       3d23h
nginx-controller-dp-6796cf7b8d   5         5         5       16s
nginx-controller-rs              10        10        10      14m

# 查看deployments控制器
[root@master-1 ~]# kubectl  get deployments.apps 
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
nginx                 1/1     1            1           3d23h
nginx-controller-dp   5/5     5            5           43s

# 查看pod
[root@master-1 ~]# kubectl get pod
NAME                                   READY   STATUS    RESTARTS   AGE
nginx-565785f75c-8gfgw                 1/1     Running   0          12m
nginx-controller-dp-6796cf7b8d-9pc2s   1/1     Running   0          100s
nginx-controller-dp-6796cf7b8d-gz6t7   1/1     Running   0          100s
nginx-controller-dp-6796cf7b8d-hz2hh   1/1     Running   0          100s
nginx-controller-dp-6796cf7b8d-v2l5q   1/1     Running   0          100s
nginx-controller-dp-6796cf7b8d-xqpd6   1/1     Running   0          100s
nginx-controller-rs-42jdt              1/1     Running   0          7m26s
nginx-controller-rs-7zfct              1/1     Running   0          7m26s
nginx-controller-rs-bc4ft              1/1     Running   0          5m53s
nginx-controller-rs-drz8k              1/1     Running   0          5m53s
nginx-controller-rs-hf7gr              1/1     Running   0          16m
nginx-controller-rs-thqq9              1/1     Running   0          16m
nginx-controller-rs-tsdz4              1/1     Running   0          5m53s
nginx-controller-rs-twj5v              1/1     Running   0          5m53s
nginx-controller-rs-wk9px              1/1     Running   0          16m
nginx-controller-rs-xtsn5              1/1     Running   0          7m26s

## 结论
通过控制RS来保证Pod始终保持相应的数量副本

# 滚动更新
[root@master-1 ~]# vim dp-nginx.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx-dp
  name: nginx-controller-dp
spec:
  replicas: 8
  selector:
    matchLabels:
      nginx: nginx01
  template:
    metadata:
      labels:
        nginx: nginx01
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        imagePullPolicy: IfNotPresent

# 查看之前的deployments控制器
[root@master-1 ~]# kubectl get deployments.apps 
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
nginx                 1/1     1            1           3d23h
nginx-controller-dp   5/5     5            5           7m7s

# 运行
[root@master-1 ~]# kubectl apply -f dp-nginx.yml 
deployment.apps/nginx-controller-dp configured

# 动态查看deployments控制器
[root@master-1 ~]# kubectl  get deployments.apps  -w
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
nginx                 1/1     1            1           3d23h
nginx-controller-dp   5/8     4            5           11m
nginx-controller-dp   6/8     4            6           11m
nginx-controller-dp   7/8     4            7           11m
nginx-controller-dp   8/8     4            8           11m
nginx-controller-dp   8/8     4            8           11m
nginx-controller-dp   7/8     4            7           11m
nginx-controller-dp   7/8     5            7           11m
nginx-controller-dp   7/8     5            7           11m
nginx-controller-dp   6/8     5            6           11m
nginx-controller-dp   6/8     6            6           11m
nginx-controller-dp   7/8     6            7           11m
nginx-controller-dp   7/8     6            7           11m
nginx-controller-dp   7/8     6            7           11m
nginx-controller-dp   7/8     7            7           11m
nginx-controller-dp   6/8     7            6           11m
nginx-controller-dp   6/8     8            6           11m
nginx-controller-dp   7/8     8            7           11m
nginx-controller-dp   7/8     8            7           11m
nginx-controller-dp   6/8     8            6           11m
nginx-controller-dp   7/8     8            7           11m
nginx-controller-dp   7/8     8            7           11m
nginx-controller-dp   6/8     8            6           11m
nginx-controller-dp   7/8     8            7           11m
nginx-controller-dp   8/8     8            8           11m

## 总结
滚动更新,替换版本时是启动好一台再把旧之前版本剔除掉

版本更新

# 版本更新
## 语法: kubectl set image -f 资源清单文件 容器名=镜像名
[root@master-1 ~]# kubectl set image -f dp-nginx.yml nginx=nginx:1.24.0
deployment.apps/nginx-controller-dp image updated

# 查看滚动更新状态
[root@master-1 ~]# kubectl rollout status deployment nginx-controller-dp 
Waiting for deployment "nginx-controller-dp" rollout to finish: 4 out of 8 new replicas have been updated...
Waiting for deployment "nginx-controller-dp" rollout to finish: 4 out of 8 new replicas have been updated...
Waiting for deployment "nginx-controller-dp" rollout to finish: 4 out of 8 new replicas have been updated...
Waiting for deployment "nginx-controller-dp" rollout to finish: 4 out of 8 new replicas have been updated...
Waiting for deployment "nginx-controller-dp" rollout to finish: 4 out of 8 new replicas have been updated...
Waiting for deployment "nginx-controller-dp" rollout to finish: 5 out of 8 new replicas have been updated...
Waiting for deployment "nginx-controller-dp" rollout to finish: 5 out of 8 new replicas have been updated...
Waiting for deployment "nginx-controller-dp" rollout to finish: 5 out of 8 new replicas have been updated...
Waiting for deployment "nginx-controller-dp" rollout to finish: 5 out of 8 new replicas have been updated...
Waiting for deployment "nginx-controller-dp" rollout to finish: 5 out of 8 new replicas have been updated...
Waiting for deployment "nginx-controller-dp" rollout to finish: 6 out of 8 new replicas have been updated...
Waiting for deployment "nginx-controller-dp" rollout to finish: 6 out of 8 new replicas have been updated...
Waiting for deployment "nginx-controller-dp" rollout to finish: 6 out of 8 new replicas have been updated...
Waiting for deployment "nginx-controller-dp" rollout to finish: 6 out of 8 new replicas have been updated...
Waiting for deployment "nginx-controller-dp" rollout to finish: 6 out of 8 new replicas have been updated...
Waiting for deployment "nginx-controller-dp" rollout to finish: 7 out of 8 new replicas have been updated...
Waiting for deployment "nginx-controller-dp" rollout to finish: 7 out of 8 new replicas have been updated...
Waiting for deployment "nginx-controller-dp" rollout to finish: 7 out of 8 new replicas have been updated...
Waiting for deployment "nginx-controller-dp" rollout to finish: 7 out of 8 new replicas have been updated...
Waiting for deployment "nginx-controller-dp" rollout to finish: 7 out of 8 new replicas have been updated...
Waiting for deployment "nginx-controller-dp" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "nginx-controller-dp" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "nginx-controller-dp" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "nginx-controller-dp" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-controller-dp" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-controller-dp" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-controller-dp" rollout to finish: 6 of 8 updated replicas are available...
Waiting for deployment "nginx-controller-dp" rollout to finish: 7 of 8 updated replicas are available...
deployment "nginx-controller-dp" successfully rolled out

# 版本回滚到上一个版本
[root@master-1 ~]# kubectl rollout undo deployment nginx-controller-dp 
deployment.apps/nginx-controller-dp rolled back

# 回滚指定版本
## 如果想查看指定版本详细信息(版本记录)
需要在创建命令后面加上 --record
例:kubectl set image -f 111.yml nginx-dp=nginx:1.18.0 --record
## 查看历史版本(默认只保留10个版本)
[root@master-1 ~]# kubectl rollout history deployment nginx-controller-dp 
deployment.apps/nginx-controller-dp 
REVISION  CHANGE-CAUSE
3         <none>
5         <none>
6         <none>
## 查看指定历史版本的详细信息
[root@master-1 ~]# kubectl rollout history deployment nginx-controller-dp --revision=3
deployment.apps/nginx-controller-dp with revision #3
Pod Template:
  Labels:       nginx=nginx01
        pod-template-hash=6796cf7b8d
  Containers:
   nginx:
    Image:      nginx:1.18
    Port:       <none>
    Host Port:  <none>
    Environment:        <none>
    Mounts:     <none>
  Volumes:      <none>
## 回滚到指定版本
[root@master-1 ~]# kubectl rollout undo deployment nginx-controller-dp --to-revision=5
deployment.apps/nginx-controller-dp rolled back
### 查看历史版本(版本5消失了,因为已经回滚了)
[root@master-1 ~]# kubectl rollout history deployment nginx-controller-dp 
deployment.apps/nginx-controller-dp 
REVISION  CHANGE-CAUSE
3         <none>
6         <none>
7         <none>

扩缩容

[root@master-1 ~]# kubectl scale deployment nginx-controller-dp --replicas=1
[root@master-1 ~]# kubectl get deployments.apps 
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
nginx                 1/1     1            1           3d23h
nginx-controller-dp   1/1     1            1           33m

DaemonSet 控制器

image-20230924200700184

# DaemonSet控制器创建nginx的pod示例
[root@master-1 ~]# cat ds-nginx.yml 
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: nginx-controller-ds
  labels:
    app: nginx-labels
spec:
  selector:
    matchLabels:
      app: nginx-labels
  template:
    metadata:
      labels:
        app: nginx-labels
    spec:
      containers:
      - name: nginx-ds
        image: nginx:alpine
        imagePullPolicy: IfNotPresent

# 运行
[root@master-1 ~]# kubectl apply -f ds-nginx.yml 
daemonset.apps/nginx-controller-ds created

# 可以观察到两个node节点各起了一个nginx
[root@master-1 ~]# kubectl get pod -o wide
NAME                                   READY   STATUS    RESTARTS   AGE   IP          NODE     NOMINATED NODE   READINESS GATES
nginx-controller-ds-8s6fh              1/1     Running   0          26s   10.2.2.77   node-2   <none>           <none>
nginx-controller-ds-t9nqs              1/1     Running   0          26s   10.2.1.84   node-1   <none>           <none>

# 查看DaemonSet控制器
[root@master-1 ~]# kubectl get ds
NAME                  DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
nginx-controller-ds   2         2         2       2            2           <none>          119s

HPA

官网地址:TP

image-20230924200646328

工作原理

HAP 通过收集来的监控指标分析所有 Pod 的负载情况,并且根据我们设定好的标准来自动扩容收缩 ReplicationController、 Deployment、ReplicaSet 或 StatefulSet 中的 Pod 数量

## HPA通过以下命令,获取到各个节点上POD负载情况
[root@master-1 ~]# kubectl top node
NAME       CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   
master-1   65m          6%     936Mi           24%       
node-1     30m          3%     526Mi           28%       
node-2     30m          3%     1108Mi          58%  
获取到POD负载情况,指标,可以根据我们自定义数量,进行扩缩容

## HPA -- MetricsServer -- Deployment
kubectl scale deployment nginx-dp --replicas=5
kubectl scale deployment nginx-dp --replicas=2

部署 Metrics-server

# 1.下载资源清单
[root@master-1 opt]# wget http://download.wodeyumengouwo.com:8888/kubernetes/metric_server.tgz

# 2.导入镜像
[root@master-1 opt]# for i in node-1 node-2;do scp metrics-server.tar $i:/root;done
[root@node-1 ~]# docker load  < metrics-server.tar 
[root@node-2 ~]# docker load  < metrics-server.tar

# 3.修改资源清单
[root@master-1 opt]# vim components.yaml 
      containers:
      - args:
        - --cert-dir=/tmp
        - --secure-port=4443
        - --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
        - --kubelet-use-node-status-port
        - --kubelet-insecure-tls        // 跳过握手
        image: metrics-server:v0.4.0    // 使用本地镜像

# 4.应用资源清单
[root@master-1 opt]# kubectl apply -f  components.yaml 

# 5.查看节点负载
[root@master-1 opt]# kubectl top node
NAME       CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   
master-1   63m          6%     2214Mi          57%       
node-1     61m          6%     903Mi           48%       
node-2     77m          7%     1001Mi          53%   

生成测试镜像

创建测试首页

cat > index.php << 'EOF'
<?php
 $x = 0.0001;
 for ($i = 0; $i <= 1000000; $i++) {
  $x += sqrt($x);
 }
 echo "OK!";
?>
EOF

创建 dockerfile

cat > dockerfile << 'EOF'
FROM php:5-apache
ADD index.php /var/www/html/index.php
RUN chmod a+rx index.php
EOF

生成镜像

docker build -t php:v1 .

保存镜像

[root@master-1 php]# docker save php:v1 > /tmp/php.tgz
[root@master-1 tmp]# for i in node-1 node-2;do scp /tmp/php.tgz $i:/root/;done
[root@node-1 ~]# docker load < php.tgz 
[root@node-2 ~]# docker load < php.tgz 

创建 Deployment 资源

[root@master-1 php]# vim php-dp.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-apache
spec:
  replicas: 1
  selector:
    matchLabels:
      run: php-apache
  template:
    metadata:
      labels:
        run: php-apache
    spec:
      containers:
      - image: php:v1
        imagePullPolicy: IfNotPresent
        name: php-apache
        resources:
          requests:
            cpu: 200m

[root@master-1 ~]# kubectl apply -f php-dp.yml 
deployment.apps/php-apache created

创建 HPA 资源

[root@master-1 php]# vim php-hpa.yml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: php-apache
spec:
  minReplicas: 1
  maxReplicas: 10
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: php-apache
  targetCPUUtilizationPercentage: 50

压测

# 查看HPA扩容情况
kubectl get hpa -w
kubectl get pod -w
kubectl top node
kubectl top pod

## 简单命令
# 创建dp
kubectl run php-apache --image=php:v1 --requests=cpu=200m --expose --port=80

# 创建hpa
kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10

# 压测
while true; do wget -q -O- http://10.2.1.47; done

# 动态观察hpa(压测时与压测结束限制过程)
[root@master-1 ~]# kubectl get hpa -w
NAME         REFERENCE               TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
php-apache   Deployment/php-apache   <unknown>/50%   1         10        0          0s
php-apache   Deployment/php-apache   0%/50%          1         10        1          15s
php-apache   Deployment/php-apache   232%/50%        1         10        1          61s
php-apache   Deployment/php-apache   232%/50%        1         10        4          86s