K8S学习笔记(4)-增加node节点

版权声明 本站原创文章 由 萌叔 发表 转载请注明 萌叔 | http://vearne.cc 1. 前言 警告:本文仅用于萌叔自己总结之用,对其它人而言可能毫无营养,没有阅读价值。 本文使用的安装工具为easzlab/kubeasz 2. 步骤 2.1 修改docker目录 通过软连接的方式修改docker 数据存储路径 ansible new-node -i hosts -m shell -a 'mkdir /data/docker' ansible new-node -i hosts -m shell -a 'cd /var/lib/ && ln -s /data/docker docker' 2.2 安装gluster fs依赖 如果pod需要用到gluster fs才需要执行此步骤 ansible new-node -i hosts -m shell -a 'yum install centos-release-gluster glusterfs-fuse -y' 2.3 安装ntpd确保时钟同步 # 安装ntd ansible new-node -i hosts -m shell -a 'yum -y install ntp' # 设置开机自启动 ansible new-node -i hosts -m shell -a 'systemctl enable ntpd' # 启动ntpd服务 ansible new-node -i hosts -m shell -a 'systemctl start ntpd' # 手动执行一次同步 ansible new-node -i hosts -m shell -a 'ntpdate -u 0.centos.pool.ntp.org' 2.4 添加新节点 easzctl add-node 192.168.1.11 新增kube-node节点大致流程为:tools/02.addnode.yml 见参考资料1 ...

December 24, 2020 · 1 min

聊聊k8s调试工具kt-connect的实现

版权声明 本站原创文章 由 萌叔 发表 转载请注明 萌叔 | http://vearne.cc 1. 引言 kt-connect是阿里开源的k8s的调试工具,它的作用类似于VPN,能够打通k8s集群和本地的网络。 传送门: alibaba/kt-connect 它有3种模式 Connect 本地网络直接访问k8s集群网络 Exchange 转发集群流量到本地 Service Mesh 支持 另外它提供了一个Dashboard可以查看k8s集群内的所有可访问的service资源以及正在进行调试的Connect和Exchange数量, 用处不大。 2. 使用介绍 这里萌叔只简单介绍Connect和Exchange2种模式,更详细的使用说明见参考资料1 2.1 Connect模式 sudo ktctl -i ik8share/kt-connect-shadow:stable connect 注意: kt-connect 依赖sshuttle, 且运行时必须拥有root权限。另外sshuttle 又依赖了iptables(linux操作系统), ptctl(macOS) -i 参数指定镜像的地址 这里ik8share/kt-connect-shadow:stable是镜像的名字,阿里默认提供的镜像地址rdc-incubator/kt-connect-shadow在萌叔的测试k8s集群中无法正常拉取。这里提供了一个docker hub的镜像地址。 2.2 Exchange模式 sudo ktctl -n test -i ik8share/kt-connect-shadow:stable exchange dm-backend-v0-0-1 --expose 3000 注意: 这里的dm-backend-v0-0-1 是k8s集群中Deployment资源的名称。 该命令会将所有发往dm-backend-v0-0-1所属Pod的3000端口的请求都转发到本地的3000端口上。 总结:Connect和Exchange 模式都是单向的,一个是从集群外部到集群内部,一个是从集群内部到集群外部。 3. 原理和实现 kt-connect设计巧妙,且最大限度的避免了重复发明轮子,值得称赞。 3.1 Connect模式 我们先来看看Connect模式要达到的目标 假定 Namespace: test Service: sv-backend-v0-0-1 Pod的IP: 172.20.1.29 ...

December 1, 2020 · 1 min

分布式任务调度平台xxl-job

版权声明 本站原创文章 由 萌叔 发表 转载请注明 萌叔 | http://vearne.cc 1.前言 xuxueli/xxl-job是一个分布式任务调度平台。它在github上有1w多个star,有多家公司都已经用在生产实践中。 在萌叔看来这是一个"Less is more"的典型。它的设计的非常简单,最大的优点是实用。 2.主要结构和逻辑 XXL-JOB任务调度平台分为2个部分,Scheduler和Executor。具体的实现Scheduler对应是xxl-job-admin,同时xxl-job-admin还配有web UI,可以配置管理任务。 Scheduler和Executor之间通过HTTP API交互,因此Executor可以通过各种语言实现。比如Golang的 xxl-job/xxl-job-executor-go 以上图为例,scheduleThread将任务通过Executor是的/run api推送给Executor { "jobId": 3, "executorHandler": "task.test", "executorParams": "x=100", "executorBlockStrategy": "SERIAL_EXECUTION", "executorTimeout": 0, "logId": 17, "logDateTime": 1606100913829, "glueType": "BEAN", "glueSource": "", "glueUpdatetime": 1606099566000, "broadcastIndex": 0, "broadcastTotal": 1 } Executor会根据executorHandler找到对应的handler,执行完之后,又会调用xxl-job-admin的/xxl-job-admin/api/callback回报任务的执行结果。从上面的描述我们可以知道,xxl-job-admin和excutor都必须暴露出api服务(都是HTTP接口)。 Scheduler可以有多个。它们之间通过MySQL进行同步。 主要的调度逻辑在JobScheduleHelper中 在每一轮执行调度逻辑之前, Scheduler必须先获得行锁 while (!scheduleThreadToStop) { ... // 加行锁 try { preparedStatement = conn.prepareStatement( "select * from xxl_job_lock where lock_name = 'schedule_lock' for update" ); preparedStatement.execute(); ... } catch (Exception e) { ... } finally { ... // 注意:锁必须正常释放 conn.commit(); ... } 由于xxl_job_lock 表中只有一条记录,所以这个逻辑与请求表锁类似,开销是比较大的。 ...

November 23, 2020 · 1 min

istio学习笔记(5)-prometheus配置改造

版权声明 本站原创文章 由 萌叔 发表 转载请注明 萌叔 | http://vearne.cc 1. 前言 警告:本文仅用于萌叔自己总结之用,对其它人而言可能毫无营养,没有阅读价值。 要让一个k8s + istio的集群真正能够在生产可用,我们需要考虑如下几类指标。 Node Metrics Container Resource Metrics Kubernetes API Server Etcd metrics Kube state-metrics 对于准备上集群的服务而言,我们会比较关注 服务所在容器的CPU、内存、网络流量、磁盘使用率等 业务指标:QPS、StatusCode、ErrorCode、请求延迟、缓存使用情况,连接池等 其中很大一部分其实都是标准指标,所有的服务都应该会有。另外istio已经对container的输入和输出流量进行了拦截,基于这些条件。萌叔希望达到如下效果 目标 通过prometheus的自动发现功能,发现并监控Container Resource Metrics 通过prometheus的自动发现功能,发现并监控envoy拦截到的部分指标 3)通过prometheus的自动发现功能,发现并监控app暴露的prometheus metrics 对于标准指标实现在grafana上的自动配置(生成Dashboard和Graph) 显然对于1)2)中的指标都是标准指标 2. 配置 根据参考资料4的说法 spec: template: metadata: annotations: prometheus.io/scrape: true # determines if a pod should be scraped. Set to true to enable scraping. prometheus.io/path: /metrics # determines the path to scrape metrics at. Defaults to /metrics. prometheus.io/port: 80 # determines the port to scrape metrics at. Defaults to 80. 服务本身如果想暴露自己的指标,可以通过在pod上增加注释prometheus.io/scrape prometheus.io/path prometheus.io/port。 但是实际使用中我发现注入了istio之后,这3项的值已经被修改为 ...

November 16, 2020 · 2 min

istio学习笔记(4)-基于istio做灰度发布

版权声明 本站原创文章 由 萌叔 发表 转载请注明 萌叔 | http://vearne.cc 1. 前言 警告:本文仅用于萌叔自己总结之用,对其它人而言可能毫无营养,没有阅读价值。 可以使用istio可以做灰度发布,下面简单记录一下步骤 2. 原理&配置 基于istio做灰度发布需要用到envoy的负载均衡功能。 回想一下我们再nginx中是如何配置负载均衡的 upstream backend { server 192.168.101.10:8080 weight=1; server 192.168.101.12:8080 weight=2; } 2.1 配置 可用通过配置权重来控制流向上游服务的流量 类似的,在istio中需要用到VirtualService apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: vs-backend namespace: test spec: gateways: - gateway-backend-gateway-io.istio-system.svc.cluster.local hosts: - backend.gateway.io http: - retries: {} route: - destination: host: sv-backend.test.svc.cluster.local subset: v0-0-1 weight: 20 - destination: host: sv-backend.test.svc.cluster.local subset: v0-0-2 weight: 80 timeout: 3s The proportion of traffic to be forwarded to the service version. (0-100). Sum of weights across destinations SHOULD BE == 100.If there is only one destination in a rule, the weight value is assumed to be 100. ...

November 5, 2020 · 1 min

ISTIO学习笔记(3)-安装istio

版权声明 本站原创文章 由 萌叔 发表 转载请注明 萌叔 | http://vearne.cc 注意: 本文涉及的服务版本 k8s: v1.18.2 istio: 1.7.3 1. 前言 警告:本文仅用于萌叔自己总结之用,对其它人而言可能毫无营养,没有阅读价值。 最近重新部署了一套k8s + istio的集群,在安装istio遇到了些问题,简单的记录一下。 2. 安装 istio 1.7.3 不再支持helm方式安装。仅支持istioctl和Operator模式进行安装。 istioctl模式 使用命令行的方式与k8s集群进行交互 Operator模式 会启动一个IstioOperator容器(常驻,不退出) 容器,然后用户可以通过kubectl提交配置文件给IstioOperator容器,由IstioOperator容器来完成istio核心模块或者相关插件的安装 2.1 istioctl模式安装 2.1.1 下载 curl -L https://istio.io/downloadIstio | sh - cd istio-1.7.3 samples/ 目录下,有示例应用程序 bin/ 目录下,包含 istioctl 的客户端文件。istioctl 工具用于手动注入 Envoy sidecar 代理。 manifests/profiles 目录中有具体的配置文件 2.1.2 将 istioctl 客户端路径增加到 path 环境变量中 export PATH=$PWD/bin:$PATH 2.1.3 显示可用配置文件的列表 $ istioctl profile list Istio configuration profiles: remote separate default demo empty minimal 2.1.4 安装 不同的配置文件区别在于安装的模块有多有少,建议使用default配置 ...

October 27, 2020 · 1 min

K8S学习笔记(3)-从私有仓库拉取镜像

版权声明 本站原创文章 由 萌叔 发表 转载请注明 萌叔 | http://vearne.cc 1. 前言 警告:本文仅用于萌叔自己总结之用,对其它人而言可能毫无营养,没有阅读价值。 在k8s中要使用自己搭建的私有仓库,还需要一些额外的配置。 萌叔使用的harbor搭建的私有仓库 goharbor/harbor, 假定地址为 https://docker-harbor.vearne.cc 2. 配置过程 要能够成功拉去image,需要解决2个问题 2.1 TLS证书验证 由于我们的私有仓库,使用的自签名的证书,所以需要能够通过TLS的握手阶段对证书。 如果证书认证失败,你可能收到如下错误 [root@xx ~]# docker pull docker-harbor.vearne.cc/ut/helloworld:0.2.6 Error response from daemon: Get https://docker-harbor.vearne.cc/ut/helloworld:0.2.6: x509: certificate signed by unknown authority 一种做法是直接添加CA根证书到操作系统获得信任。但是萌叔尝试后,发现无效。 这里验证有效的做法是,使用dockerd的--insecure-registry参数 2.1.1 修改dockerd的配置文件 默认路径为/etc/docker/daemon.json { "insecure-registries" : ["docker-harbor.vearne.cc"] } 2.1.2 重启dockerd systemctl restart docker 2.2 登录 要想在宿主机拉取镜像,可以用 docker login docker-harbor.vearne.cc 在k8s集群中拉取镜像可以使用 kubectl create secret docker-registry regcred \ --docker-server=<你的镜像仓库服务器> \ --docker-username=<你的用户名> \ --docker-password=<你的密码> \ --docker-email=<你的邮箱地址> 创建一个密钥,用于拉取镜像 检查 Secret regcred kubectl get secret regcred --output="jsonpath={.data.\.dockerconfigjson}" | base64 --decode 输出和下面类似: ...

October 20, 2020 · 1 min

k8s学习笔记(2)-删除namespace失败处理

版权声明 本站原创文章 由 萌叔 发表 转载请注明 萌叔 | http://vearne.cc 1. 前言 警告:本文仅用于萌叔自己总结之用,对其它人而言可能毫无营养,没有阅读价值。 删除namespace出现下面的情况 ╰─$ kubectl get ns NAME STATUS AGE default Active 5d3h ingress-nginx Active 4d22h istio-system Active 4d3h kube-node-lease Active 5d3h kube-public Active 5d3h kube-system Active 5d3h kubernetes-dashboard Active 39m ns-helloworld Terminating 3h56m 2.解决方法 2.1 导出namespace配置 kubectl get namespace ns-helloworld -o json > tmp.json namespace无法删除是因为namespace相关联的资源无法释放。 2.2 清空spec中的内容 2.3 触发 kubectl proxy curl -k -H "Content-Type: application/json" -X PUT --data-binary @tmp.json http://127.0.0.1:8001/api/v1/namespaces/ns-helloworld/finalize 参考资料 kubernetes无法删除namespace 提示 Terminating 请我喝瓶饮料

October 19, 2020 · 1 min

k8s学习笔记(1)-安装dashboard

版权声明 本站原创文章 由 萌叔 发表 转载请注明 萌叔 | http://vearne.cc 1. 前言 警告:本文仅用于萌叔自己总结之用,对其它人而言可能毫无营养,没有阅读价值。 Dashboard 是基于网页的 Kubernetes 用户界面。 你可以使用 Dashboard 将容器应用部署到 Kubernetes 集群中,也可以对容器应用排错,还能管理集群资源。 你可以使用 Dashboard 获取运行在集群中的应用的概览信息,也可以创建或者修改 Kubernetes 资源 (如 Deployment,Job,DaemonSet 等等)。 通过一段时间的使用感受而言,Dashboard虽然做的差强人意,但聊胜于无。 2. 安装&配置 wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.4/aio/deploy/recommended.yaml 建议脚本下载以后,先大致阅读以下 2.1 修改yaml kind: Service apiVersion: v1 metadata: labels: k8s-app: kubernetes-dashboard name: kubernetes-dashboard namespace: kubernetes-dashboard spec: ports: - port: 443 targetPort: 8443 type: NodePort # 改为NodePort方式暴露service selector: k8s-app: kubernetes-dashboard 2.2 安装 kubectl apply -f recommended.yaml 安装完成以后 ╰─$ kubectl get svc -n kubernetes-dashboard NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE dashboard-metrics-scraper ClusterIP 10.68.53.228 <none> 8000/TCP 18h kubernetes-dashboard NodePort 10.68.254.87 <none> 443:37736/TCP 17h 3. 访问web UI 3.1 方式1 通过 kubectl proxy 默认K8S采用Flannel网络的模型的情况下,集群内部和外部网络是不能直接互联的,但是能够通过kubectl proxy作为代理,进行通讯 ...

October 12, 2020 · 2 min

玩转PROMETHEUS(6) 实现自定义的Collector

版权声明 本站原创文章 由 萌叔 发表 转载请注明 萌叔 | http://vearne.cc 1. 前言 prometheus的官方和社区为了我们提供了丰富的exporter。对常见的硬件设备、数据库、消息队列以及常见的软件进行监控。另外官方还为我们提供了4种指标类型方便我们自定义exporter Counter Counter代表累计指标,它表示单调递增的计数器,通常用于表示服务请求数,完成的任务数,错误的数量。 Gauge Gauge表示某种瞬时状态,某一时刻的内存使用率、消息队列中的消息数量等等。它的值既可以增大,也可以减小。 Histogram 通常用于top percentile,比如请求耗时的TP90、TP99等 Summary 类似于Histogram 我们回顾一下prometheus的指标采集的一般过程 1) 创建指标 HTTPReqTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ Name: "http_requests_total", Help: "Total number of HTTP requests made.", }, []string{"method", "path", "status"}) 2) 指标注册到 DefaultRegisterer prometheus.MustRegister( HTTPReqTotal, ) 3) 指标和对应的值通过HTTP API暴露出来 The caller of the Gather method can then expose the gathered metrics in some way. Usually, the metrics are served via HTTP on the /metrics endpoint. ...

August 23, 2020 · 2 min