分钟级搭建本地开发环境
前置说明
搭建方式
Kind (本篇选择)
- Rancher Desktop
《无限水平扩容服务之 app 和 串联 Higress 三合一云原生网关》,
准备好 Docker Desktop 环境,按提供的下载链接 Kind,Helm,Kubectl 下载并配置好环境变量。
搭建过程
定义好 常用软件端口号映射(NodePort服务类型),不定义则可通过 kubectl port-forward ... 进行端口转发
端口转发方式,若关闭了命令执行窗口,得重新执行命令。
cluster.yml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "ingress-ready=true"
extraPortMappings:
- {containerPort: 80, hostPort: 80} # Higress
- {containerPort: 443, hostPort: 443}
- {containerPort: 30001, hostPort: 2181} # Zookeeper
- {containerPort: 30002, hostPort: 3306} # MySQL Primary
- {containerPort: 30003, hostPort: 3307} # MySQL Secondary
- {containerPort: 30004, hostPort: 5432} # PostgreSQL
- {containerPort: 30005, hostPort: 6379} # Redis
- {containerPort: 30006, hostPort: 8123} # ClickHouse
- {containerPort: 30007, hostPort: 9000} # MinIO API
- {containerPort: 30008, hostPort: 9001} # MinIO Console
- {containerPort: 30009, hostPort: 9092} # Kafka
- {containerPort: 30010, hostPort: 9200} # ElasticSearch
- {containerPort: 30011, hostPort: 27017} # MongoDB
kind create cluster --config=cluster.yml
kubectl config use-context kind-kind
添加 Helm charts 仓库
helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
helm repo list
Helm 软件搜索
https://artifacthub.io/packages/search
helm search repo xxx
Helm 安装 Redis,Zookeeper,MySQL主从
Windows 10 换行符为 `, Linux 换行符为 \, 服务类型指定为 NodePort
不指定默认为 ClusterIP, 则需要端口转发。通过 kubectl patch service ... 更改服务类型参数。
helm install my-redis bitnami/redis `
--set architecture=standalone `
--set global.redis.password=Aa123456 `
--set master.persistence.size=512Mi `
--set master.service.type=NodePort `
--set master.service.nodePorts.redis=30005
helm install my-zookeeper bitnami/zookeeper `
--set service.type=NodePort `
--set service.nodePorts.client=30001 `
--set replicaCount=1 `
--set persistence.size=512Mi
helm install my-mysql-cluster bitnami/mysql `
--set architecture=replication `
--set secondary.replicaCount=1 `
--set primary.service.type=NodePort `
--set secondary.service.type=NodePort `
--set primary.service.nodePorts.mysql=30002 `
--set secondary.service.nodePorts.mysql=30003 `
--set auth.rootPassword=Aa123456 `
--set primary.persistence.size=512Mi --set secondary.persistence.size=512Mi
查看状态
PS C:\Users\Jazz\Desktop> kubectl get all
NAME READY STATUS RESTARTS AGE
pod/my-mysql-cluster-primary-0 1/1 Running 0 103m
pod/my-mysql-cluster-secondary-0 1/1 Running 0 103m
pod/my-redis-master-0 1/1 Running 1 (89m ago) 103m
pod/my-zookeeper-0 1/1 Running 1 (89m ago) 103m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 105m
service/my-mysql-cluster-primary NodePort 10.96.25.228 <none> 3306:30002/TCP 103m
service/my-mysql-cluster-primary-headless ClusterIP None <none> 3306/TCP 103m
service/my-mysql-cluster-secondary NodePort 10.96.151.166 <none> 3306:30003/TCP 103m
service/my-mysql-cluster-secondary-headless ClusterIP None <none> 3306/TCP 103m
service/my-redis-headless ClusterIP None <none> 6379/TCP 103m
service/my-redis-master NodePort 10.96.209.202 <none> 6379:30005/TCP 103m
service/my-zookeeper NodePort 10.96.54.72 <none> 2181:30001/TCP,2888:32042/TCP,3888:32703/TCP 103m
service/my-zookeeper-headless ClusterIP None <none> 2181/TCP,2888/TCP,3888/TCP 103m
NAME READY AGE
statefulset.apps/my-mysql-cluster-primary 1/1 103m
statefulset.apps/my-mysql-cluster-secondary 1/1 103m
statefulset.apps/my-redis-master 1/1 103m
statefulset.apps/my-zookeeper 1/1 103m
PS C:\Users\Jazz\Desktop> helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
my-mysql-cluster default 1 2023-05-16 16:44:30.296044 +0800 CST deployed mysql-9.9.1 8.0.33
my-redis default 1 2023-05-16 16:44:12.3578441 +0800 CST deployed redis-17.10.3 7.0.11
my-zookeeper default 1 2023-05-16 16:43:55.5003718 +0800 CST deployed zookeeper-11.3.2 3.8.1
删除集群
kind get clusters
kind delete cluster xxx
kind delete clusters --all
其他软件,搜索 Helm 仓库,查看参数说明自行安装。