Kubernetes 开发环境使用 Helm Charts 快速安装开源软件

Helm

Ubuntu 安装: sudo snap install helm --classic

Helm Charts

Helm Chart 是用来封装 Kubernetes 原生应用程序的一系列 YAML 文件

可以在你部署应用的时候自定义应用程序的一些 Metadata,以便于应用程序的分发

开源社区提供了 Helm Chart Hub ,方便共享 Helm Chart

网址: https://artifacthub.io/

实例:安装 Redis 主从

在网站 https://artifacthub.io/ 搜索 Redis

找到 https://artifacthub.io/packages/helm/bitnami/redis

文中有说明使用命令: helm install my-release oci://registry-1.docker.io/bitnamicharts/redis

执行以上命令即可

然后本地 K8s po 会 pending 在:

❯ kubectl get pvc
NAME                                     STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
redis-data-my-release-redis-master-0     Pending                                                     <unset>                 7m48s
redis-data-my-release-redis-replicas-0   Pending                                                     <unset>                 7m48s
redis-data-my-release-redis-replicas-1   Pending                                                     <unset>                 7m48s
redis-data-my-release-redis-replicas-2   Pending                                                     <unset>                 7m48s

这是 helm 安装,唯一需要自己处理的地方,要给 Redis 分配挂接目录的资源

可以编写 yaml :

apiVersion: v1
kind: PersistentVolume
metadata:
  name: redis-data-my-release-redis-master-pv-0
spec:
  capacity:
    storage: 8Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /data/redis-data-my-release-redis-master-data-0

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: redis-data-my-release-redis-replicas-pv-0
spec:
  capacity:
    storage: 8Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /data/redis-data-my-release-redis-replicas-data-0

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: redis-data-my-release-redis-replicas-pv-1
spec:
  capacity:
    storage: 8Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /data/redis-data-my-release-redis-replicas-data-1

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: redis-data-my-release-redis-replicas-pv-2
spec:
  capacity:
    storage: 8Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /data/redis-data-my-release-redis-replicas-data-2

以下目录,可以实现建好,并有写权限

  • /data/redis-data-my-release-redis-master-data-0
  • /data/redis-data-my-release-redis-replicas-data-0
  • /data/redis-data-my-release-redis-replicas-data-1
  • /data/redis-data-my-release-redis-replicas-data-2

会自动绑定资源。再次查看 pvc :

❯ kubectl get pvc
NAME                                     STATUS   VOLUME                                      CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
redis-data-my-release-redis-master-0     Bound    redis-data-my-release-redis-master-pv-0     8Gi        RWO                           <unset>                 52m
redis-data-my-release-redis-replicas-0   Bound    redis-data-my-release-redis-replicas-pv-0   8Gi        RWO                           <unset>                 52m
redis-data-my-release-redis-replicas-1   Bound    redis-data-my-release-redis-replicas-pv-1   8Gi        RWO                           <unset>                 19m
redis-data-my-release-redis-replicas-2   Bound    redis-data-my-release-redis-replicas-pv-2   8Gi        RWO                           <unset>                 2m41s

再查看 pod :

❯ kubectl get po
NAME                          READY   STATUS    RESTARTS        AGE
my-release-redis-master-0     1/1     Running   5 (20m ago)     51m
my-release-redis-replicas-0   1/1     Running   5 (20m ago)     51m
my-release-redis-replicas-1   1/1     Running   3 (2m48s ago)   18m
my-release-redis-replicas-2   1/1     Running   0               2m6s

修改默认 Helm Charts 配置

上面 Redis 建立了一个 1 master 3 slave

预期想要 1 master 2 slave 怎么办?

先获取这个 Helm Charts 的 values.yaml 的格式:

helm show values oci://registry-1.docker.io/bitnamicharts/redis > redis-values.yaml

查看 redis-values.yaml ,找 replicas 相关配置 ( line: 593 ):

replica:
  ## @param replica.kind Use either DaemonSet or StatefulSet (default)
  ## ref: https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/
  ##
  kind: StatefulSet
  ## @param replica.replicaCount Number of Redis® replicas to deploy
  ##
  replicaCount: 3

默认值是 3 。那么重新编写 redis-values.yaml 内容为:

replica:
  replicaCount: 2

然后先删掉之前安装的(安装阶段随意,不需要考虑数据安全等问题)

helm uninstall my-release

再执行:

helm install my-release oci://registry-1.docker.io/bitnamicharts/redis -f redis-values.yaml

查看 pod :

❯ kubectl get pod
NAME                          READY   STATUS    RESTARTS   AGE
my-release-redis-master-0     1/1     Running   0          3m19s
my-release-redis-replicas-0   1/1     Running   0          3m19s
my-release-redis-replicas-1   1/1     Running   0          2m43s

实例:安装 Redis 集群

在网站 https://artifacthub.io/ 搜索Redis Cluster

找到 https://artifacthub.io/packages/helm/bitnami/redis-cluster

  1. 编写 redis-pv.yaml
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: redis-data-my-release-redis-cluster-pv-0
    spec:
      capacity:
        storage: 8Gi
      volumeMode: Filesystem
      accessModes:
        - ReadWriteOnce
      persistentVolumeReclaimPolicy: Retain
      hostPath:
        path: /data/redis-data-my-release-redis-cluster-data-0
    
    ---
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: redis-data-my-release-redis-cluster-pv-1
    spec:
      capacity:
        storage: 8Gi
      volumeMode: Filesystem
      accessModes:
        - ReadWriteOnce
      persistentVolumeReclaimPolicy: Retain
      hostPath:
        path: /data/redis-data-my-release-redis-cluster-data-1
    
    ---
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: redis-data-my-release-redis-cluster-pv-2
    spec:
      capacity:
        storage: 8Gi
      volumeMode: Filesystem
      accessModes:
        - ReadWriteOnce
      persistentVolumeReclaimPolicy: Retain
      hostPath:
        path: /data/redis-data-my-release-redis-cluster-data-2
    
    ---
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: redis-data-my-release-redis-cluster-pv-3
    spec:
      capacity:
        storage: 8Gi
      volumeMode: Filesystem
      accessModes:
        - ReadWriteOnce
      persistentVolumeReclaimPolicy: Retain
      hostPath:
        path: /data/redis-data-my-release-redis-cluster-data-3
    
    ---
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: redis-data-my-release-redis-cluster-pv-4
    spec:
      capacity:
        storage: 8Gi
      volumeMode: Filesystem
      accessModes:
        - ReadWriteOnce
      persistentVolumeReclaimPolicy: Retain
      hostPath:
        path: /data/redis-data-my-release-redis-cluster-data-4
    
    ---
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: redis-data-my-release-redis-cluster-pv-5
    spec:
      capacity:
        storage: 8Gi
      volumeMode: Filesystem
      accessModes:
        - ReadWriteOnce
      persistentVolumeReclaimPolicy: Retain
      hostPath:
        path: /data/redis-data-my-release-redis-cluster-data-5
    
  2. 执行安装
    kubectl apply -f redis-pv.yaml
    helm install my-release oci://registry-1.docker.io/bitnamicharts/redis-cluster -f redis-values.yaml
    

查看结果:

❯ kubectl get po
NAME                         READY   STATUS    RESTARTS      AGE
my-release-redis-cluster-0   1/1     Running   1 (12m ago)   13m
my-release-redis-cluster-1   1/1     Running   1 (12m ago)   13m
my-release-redis-cluster-2   1/1     Running   1 (13m ago)   13m
my-release-redis-cluster-3   1/1     Running   1 (12m ago)   13m
my-release-redis-cluster-4   1/1     Running   1 (12m ago)   13m
my-release-redis-cluster-5   1/1     Running   1 (12m ago)   13m
❯ kubectl exec -it my-release-redis-cluster-0 -- bash
I have no name!@my-release-redis-cluster-0:/$ redis-cli -c cluster nodes
c209a1f8457848d46c8114177f1c1ceb502d2ba9 10.244.0.28:6379@16379 slave 62945c0986a75fcf89eb821954fe7f21f28a10a5 0 1702709349000 2 connected
dcf644091e72378da97812dc34ba0c70f7448b24 10.244.0.24:6379@16379 master - 0 1702709350389 3 connected 10923-16383
62945c0986a75fcf89eb821954fe7f21f28a10a5 10.244.0.27:6379@16379 master - 0 1702709351393 2 connected 5461-10922
926ed36c060b20f8c2891cb30222b5d37dda0c47 10.244.0.29:6379@16379 slave ffd14445028c2a0adf21ad5aa051bfdd5a1089b6 0 1702709352404 1 connected
f759496539050a0101b7e08955f5b978164065cc 10.244.0.26:6379@16379 slave dcf644091e72378da97812dc34ba0c70f7448b24 0 1702709349000 3 connected
ffd14445028c2a0adf21ad5aa051bfdd5a1089b6 10.244.0.25:6379@16379 myself,master - 0 1702709348000 1 connected 0-5460

你可能感兴趣的:(kubernetes,容器,云原生)