文章目录
一、HPA
1、运行 php-apache 服务器并暴露服务
vim hpa1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: php-apache
spec:
selector:
matchLabels:
run: php-apache
replicas: 1
template:
metadata:
labels:
run: php-apache
spec:
containers:
- name: php-apache
image: hpa-example
ports:
- containerPort: 80
resources:
limits:
cpu: 500m
requests:
cpu: 200m
---
apiVersion: v1
kind: Service
metadata:
name: php-apache
labels:
run: php-apache
spec:
ports:
- port: 80
selector:
run: php-apache
kubectl get pod
kubectl get svc
curl 10.103.173.42
会得到ok
2、创建 Horizontal Pod Autoscale
php-apache 服务器已经运行,我们将通过 kubectl autoscale 命令创建 Horizontal Pod
Autoscaler。 以下命令将创建一个 Horizontal Pod Autoscaler 用于控制我们上一步骤中创建的
Deployment,使 Pod 的副本数量维持在 1 到 10 之间。 大致来说,HPA 将(通过 Deployment)增加或者减少
Pod 副本的数量以保持所有 Pod 的平均 CPU 利用率在 50% 左右(由于每个 Pod 请求 200 毫核的 CPU,这意味着平均
CPU 用量为 100 毫核)。
kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10
kubectl get hpa #查看 Autoscaler 的状态
3、增加负载
我们将启动一个容器,并通过一个循环向 php-apache 服务器发送无限的查询请求
kubectl run -i --tty load-generator --rm --image=busybox --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://php-apache; done"
CPU 负载升高,达到了223%,Deployment 的副本数量已经增长到了 5
4、停止负载
在创建 busybox 容器的终端中,输入 + C 来终止负载的产生
二、Helm
Helm是Kubernetes 应用的包管理工具,主要用来管理 Charts,类似Linux系统的yum。
Helm Chart 是用来封装 Kubernetes 原生应用程序的一系列 YAML 文件。可以在你部署应用的时候自定义应用程序的一些
Metadata,以便于应用程序的分发。对于应用发布者而言,可以通过 Helm 打包应用、管理应用依赖关系、管理应用版本并发布应用到软件仓库。
对于使用者而言,使用 Helm 后不用需要编写复杂的应用部署文件,可以以简单的方式在 Kubernetes
上查找、安装、升级、回滚、卸载应用程序。
1、Helm安装
Helm官网:https://helm.sh/docs/intro/
每个Helm 版本都提供了各种操作系统的二进制版本,这些版本可以手动下载和安装。
下载 需要的版本https://github.com/helm/helm/releases
解压(tar -zxvf helm-v3.x.x-linux-amd64.tar.gz)
在解压目中找到helm程序,移动到需要的目录中(mv linux-amd64/helm /usr/local/bin/helm
然后启动补齐
echo "source <(helm completion bash)" >> ~/.bashrc
source ~/.bashrc
搜索官方helm hub chart库并安装
helm search hub chartname
helm repo add bitnami https://charts.bitnami.com/bitnami
helm install my-release bitnami/nginx
helm repo list
helm search repo 从你添加(使用 helm repo add)到本地 helm 客户端中的仓库中进行查找。该命令基于本地数据进行搜索,无需连接互联网。
2、创建chart库
构建一个 Helm Chart
将目录打包
在harbor创建本地chart仓库
安装push插件
helm env
mkdir ~/.local/share/helm/plugins/push
tar zxf helm-push_0.8.1_linux_amd64.tar.gz -C ~/.local/shar/helm/plugins/push
helm push --help
helm repo add westos https://reg.westos.org/chartrepo/charts
Error: looks like "https://reg.westos.org/chartrepo/charts" is not a valid chart repository or cannot be reached: Get "https://reg.westos.org/chartrepo/charts/index.yaml": x509: certificate signed by unknown authority
将认证文件 /etc/docker/certs.d/reg.westos.org/ca.crt 复制到 /etc/pki/ca-trust/source/anchors/,执行更新命令 update-ca-trust,这是之前做harbor仓库时候弄的认证
helm push mychart-0.1.0.tgz westos --insecure -u admin -p westos
然后去查看harbor仓库