目 录CONTENT

文章目录

中间件容器化

简中仙
2023-06-27 / 0 评论 / 0 点赞 / 84 阅读 / 0 字 / 正在检测是否收录...
温馨提示:
本文最后更新于2024-01-14,若内容或图片失效,请留言反馈。 本文如有错误或者侵权的地方,欢迎您批评指正!

这篇学习笔记是基于杜宽老师在51CTO上发布的视频课程制作的。在此,感谢杜宽老师的分享和教学。如有侵权,请及时联系我。版权归原作者所有,未经允许不得转载或使用。

一、中间件单实例部署

各个中间件,比如rabbitmq安装到k8s系统中

1、找到官方镜像:https://hub.docker.com/

2、确认需要的配置:环境变量或配置文件

3、选择部署方式:Deployment或其他的

apiVersion: v1
kind: Service
metadata:
  name: rabbitmq
  namespace: public-service
spec:
  ports:
  - name: web
    port: 5672
    protocol: TCP
    targetPort: 5672
  - name: http
    port: 15672
    protocol: TCP
    targetPort: 15672
  selector:
    app: rabbitmq
  sessionAffinity: None
  type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations: {}
  labels:
    app: rabbitmq
  name: rabbitmq
  namespace: public-service
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rabbitmq
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: rabbitmq
    spec:
      affinity: {}
      containers:
      - env:
        - name: TZ
          value: Asia/Shanghai
        - name: LANG
          value: C.UTF-8
        image: rabbitmq:3.8.17-management
        imagePullPolicy: IfNotPresent
        lifecycle: {}
        livenessProbe:
          failureThreshold: 2
          initialDelaySeconds: 30
          periodSeconds: 10
          successThreshold: 1
          tcpSocket:
            port: 5672
          timeoutSeconds: 2
        name: rabbitmq
        ports:
        - containerPort: 5672
          name: web
          protocol: TCP
        readinessProbe:
          failureThreshold: 2
          initialDelaySeconds: 30
          periodSeconds: 10
          successThreshold: 1
          tcpSocket:
            port: 5672
          timeoutSeconds: 2
        resources:
          limits:
            cpu: 998m
            memory: 1019Mi
          requests:
            cpu: 251m
            memory: 501Mi
      restartPolicy: Always

4、配置访问:TCP或HTTP

二、k8s包管理工具

一句话总结功能就是可以很方便管理一些比较复杂的应用,比如MySQL集群、Redis集群等,可以一键式创建集群、扩容、备份等。常用的两种包管理工具是Operator和Helm。

Helm:更倾向于无状态应用的部署,比如公司的服务、某些不需要持久化数据的中间件、不需要实现额外功能的服务,比如备份、回滚等。

Operator:管理更为复杂的有状态服务,比如MySQL集群、Redis集群、Rook等。并且可以利用Operator实现扩容、备份、回滚等功能

部署方式HelmOperator
安装Helm客户端工具创建Operator控制器
添加Helm仓库创建自定义资源
Helm install一键启动执行相关逻辑

1、使用Operator创建Redis集群

模板:https://github.com/operator-framework/awesome-operators

Redis Cluster Operator: https://github.com/ucloud/redis-cluster-operator

1、创建Operator

git clone https://github.com/ucloud/redis-cluster-operator.git
kubectl create -f deploy/crds/redis.kun_distributedredisclusters_crd.yaml
kubectl create -f deploy/crds/redis.kun_redisclusterbackups_crd.yaml
kubectl create ns redis-cluster
kubectl create -f deploy/service_account.yaml -n redis-cluster
kubectl create -f deploy/namespace/role.yaml -n redis-cluster
kubectl create -f deploy/namespace/role_binding.yaml -n redis-cluster
kubectl create -f deploy/namespace/operator.yaml -n redis-cluster

2、创建Redis集群

Namespace级别的需要更改配置

kubectl create -f deploy/example/redis.kun_v1alpha1_distributedrediscluster_cr.yaml -n redis-cluster

【可选】提示:如果集群规模不大,资源少,可以自定义资源,把请求的资源降低

kubectl create -f deploy/example/custom-resources.yaml -n redis-cluster

3、查看集群状态

kubectl get distributedrediscluster -n redis-cluster

4、扩容Redis集群

grep "master" deploy/example/redis.kun_v1alpha1_distributedrediscluster_cr.yaml
		 masterSize: 4
kubectl replace -f deploy/example/redis.kun_v1alpha1_distributedrediscluster_cr.yaml -n redis-cluster

5、卸载集群

kubectl delete -f deploy/example/redis.kun_v1alpha1_distributedrediscluster_cr.yaml -n redis-cluster
kubectl delete -f deploy/cluster/operator.yaml -n redis-cluster
kubectl delete -f deploy/cluster/cluster_role_binding.yaml -n redis-cluster
kubectl delete -f deploy/cluster/cluster_role.yaml -n redis-cluster
kubectl delete -f deploy/service_account.yaml -n redis-cluster
kubectl delete -f deploy/crds/redis.kun_redisclusterbackups_crd.yaml -n redis-cluster
kubectl delete -f deploy/crds/redis.kun_distributedredisclusters_crd.yaml -n redis-cluster

2、使用Helm创建Kafka、Zookeeper集群

1、helm 客户端安装

Helm 是 Kubernetes 的包管理器。包管理器类似于我们在 Ubuntu 中使用的apt、Centos中使用的yum 或者Python中的 pip 一样,能快速查找、下载和安装软件包。Helm 由客户端组件 helm 和服务端组件 Tiller 组成, 能够将一组K8S资源打包统一管理, 是查找、共享和使用为Kubernetes构建的软件的最佳方式。

文档:https://helm.sh/docs/intro/install/

curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
helm version
# 命令
helm -help

添加bitnami和官方helm仓库

helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo add stable https://charts.helm.sh/stable

安装方式一:先下载后安装

helm pull bitnami/zookeeper

修改values.yaml相应配置:副本数、auth、持久化

helm install -n public-service zookeeper  .

安装方式二:直接安装

helm install kafka bitnami/kafka --set zookeeper.enabled=false --set replicaCount=3 --set externalZookeeper.servers=zookeeper --set persistence.enabled=false -n public-service

2、Helm v3 Chart目录层级解析

创建一个Chart

helm create helm-test
├── charts # 依赖文件
├── Chart.yaml # 当前chart的基本信息
	apiVersion:Chart的apiVersion,目前默认都是v2
	name:Chart的名称
	type:图表的类型[可选]
	version:Chart自己的版本号
	appVersion:Chart内应用的版本号[可选]
	description:Chart描述信息[可选]
├── templates # 模板位置
│   ├── deployment.yaml
│   ├── _helpers.tpl # 自定义的模板或者函数
│   ├── ingress.yaml
│   ├── NOTES.txt #Chart安装完毕后的提醒信息
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests # 测试文件
│       └── test-connection.yaml
└── values.yaml #配置全局变量或者一些参数

判断语法是否正确, 只打印不部署

helm install test --dry-run .

3、Helm内置变量的使用

Release.Name       - 实例的名称,helm install指定的名字。
Release.Namespace  - 应用实例的命名空间
Release.IsUpgrade  - 如果当前对实例的操作是更新或者回滚,这个变量的值就会被置为true
Release.IsInstall  - 如果当前对实例的操作是安装,则这边变量被置为true
Release.Revision   - 此次修订的版本号,从1开始,每次升级回滚都会增加1
Chart			   - Chart.yaml文件中的内容,可以使用Chart.Version表示应用版本,Chart.Name表示Chart的名称

4、Helm常用函数的使用

常用函数:http://masterminds.github.io/sprig/strings.html

5、Helm流程控制

https://helm.sh/zh/docs/chart_template_guide/control_structures/

0

评论区