k8s 修炼手册

[复制链接]
发表于 2026-1-17 15:01:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
deployment
  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4.   name: hello-deploy
  5. spec:
  6.   replicas: 10
  7.   selector:
  8.     matchLabels:
  9.       app: hello-world # Pod的label # 这个Label与Service的Label筛选器是匹配的
  10.   revisionHistoryLimit: 5
  11.   progressDeadlineSeconds: 300
  12.   minReadySeconds: 10 # 每个pod更新动作间隔10s
  13.   strategy:
  14.     type: RollingUpdate # 使用RollingUpdate方式更新
  15.     rollingUpdate: # 智能同时更新最多9个
  16.       maxUnavailable: 1 # 不允许出现比期望状态指定的pod数量少超过一个的情况
  17.       maxSurge: 1 # 不允许出现比期望状态指定的pod数量多超过一个的情况
  18.   template:
  19.     metadata:
  20.       labels:
  21.         app: hello-world
  22.     spec:
  23.       containers:
  24.       - name: hello-pod
  25.         image: nigelpoulton/k8sbook:1.0
  26.         ports:
  27.         - containerPort: 8080
复制代码
创建对应svc
  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4.   name: hello-svc
  5.   labels:
  6.     app: hello-world # Label 筛选器 service正在查找带有app=hello-world的Pod
  7. spec:
  8.   type: NodePort
  9.   ports:
  10.   - port: 8080
  11.     nodePort: 30001
  12.     protocol: TCP
  13.   selector:
  14.     app: hello-world
复制代码
滚动更新查察下令
  1. kubectl rollout status
复制代码
查察汗青版本
  1. kubectl rollout history
复制代码
查察更新后rs
  1. kubectl get rs
复制代码
根据版本信息回滚
  1. kubectl rollout undo deployment hello-deploy --to-revision=1
复制代码
Service
  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4.   name: web-deploy
  5. spec:
  6.   replicas: 10
  7.   selector:
  8.     matchLabels:
  9.       app: hello-world
  10.   template:
  11.     metadata:
  12.       labels:
  13.         app: hello-world
  14.     spec:
  15.       containers:
  16.       - name: hello-ctr
  17.         image: nigelpoulton/k8sbook:latest
  18.         ports:
  19.         - containerPort: 8080
复制代码
下令行创建svc
  1. kubectl expose deployment web-deploy --name=hello-svc --target-port=8080 --type=NodePort
复制代码
查察svc
  1. kubectl describe svc hello-svc
  2. [root@master svc]# kubectl describe svc hello-svc
  3. Name:                     hello-svc
  4. Namespace:                default
  5. Labels:                   <none>
  6. Annotations:              <none>
  7. Selector:                 app=hello-world # Label筛选器定义的Label
  8. Type:                     NodePort
  9. IP Family Policy:         SingleStack
  10. IP Families:              IPv4
  11. IP:                       10.12.205.3 # svc内部的ClusterIP(VIP)
  12. IPs:                      10.12.205.3
  13. Port:                     <unset>  8080/TCP
  14. TargetPort:               8080/TCP # 应用正在监听的pod端口
  15. NodePort:                 <unset>  32458/TCP # 集群外可访问的svc端口
  16. Endpoints:                10.244.104.59:8080,10.244.104.60:8080,10.244.104.61:8080 + 7 more... # 能够匹配到Label筛选器的健康Pod的IP动态列表
  17. Session Affinity:         None
  18. External Traffic Policy:  Cluster
  19. Events:                   <none>
复制代码
声明式创建svc
  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4.   name: hello-svc
  5.   labels:
  6.     chapter: services
  7. spec:
  8. # ipFamilyPolicy: PreferDualStack
  9. # ipFamilies:
  10. # - IPv4
  11. # - IPv6
  12.   type: NodePort
  13.   ports:
  14.   - port: 8080
  15.     nodePort: 30001
  16.     targetPort: 8080
  17.     protocol: TCP
  18.   selector:
  19.     app: hello-world
复制代码
查察svc

kubectl get svc hello-svc
kubectl describe svc hello-svc
查察Endpoint

kubectl get ep hello-svc
滚动更新

原始状态

Service app=biz1zone=pord
Pod1app=biz1zone=pordver=4.1Pod2app=biz1zone=pordver=4.1更新中

pod label 有ver 这时间新旧版本pod svc都提供服务
Service app=biz1zone=pord
Pod1app=biz1zone=pordver=4.1
Pod2app=biz1zone=pordver=4.1
Pod3app=biz1zone=pordver=4.2
Pod4app=biz1zone=pordver=4.2
更新后

svc 添加label ver=4.2 此时svc流量至通向新版本pod 修改为ver=4.1 svc流量则通向旧版本
Service app=biz1zone=pordver=4.2
Pod1app=biz1zone=pordver=4.1Pod2app=biz1zone=pordver=4.1Pod3app=biz1zone=pordver=4.2Pod4app=biz1zone=pordver=4.2服务发现及注册
  1. kubectl get service
  2. kubectl get endpoint
复制代码
服务注册



    • post Service 设置到API Service


    • 分配ClutserIP


    • 设置长期化到集群存储


    • 维护有Pod IP的Endpoint被创建


    • 集群DNS发现新的Service


    • 创建DNS纪录


    • kube-proxy拉取Service的设置


    • 创建IPVS规则举行负载平衡

服务发现



    • 哀求DNS分析Service名称


    • 收到ClusterIP


    • 发送流量到ClusterIP


    • 无路由,发送至容器的默认网关


    • 转发至节点


    • 无路由,发送至容器的默认网关


    • 被节点内核处置惩罚


    • 捕获(IPVS规则)


    • 将目标IP的值重写为Pod的IP

  1. apiVersion: v1
  2. kind: Namespace
  3. metadata:
  4.   name: dev
  5. ---
  6. apiVersion: v1
  7. kind: Namespace
  8. metadata:
  9.   name: prod
  10. ---
  11. kind: Deployment
  12. apiVersion: apps/v1
  13. metadata:
  14.   name: enterprise
  15.   namespace: dev
  16.   labels:
  17.     app: enterprise
  18. spec:
  19.   selector:
  20.     matchLabels:
  21.       app: enterprise
  22.   replicas: 2
  23.   template:
  24.     metadata:
  25.       labels:
  26.         app: enterprise
  27.     spec:
  28.       terminationGracePeriodSeconds: 1
  29.       containers:
  30.       - image: nigelpoulton/k8sbook:text-dev
  31.         name: enterprise-ctr
  32.         ports:
  33.         - containerPort: 8080
  34. ---
  35. kind: Deployment
  36. apiVersion: apps/v1
  37. metadata:
  38.   name: enterprise
  39.   namespace: prod
  40.   labels:
  41.     app: enterprise
  42. spec:
  43.   selector:
  44.     matchLabels:
  45.       app: enterprise
  46.   replicas: 2
  47.   template:
  48.     metadata:
  49.       labels:
  50.         app: enterprise
  51.     spec:
  52.       terminationGracePeriodSeconds: 1
  53.       containers:
  54.       - image: nigelpoulton/k8sbook:text-prod
  55.         name: enterprise-ctr
  56.         ports:
  57.         - containerPort: 8080
  58. ---
  59. apiVersion: v1
  60. kind: Service
  61. metadata:
  62.   name: ent
  63.   namespace: dev
  64. spec:
  65.   ports:
  66.   - port: 8080
  67.   selector:
  68.     app: enterprise
  69. ---
  70. apiVersion: v1
  71. kind: Service
  72. metadata:
  73.   name: ent
  74.   namespace: prod
  75. spec:
  76.   ports:
  77.   - port: 8080
  78.   selector:
  79.     app: enterprise
  80. ---
  81. apiVersion: v1
  82. kind: Pod
  83. metadata:
  84.   name: jump
  85.   namespace: dev
  86. spec:
  87.   terminationGracePeriodSeconds: 5
  88.   containers:
  89.   - image: ubuntu
  90.     name: jump
  91.     tty: true
  92.     stdin: true
复制代码
  1. [root@master ~]# kubectl get all -n dev
  2. NAME                             READY   STATUS    RESTARTS   AGE
  3. pod/enterprise-76fc64bd9-h5gqg   1/1     Running   0          3h20m
  4. pod/enterprise-76fc64bd9-kpxh9   1/1     Running   0          3h20m
  5. pod/jump                         1/1     Running   0          3h20m
  6. NAME          TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
  7. service/ent   ClusterIP   10.7.27.61   <none>        8080/TCP   3h20m
  8. NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
  9. deployment.apps/enterprise   2/2     2            2           3h20m
  10. NAME                                   DESIRED   CURRENT   READY   AGE
  11. replicaset.apps/enterprise-76fc64bd9   2         2         2       3h20m
  12. [root@master ~]# kubectl get all -n prod
  13. NAME                              READY   STATUS    RESTARTS   AGE
  14. pod/enterprise-5cfcd578d7-lknbj   1/1     Running   0          3h27m
  15. pod/enterprise-5cfcd578d7-mwzcb   1/1     Running   0          3h27m
  16. NAME          TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)    AGE
  17. service/ent   ClusterIP   10.2.20.188   <none>        8080/TCP   3h27m
  18. NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
  19. deployment.apps/enterprise   2/2     2            2           3h27m
  20. NAME                                    DESIRED   CURRENT   READY   AGE
  21. replicaset.apps/enterprise-5cfcd578d7   2         2         2       3h27m
  22. root@master ~]# kubectl exec -it jump -n dev -- bash
  23. root@jump:/# cat /etc/resolv.conf
  24. nameserver 10.0.0.10
  25. search dev.svc.cluster.local svc.cluster.local cluster.local
  26. options ndots:5
  27. root@jump:/# apt-get update &&u apt-get install curl -y
  28. root@jump:/# curl ent:8080
  29. Hello from the DEV Namespace!
  30. Hostname: enterprise-76fc64bd9-h5gqg
  31. root@jump:/# curl ent.dev.svc.cluster.local:8080
  32. Hello from the DEV Namespace!
  33. Hostname: enterprise-76fc64bd9-h5gqg
  34. root@jump:/# curl ent.prod.svc.cluster.local:8080
  35. Hello from the PROD Namespace!
  36. Hostname: enterprise-5cfcd578d7-mwzcb
  37. # pod内curl其他pod及端口
  38. # serviceName.namespace.svc.cluster.local:port
复制代码
服务排查


  • Pod: 由coredns Deployment管理
  • Service: 一个名为kube-dns的ClusterIP Service,其监听端口为TCP/UDP 53
  • Endpoint: 也叫做kube-dns
    全部与集群DNS干系的对象都有k8s-app=kube-dns的Label

  • 起首排查coredns Deployment呆板管理的Pod试运行状态的
  1. [root@master ~]# kubectl get deploy -n kube-system -l k8s-app=kube-dns
  2. NAME      READY   UP-TO-DATE   AVAILABLE   AGE
  3. coredns   2/2     2            2           33d
  4. [root@master ~]# kubectl get pods -n kube-system -l k8s-app=kube-dns
  5. NAME                       READY   STATUS    RESTARTS        AGE
  6. coredns-857d9ff4c9-6cb2b   1/1     Running   28 (3d5h ago)   33d
  7. coredns-857d9ff4c9-tvrff   1/1     Running   28 (3d5h ago)   33d
  8. [root@master ~]# kubectl logs -n kube-system coredns-857d9ff4c9-6cb2b
  9. [INFO] plugin/kubernetes: waiting for Kubernetes API before starting server
  10. [INFO] plugin/ready: Still waiting on: "kubernetes"
  11. [INFO] plugin/ready: Still waiting on: "kubernetes"
  12. [INFO] plugin/kubernetes: waiting for Kubernetes API before starting server
  13. .:53
  14. [INFO] plugin/reload: Running configuration SHA512 = 591cf328cccc12bc490481273e738df59329c62c0b729d94e8b61db9961c2fa5f046dd37f1cf888b953814040d180f52594972691cd6ff41be96639138a43908
  15. CoreDNS-1.11.1
  16. linux/amd64, go1.20.7, ae2bbc2
复制代码

  • 查察Service呆板Endpoint对象 包管ClutserIP由IP所在病监听TCP/UDP 53端口
  1. [root@master ~]# kubectl get svc kube-dns -n kube-system
  2. NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
  3. kube-dns   ClusterIP   10.0.0.10    <none>        53/UDP,53/TCP,9153/TCP   33d
  4. [root@master ~]# kubectl get ep kube-dns -n kube-system
  5. NAME       ENDPOINTS                                                        AGE
  6. kube-dns   10.244.219.68:53,10.244.219.69:53,10.244.219.68:53 + 3 more...   33d
复制代码

  • 确定DNS组件都正常后,使用 gcr.io/kubernetes-e2e-test-images/dnsutils:latest 镜像
    镜像包罗ping traceroute curl dig nslookup下令
  1. apt install iputils-ping  -y
  2. apt install dnsutils
  3. apt install traceroute
  4. apt install nslookup
  5. root@ubuntu-pod:/# nslookup kubernetes
  6. # 返回
  7. ;; Got recursion not available from 10.0.0.10
  8. Server:         10.0.0.10
  9. Address:        10.0.0.10#53
  10. Name:   kubernetes.default.svc.cluster.local
  11. Address: 10.0.0.1
  12. ;; Got recursion not available from 10.0.0.10
复制代码
volume

nfs
  1. yum install -y nfs-common nfs-utils rpcbind
  2. mkdir /nfsdata
  3. chmod 666 /nfsdata
  4. chown nfsnobody /nfsdata
  5. chgrp nfsnobody /nfsdata # 没有nfsnobody 使用nobody
  6. cat /etc/exports
  7. /nfsdata *(rw,no_root_squash,no_all_squash,sync)
  8. systemctl restart nfs-server
  9. systemctl restart rpcbind
  10. [root@master script]# ssh node1
  11. [root@node1 ~]# mount -t nfs master:/nfsdata /nfsdata
  12. [root@node1 ~]# cat /etc/fstab
  13. 10.0.17.100:/nfsdata /nfsdata nfs default,_netdev 0 0
复制代码
pv
  1. apiVersion: v1
  2. kind: PersistentVolume
  3. metadata:
  4. name: nfspv0 # pv 名字
  5. spec:
  6. capacity: #容量
  7.   storage: 10Gi #存储空间
  8. accessModes: #存储模式
  9.   - ReadWriteOnce #单个节点读写模式,即卷可以被一个节点以读写方式挂载 块存储只支持RWO
  10.   # - ReadWriteMany #多个节点读写模式,即卷可以被多个节点以读写方式挂载 NFS
  11.   # - ReadOnlyMany #只读方式绑定多个PVC
  12. persistentVolumeReclaimPolicy: Recycle #持久卷回收策略
  13. storageClassName: nfs # 存储类的名字
  14. nfs:
  15.   path: /nfsdata/share # nfs共享路径
  16.   server: 10.0.17.100 # nfs服务器地址
复制代码
  1. [root@master volume]# kubectl get pv
  2. NAME     CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM             STORAGECLASS   VOLUMEATTRIBUTESCLASS   REASON   AGE
  3. nfspv0   10Gi       RWO            Recycle          Bound    default/nfspvc0   nfs            <unset>
复制代码
pvc
  1. apiVersion: v1
  2. kind: PersistentVolumeClaim
  3. metadata:
  4. name: nfspvc0 # pv 名字
  5. spec:
  6. accessModes: #存储模式
  7.   - ReadWriteOnce #单个节点读写模式,即卷可以被一个节点以读写方式挂载 块存储只支持RWO
  8.   # - ReadWriteMany #多个节点读写模式,即卷可以被多个节点以读写方式挂载 NFS
  9.   # - ReadOnlyMany #只读方式绑定多个PVC
  10. storageClassName: nfs # 存储类的名字
  11. resources:
  12.   requests:
  13.    storage: 5Gi
复制代码
  1. [root@master volume]# kubectl get pvc
  2. NAME      STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
  3. nfspvc0   Bound    nfspv0   10Gi       RWO            nfs            <unset>                 11m
复制代码
创建ubuntu 体系/data绑定nfspvc0
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4.   name: volpod
  5. spec:
  6.   volumes:
  7.   - name: data
  8.     persistentVolumeClaim:
  9.       claimName: nfspvc0 # 使用的pvc
  10.   containers:
  11.   - name: ubuntu-ctr
  12.     image: ubuntu:latest
  13.     command:
  14.     - /bin/bash
  15.     - "-c"
  16.     - "sleep 60m"
  17.     volumeMounts:
  18.     - mountPath: /data # ubuntu系统挂载点
  19.       name: data
复制代码
  1. [root@master volume]# kubectl exec -it volpod -- bash
  2. root@volpod:/# cd /data/
  3. root@volpod:/data# ls
  4. 1  3  8716283  876  default
  5. # 进入/data目录发现/nfsdata/share中的文件
复制代码
StorageClass
  1. yum install -y nfs-utils rpcbind
  2. mkdir /nfsdata/share
  3. chown nobody /nfsdata/share
  4. echo "/nfsdata/share   *(rw,sync,no_subtree_check)" >> /etc/exports
  5. systemctl enable nfs-server && systemctl enable nfs-server
  6. systemctl restart nfs-server && systemctl restart nfs-server
  7. showmount -e master
复制代码
摆设nfs-client-provisioner
  1. vim nfs-client-provisioner.yaml
  2. kind: Deployment
  3. apiVersion: apps/v1
  4. metadata:
  5. name: nfs-client-provisioner
  6. namespace: nfs-storageclass
  7. spec:
  8. replicas: 1
  9. selector:
  10.   matchLabels:
  11.    app: nfs-client-provisioner
  12. strategy:
  13.   type: Recreate
  14. template:
  15.   metadata:
  16.    labels:
  17.     app: nfs-client-provisioner
  18.   spec:
  19.    serviceAccountName: nfs-client-provisioner
  20.    containers:
  21.     - name: nfs-client-provisioner
  22.       # image: registry.k8s.io/sig-storage/nfs-subdir-external-provisioner:v4.0.2
  23.       # image: k8s.dockerproxy.com/sig-storage/nfs-subdir-external-provisioner:v4.0.2
  24.       image: registry.cn-beijing.aliyuncs.com/blice_haiwai/nfs-subdir-external-provisioner:v4.0.2
  25.       volumeMounts:
  26.       - name: nfs-client-root
  27.         mountPath: /persistentvolumes
  28.       env:
  29.       - name: PROVISIONER_NAME
  30.         value: k8s-sigs.io/nfs-subdir-external-provisioner
  31.       - name: NFS_SERVER
  32.        # value: <YOUR NFS SERVER HOSTNAME>
  33.         value: 10.0.17.100
  34.       - name: NFS_PATH
  35.        # value: /var/nfs
  36.         value: /nfsdata/share
  37.    volumes:
  38.     - name: nfs-client-root
  39.       nfs:
  40.        # server: <YOUR NFS SERVER HOSTNAME>
  41.        server: 10.0.17.100
  42.        # share nfs path
  43.        path: /nfsdata/share
复制代码
ca认证
  1. vim RBAC.yaml
  2. apiVersion: v1
  3. kind: ServiceAccount
  4. metadata:
  5. name: nfs-client-provisioner
  6. # replace with namespace where provisioner is deployed
  7. namespace: nfs-storageclass
  8. ---
  9. kind: ClusterRole
  10. apiVersion: rbac.authorization.k8s.io/v1
  11. metadata:
  12. name: nfs-client-provisioner-runner
  13. rules:
  14. - apiGroups: [""]
  15.    resources: ["nodes"]
  16.    verbs: ["get", "list", "watch"]
  17. - apiGroups: [""]
  18.    resources: ["persistentvolumes"]
  19.    verbs: ["get", "list", "watch", "create", "delete"]
  20. - apiGroups: [""]
  21.    resources: ["persistentvolumeclaims"]
  22.    verbs: ["get", "list", "watch", "update"]
  23. - apiGroups: ["storage.k8s.io"]
  24.    resources: ["storageclasses"]
  25.    verbs: ["get", "list", "watch"]
  26. - apiGroups: [""]
  27.    resources: ["events"]
  28.    verbs: ["create", "update", "patch"]
  29. ---
  30. kind: ClusterRoleBinding
  31. apiVersion: rbac.authorization.k8s.io/v1
  32. metadata:
  33. name: run-nfs-client-provisioner
  34. subjects:
  35. #创建名字空间
  36. - kind: ServiceAccount
  37.    name: nfs-client-provisioner
  38.    # replace with namespace where provisioner is deployed
  39.    namespace: nfs-storageclass
  40. roleRef:
  41. kind: ClusterRole
  42. name: nfs-client-provisioner-runner
  43. apiGroup: rbac.authorization.k8s.io
  44. ---
  45. kind: Role
  46. apiVersion: rbac.authorization.k8s.io/v1
  47. metadata:
  48. name: leader-locking-nfs-client-provisioner
  49. # replace with namespace where provisioner is deployed
  50. namespace: nfs-storageclass
  51. rules:
  52. - apiGroups: [""]
  53.    resources: ["endpoints"]
  54.    verbs: ["get", "list", "watch", "create", "update", "patch"]
  55. ---
  56. kind: RoleBinding
  57. apiVersion: rbac.authorization.k8s.io/v1
  58. metadata:
  59. name: leader-locking-nfs-client-provisioner
  60. # replace with namespace where provisioner is deployed
  61. namespace: nfs-storageclass
  62. subjects:
  63. - kind: ServiceAccount
  64.    name: nfs-client-provisioner
  65.    # replace with namespace where provisioner is deployed
  66.    namespace: nfs-storageclass
  67. roleRef:
  68. kind: Role
  69. name: leader-locking-nfs-client-provisioner
  70. apiGroup: rbac.authorization.k8s.io
复制代码
创建StorageClass
  1. vim StorageClass.yaml
  2. apiVersion: storage.k8s.io/v1
  3. kind: StorageClass
  4. metadata:
  5. name: nfs-client
  6. namespace: nfs-storageclass
  7. provisioner: k8s-sigs.io/nfs-subdir-external-provisioner
  8. parameters:
  9. pathPattern: ${.PVC.namespace}/${.PVC.name}
  10. onDelete: delete #删除模式
复制代码
测试pod
  1. vim test.yaml
  2. kind: PersistentVolumeClaim # 创建pvc
  3. apiVersion: v1
  4. metadata:
  5. name: test-claim
  6. annotations:
  7. spec:
  8. accessModes:
  9.    - ReadWriteMany # 多节点读写
  10. resources:
  11.    requests:
  12.      storage: 1Mi # 请求资源大小1MB
  13. storageClassName: nfs-client # 存储类名字
  14. ---
  15. kind: Pod
  16. apiVersion: v1
  17. metadata:
  18. name: test-pod
  19. spec:
  20. containers:
  21. - name: test-pod
  22.    image: wangyanglinux/myapp:v1.0
  23.    volumeMounts:
  24.      - name: nfs-pvc
  25.        mountPath: "/usr/local/nginx/html"
  26. restartPolicy: "Never"
  27. volumes: # 定义卷
  28.    - name: nfs-pvc #pvc提供的卷
  29.      persistentVolumeClaim:
  30.        claimName: test-claim
复制代码
  1. [root@master ~]# ls /nfsdata/share/default/test-claim/
  2. hostname.html  pppppp
复制代码
cm

ConfigMap通常用于存储如下非敏感数据

  • 情况变量的值
  • 整个设置文件(比如Web Server的设置和数据库的设置)
  • 主机名(hostname)
  • 服务端口(Service Port)
  • 账号名称(Account name)
格式:
key: value
主容器的方式

  • 情况变量
  • 容器启动下令参数
  • 某个卷(volume)上的文件 最机动的方式
下令方式创建
  1. # --from-literal 表示字面key=value
  2. kubectl create configmap test1map \
  3. --from-literal shortname=msb.com \
  4. --from-literal longname=magicsandbox.com
  5. [root@master ~]# kubectl get cm test1map
  6. NAME       DATA   AGE
  7. test1map   2      14s
  8. [root@master ~]# kubectl describe cm test1map
  9. Name:         test1map
  10. Namespace:    default
  11. Labels:       <none>
  12. Annotations:  <none>
  13. Data
  14. ====
  15. longname:
  16. ----
  17. magicsandbox.com
  18. shortname:
  19. ----
  20. msb.com
  21. BinaryData
  22. ====
  23. Events:  <none>
  24. # --from-file 表示通过文件创建
  25. [root@master cm]# kubectl create cm testmap2 --from-file test.txt
  26. configmap/testmap2 created
  27. [root@master cm]# kubectl describe cm testmap2
  28. Name:         testmap2
  29. Namespace:    default
  30. Labels:       <none>
  31. Annotations:  <none>
  32. Data
  33. ====
  34. test.txt:
  35. ----
  36. ConfigMap,HelloWorld!
  37. BinaryData
  38. ====
  39. Events:  <none>
  40. [root@master cm]# kubectl get cm testmap2 -o yaml
  41. apiVersion: v1
  42. data:
  43.   test.txt: |
  44.     ConfigMap,HelloWorld!
  45. kind: ConfigMap
  46. metadata:
  47.   creationTimestamp: "2024-09-25T01:34:00Z"
  48.   name: testmap2
  49.   namespace: default
  50.   resourceVersion: "719604"
  51.   uid: 6c0fd794-5e89-40de-b3a3-74c02799f9cd
复制代码
声明式创建
  1. kind: ConfigMap
  2. apiVersion: v1
  3. metadata:
  4.   name: multimap
  5. data:
  6.   given: Nigel
  7.   family: Poulton
复制代码
  1. [root@master cm]# kubectl apply -f multimap.yaml
  2. configmap/multimap created
  3. [root@master cm]# kubectl describe cm multimap
  4. Name:         multimap
  5. Namespace:    default
  6. Labels:       <none>
  7. Annotations:  <none>
  8. Data
  9. ====
  10. family:
  11. ----
  12. Poulton
  13. given:
  14. ----
  15. Nigel
  16. BinaryData
  17. ====
  18. Events:  <none>
复制代码
界说只有一个entry的map
  1. # entry为test.conf | 后面的所有内容需要座位一个字面值看待
  2. key: test.conf
  3. value:     env = plex-test endpoint = 0.0.0.0:31001 char = utf8 vault = PLEX/test log-size = 512M
  4. kind: ConfigMap
  5. apiVersion: v1
  6. metadata:
  7.   name: singlemap
  8. data:
  9.   test.conf: |
  10.     env = plex-test
  11.     endpoint = 0.0.0.0:31001
  12.     char = utf8
  13.     vault = PLEX/test
  14.     log-size = 512M
复制代码
  1. [root@master cm]# kubectl describe cm singlemap
  2. Name:         singlemap
  3. Namespace:    default
  4. Labels:       <none>
  5. Annotations:  <none>
  6. Data
  7. ====
  8. test.conf:
  9. ----
  10. env = plex-test
  11. endpoint = 0.0.0.0:31001
  12. char = utf8
  13. vault = PLEX/test
  14. log-size = 512M
  15. BinaryData
  16. ====
  17. Events:  <none>
复制代码
作为情况变量
  1. kind: ConfigMap
  2. apiVersion: v1
  3. metadata:
  4.   name: multimap
  5. data:
  6.   given: Nigel
  7.   family: Poulton
  8. ---
  9. apiVersion: v1
  10. kind: Pod
  11. metadata:
  12.   labels:
  13.     chapter: configmaps
  14.   name: envpod
  15. spec:
  16.   containers:
  17.     - name: ctr1
  18.       image: busybox
  19.       command: ["sleep"]
  20.       args: ["infinity"]
  21.       env:  # 环境变量配置
  22.         - name: FIRSTNAME # 环境变量名称
  23.           valueFrom:
  24.             configMapKeyRef:
  25.               name: multimap # 需要引用的cm名称
  26.               key: given # 需要引用的key 值为Nigel
  27.         - name: LASTNAME
  28.           valueFrom:
  29.             configMapKeyRef:
  30.               name: multimap
  31.               key: family
复制代码
查察pod情况变量
  1. [root@master cm]# kubectl exec envpod -- env | grep NAME
  2. HOSTNAME=envpod
  3. FIRSTNAME=Nigel
  4. LASTNAME=Poulton
复制代码
作为容器的启动下令
  1. kind: ConfigMap
  2. apiVersion: v1
  3. metadata:
  4.   name: multimap
  5. data:
  6.   given: Nigel
  7.   family: Poulton
  8. ---
  9. apiVersion: v1
  10. kind: Pod
  11. metadata:
  12.   name: startup-pod
  13.   labels:
  14.     chapter: configmaps
  15. spec:
  16.   restartPolicy: OnFailure
  17.   containers:
  18.     - name: args1
  19.       image: busybox
  20.       command: [ "/bin/sh", "-c", "echo First name $(FIRSTNAME) last name $(LASTNAME)", "wait" ] # 输出环境变量中的$(FIRSTNAME)和$(LASTNAME)
  21.       env: # 以环境变量的方式
  22.         - name: FIRSTNAME
  23.           valueFrom:
  24.             configMapKeyRef:
  25.               name: multimap
  26.               key: given
  27.         - name: LASTNAME
  28.           valueFrom:
  29.             configMapKeyRef:
  30.               name: multimap
  31.               key: family
复制代码
  1. [root@master cm]# kubectl describe pod startup-pod
  2.     Environment:
  3.       FIRSTNAME:  <set to the key 'given' of config map 'multimap'>   Optional: false
  4.       LASTNAME:   <set to the key 'family' of config map 'multimap'>  Optional: false
  5. [root@master cm]# kubectl logs startup-pod
  6. First name Nigel last name Poulton
复制代码
ConfigMap与Volume

步调

  • 创建ConfigMap
  • 在Pod模版中创建一个ConfigMap卷
  • 将ConfigMap卷挂载到容器中
  • ConfigMap中的entry会分别作为单独文件出现在容器中
  1. kind: ConfigMap
  2. apiVersion: v1
  3. metadata:
  4.   name: multimap
  5. data:
  6.   given: Nigel
  7.   family: Poulton
  8. ---
  9. apiVersion: v1
  10. kind: Pod
  11. metadata:
  12.   labels:
  13.     chapter: configmaps
  14.   name: volmap
  15. spec:
  16.   volumes:
  17.   - name: volmap
  18.     configMap:
  19.       name: multimap
  20.   containers:
  21.   - name: ctr1
  22.     image: ubuntu
  23.     command: [ "sleep" ]
  24.     args: [ "3600" ]
  25.     volumeMounts:
  26.     - name: volmap
  27.       mountPath: /etc/name
复制代码
  1. kubectl exec -it volmap -- bash
  2. root@volmap:~# ll /etc/name
  3. total 0
  4. drwxrwxrwx 3 root root 87 Sep 25 02:49 ./
  5. drwxr-xr-x 1 root root 18 Sep 25 02:49 ../
  6. drwxr-xr-x 2 root root 33 Sep 25 02:49 ..2024_09_25_02_49_39.2473126764/
  7. lrwxrwxrwx 1 root root 32 Sep 25 02:49 ..data -> ..2024_09_25_02_49_39.2473126764/
  8. lrwxrwxrwx 1 root root 13 Sep 25 02:49 family -> ..data/family
  9. lrwxrwxrwx 1 root root 12 Sep 25 02:49 given -> ..data/given
复制代码
StatefulSet(摆设有状态应用 会话数据 数据库)

StatefulSet特点


  • Pod的名字是可预知和保持稳固的
  • DNS主机名是可预知和保持稳固的
  • 卷绑定是可预知和保持稳固的
创建StatefulSet

起首依照volume章创建StorageClass
后创建governing headless Service 无头服务 管理该StatefulSet全部DNS子域名
  1. # Headless Service for StatefulSet Pod DNS names
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5.   name: dullahan
  6.   labels:
  7.     app: web
  8. spec:
  9.   ports:
  10.   - port: 80
  11.     name: web
  12.   clusterIP: None
  13.   selector:
  14.     app: web
复制代码
摆设StatefulSet
  1. apiVersion: apps/v1
  2. kind: StatefulSet
  3. metadata:
  4.   name: tkb-sts # StatefulSet的名字 所有pod都基于tkb-sts
  5. spec:
  6.   replicas: 3 # 定义了三个副本 tkb-sts-0 tkb-sts-1 tkb-sts-2 依续创建
  7.   selector:
  8.     matchLabels:
  9.       app: web
  10.   serviceName: "dullahan"  # 指定 governing Service 名字为上面创建的Service
  11.   template: # 定义Pod模版
  12.     metadata:
  13.       labels:
  14.         app: web
  15.     spec:
  16.       terminationGracePeriodSeconds: 10
  17.       containers:
  18.       - name: ctr-web
  19.         image: nginx:latest
  20.         ports:
  21.         - containerPort: 80
  22.           name: web
  23.         volumeMounts:
  24.         - name: webroot
  25.           mountPath: /usr/share/nginx/html
  26.   volumeClaimTemplates: # 卷申请模板 每次创建新Pod是,自动创建pvc 自动命名
  27.   - metadata:
  28.       name: webroot
  29.     spec:
  30.       accessModes: [ "ReadWriteOnce" ]
  31.       storageClassName: "nfs-client" # StorageClass名字
  32.       resources:
  33.         requests:
  34.           storage: 1Gi
复制代码
  1. [root@master calico]# kubectl get sts
  2. NAME      READY   AGE
  3. tkb-sts   3/3     14m
  4. [root@master calico]# kubectl get pods -o wide
  5. NAME        READY   STATUS    RESTARTS   AGE     IP               NODE    NOMINATED NODE   READINESS GATES
  6. tkb-sts-0   1/1     Running   0          11m     10.244.166.130   node1   <none>           <none>
  7. tkb-sts-1   1/1     Running   0          7m47s   10.244.104.0     node2   <none>           <none>
  8. tkb-sts-2   1/1     Running   0          7m10s   10.244.104.1     node2   <none>           <none>
  9. [root@master calico]# kubectl get pv
  10. NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                       STORAGECLASS   VOLUMEATTRIBUTESCLASS   REASON   AGE
  11. pvc-381d656d-342f-4276-aa95-7af89ea75ea3   1Gi        RWO            Delete           Bound    default/webroot-tkb-sts-2   nfs-client     <unset>                          9m50s
  12. pvc-c5f418d8-90b7-40aa-adeb-e5cb63ce9cf6   1Gi        RWO            Delete           Bound    default/webroot-tkb-sts-0   nfs-client     <unset>                          14m
  13. pvc-d9004ba6-3a5f-4251-b741-21e2adc8a9a3   1Gi        RWO            Delete           Bound    default/webroot-tkb-sts-1   nfs-client     <unset>                          10m
  14. [root@master calico]# kubectl get pvc
  15. NAME                STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
  16. webroot-tkb-sts-0   Bound    pvc-c5f418d8-90b7-40aa-adeb-e5cb63ce9cf6   1Gi        RWO            nfs-client     <unset>                 14m
  17. webroot-tkb-sts-1   Bound    pvc-d9004ba6-3a5f-4251-b741-21e2adc8a9a3   1Gi        RWO            nfs-client     <unset>                 10m
  18. webroot-tkb-sts-2   Bound    pvc-381d656d-342f-4276-aa95-7af89ea75ea3   1Gi        RWO            nfs-client     <unset>                 10m
  19. [root@master ~]# kubectl get svc -o wide
  20. NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE   SELECTOR
  21. dullahan     ClusterIP   None           <none>        80/TCP           53m   app=web
复制代码
对点测试
摆设jumppod
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4.   name: jump-pod
  5. spec:
  6.   terminationGracePeriodSeconds: 1
  7.   containers:
  8.   - image: nigelpoulton/curl:1.0
  9.     name: jump-ctr
  10.     tty: true
  11.     stdin: true
复制代码
进入jump测试
  1. [root@master ~]# kubectl exec -it jump-pod -- bash
  2. root@jump-pod:/# dig SRV dullahan.default.svc.cluster.local
  3. ;; ANSWER SECTION:
  4. dullahan.default.svc.cluster.local. 30 IN SRV   0 33 80 tkb-sts-0.dullahan.default.svc.cluster.local.
  5. dullahan.default.svc.cluster.local. 30 IN SRV   0 33 80 tkb-sts-1.dullahan.default.svc.cluster.local.
  6. dullahan.default.svc.cluster.local. 30 IN SRV   0 33 80 tkb-sts-2.dullahan.default.svc.cluster.local.
复制代码
分析不到删除pod coredns 让k8s自愈
新建一个ubuntu pod
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4.   name: ubuntu-pod
  5.   labels:
  6.     app: web # svc提供服务标识
  7. spec:
  8.   containers:
  9.   - name: ubuntu
  10.     image: ubuntu
  11.     command: ["sleep"]
  12.     args: ["3600"]
  13.     ports:
  14.         - containerPort: 80
  15.           name: web
复制代码
  1. root@jump-pod:/# dig SRV dullahan.default.svc.cluster.local
  2. tkb-sts-1.dullahan.default.svc.cluster.local. 30 IN A 10.244.104.0
  3. tkb-sts-0.dullahan.default.svc.cluster.local. 30 IN A 10.244.166.130
  4. tkb-sts-2.dullahan.default.svc.cluster.local. 30 IN A 10.244.104.1
  5. 10-244-166-133.dullahan.default.svc.cluster.local. 30 IN A 10.244.166.133
复制代码
StatefulSet扩缩容

修改StatefulSet内容
  1. replicas: 2
  2. [root@master ~]# kubectl edit sts tkb-sts
  3. statefulset.apps/tkb-sts edited
  4. [root@master ~]# kubectl get sts
  5. NAME      READY   AGE
  6. tkb-sts   2/2     19h
  7. [root@master ~]# kubectl get pods
  8. NAME         READY   STATUS    RESTARTS   AGE   
  9. tkb-sts-0    1/1     Running   0          19h
  10. tkb-sts-1    1/1     Running   0          19h
  11. # 查看pvc还有3个  缩容扩容不会删除pod副本相关的pvc
  12. [root@master ~]# kubectl get pvc
  13. NAME                STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
  14. webroot-tkb-sts-0   Bound    pvc-c5f418d8-90b7-40aa-adeb-e5cb63ce9cf6   1Gi        RWO            nfs-client     <unset>                 19h
  15. webroot-tkb-sts-1   Bound    pvc-d9004ba6-3a5f-4251-b741-21e2adc8a9a3   1Gi        RWO            nfs-client     <unset>                 19h
  16. webroot-tkb-sts-2   Bound    pvc-381d656d-342f-4276-aa95-7af89ea75ea3   1Gi        RWO            nfs-client     <unset>                 19h
  17. #查看挂载情况
  18. [root@master ~]# kubectl describe pvc webroot-tkb-sts-0 | grep Used
  19. Used By:       tkb-sts-0
  20. [root@master ~]# kubectl describe pvc webroot-tkb-sts-1 | grep Used
  21. Used By:       tkb-sts-1
  22. [root@master ~]# kubectl describe pvc webroot-tkb-sts-2 | grep Used
  23. Used By:       <none>
  24. # 修改StatefulSet 副本数量为4
  25. [root@master ~]# kubectl get sts
  26. NAME      READY   AGE
  27. tkb-sts   4/4     19h
  28. [root@master ~]# kubectl get pvc
  29. NAME                STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
  30. webroot-tkb-sts-0   Bound    pvc-c5f418d8-90b7-40aa-adeb-e5cb63ce9cf6   1Gi        RWO            nfs-client     <unset>                 19h
  31. webroot-tkb-sts-1   Bound    pvc-d9004ba6-3a5f-4251-b741-21e2adc8a9a3   1Gi        RWO            nfs-client     <unset>                 19h
  32. webroot-tkb-sts-2   Bound    pvc-381d656d-342f-4276-aa95-7af89ea75ea3   1Gi        RWO            nfs-client     <unset>                 19h
  33. webroot-tkb-sts-3   Bound    pvc-aa15e166-d805-4820-a592-1350bcfcb179   1Gi        RWO            nfs-client     <unset>                 43s
  34. # 查看挂载情况
  35. [root@master ~]# kubectl describe pvc webroot-tkb-sts-0 | grep Used
  36. Used By:       tkb-sts-0
  37. [root@master ~]# kubectl describe pvc webroot-tkb-sts-1 | grep Used
  38. Used By:       tkb-sts-1
  39. [root@master ~]# kubectl describe pvc webroot-tkb-sts-2 | grep Used
  40. Used By:       tkb-sts-2
  41. [root@master ~]# kubectl describe pvc webroot-tkb-sts-3 | grep Used
  42. Used By:       tkb-sts-3
  43. # 查看nfs挂载点的文件夹
  44. [root@master ~]# ls /nfsdata/share/default/
  45. webroot-tkb-sts-0  webroot-tkb-sts-1  webroot-tkb-sts-2  webroot-tkb-sts-3
复制代码
调用Pod次序

通过spec.PodManagermentPolicy控制Pod启动和制止次序

  • OrderedReady 按序管理计谋
  • Prarllel pod创建删除并行
实验滚动升级

升级按次序从索引号最大的Pod开始,每次更新一个,直到最小索引号的Pod
模仿故障

删除Pod 查察状态
  1. [root@master ~]# kubectl get pods
  2. NAME        READY   STATUS    RESTARTS   AGE
  3. tkb-sts-0   1/1     Running   0          19h
  4. tkb-sts-1   1/1     Running   0          19h
  5. tkb-sts-2   1/1     Running   0          12m
  6. tkb-sts-3   1/1     Running   0          12m
  7. [root@master ~]# kubectl describe pod tkb-sts-1
  8. Name:             tkb-sts-1
  9. Namespace:        default
  10. Status:           Running
  11. IP:               10.244.104.0
  12. Volumes:
  13.   webroot:
  14.     Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
  15.     ClaimName:  webroot-tkb-sts-1
  16.   
  17. [root@master ~]# kubectl delete pod tkb-sts-1
  18. [root@master ~]# kubectl get pods -w
  19. NAME        READY   STATUS        RESTARTS   AGE
  20. tkb-sts-0   1/1     Running       0          19h
  21. tkb-sts-1   0/1     Terminating   0          1s
  22. tkb-sts-2   1/1     Running       0          15m
  23. tkb-sts-3   1/1     Running       0          15m
  24. tkb-sts-1   0/1     Terminating   0          2s
  25. tkb-sts-1   0/1     Terminating   0          2s
  26. tkb-sts-1   0/1     Terminating   0          2s
  27. tkb-sts-1   0/1     Pending       0          0s
  28. tkb-sts-1   0/1     Pending       0          0s
  29. tkb-sts-1   0/1     ContainerCreating   0          0s
  30. tkb-sts-1   0/1     ContainerCreating   0          0s
  31. [root@master ~]# kubectl describe pod tkb-sts-1 | grep ClaimName
  32.     ClaimName:  webroot-tkb-sts-1
复制代码
删除StatefulSet

按次序关闭Pod
  1. [root@master ~]# kubectl scale statefulset tkb-sts --replicas=0
  2. statefulset.apps/tkb-sts scaled
  3. [root@master ~]# kubectl get sts tkb-sts
  4. NAME      READY   AGE
  5. tkb-sts   0/0     19h
  6. [root@master ~]# kubectl delete sts tkb-sts
  7. statefulset.apps "tkb-sts" deleted
  8. [root@master ~]# kubectl delete svc dullahan
  9. service "dullahan" deleted
  10. [root@master ~]# kubectl delete pvc webroot-tkb-sts-0 webroot-tkb-sts-1 webroot-tkb-sts-2 webroot-tkb-sts-3
  11. persistentvolumeclaim "webroot-tkb-sts-0" deleted
  12. persistentvolumeclaim "webroot-tkb-sts-1" deleted
  13. persistentvolumeclaim "webroot-tkb-sts-2" deleted
  14. persistentvolumeclaim "webroot-tkb-sts-3" deleted
  15. #由于使用了StorageClass pvc删除后 pv自动删除 但是文件还在 保证持久化
  16. [root@master ~]# ls /nfsdata/share/default/
  17. webroot-tkb-sts-0  webroot-tkb-sts-2
  18. webroot-tkb-sts-1  webroot-tkb-sts-3
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表