目录
K8S控制器Controller资源
控制器Controller
控制器作用
1.pod类型的资源,删除pod后,不会重建
2.替用户监视并保证相应的节点上始终有用户所期望的副本数量的pod在运行
3.如果所运行的pod副本数超过了用户期望的,那么控制器就会删掉,直到和用户期望的一致
4.如果所运行的pod副本数低于用户期望的,那么控制器就会创建,直到和用户期望的一致
控制器类型
RC:控制器
RS:ReplicaSet
按用户期望的副本创建pod,并始终保持相应数量副本
Deploment
1)Deployment通过控制RS来保证POD始终保持相应的数量副本
2)支持滚动更新,回滚,回滚默认保留10个版本
3)提供声明式配置,支持动态修改
4)管理无状态应用最理想的控制器
5)node节点可能会运行0个或多个POD
DaemonSet
一个节点只运行一个,必须是始终运行的状态
StatefullSet
有状态的应用
RS控制器
[root@master-1 rc]# cat rc.yaml
apiVersion: "apps/v1"
kind: "ReplicaSet"
metadata:
name: nginx-rs
spec:
replicas: 10
selector:
matchLabels:
app: nginx2
template:
metadata:
name: nginx2
labels:
app: nginx2
spec:
containers:
- image: nginx:alpine
name: nginx2
imagePullPolicy: IfNotPresent
# 以rs名称启动pod,后衔接不同字符
[root@master-1 rc]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-rs-dphvj 1/1 Running 0 76s
nginx-rs-f64vr 1/1 Running 0 76s
nginx-rs-fnlpn 1/1 Running 0 76s
nginx-rs-fwwm2 1/1 Running 0 76s
nginx-rs-h8lcp 1/1 Running 0 76s
nginx-rs-mj7tn 1/1 Running 0 76s
nginx-rs-xgsmp 1/1 Running 0 76s
nginx-rs-xht46 1/1 Running 0 76s
nginx-rs-z5tk7 1/1 Running 0 76s
nginx-rs-zjbjq 1/1 Running 0 76s
[root@master-1 rc]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-rs 10 10 10 4s
[root@master-1 rc]# kubectl get replicasets
NAME DESIRED CURRENT READY AGE
nginx-rs 10 10 10 23s
## rs控制器扩缩容(直接手动更改)
[root@master-1 opt]# kubectl edit rs nginx-rs
....
replicas: 10
# 命令扩容
[root@master-1 opt]# kubectl scale rs nginx-rs --replicas=1
# 更改文件扩容
kubectl apply -f rs.yaml
replicas: 10
# 删除
[root@master-1 rc]# kubectl delete rs nginx-rs
Deployment控制器
有内容更改后,启动一个后(能正常访问,就绪性探针)在停止之前的老版本,但之前的启动器继续保留(默认最大10),可回退
[root@master-1 rc]# cat deploy.yaml
apiVersion: "apps/v1"
kind: "Deployment"
metadata:
name: nginx-dp
spec:
replicas: 6
selector:
matchLabels:
app: nginx2
template:
metadata:
name: nginx2
labels:
app: nginx2
spec:
containers:
- image: nginx:alpine
name: nginx2
imagePullPolicy: IfNotPresent
[root@master-1 rc]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-dp-76889b99f9-2c2qq 1/1 Running 0 8s
nginx-dp-76889b99f9-6bk87 1/1 Running 0 8s
nginx-dp-76889b99f9-88dfj 1/1 Running 0 8s
nginx-dp-76889b99f9-b5jv2 1/1 Running 0 8s
nginx-dp-76889b99f9-j776c 1/1 Running 0 8s
nginx-dp-76889b99f9-s77xd 1/1 Running 0 8s
[root@master-1 rc]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-dp 6/6 6 6 90s
[root@master-1 rc]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-dp 6/6 6 6 102s
# 更改文件扩容
kubectl apply -f deployment.yaml
replicas: 6
# 删除
[root@master-1 rc]# kubectl delete deploy nginx-dp
版本更新
# 更新镜像版本
[root@master-1 rc]# kubectl set image -f deploy.yaml nginx2=nginx:1.16.0
## 查看滚动更新状态
[root@master-1 rc]# kubectl rollout status deployment nginx-dp
## 回滚到上一个版本
[root@master-1 rc]# kubectl rollout undo deployment nginx-dp
## 查看历史版本(为什么没有1,因为现在版本就是1)
[root@master-1 rc]# kubectl rollout history deployment nginx-dp
deployment.apps/nginx-dp
REVISION CHANGE-CAUSE
2 <none>
3 <none>
## 查看指定历史版本的详细信息
[root@master-1 rc]# kubectl rollout history deployment nginx-dp --revision=2
## 改版时,加上记录(nginx2=nginx:1.14会覆盖文中的,镜像拉的就是这个1.14版本的,不是随便写的)
[root@master-1 rc]# kubectl set image -f deploy.yaml nginx2=nginx:1.14 --record
## 回滚到指定版本
[root@master-1 opt]# kubectl rollout undo deployment nginx-dp --to-revision=2
扩缩容
# 副本数扩展为 5 个
kubectl scale deployment nginx-dp --replicas=5
kubectl scale deployment nginx-dp --replicas=2
DaemonSet
简单来说就是每个节点部署一个POD副本
常见的应用场景:
监控容器
日志收集容器
[root@master-1 rc]# cat daemonset.yaml
apiVersion: "apps/v1"
kind: "DaemonSet"
metadata:
name: nginx-ds
spec:
selector:
matchLabels:
app: nginx2
template:
metadata:
name: nginx2
labels:
app: nginx2
spec:
containers:
- image: nginx:alpine
name: nginx2
imagePullPolicy: IfNotPresent
[root@master-1 rc]# kubectl get ds
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
nginx-ds 2 2 2 2 2 <none> 35s
# 查看具体信息
[root@master-1 rc]# kubectl describe ds nginx-ds
Name: nginx-ds
Selector: app=nginx2
Node-Selector: <none>
Labels: <none>
Annotations: deprecated.daemonset.template.generation: 1
Desired Number of Nodes Scheduled: 2
Current Number of Nodes Scheduled: 2
Number of Nodes Scheduled with Up-to-date Pods: 2
Number of Nodes Scheduled with Available Pods: 2
Number of Nodes Misscheduled: 0
Pods Status: 2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=nginx2
Containers:
nginx2:
Image: nginx:alpine
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 74s daemonset-controller Created pod: nginx-ds-z5gwz
Normal SuccessfulCreate 74s daemonset-controller Created pod: nginx-ds-mrlbw
HPA
官网地址
https://kubernetes.io/zh-cn/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/
工作原理
HAP通过收集来的监控指标分析所有Pod的负载情况,并且根据我们设定好的标准来自动扩容收缩ReplicationController、 Deployment、ReplicaSet 或 StatefulSet 中的 Pod 数量
## HPA通过以下命令,获取到各个节点上POD负载情况(下面这个情况是还没有安装)
[root@master-1 opt]# kubectl top node
error: Metrics API not available
获取到POD负载情况,指标,可以根据我们自定义数量,进行扩缩容
## HPA -- MetricsServer -- Deployment
kubectl scale deployment nginx-dp --replicas=5
kubectl scale deployment nginx-dp --replicas=2
部署Metric-server
# 1.下载资源清单
[root@master-1 opt]# wget https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.4.0/components.yaml
# 2.下载镜像,导入镜像(不下也可以,镜像在资源清单里,image:)
[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.修改资源清单
- --kubelet-insecure-tls // 跳过握手
image: metrics-server:v0.4.0 // 使用本地镜像
# 4.应用资源清单
[root@master-1 opt]# kubectl apply -f components.yaml
# 5.查看节点负载
[root@master-1 ~]# kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
master-1 55m 5% 2095Mi 54%
node-1 29m 2% 659Mi 35%
node-2 34m 3% 664Mi 35%
生成测试镜像
1)创建测试首页
cat > index.php << 'EOF'
<?php
$x = 0.0001;
for ($i = 0; $i <= 1000000; $i++) {
$x += sqrt($x);
}
echo "OK!";
?>
EOF
2)创建dockerfile
cat > dockerfile << 'EOF'
FROM php:5-apache
ADD index.php /var/www/html/index.php
RUN chmod a+rx index.php
EOF
3)生成镜像
docker build -t php:v1 .
4)保存镜像 & 导入镜像
[root@master-1 php-apache]# docker save php:v1 > /tmp/php.tgz
[root@master-1 php-apache]# 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
5)启动Deployment资源
[root@master-1 hpa]# cat php-dp.yaml
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
5.1) 启动hpa资源
[root@master-1 hpa]# cat hpa-php.yaml
apiVersion: "autoscaling/v1"
kind: "HorizontalPodAutoscaler"
metadata:
name: php-apache
namespace: default
spec:
maxReplicas: 10
minReplicas: 1
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
targetCPUUtilizationPercentage: 50
6)压测
##查看HPA扩所容情况
kubectl get hpa -w
kubectl get pod -w
kubectl top node
kubectl top pod
# 压测
while true; do wget -q -O- http://10.2.1.47; done
# 创建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
wordpress压测
## 压测工具siege
[root@master-1 ai]# yum install -y siege
# 50个并发,持续1分钟
[root@master-1 ai]# siege -c 50 -t 1m http://10.0.0.111:31963/wp-admin/install.php
# 查看每个服务的cpu占用(解决的疑问点:新启动的服务是不是缓解服务压力了)
[root@master-1 ai]# kubectl top pod
NAME CPU(cores) MEMORY(bytes)
mysql-5bf4b6549b-lc87m 77m 223Mi
wordpress-8549676bc6-26lxj 29m 99Mi
wordpress-8549676bc6-gkwvr 30m 110Mi
wordpress-8549676bc6-nzwwk 24m 95Mi
wordpress-8549676bc6-pp9w6 45m 125Mi
wordpress-8549676bc6-swgx8 28m 107Mi
wordpress-8549676bc6-xwtzl 29m 118Mi
wordpress-8549676bc6-zt67p 36m 115Mi
标签:kubectl,控制器,rs,nginx,Controller,master,K8S,root,dp
From: https://www.cnblogs.com/xiutai/p/17749245.html