1. 使用名称创建chart目录

[root@k8s-master01 ~]# helm create mychart

[root@k8s-master01 ~]# cd mychart/
[root@k8s-master01 mychart]# ls
charts  Chart.yaml  templates  values.yaml

#目录结构说明
charts      #普通目录
Chart.yaml   #用于配置chart 的基本信息
templates    #自定义yaml文件存放目录
values.yaml  #全局变量文件

2. 在templatest目录下创建yaml文件

cd templates/

添加yaml文件

cat > deployment.yaml  <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web
    spec:
      containers:
      - image: nginx:1.15
        name: nginx
        resources: {}
status: {}
EOF


cat > service.yaml <<EOF
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: web
  type: ClusterIP
status:
  loadBalancer: {}
EOF

3. 安装部署

#回到chart目录的同级目录
cd ~

#部署
helm install web mychart/

返回

NAME: web
LAST DEPLOYED: Mon Feb  1 10:47:39 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None

查看pod

[root@k8s-master01 ~]# kubectl get pod,svc | grep web
.pod/web-7d9697b7f8-hwpbf                               1/1     Running   0          15s
service/web                   ClusterIP   10.0.0.122   <none>        80/TCP         15s

#可以看到,已经根据我们编写的yaml创建出对应的pod

4. helm更新

现在我们想要将nginx的镜像改为1.16版本,并且将svc改为nodeport模式

vi deployment.yaml

    spec:
      containers:
      - image: nginx:1.16     #修改

vi service.yaml
#修改
  type: NodePort

 更新helm

helm upgrade web mychart/

返回

Release "web" has been upgraded. Happy Helming!
NAME: web
LAST DEPLOYED: Mon Feb  1 10:54:35 2021
NAMESPACE: default
STATUS: deployed
REVISION: 2
TEST SUITE: None

查看

[root@k8s-master01 ~]# kubectl get deployment web -o yaml  | grep image:
      - image: nginx:1.16
[root@k8s-master01 ~]# kubectl get svc | grep web
web                   NodePort    10.0.0.122   <none>        80:30104/TCP   4m55s

#可以看到已经被更新了

 

5. 卸载helm部署的应用

[root@k8s-master01 ~]# helm list
NAME   	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART             	APP VERSION
test-ui	default  	1       	2021-02-01 10:03:45.038617594 +0800 CST	deployed	weave-scope-1.1.12	1.12.0     
web    	default  	2       	2021-02-01 10:54:35.012617033 +0800 CST	deployed	mychart-0.1.0     	1.16.0    

卸载

helm uninstall web

查看

[root@k8s-master01 ~]# helm list
NAME   	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART             	APP VERSION
test-ui	default  	1       	2021-02-01 10:03:45.038617594 +0800 CST	deployed	weave-scope-1.1.12	1.12.0     

 

案例2 mysql

#查询相关资源
helm search repo mysql

设置变量

cat > config.yaml <<EOF
persistence:
  enabled: false     #我们现在没有做pv卷,这里暂时不用
  storageClass: "managed-nfs-storage"
  accessMode: ReadWriteOnce
  size: 1Gi
mysqlUser: "k8s"
mysqlPassword: "123456"
mysqlDatabase: "k8s"
EOF

部署

 helm install db -f config.yaml weiruan/mysql

返回

WARNING: This chart is deprecated
NAME: db
LAST DEPLOYED: Mon Feb  1 16:33:26 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
db-mysql.default.svc.cluster.local

To get your root password run:

    MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default 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 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/db-mysql 3306

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

创建测试容器

#进入mysql容器
kubectl exec -it db-mysql-58fcd8bbbf-kn74f   sh


#登陆mysql
mysql -hdb-mysql -uk8s -p123456

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| k8s                |
+--------------------+
2 rows in set (0.00 sec)

#以上将创建具有名称的默认 MySQL 用户 k8s,
#并授予此用户访问新创建的 k8s 数据库的权限,但将接受该图表的所有其余默认值。

 

也可以使用命令行

#卸载刚才部署的
helm uninstall db


#使用命令行set添加变量使用
helm install db \
--set persistence.enabled="false" \
weiruan/mysql

yaml格式转换set格式

yaml类型set类型
name: value--set name=value

a: b

c: d

--set a=b,c=d

Outer:

   inner: value

--set Outer.inner=value

name:

  - a

  - b

  - c

set name={a,b,c}

servers:

  - port: 80

--set servers[0].port=80

servers:

  - port: 80

    host: example

--set servers[0].port=80,servers[0].host=example
name: "value1,value2"--set name=value1\,value2

nodeSelector:

  kubernetes.io/role: master

--set nodeSelector."kubernetes"\.io/role"=master

 

Logo

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

更多推荐