存儲(chǔ)
- config map,相當(dāng)于配置中心,當(dāng)config map 發(fā)生改變了,所有引用這個(gè)的pod的配置都發(fā)生改變
- 根據(jù)文件創(chuàng)建 kubectl create configmap game-config --from-file=filepath , 鍵名為文件名,鍵值為文件內(nèi)容
- 根據(jù)字面值創(chuàng)建, kubectl create configmap special-config --from-literal=special.how=very --from-literal=xx=x
apiVersion: v1
kind: ConfigMap
metadata:
name: xx
data:
x: x
xx: xx
# 使用
spec:
containers:
env:
- name: test-container
image: xx
command: x
env:
- name: xxx
valueFrom:
configMapKeyRef:
name: special-config
key: special.how
envFrom:
- configMapRef:
name: env-config
spec:
containers:
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap: # 這里也可以是secret
name: special-config # configmap的名字
可以發(fā)現(xiàn),鍵名為文件名,鍵值為文件內(nèi)容
- configMap 熱更新 kubectl edit configmap log-config(configmap 名),修改后,容器內(nèi)的文件就會(huì)對(duì)應(yīng)發(fā)生修改
- secret三種類型 service account,集群內(nèi)的用戶,會(huì)放在pod的/run/secrets/kubernetes.io/serviceaccount; opaque base64的secret;kubernetes.io/dockerconfigjson 用來存儲(chǔ)docker私有倉庫的認(rèn)證信息
apiVersion: v1
kind: Secret
metadata:
nmae: mysecret
type: Opaque
data:
password: base64的內(nèi)容
username: base64的用戶名
- kubectl create secret docker-registry myregistrykey --docker-server=docker hub server --docker-usernmae=docker_user --docker-password=docker_pass --docker-email=xx 創(chuàng)建docker 鏡像倉庫登錄的信息
apiVersion: v1
kind: Pod
metadata:
name: foo
spec:
containers:
- nmae: foo
image: roc/awantyang:v1
imagePullSecrets: # docker 登錄的賬密
- name: myregistrykey
掛載類型
- empty dir
- hostpath 只要文件系統(tǒng)能在pod中創(chuàng)建容器,就可以使用其云存儲(chǔ),某些目錄可能沒有權(quán)限寫入,需要把主機(jī)目錄的權(quán)限修改成k8s運(yùn)行的屬組,或者是改成任何用戶可讀寫的權(quán)限,或者是k8s以root運(yùn)行,則不需要修改
- pv和pvc,創(chuàng)建pod的時(shí)候,聲明一個(gè)pvc,然后pvc會(huì)根據(jù)配置尋找相關(guān)的pv,進(jìn)行綁定,pv有獨(dú)立的生命周期,pod重啟后數(shù)據(jù)不丟失
- 靜態(tài)pv和動(dòng)態(tài)pv,靜態(tài)pv是先創(chuàng)建好pv,等pvc調(diào)用,動(dòng)態(tài)pv為,通過pvc進(jìn)行動(dòng)態(tài)的創(chuàng)建,一般用于云存儲(chǔ)
- pv和pvc一一對(duì)應(yīng)
apiVersion: v1
kind: persistentVolume
metadata:
name: nfspv
spec:
capacity:
storage: 1G
accessModes:
- ReadWriteOnce
storageClassName: nfs
persistentVolumeReclaimPolicy: Retain # 回收策略
nfs:
path: /nfs # nfs目錄
server: 192.158.1.1 # nfs服務(wù)地址
調(diào)度過程
- 分為預(yù)選和優(yōu)選
- 預(yù)選
- PodFitsResources,查看剩余的資源是否大于pod請(qǐng)求的
- PodFitsHost,查看nodename
- PodFitsHostPorts,已使用和申請(qǐng)的port是否匹配
- PodSelectorMathches,label的匹配情況
- NoDiskConflict mount的vol和pod指定的vol
- 如果沒有合適的節(jié)點(diǎn)就會(huì)一直pending
- 優(yōu)選
- LeastRequestedPriority 通過計(jì)算cpu和mem的使用率決定權(quán)重
- BalanceResourse
- 親和和反親和性,需要各個(gè)相關(guān)的pod對(duì)象運(yùn)行于同一位置,這個(gè)同一位置使用topology key定義,將labelSelector中匹配的pod,部署到包含同一topology的區(qū)域內(nèi)
污點(diǎn)與容忍度
- 使節(jié)點(diǎn)能夠排斥一類特定的pod
- taint表示節(jié)點(diǎn)不能部署包含這個(gè)taint的pod,如果pod上有toleration則表示,這個(gè)pod調(diào)度到具有匹配的taint節(jié)點(diǎn)上。即為我能容忍這個(gè)污點(diǎn),可以往上部署
- 污點(diǎn)是設(shè)置在node上的,容忍度是設(shè)置在pod上的
- NoSchedule,表示k8s不會(huì)將pod調(diào)度到具有該污點(diǎn)的node上
- PreferNoSchedule 避免調(diào)度到有這個(gè)污點(diǎn)的node上
- NoExecute 不會(huì)調(diào)度到node上,并且,已經(jīng)部署的pod,也會(huì)驅(qū)逐
- 設(shè)置污點(diǎn) kubectl taint nodes node1 key1=value1:NoSchedule
- 去除污點(diǎn) kubectl taint nodes node1 key1:NoSchedule-
- 可以使用NodeName和NodeSelector來決定pod部署的node
# 容忍度配置
tolerations:
- key: "key"
operator: "Equal"
value: "value"
effect: "NoSchedule"
tolerationSeconds: 3600
key、value、effect要與node上設(shè)置一致,,tolerationSeconds用于描述當(dāng)Pod需要被驅(qū)逐時(shí)可以在Pod上繼續(xù)保留運(yùn)行的時(shí)間。不指定key和effect表示容忍所有key和effect
有多個(gè)master時(shí),可以設(shè)置盡量不在這上面執(zhí)行,kubectl taint nodes Node-name node-role.kubernetes.io/master=:PreferNoSchedule
helm
- k8s的應(yīng)用管理,能動(dòng)態(tài)生成k8s資源清單文件。自動(dòng)執(zhí)行k8s資源部署
- 包含chart和release,chart是應(yīng)用信息合集,包括配置模板、參數(shù)定義、依賴關(guān)系、文檔說明等;chart每次運(yùn)行就是個(gè)release,chart可以多次安裝到同一集群,生成多個(gè)release
helm安裝
- elf文件,軟連到/usr/bin 下即可
- 創(chuàng)建sa,用于helm
apiVersion: v1
kind: ServiceAccount
metadata:
name: tiller
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: tiller
roleRef:
apigroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: tiller
namespace: kube-system
- helm init --service-account tiller --skip-refresh 配置helm使用的sa
helm自定義模板
- 創(chuàng)建文件夾
- 創(chuàng)建愛你chart.yaml
# Chart.yaml
name: hello-world
version: 1.0.0
# 創(chuàng)建模板文件 templates/deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: hello-world
spec:
replicas: 1
template:
metadata:
labels:
app: hello-world
spec:
containers:
- name: hello-world
image: xx
ports:
- containerPort: 8080
protocol: TCP
# 創(chuàng)建svc配置 templates/service.yaml
- helm install
- helm list 列出所有release
- helm upgrade xxx 更新,會(huì)自動(dòng)實(shí)現(xiàn)升級(jí)
- helm history 查看歷史版本
- helm status name 查看信息
- heml delete xxx 移除release,軟刪除
- helm rollback xx 版本 回滾
- helm delete --purge release_name 移除所有指定release的k8s資源及記錄,硬刪除
- helm list --deleted 查看軟刪除的內(nèi)容
- 通過配置修改chart
# values.yaml
image:
repository: xx
tag: xx
# 模板中使用。VAlues對(duì)象訪問
image: {{.Values.image.repositoiry}}:{{.Values.image.tag}}
- 也可以通過helm install --set image.tag='latest' 進(jìn)行設(shè)置 或者h(yuǎn)elm install --values values.yaml 指定文件引入
- 測(cè)試是否可以創(chuàng)建 helm install --try-run
dashboard可視化配置管理
- helm fetch stable/kubernetes-dashboard
- helm install stable/kubernetes-dashboard -n kubernetes-dashboard --namespace kube-system -f kubernetes-dashboard.yaml
- 訪問需要用kube/config文件或者是導(dǎo)入一個(gè)token,kubectl -n kube-system get secret | grep dashboard-token 得到名字,然后kubectl describe secret name
集群監(jiān)控
- kubectl top node/pod 查看節(jié)點(diǎn)的top情況,需要kubernetes或者metric 插件支持
- prometheus有自己DSL語言
- 可視化監(jiān)控界面 grafana
動(dòng)態(tài)伸縮
- kubectl run php-apache --image=xxx --requests=cup=200m --expose --port=80 限制運(yùn)行的cpu
- kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10 最少一個(gè),最多10個(gè)
資源限制
- request為初始化大小,limit是限制
spec:
containers:
- image: xxx
imagePullPolicy: Always
name: auth
ports:
- containerPort: 80
protocol: TCP
resources:
limits:
cpu: "100m"
memory: "2g"
- quota, 資源限制的合集
apiVersion: v1
kind: ResourceQuota
metadata:
name: c
namespace: s
spec:
hard:
pods: "20" # 20個(gè)pod的總資源限制 只能創(chuàng)建20個(gè)pod
requests.cpu: "2"
requests.memory: 100G
limits.cpu: 40
limits.memory: 200G
configmaps: 10 # 只能創(chuàng)建10個(gè)config map
persistentvolumeclaims: 3 # pvc格式
secrets: 10
services: 10
services.loadbalancers: 3 # LB個(gè)數(shù)
- pod創(chuàng)建如果沒有限制資源,則會(huì)使用ns下的最大資源,如果ns也沒有限制,則使用集群的最大資源,使用limitRange設(shè)置默認(rèn)值
kind: LimitRange
spec:
limits:
- default:
memory: 100G
cpu: 5
defalutRequest:
memeory: 10G
cpu: 1
type: Container # 定義為容器的默認(rèn)值
efk
- es fluentd k
k8s高可用
- controller Schedule 如果存在多個(gè),只有一個(gè)工作,其他掛起,自動(dòng)實(shí)現(xiàn)了高可用
- etcd會(huì)形成集群部署,實(shí)現(xiàn)高可用
- 方案,冗余部署nginx或者h(yuǎn)a等負(fù)載均衡器,發(fā)往apiserver的請(qǐng)求都經(jīng)過負(fù)載均衡器的代理,實(shí)現(xiàn)多個(gè)apiserver的高可用
安全
- http token,token放在header中去請(qǐng)求api server,token對(duì)應(yīng)的用戶名放在apiserver可以訪問你的文件里
- http base
- https 雙向認(rèn)證,客戶端和服務(wù)端分別向api server申請(qǐng)ca證書,進(jìn)行tls的雙向認(rèn)證
- 需要認(rèn)證的資源
- 默認(rèn)情況下,通過127.0.0.1進(jìn)行本機(jī)訪問的,不是用https,可以在配置里進(jìn)行關(guān)閉
- kubectl kubelet kubeproxy 外部王文需要雙向認(rèn)證
- kubeconfig為集群的配置文件,包含集群參數(shù),ca證書,apiserver 地址,客戶端參數(shù),集群context信息,kubernetes啟動(dòng)時(shí)指定不同的kubeconfig文件切換不同的集群
- pod動(dòng)態(tài)創(chuàng)建,如果使用證書會(huì)頻繁申請(qǐng)創(chuàng)建證書,所以pod使用sa進(jìn)行訪問