Kubernetes 是一个开源的容器编排引擎,可用来对容器化应用进行自动化部署、扩缩和管理。本文将会描述如何搭建一个可用于生产环境的高可用集群。
若第一次接触 k8s,建议看完 k8s - 快速入门 再查看此文。
一、高可用集群(etcd)
文档:https://docs.k3s.io/zh/datastore/ha-embedded
1. 部署 Server 节点 1 2 3 4 5 6 7 curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -s - server \ --disable ="traefik" \ --embedded-registry \ --cluster-cidr="10.0.0.0/8" \ --service-cidr="172.16.0.0/12" \ --cluster-dns="172.16.0.10" \ --cluster-init
–disable=traefik 禁用默认traefik控制器
–embedded-registry 启动集群内部镜像共享
–cluster-cidr 增加集群 CIDR 的子网大小,以免耗尽 Pod 的 IP 空间
–cluster-domain 集群域名,默认cluster.local,内部pod可以通过..svc.cluster.local相互访问 更多内容可关注官方文档:https://docs.k3s.io/zh/quick-start
2.查看TOKEN 1 sudo cat /var/lib/rancher/k3s/server/node-token
3. 添加更多 Server 节点
etcd高可用建议奇数个节点
1 2 3 4 5 6 7 8 export K3S_TOKEN={替换为上一步查看到的token}curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -s - server \ --disable ="traefik" \ --embedded-registry \ --cluster-cidr="10.0.0.0/8" \ --service-cidr="172.16.0.0/12" \ --cluster-dns="172.16.0.10" \ --server https://k3s-master:6443
4. 查看节点 1 2 3 4 5 $ sudo kubectl get nodes NAME STATUS ROLES AGE VERSION node1 Ready control-plane,etcd,master 14m v1.29.6+k3s2 node2 Ready control-plane,etcd,master 12m v1.29.6+k3s2 node3 Ready control-plane,etcd,master 10m v1.29.6+k3s2
二、高可用集群(数据库)
文档:https://docs.k3s.io/zh/datastore/ha
1. 准备数据库 需要准备一台数据库服务器,例如购买服务器并搭建好 PostgreSQL、MySQL 等数据库。
1 2 3 4 postgres://username:password@hostname:5432/database-name mysql://username:password@tcp(hostname:3306)/database-name
2. 部署 Server 节点 执行命令即可,需要注意的是高可用集群节点选举较慢,若 kubectl get nodes
查不到任何资源,可以等待一段时间再试试。
1 2 3 4 5 6 7 curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -s - server \ --disable ="traefik" \ --embedded-registry \ --cluster-cidr="10.0.0.0/8" \ --service-cidr="172.16.0.0/12" \ --cluster-dns="172.16.0.10" \ --datastore-endpoint="postgres://username:password@hostname:5432/database-name"
3. 添加更多 Server 节点
只需要末尾多出指定 K3S_TOKEN 即可 K3S_TOKEN 使用的值存储在 Server 节点上的 /var/lib/rancher/k3s/server/token
中,可使用 cat
查看。
1 2 3 4 5 6 7 8 export K3S_TOKEN={替换为查看到的token} curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -s - server \ --disable="traefik" \ --embedded-registry \ --cluster-cidr="10.0.0.0/8" \ --service-cidr="172.16.0.0/12" \ --cluster-dns="172.16.0.10" \ --datastore-endpoint="postgres://username:password@hostname:5432/database-name"
4. 添加 Agent 节点 (可选) K3s Server 节点默认是可调度的,所以 HA K3s 集群不需要 Agent 节点。
但是你仍然想用使用专门的 Agent 节点来运行应用程序和服务的话,可以根据 k3s.io/添加Agent节点 进行操作,本文不再赘述。
5. 查看节点 1 2 3 4 $ sudo kubectl get nodes NAME STATUS ROLES AGE VERSION node1 Ready control-plane,master 24h v1.29.6+k3s2 node2 Ready control-plane,master 23h v1.29.6+k3s2
三、Deployment 资源限制 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 apiVersion: v1 kind: Namespace metadata: name: whoami --- kind: Deployment apiVersion: apps/v1 metadata: name: whoami namespace: whoami labels: app: whoami spec: replicas: 3 revisionHistoryLimit: 1 selector: matchLabels: app: whoami template: metadata: labels: app: whoami spec: containers: - name: whoami image: traefik/whoami:latest resources: requests: cpu: 10m memory: 6Mi limits: cpu: 100m memory: 64Mi env: - name: A value: theAValue - name: B value: theBValue imagePullPolicy: Always ports: - name: whoami-port containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: whoami namespace: whoami spec: ports: - name: whoami-port port: 80 targetPort: whoami-port selector: app: whoami
四、动态扩缩容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: whoami-autoscaler spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: whoami minReplicas: 1 maxReplicas: 10 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 500 - type: Resource resource: name: memory target: type: Utilization averageUtilization: 700 behavior: scaleDown: policies: - type: Pods value: 1 periodSeconds: 60 scaleUp: policies: - type: Pods value: 1 periodSeconds: 60