k8s 一小時(shí)入門實(shí)操(二)

存儲(chǔ)

  1. 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)容

  1. configMap 熱更新 kubectl edit configmap log-config(configmap 名),修改后,容器內(nèi)的文件就會(huì)對(duì)應(yīng)發(fā)生修改
  2. 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的用戶名
  1. 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

掛載類型

  1. empty dir
  2. hostpath 只要文件系統(tǒng)能在pod中創(chuàng)建容器,就可以使用其云存儲(chǔ),某些目錄可能沒有權(quán)限寫入,需要把主機(jī)目錄的權(quán)限修改成k8s運(yùn)行的屬組,或者是改成任何用戶可讀寫的權(quán)限,或者是k8s以root運(yùn)行,則不需要修改
  3. pv和pvc,創(chuàng)建pod的時(shí)候,聲明一個(gè)pvc,然后pvc會(huì)根據(jù)配置尋找相關(guān)的pv,進(jìn)行綁定,pv有獨(dú)立的生命周期,pod重啟后數(shù)據(jù)不丟失
  4. 靜態(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ǔ)
  5. 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)度過程

  1. 分為預(yù)選和優(yōu)選
  2. 預(yù)選
    • PodFitsResources,查看剩余的資源是否大于pod請(qǐng)求的
    • PodFitsHost,查看nodename
    • PodFitsHostPorts,已使用和申請(qǐng)的port是否匹配
    • PodSelectorMathches,label的匹配情況
    • NoDiskConflict mount的vol和pod指定的vol
    • 如果沒有合適的節(jié)點(diǎn)就會(huì)一直pending
  3. 優(yōu)選
    • LeastRequestedPriority 通過計(jì)算cpu和mem的使用率決定權(quán)重
    • BalanceResourse
  4. 親和和反親和性,需要各個(gè)相關(guān)的pod對(duì)象運(yùn)行于同一位置,這個(gè)同一位置使用topology key定義,將labelSelector中匹配的pod,部署到包含同一topology的區(qū)域內(nèi)

污點(diǎn)與容忍度

  1. 使節(jié)點(diǎn)能夠排斥一類特定的pod
  2. taint表示節(jié)點(diǎn)不能部署包含這個(gè)taint的pod,如果pod上有toleration則表示,這個(gè)pod調(diào)度到具有匹配的taint節(jié)點(diǎn)上。即為我能容忍這個(gè)污點(diǎn),可以往上部署
  3. 污點(diǎn)是設(shè)置在node上的,容忍度是設(shè)置在pod上的
  4. NoSchedule,表示k8s不會(huì)將pod調(diào)度到具有該污點(diǎn)的node上
  5. PreferNoSchedule 避免調(diào)度到有這個(gè)污點(diǎn)的node上
  6. NoExecute 不會(huì)調(diào)度到node上,并且,已經(jīng)部署的pod,也會(huì)驅(qū)逐
  7. 設(shè)置污點(diǎn) kubectl taint nodes node1 key1=value1:NoSchedule
  8. 去除污點(diǎn) kubectl taint nodes node1 key1:NoSchedule-
  9. 可以使用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

  1. k8s的應(yīng)用管理,能動(dòng)態(tài)生成k8s資源清單文件。自動(dòng)執(zhí)行k8s資源部署
  2. 包含chart和release,chart是應(yīng)用信息合集,包括配置模板、參數(shù)定義、依賴關(guān)系、文檔說明等;chart每次運(yùn)行就是個(gè)release,chart可以多次安裝到同一集群,生成多個(gè)release

helm安裝

  1. elf文件,軟連到/usr/bin 下即可
  2. 創(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
  1. helm init --service-account tiller --skip-refresh 配置helm使用的sa

helm自定義模板

  1. 創(chuàng)建文件夾
  2. 創(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
  1. helm install
  2. helm list 列出所有release
  3. helm upgrade xxx 更新,會(huì)自動(dòng)實(shí)現(xiàn)升級(jí)
  4. helm history 查看歷史版本
  5. helm status name 查看信息
  6. heml delete xxx 移除release,軟刪除
  7. helm rollback xx 版本 回滾
  8. helm delete --purge release_name 移除所有指定release的k8s資源及記錄,硬刪除
  9. helm list --deleted 查看軟刪除的內(nèi)容
  10. 通過配置修改chart
# values.yaml
image:
  repository: xx
  tag: xx

# 模板中使用。VAlues對(duì)象訪問
image: {{.Values.image.repositoiry}}:{{.Values.image.tag}}
  1. 也可以通過helm install --set image.tag='latest' 進(jìn)行設(shè)置 或者h(yuǎn)elm install --values values.yaml 指定文件引入
  2. 測(cè)試是否可以創(chuàng)建 helm install --try-run

dashboard可視化配置管理

  1. helm fetch stable/kubernetes-dashboard
  2. helm install stable/kubernetes-dashboard -n kubernetes-dashboard --namespace kube-system -f kubernetes-dashboard.yaml
  3. 訪問需要用kube/config文件或者是導(dǎo)入一個(gè)token,kubectl -n kube-system get secret | grep dashboard-token 得到名字,然后kubectl describe secret name

集群監(jiān)控

  1. kubectl top node/pod 查看節(jié)點(diǎn)的top情況,需要kubernetes或者metric 插件支持
  2. prometheus有自己DSL語言
  3. 可視化監(jiān)控界面 grafana

動(dòng)態(tài)伸縮

  1. kubectl run php-apache --image=xxx --requests=cup=200m --expose --port=80 限制運(yùn)行的cpu
  2. kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10 最少一個(gè),最多10個(gè)

資源限制

  1. request為初始化大小,limit是限制
spec: 
  containers:
  - image: xxx
    imagePullPolicy: Always
    name: auth
    ports:
    - containerPort: 80
      protocol: TCP
  resources:
    limits:
      cpu: "100m"
      memory: "2g"
  1. 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ù)
  1. 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

  1. es fluentd k

k8s高可用

  1. controller Schedule 如果存在多個(gè),只有一個(gè)工作,其他掛起,自動(dòng)實(shí)現(xiàn)了高可用
  2. etcd會(huì)形成集群部署,實(shí)現(xiàn)高可用
  3. 方案,冗余部署nginx或者h(yuǎn)a等負(fù)載均衡器,發(fā)往apiserver的請(qǐng)求都經(jīng)過負(fù)載均衡器的代理,實(shí)現(xiàn)多個(gè)apiserver的高可用

安全

  1. http token,token放在header中去請(qǐng)求api server,token對(duì)應(yīng)的用戶名放在apiserver可以訪問你的文件里
  2. http base
  3. https 雙向認(rèn)證,客戶端和服務(wù)端分別向api server申請(qǐng)ca證書,進(jìn)行tls的雙向認(rèn)證
  4. 需要認(rèn)證的資源
    • 默認(rèn)情況下,通過127.0.0.1進(jìn)行本機(jī)訪問的,不是用https,可以在配置里進(jìn)行關(guān)閉
    • kubectl kubelet kubeproxy 外部王文需要雙向認(rèn)證
  5. kubeconfig為集群的配置文件,包含集群參數(shù),ca證書,apiserver 地址,客戶端參數(shù),集群context信息,kubernetes啟動(dòng)時(shí)指定不同的kubeconfig文件切換不同的集群
  6. pod動(dòng)態(tài)創(chuàng)建,如果使用證書會(huì)頻繁申請(qǐng)創(chuàng)建證書,所以pod使用sa進(jìn)行訪問
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容