Helm 3的内部实现已从Helm 2发生了很大变化。最明显的变化是Tiller的删。除添加了一组丰富的新功能。某些功能已被弃用或重构,使其与Helm 2不兼容。还引入了一些新的实验功能,包括OCI支持。

1.安装Helm,并查看版本,参考官方地址:https://helm.sh/docs/intro/install/

# curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
  
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  6794  100  6794    0     0   2844      0  0:00:02  0:00:02 --:--:--  2843
Downloading https://get.helm.sh/helm-v3.1.0-linux-amd64.tar.gz
Preparing to install helm into /usr/local/bin
helm installed into /usr/local/bin/helm

# helm version
version.BuildInfo{Version:"v3.1.0", GitCommit:"b29d20baf09943e134c2fa5e1e1cab3bf93315fa", GitTreeState:"clean", GoVersion:"go1.13.7"}

2.添加chart(图表)官方仓库

# helm repo add stable https://kubernetes-charts.storage.googleapis.com/
"stable" has been added to your repositories

# helm repo list
NAME  	URL                                              
stable	https://kubernetes-charts.storage.googleapis.com/

# helm repo update 
Hang tight while we grab the latest from your chart repositories...
...Unable to get an update from the "stable" chart repository (https://kubernetes-charts.storage.googleapis.com/):
	read tcp 172.25.0.10:40168->34.64.4.112:443: read: connection reset by peer
Update Complete. ⎈ Happy Helming!⎈ 

3.搜素mysql图表

# helm search repo mysql
NAME                            	CHART VERSION	APP VERSION	DESCRIPTION                                       
stable/mysql                    	1.6.2        	5.7.28     	Fast, reliable, scalable, and easy to use open-...
stable/mysqldump                	2.6.0        	2.4.1      	A Helm chart to help backup MySQL databases usi...
stable/prometheus-mysql-exporter	0.5.2        	v0.11.0    	A Helm chart for prometheus mysql exporter with...
stable/percona                  	1.2.0        	5.7.17     	free, fully compatible, enhanced, open source d...
stable/percona-xtradb-cluster   	1.0.3        	5.7.19     	free, fully compatible, enhanced, open source d...
stable/phpmyadmin               	4.3.0        	5.0.1      	phpMyAdmin is an mysql administration frontend    
stable/gcloud-sqlproxy          	0.6.1        	1.11       	DEPRECATED Google Cloud SQL Proxy                 
stable/mariadb                  	7.3.10       	10.3.22    	Fast, reliable, scalable, and easy to use open-...

4.查看mysql信息图表

# helm show chart stable/mysql 
apiVersion: v1
appVersion: 5.7.28
description: Fast, reliable, scalable, and easy to use open-source relational database
  system.
home: https://www.mysql.com/
icon: https://www.mysql.com/common/logos/logo-mysql-170x115.png
keywords:
- mysql
- database
- sql
maintainers:
- email: o.with@sportradar.com
  name: olemarkus
- email: viglesias@google.com
  name: viglesiasce
name: mysql
sources:
- https://github.com/kubernetes/charts
- https://github.com/docker-library/mysql
version: 1.6.2

5.自定义mysql图表安装

# helm show values stable/mysql 
## mysql image version
## ref: https://hub.docker.com/r/library/mysql/tags/
##
image: "mysql"
imageTag: "5.7.28"

strategy:
  type: Recreate

busybox:
  image: "busybox"
  tag: "1.29.3"

testFramework:
  enabled: true
  image: "dduportal/bats"
  tag: "0.4.0"

## Specify password for root user
##
## Default: random 10 character string
# mysqlRootPassword: testing

## Create a database user
##
# mysqlUser:
## Default: random 10 character string
# mysqlPassword:

## Allow unauthenticated access, uncomment to enable
##
# mysqlAllowEmptyPassword: true

## Create a database
##
# mysqlDatabase:

## Specify an imagePullPolicy (Required)
## It's recommended to change this to 'Always' if the image tag is 'latest'
## ref: http://kubernetes.io/docs/user-guide/images/#updating-images
##
imagePullPolicy: IfNotPresent
......
# helm install stable/mysql --set mysqlUser="user",mysqlPassword="abc123"  --generate-name
NAME: mysql-1583334217
LAST DEPLOYED: Wed Mar  4 23:03:40 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
mysql-1583334217.default.svc.cluster.local

To get your root password run:

    MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mysql-1583334217 -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo)

To connect to your database:

1. Run an Ubuntu pod that you can use as a client:

    kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il

2. Install the mysql client:

    $ apt-get update && apt-get install mysql-client -y

3. Connect using the mysql cli, then provide your password:
    $ mysql -h mysql-1583334217 -p

To connect to your database directly from outside the K8s cluster:
    MYSQL_HOST=127.0.0.1
    MYSQL_PORT=3306

    # Execute the following command to route the connection:
    kubectl port-forward svc/mysql-1583334217 3306

    mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}

#以下是查看版本列表
# helm list
NAME            	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART      	APP VERSION
mysql-1583334217	default  	1       	2020-03-04 23:03:40.632512413 +0800 CST	deployed	mysql-1.6.2	5.7.28     
   
  
#以下是查看pod
# # kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
mysql-1583334217-784c78fdd5-mrcg4   0/1     Pending   0          2m28s


#以下是Pending(等待)状态,因为还没有准备PersistentVolume,所以release不可用
# kubectl get pvc
NAME               STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql-1583334217   Pending                                                     3m29s

6.创建一个PersistentVolume,如果不会安装nfs可以参考:https://blog.csdn.net/qq_41709494/article/details/104360014

# mkdir /nfsdata/mysql-pv

# vi nfs-pv-mysql.yml
kind: PersistentVolume
apiVersion: v1
metadata:
  name: nfs-pv-mysql
spec:
  capacity:
    storage: 8Gi
  accessModes:
   - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  nfs:
   path: /nfsdata/mysql-pv
   server: 172.25.0.10


# kubectl apply -f nfs-pv-mysql.yml 
persistentvolume/nfs-pv-mysql created

#以下是查看pv
# kubectl get pv
NAME            CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS     CLAIM                      STORAGECLASS   REASON   AGE
nfs-pv-mysql    8Gi        RWO            Retain           Bound      default/mysql-1583334217                           94s

#以下是查看pod,但是状态为ErrImagePull
# kubectl get pod
NAME                                READY   STATUS         RESTARTS   AGE
mysql-1583334217-784c78fdd5-mrcg4   0/1     ErrImagePull   0          20h

#以下是查看pod详细的信息,发现了pull(拉取)不了image(镜像)
# kubectl describe  pod mysql-1583334217-784c78fdd5-mrcg4 
Name:         mysql-1583334217-784c78fdd5-mrcg4
Namespace:    default
Priority:     0
Node:         computer/172.25.0.20
Start Time:   Wed, 04 Mar 2020 23:11:01 +0800
Labels:       app=mysql-1583334217
              pod-template-hash=784c78fdd5
              release=mysql-1583334217
Annotations:  <none>
Status:       Pending
IP:           10.244.1.143
IPs:
  IP:           10.244.1.143
Controlled By:  ReplicaSet/mysql-1583334217-784c78fdd5
Init Containers:
  remove-lost-found:
    Container ID:  docker://8558c2710f706689f3ae8c54ea5b5069b5aac6132dd5730c7e6758696ca945c3
    Image:         busybox:1.29.3
    Image ID:      docker-pullable://busybox@sha256:8ccbac733d19c0dd4d70b4f0c1e12245b5fa3ad24758a11035ee505c629c0796
    Port:          <none>
    Host Port:     <none>
    Command:
      rm
      -fr
      /var/lib/mysql/lost+found
    State:          Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Wed, 04 Mar 2020 23:11:10 +0800
      Finished:     Wed, 04 Mar 2020 23:11:10 +0800
    Ready:          True
    Restart Count:  0
    Requests:
      cpu:        10m
      memory:     10Mi
    Environment:  <none>
    Mounts:
      /var/lib/mysql from data (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-gnkdd (ro)
Containers:
  mysql-1583334217:
    Container ID:   
    Image:          mysql:5.7.28
    Image ID:       
    Port:           3306/TCP
    Host Port:      0/TCP
    State:          Waiting
      Reason:       ErrImagePull
    Ready:          False
    Restart Count:  0
    Requests:
      cpu:      100m
      memory:   256Mi
    Liveness:   exec [sh -c mysqladmin ping -u root -p${MYSQL_ROOT_PASSWORD}] delay=30s timeout=5s period=10s #success=1 #failure=3
    Readiness:  exec [sh -c mysqladmin ping -u root -p${MYSQL_ROOT_PASSWORD}] delay=5s timeout=1s period=10s #success=1 #failure=3
    Environment:
      MYSQL_ROOT_PASSWORD:  <set to the key 'mysql-root-password' in secret 'mysql-1583334217'>  Optional: false
      MYSQL_PASSWORD:       <set to the key 'mysql-password' in secret 'mysql-1583334217'>       Optional: false
      MYSQL_USER:           user
      MYSQL_DATABASE:       
    Mounts:
      /var/lib/mysql from data (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-gnkdd (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  data:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  mysql-1583334217
    ReadOnly:   false
  default-token-gnkdd:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-gnkdd
    Optional:    false
QoS Class:       Burstable
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason   Age                  From               Message
  ----     ------   ----                 ----               -------
  Normal   BackOff  19h (x108 over 20h)  kubelet, computer  Back-off pulling image "mysql:5.7.28"
  Warning  Failed   19h (x123 over 20h)  kubelet, computer  Error: ImagePullBackOff
  Warning  Failed   26m (x11 over 20h)   kubelet, computer  Failed to pull image "mysql:5.7.28": rpc error: code = Unknown desc = context canceled
  Warning  Failed   26m (x11 over 20h)   kubelet, computer  Error: ErrImagePull



7.自定values.yaml

# vi values.yaml

imageTag: "5.7.15"
mysqlUser: user
mysqlPassword: abc123

8.部署mysql图表

#以下是删除之前安装的mysql图表和pv
# helm uninstall mysql-1583334217
# kubectl delete pv nfs-pv-mysql

#以下是部署pv
# kubectl apply -f nfs-pv-mysql.yml 
persistentvolume/nfs-pv-mysql unchanged

#以下是安装mysql图表
# helm install -f values.yaml my-db stable/mysql
NAME: my-db
LAST DEPLOYED: Sat Mar  7 00:12:46 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
my-db-mysql.default.svc.cluster.local

To get your root password run:

    MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default my-db-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo)

To connect to your database:

1. Run an Ubuntu pod that you can use as a client:

    kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il

2. Install the mysql client:

    $ apt-get update && apt-get install mysql-client -y

3. Connect using the mysql cli, then provide your password:
    $ mysql -h my-db-mysql -p

To connect to your database directly from outside the K8s cluster:
    MYSQL_HOST=127.0.0.1
    MYSQL_PORT=3306

    # Execute the following command to route the connection:
    kubectl port-forward svc/my-db-mysql 3306

    mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}

#以下是查看pod是否运行
# kubectl get pod -o wide
NAME                           READY   STATUS    RESTARTS   AGE   IP             NODE       NOMINATED NODE   READINESS GATES
my-db-mysql-758d8c5544-86bn7   1/1     Running   0          12h   10.244.1.145   computer   <none>           <none>

#以下是查看新设置是否生效
# helm get values my-db
USER-SUPPLIED VALUES:
imageTag: 5.7.15
mysqlPassword: abc123
mysqlUser: user

 

 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐