51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

k8s 自定义Chart-使用”values.yaml”

自定义Chart-使用"values.yaml"

(1)编写资源values.yaml文件

cat > baimeilinux/values.yaml <<'EOF'
image:
  repository: harbor.baimei.com/baimei-tomcat/tomcat-app
  tag: v1

storage:
  pvc: baimei-tomcat-pvc
  sc: managed-nfs-storage

apps:
  school: baimei
  class: linux86

name: tomcat
version: v0.1
EOF

(2)自定义安装服务的提示信息文件

cat > baimeilinux/templates/NOTES.txt   <<'EOF'
welcome to use baimei tomcat apps ...

白眉大叔教育欢迎您,官网地址: https://www.baimeidashu.com/


本次您部署的服务是[{{ .Values.image.repository }}:{{ .Values.image.tag }}]

您的所属学校 --->【{{ .Values.apps.school }}】
您的所属班级 --->【{{ .Values.apps.class }}】


Successful deploy {{ .Values.name }}:{{ .Values.version }} !!!
EOF

(3)编写资源清单文件,引用values.yaml中预定义的变量

rm -f  baimeilinux/templates/01-deploy-tomcat.yaml 

cat > baimeilinux/templates/baimei-deploy-mysql.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  replicas: 1
  selector:
    matchExpressions:
    - key: app
      operator: Exists
  template:
    metadata:
      labels:
        app: baimei-mysql
    spec:
      volumes:
      - name: data
        persistentVolumeClaim:
           claimName: {{ .Values.storage.pvc }}
      containers:
        - name: mysql
          image: harbor.baimei.com/baimei-db/mysql:5.7
          ports:
          - containerPort: 3306
          env:
          - name: MYSQL_ROOT_PASSWORD
            value: '123456'
          volumeMounts:
          - name: data
            mountPath: /var/lib/mysql
EOF

cat > baimeilinux/templates/baimei-deploy-tomcat.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: baimei-tomcat-app
spec:
  replicas: 1
  selector:
    matchExpressions:
    - key: app
      operator: Exists
  template:
    metadata:
      labels:
        app: baimei-tomcat-app
    spec:
      containers:
        - name: myweb
          image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
          ports:
          - containerPort: 8080
          env:
          - name: MYSQL_SERVICE_HOST
            value: baimei-mysql
          - name: MYSQL_SERVICE_PORT
            value: '3306'
EOF

cat > baimeilinux/templates/baimei-mysql-svc.yaml <<'EOF'
apiVersion: v1
kind: Service
metadata:
  name: baimei-mysql
spec:
  selector:
     app: baimei-mysql
  ports:
  - port: 3306
    targetPort: 3306
EOF

cat > baimeilinux/templates/baimei-sc-pvc.yaml <<'EOF'
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: {{ .Values.storage.pvc }}
  annotations:
    # 声明使用的动态存储类名称,根据您的k8s环境自行修改即可,sc名称必须存在哈!
    volume.beta.kubernetes.io/storage-class: {{ .Values.storage.sc }}
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 100Mi
EOF

cat > baimeilinux/templates/baimei-tomcat-svc.yaml <<'EOF'
apiVersion: v1
kind: Service
metadata:
  name: baimei-tomcat-app
spec:
  type: NodePort
  selector:
     app: baimei-tomcat-app
  ports:
  - port: 8080
    targetPort: 8080
    nodePort: 8080
EOF

(4)安装Chart服务

 helm -n baimei-helm install linux86-tomcat baimeilinux/

(5)验证服务是否安装成功,在k8s集群查看资源

kubectl get all -n baimei-helm 
kubectl get pv,pvc -n baimei-helm 

(6)清空资源

helm -n baimei-helm uninstall  linux86-tomcat 
赞(6)
未经允许不得转载:工具盒子 » k8s 自定义Chart-使用”values.yaml”