整体实验架构如下
客户端通过 ingress 的入口 slb 访问,slb 将请求转发到 nginx-ingress-controller,然后 nginx-ingress-controller 将代理请求发到后端 pod;同时,hpa 来控制台 pod 数量的变化。
1.环境准备
证书下载地址 :> http://xiniao-ceshi.oss-cn-beijing.aliyuncs.com/4211665_xiniao.aliyuntest.xyz_nginx.zip
ping百度,测试是否连通。
2.创建 deploy 文件
touch myweb.yaml
编辑内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpbin
spec:
replicas: 1
selector:
matchLabels:
app: httpbin
template:
metadata:
labels:
app: httpbin
spec:
containers:
- image: 'docker.io/kennethreitz/httpbin:latest'
imagePullPolicy: IfNotPresent
env:
# 该env 会指定 采集该Pod 标准输出和标准出错的内容
# 到名为 log-stdout 的 logstore 中,配置参考
- name: aliyun_logs_log-stdout
value: stdout
name: httpbin
ports:
- containerPort: 80
protocol: TCP
resources:
limits:
cpu: 500m
memory: 100Mi
requests:
cpu: 250m
memory: 32Mi
使用命令创建 deploy
3.创建 service 暴露服务
touch myservice.yaml
编辑内容如下:
apiVersion: v1
kind: Service
metadata:
name: myweb
namespace: default
spec:
ports:
- name: http80
port: 80
protocol: TCP
targetPort: 80
selector:
app: httpbin
type: ClusterIP
4.创建 secret 文件
在CloudShell上下载证书,并将证书文件解压
wget http://xiniao-ceshi.oss-cn-beijing.aliyuncs.com/4211665_xiniao.aliyuntest.xyz_nginx.zip
unzip 4211665_xiniao.aliyuntest.xyz_nginx.zip
执行如下命令:(此处为一行,是生成秘钥的固定语法,?为脱敏信息,按照自己的文件名称填写即可)
kubectl create secret tls mysecret --key ???????_xiniao.aliyuntest.xyz.key --cert ???????_xiniao.aliyuntest.xyz.pem
5.创建 Ingress 规则
touch Ingress.yaml
编辑内容如下:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: httpbin
namespace: default
spec:
ingressClassName: nginx
rules:
- host: xiniao.aliyuntest.xyz
http:
paths:
- backend:
serviceName: myweb
servicePort: 80
path: /
tls:
- hosts:
- xiniao.aliyuntest.xyz
secretName: mysecret
创建 Ingress:
kubectl create -f Ingress.yaml
6.在 hosts 里绑定域名和 IP
kubectl get ingress httpbin 得到 ingress 的域名和 ip(域名在“HOSTS”字段下, ip 在 “ADDRESS”字段下)
编辑客户端(这里可以用个人的windows-pc)的C:\Windows\System32\drivers\etc\hosts 文件,添加一行:
47.114.175.34 xiniao.aliyuntest.xyz
7.亲和性调度
任务:将 httpbin 这个 deployment 尽可能调度到与 redis 的 pod 相同的节点。
创建 redis 的 deployment。
touch redis.yaml
文件内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: redis
name: redis
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- image: 'redis:latest'
imagePullPolicy: IfNotPresent
name: redis
resources:
requests:
cpu: 250m
memory: 512Mi
编辑 httpbin 这个 deployment:
kubectl edit deployment httpbin
添加如下字段:
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpbin
spec:
replicas: 1
selector:
matchLabels:
app: httpbin
template:
metadata:
labels:
app: httpbin
spec:
# 添加亲和性配置
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- redis
topologyKey: kubernetes.io/hostname
# 结束添加
containers:
- image: 'docker.io/kennethreitz/httpbin:latest'
imagePullPolicy: IfNotPresent
env:
- name: aliyun_logs_log-stdout
value: stdout
name: httpbin
ports:
- containerPort: 80
protocol: TCP
resources:
limits:
cpu: 500m
memory: 100Mi
requests:
cpu: 250m
memory: 32Mi
保存后,验证 redis 和 httpbin 的 pod 在同一个节点:
8.实现 httpbin 水平伸缩
创建 hpa:
touch httpbin-hpa.yaml
输入以下内容:
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: httpbin-hpa
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: httpbin
minReplicas: 1
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 80
执行
kubectl create -f httpbin-hpa.yaml
创建HPA, 然后使用
kubectl get hpa
查看 HPA:
使用jmeter进行压测(JMeter下载安装及入门教程https://blog.csdn.net/wust_lh/article/details/86095924?spm=a2c6h.13858378.0.0.8d751575cbYbFF,或者可选使用阿里云pts压测)
jmeter配置参考附件中的配置文件,xiniaotest.jmx
打开jmeter,导入xiniaotest.jmx配置
修改 http 请求,,输入要访问的域名和端口,参考:
点击“start”按钮运行 jmeter 压测:
再次查看 hpa:
可以看到,通过水平伸缩,副本数已经变成 5 了。但此时 targets 还很大,因为此时达到了 hpa 设定的最大副本数限制。