溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Helm模板常用組件

發(fā)布時(shí)間:2020-07-03 22:42:27 來源:網(wǎng)絡(luò) 閱讀:196 作者:藍(lán)葉子Sheep 欄目:系統(tǒng)運(yùn)維

應(yīng)用鏡像

應(yīng)用鏡像是chart包的核心,必須包含:鏡像倉庫地址、鏡像名稱、鏡像版本,values.yaml字段規(guī)范如下:

repository:
??hub:?docker.io
??image:?gitlab/gitlab-ce
??tag:?11.1.4-ce.0
deployment中引用:
image:?{{?.Values.repository.hub?}}/{{?.Values.repository.image?}}:{{?.Values.repository.tag?}}

探針

應(yīng)用的探針用于檢測該應(yīng)用是否健康,是否準(zhǔn)備好對外提供服務(wù)。健康狀況是非常關(guān)鍵的屬性,因此每個(gè)應(yīng)用必須都明確表示如何進(jìn)行健康檢測。

values.yaml文件必須包含以下探針屬性:

livenessProbe:?#?存活探針
???enabled:?true
???path:?/??#?心跳檢測的接口,默認(rèn)都是httpGet請求
???initialDelaySeconds:?30?#?容器啟動后第一次執(zhí)行探測是需要等待多少秒。
???periodSeconds:?60?#?執(zhí)行探測的頻率。默認(rèn)是10秒,最小1秒。
???timeoutSeconds:?5?#?探測超時(shí)時(shí)間。默認(rèn)1秒,最小1秒。
???successThreshold:?1?#?探測失敗后,最少連續(xù)探測成功多少次才被認(rèn)定為成功。默認(rèn)是1
???failureThreshold:?5?#?探測成功后,最少連續(xù)探測失敗多少次才被認(rèn)定為失敗。默認(rèn)是3
?readinessProbe:?#?就緒探針
???enabled:?true
???path:?/??#?心跳檢測的接口,默認(rèn)都是httpGet請求
???initialDelaySeconds:?10
???periodSeconds:?10
???timeoutSeconds:?5
???successThreshold:?1
???failureThreshold:?5

deployment.yaml文件引用探針如下:

{{-?if?.Values.livenessProbe.enabled?}}
livenessProbe:
??initialDelaySeconds:?{{?.Values.livenessProbe.initialDelaySeconds?}}
??periodSeconds:?{{?.Values.livenessProbe.periodSeconds?}}
??timeoutSeconds:?{{?.Values.livenessProbe.timeoutSeconds?}}
??successThreshold:?{{?.Values.livenessProbe.successThreshold?}}
??failureThreshold:?{{?.Values.livenessProbe.failureThreshold?}}
??httpGet:
??????path:?{{?.Values.livenessProbe.path?}}
??????port:?http
{{-?end?}}
{{-?if?.Values.readinessProbe.enabled?}}
readinessProbe:
??initialDelaySeconds:?{{?.Values.readinessProbe.initialDelaySeconds?}}
??periodSeconds:?{{?.Values.readinessProbe.periodSeconds?}}
??timeoutSeconds:?{{?.Values.readinessProbe.timeoutSeconds?}}
??successThreshold:?{{?.Values.readinessProbe.successThreshold?}}
??failureThreshold:?{{?.Values.readinessProbe.failureThreshold?}}
??httpGet:
??????path:?{{.Values.readinessProbe.path?}}
??????port:?http
{{-?end?}}

資源限制

每個(gè)應(yīng)用都需要使用到CPU和內(nèi)存資源,如果不加以明確說明,則會導(dǎo)致資源的無畏浪費(fèi),并且在資源不足時(shí)第一時(shí)間就被回收。

因此應(yīng)用都應(yīng)當(dāng)明確申明自己所需的資源大小。

values.yaml文件包含以下屬性:

resources:
??requests:??#?聲明最少使用的資源,不夠的話則應(yīng)用無法啟動成功
????memory:?256Mi
????cpu:?100m
??limits:????#?聲明最大使用的資源,超過即重啟應(yīng)用pod
????memory:?256Mi
????cpu:?100m

持久化存儲

應(yīng)用如果不使用持久化存儲,那么當(dāng)應(yīng)用重啟的時(shí)候,之前保存的數(shù)據(jù)將會都清空。在web無狀態(tài)應(yīng)用中,這是保持環(huán)境干凈很好的一種方式。

但是如果我們應(yīng)用有數(shù)據(jù)需要持久保留的話,就需要使用到持久化存儲了。以下是持久化存儲規(guī)范:

values.yaml文件需包含以下屬性:

persistence:
??enabled:?true
??local:
????enabled:?false?#?是否啟用本地存儲
????name:?gitlab-pg??#?對應(yīng)本地存儲名稱
??storageClass:?"nfs-dynamic-class"????#?集群共享存儲
??accessMode:?ReadWriteOnce?#?存儲訪問模式
??size:?30Mi???#?聲明所需存儲的大小
??annotations:?{}

templates目錄下新建auto-pvc.yaml文件,這里之所以要加一個(gè)auto前綴就是為了保證安裝時(shí)首先執(zhí)行安裝pvc,默認(rèn)helm是安裝文件名順序來執(zhí)行安裝的。

內(nèi)容如下:

{{-?if?and?.Values.persistence.enabled?(not?.Values.persistence.existingClaim)?}}
kind:?PersistentVolumeClaim
apiVersion:?v1
metadata:
??name:?{{?.Release.Name?}}-pvc
{{-?with?.Values.persistence.annotations??}}
??annotations:
{{?toYaml?.?|?indent?4?}}
{{-?end?}}
??labels:
????app:?{{?.Release.Name?}}
spec:
{{-?if?.Values.persistence.local.enabled?}}
??volumeName:?"{{?.Values.persistence.local.name?}}"
{{-?end?}}
??accessModes:
????-?{{?.Values.persistence.accessMode?|?quote?}}
??resources:
????requests:
??????storage:?{{?.Values.persistence.size?|?quote?}}
{{-?if?.Values.persistence.storageClass?}}
{{-?if?(eq?"-"?.Values.persistence.storageClass)?}}
??storageClassName:?""
{{-?else?}}
??storageClassName:?"{{?.Values.persistence.storageClass?}}"
{{-?end?}}
{{-?end?}}
{{-?end?}}

最后根據(jù)應(yīng)用需要讀寫的路徑,掛載到容器內(nèi),修改deployment.yaml文件,添加以下內(nèi)容:

volumes:
-?name:?data
??{{-?if?.Values.persistence.enabled?}}
??persistentVolumeClaim:
????claimName:?{{?.Release.Name?}}-pvc
??{{-?else?}}
??emptyDir:?{}
??{{-?end?}}

# containers子標(biāo)簽中添加

volumeMounts:
-?name:?data
??mountPath:?/mnt?#?應(yīng)用容器內(nèi)需要讀寫的路徑

本地存儲

我們以后有很多應(yīng)用都需要支持本地存儲,本地存儲修改內(nèi)容如下:

values.yaml
persistence:
??enabled:?true
??local:
????enabled:?true????#?啟用本地存儲
????name:?gitlab-pg??#?對應(yīng)本地存儲名稱
??accessMode:?ReadWriteOnce
??size:?20Gi
??storageClass:?"-"??#?取消共享存儲
??annotations:?{}
nodeSelector:
??kubernetes.io/hostname:?10.160.144.72??#?對應(yīng)本地運(yùn)行主機(jī)

安全

設(shè)置Pod運(yùn)行的用戶,一般情況為了安全起見,容器內(nèi)不建議使用root用戶進(jìn)行運(yùn)行。但是如果需要使用本地存儲,那么就必須要使用root用戶運(yùn)行了。

另外在使用共享存儲時(shí),可以將usePodSecurityContext設(shè)置為false。

設(shè)置以root運(yùn)行,在values.yaml文件添加:

security:
??usePodSecurityContext:?true
??runAsUser:?0
??fsGroup:?0

在deployment.yaml文件中添加:

{{-?if?.Values.security.usePodSecurityContext?}}
??????securityContext:
????????runAsUser:?{{?default?0?.Values.security.runAsUser?}}
{{-?if?and?(.Values.security.runAsUser)?(.Values.security.fsGroup)?}}
????????fsGroup:?{{?.Values.security.fsGroup?}}
{{-?end?}}
{{-?end?}}
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI