ACK集群应用部署进阶
1. 了解实验架构
客户端通过 ingress 的入口 slb 访问,slb 将请求转发到 nginx-ingress-controller,然后 nginx-ingress-controller 将代理请求发到后端 pod;同时,hpa 来控制台 pod 数量的变化。
2. 环境准备
注:后台已创建好了对应的云产品资源,这里仅核实环境和相关配置。
1. 证书下载地址 :http://xiniao-ceshi.oss-cn-beijing.aliyuncs.com/4211665_xiniao.aliyuntest.xyz_nginx.zip
2. 使用ack里面的Cloud Shell进行连接以配置管理集群。
3.确认集群具有公网能力,保证可以拉去实验镜像。
3. 创建 deploy 文件
1. 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
2. 使用命令创建 deploy: kubectl create -f myweb.yaml
4. 创建 service 暴露服务
1. 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
2. 执行:kubectl create -f myservice.yaml
5. 创建 secret 文件
1、在CloudShell上下载证书,并将证书文件解压
wget http://xiniao-ceshi.oss-cn-beijing.aliyuncs.com/4211665_xiniao.aliyuntest.xyz_nginx.zip
unzip 4211665_xiniao.aliyuntest.xyz_nginx.zip
2. 执行命令:
kubectl create secret tls mysecret --key 4211665_xiniao.aliyuntest.xyz.key --cert
4211665_xiniao.aliyuntest.xyz.pem
6. 创建 Ingress 规则
1. 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
2. 创建 Ingress: kubectl create -f Ingress.yaml
7. 在 hosts 里绑定域名和 IP
1. kubectl get ingress httpbin 得到 ingress 的域名和 ip(域名在“HOSTS”字段下, ip 在 “ADDRESS”字段下) 2、编辑客户端(这里可以用个人的windows-pc)的C:\Windows\System32\drivers\etc\hosts 文件,添加一行:
121.196.13.XX xiniao.aliyuntest.xyz
8. 验证网站访问
1. 浏览器访问测试: https://xiniao.aliyuntest.xyz/delay/1 能正常显示页面内容返回结果。
9. 亲和性调度
任务:将 httpbin 这个 deployment 尽可能调度到与 redis 的 pod 相同的节点。
1. 创建 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
执行 kubectl create -f redis.yaml 创建。
2. 编辑 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 在同一个节点:
10. 实现 httpbin 水平伸缩
1. 创建 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
2. 执行 kubectl create -f httbpin-hpa.yaml,创建HPA, 然后使用,kubectl get hpa 查看 HPA:
3、使用jmeter进行压测(JMeter下载安装及入门教程,或者可选使用阿里云pts压测)。
jmeter配置参考附件中的配置文件,xiniaotest.jmx
打开jmeter,导入xiniaotest.jmx配置(配置下载地址):
修改 http 请求,,输入要访问的域名和端口,参考:
4. 点击“start”按钮运行 jmeter 压测:
5. 再次查看 hpa:
可以看到,通过水平伸缩,副本数已经变成 5 了。但此时 targets 还很大,因为此时达到了 hpa 设定的最大副本数限制。
实验链接:https://developer.aliyun.com/adc/scenario/47fd1ae45fa347d893834b9a9274de2c