k8s 之statefulset有状态应用

StatefulSet 由以下几个部分组成:

1. Headless Service:用来定义 pod 网路标识,生成可解析的 DNS 记录

2. volumeClaimTemplates:存储卷申请模板,创建 pvc,指定 pvc 名称大小,自动创建 pvc,且 pvc 由存储类供应。

3. StatefulSet:管理 pod 的

Headless service 不分配 clusterIP,headless service 可以通过解析 service 的 DNS,返回所 有 Pod 的 dns 和 ip 地址 (statefulSet 部署的 Pod 才有 DNS),普通的 service,只能通过解析 service 的 DNS 返回 service 的 ClusterIP。

在用 statefulset 管理 pod 时要求 pod 名称必须是有序的 ,每一个 pod 不能被随意取代,pod 重建后 pod 名称还是 一样的。因为 pod IP 是变化的,所以要用 Pod 名称来识别。pod 名称是 pod 唯一性的标识符, 必须持久稳定有效。这时候要用到无头服务,它可以给每个 Pod 一个唯一的名称。

解析

<service name>.$<namespace name>.svc.cluster.local

  

Statefulset 资源清单文件编写

[root@master-1 statefulset]# cat statefulset.yaml 
apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports: 
  - port: 80
    name: web
  clusterIP: None
  selector: 
    app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "nginx"
  replicas: 2
  selector:
    matchLabels:
      app: nginx

  template: 
    metadata: 
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:  #动态申请存储
  - metadata:
       name: www
    spec:
      accessModes: 
      - ReadWriteOnce
      resources:
        requests:
          storage: 1Gi  
[root@master-1 statefulset]# kubectl apply -f statefulset.yaml 
service/nginx unchanged
statefulset.apps/web created
[root@master-1 statefulset]# kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                   STORAGECLASS          REASON   AGE
pvc-014f93a3-c32a-4a67-8d89-5601c4ebc85c   1Gi        RWX            Delete           Bound    default/test-claim      managed-nfs-storage            131m
pvc-2f3fd42e-20bf-45a2-a743-36995db15701   2Gi        RWO            Delete           Bound    default/www-storage-1   managed-nfs-storage            33m
pvc-4a77164b-ec51-4a0f-8070-4fc4721288c5   1Gi        RWX            Delete           Bound    default/test-claim1-1   managed-nfs-storage            130m
pvc-4a8303f8-38fe-42f2-994b-ed4626d9ac5e   2Gi        RWO            Delete           Bound    default/www-storage-0   managed-nfs-storage            33m
pvc-a7ea9d95-cd35-4e64-8a91-aadd0470a5c0   1Gi        RWO            Delete           Bound    default/www-web-1       managed-nfs-storage            3s
pvc-c0aec411-f567-4926-b2a4-bf84883e9392   1Gi        RWO            Delete           Bound    default/www-web-0       managed-nfs-storage            14s
[root@master-1 statefulset]# kubectl get pvc
NAME            STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS          AGE
test-claim      Bound    pvc-014f93a3-c32a-4a67-8d89-5601c4ebc85c   1Gi        RWX            managed-nfs-storage   131m
test-claim1-1   Bound    pvc-4a77164b-ec51-4a0f-8070-4fc4721288c5   1Gi        RWX            managed-nfs-storage   130m
www-storage-0   Bound    pvc-4a8303f8-38fe-42f2-994b-ed4626d9ac5e   2Gi        RWO            managed-nfs-storage   33m
www-storage-1   Bound    pvc-2f3fd42e-20bf-45a2-a743-36995db15701   2Gi        RWO            managed-nfs-storage   33m
www-web-0       Bound    pvc-c0aec411-f567-4926-b2a4-bf84883e9392   1Gi        RWO            managed-nfs-storage   12s
www-web-1       Bound    pvc-a7ea9d95-cd35-4e64-8a91-aadd0470a5c0   1Gi        RWO            managed-nfs-storage   1s

  动态跟踪pod

[root@master-1 statefulset]# kubectl get pods -l app=nginx -w

  循环查看主机名

[root@master-1 statefulset]# for i in 0 1;do kubectl exec web-$i -- sh -c 'hostname';done
web-0
web-1

  进入pod 容器

[root@master-1 statefulset]# kubectl exec -it web-1 -c nginx -- /bin/sh
# /bin/bash
root@web-1:/#  apt install -y dnsutils
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  bind9-dnsutils bind9-host bind9-libs libedit2 libfstrm0 libjson-c5 liblmdb0 libmaxminddb0 libprotobuf-c1 libuv1
Suggested packages:
  mmdb-bin
The following NEW packages will be installed:
  bind9-dnsutils bind9-host bind9-libs dnsutils libedit2 libfstrm0 libjson-c5 liblmdb0 libmaxminddb0 libprotobuf-c1 libuv1
0 upgraded, 11 newly installed, 0 to remove and 0 not upgraded.
Need to get 2798 kB of archives.
After this operation, 6269 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian bullseye/main amd64 libuv1 amd64 1.40.0-2 [132 kB]
Get:2 http://deb.debian.org/debian bullseye/main amd64 libfstrm0 amd64 0.6.0-1+b1 [21.5 kB]
Get:3 http://deb.debian.org/debian bullseye/main amd64 libjson-c5 amd64 0.15-2 [42.8 kB]
Get:4 http://deb.debian.org/debian bullseye/main amd64 liblmdb0 amd64 0.9.24-1 [45.0 kB]
Get:5 http://deb.debian.org/debian bullseye/main amd64 libmaxminddb0 amd64 1.5.2-1 [29.8 kB]
Get:6 http://deb.debian.org/debian bullseye/main amd64 libprotobuf-c1 amd64 1.3.3-1+b2 [27.0 kB]
Get:7 http://deb.debian.org/debian bullseye/main amd64 bind9-libs amd64 1:9.16.37-1~deb11u1 [1424 kB]
Get:8 http://deb.debian.org/debian bullseye/main amd64 bind9-host amd64 1:9.16.37-1~deb11u1 [308 kB]
Get:9 http://deb.debian.org/debian bullseye/main amd64 libedit2 amd64 3.1-20191231-2+b1 [96.7 kB]
Get:10 http://deb.debian.org/debian bullseye/main amd64 bind9-dnsutils amd64 1:9.16.37-1~deb11u1 [404 kB]
Get:11 http://deb.debian.org/debian bullseye/main amd64 dnsutils all 1:9.16.37-1~deb11u1 [267 kB]
Fetched 2798 kB in 1s (3423 kB/s)  
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libuv1:amd64.
(Reading database ... 7826 files and directories currently installed.)
Preparing to unpack .../00-libuv1_1.40.0-2_amd64.deb ...
Unpacking libuv1:amd64 (1.40.0-2) ...........................................................................................................................................................................] 
Selecting previously unselected package libfstrm0:amd64......................................................................................................................................................] 
Preparing to unpack .../01-libfstrm0_0.6.0-1+b1_amd64.deb ...
Unpacking libfstrm0:amd64 (0.6.0-1+b1) ......................................................................................................................................................................] 
Selecting previously unselected package libjson-c5:amd64.....................................................................................................................................................] 
Preparing to unpack .../02-libjson-c5_0.15-2_amd64.deb ...
Unpacking libjson-c5:amd64 (0.15-2) .........................................................................................................................................................................] 
Selecting previously unselected package liblmdb0:amd64.......................................................................................................................................................] 
Preparing to unpack .../03-liblmdb0_0.9.24-1_amd64.deb ...
Unpacking liblmdb0:amd64 (0.9.24-1) ...########..............................................................................................................................................................] 
Selecting previously unselected package libmaxminddb0:amd64..................................................................................................................................................] 
Preparing to unpack .../04-libmaxminddb0_1.5.2-1_amd64.deb ...
Unpacking libmaxminddb0:amd64 (1.5.2-1) ...############......................................................................................................................................................] 
Selecting previously unselected package libprotobuf-c1:amd64.................................................................................................................................................] 
Preparing to unpack .../05-libprotobuf-c1_1.3.3-1+b2_amd64.deb ...
Unpacking libprotobuf-c1:amd64 (1.3.3-1+b2) ...################..............................................................................................................................................] 
Selecting previously unselected package bind9-libs:amd64.##########..........................................................................................................................................] 
Preparing to unpack .../06-bind9-libs_1%3a9.16.37-1~deb11u1_amd64.deb ...
Unpacking bind9-libs:amd64 (1:9.16.37-1~deb11u1) ...####################.....................................................................................................................................] 
Selecting previously unselected package bind9-host.#########################.................................................................................................................................] 
Preparing to unpack .../07-bind9-host_1%3a9.16.37-1~deb11u1_amd64.deb ...
Unpacking bind9-host (1:9.16.37-1~deb11u1) ...##################################.............................................................................................................................] 
Selecting previously unselected package libedit2:amd64.#############################.........................................................................................................................] 
Preparing to unpack .../08-libedit2_3.1-20191231-2+b1_amd64.deb ...
Unpacking libedit2:amd64 (3.1-20191231-2+b1) ...########################################.....................................................................................................................] 
Selecting previously unselected package bind9-dnsutils.#####################################.................................................................................................................] 
Preparing to unpack .../09-bind9-dnsutils_1%3a9.16.37-1~deb11u1_amd64.deb ...
Unpacking bind9-dnsutils (1:9.16.37-1~deb11u1) ...##############################################.............................................................................................................] 
Selecting previously unselected package dnsutils.####################################################........................................................................................................] 
Preparing to unpack .../10-dnsutils_1%3a9.16.37-1~deb11u1_all.deb ...
Unpacking dnsutils (1:9.16.37-1~deb11u1) ...#############################################################....................................................................................................] 
Setting up liblmdb0:amd64 (0.9.24-1) ...#####################################################################................................................................................................] 
Setting up libmaxminddb0:amd64 (1.5.2-1) ...#########################################################################........................................................................................] 
Setting up libfstrm0:amd64 (0.6.0-1+b1) ...###################################################################################...............................................................................] 
Setting up libedit2:amd64 (3.1-20191231-2+b1) ...#####################################################################################.......................................................................] 
Setting up libprotobuf-c1:amd64 (1.3.3-1+b2) ...##############################################################################################...............................................................] 
Setting up libuv1:amd64 (1.40.0-2) ...################################################################################################################.......................................................] 
Setting up libjson-c5:amd64 (0.15-2) ...#######################################################################################################################..............................................] 
Setting up bind9-libs:amd64 (1:9.16.37-1~deb11u1) ...##################################################################################################################......................................] 
Setting up bind9-host (1:9.16.37-1~deb11u1) ...################################################################################################################################..............................] 
Setting up bind9-dnsutils (1:9.16.37-1~deb11u1) ...#####################################################################################################################################.....................] 
Setting up dnsutils (1:9.16.37-1~deb11u1) ...###################################################################################################################################################.............] 
Processing triggers for libc-bin (2.31-13+deb11u6) ...##################################################################################################################################################.....] 

# statefulset中pod DNS 记录组成部分 statefulset名字-随机数.servicename.namespace(名称空间).svc.cluster.local.
root@web-1:/# nslookup web-0.nginx.default.svc.cluster.local.
Server:		10.96.0.10
Address:	10.96.0.10#53

Name:	web-0.nginx.default.svc.cluster.local
Address: 10.244.84.166

[root@master-1 statefulset]# kubectl get pods -o wide  查看pod 详细信息对比ip
NAME                                      READY   STATUS    RESTARTS   AGE     IP              NODE     NOMINATED NODE   READINESS GATES
nfs-client-provisioner-7c7bf9f6bd-dp5h8   1/1     Running   0          3h26m   10.244.84.156   node-1   <none>           <none>
nginx-test                                1/1     Running   4          105d    10.244.84.148   node-1   <none>           <none>
read-pod                                  1/1     Running   0          173m    10.244.84.157   node-1   <none>           <none>
storage-0                                 1/1     Running   0          75m     10.244.84.164   node-1   <none>           <none>
storage-1                                 1/1     Running   0          75m     10.244.84.165   node-1   <none>           <none>
web-0                                     1/1     Running   0          51m     10.244.84.166   node-1   <none>           <none>
web-1                                     1/1     Running   0          51m     10.244.84.167   node-1   <none>           <none>
root@web-1:/# apt install -y inetutils-ping
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  netbase
The following NEW packages will be installed:
  inetutils-ping netbase
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 265 kB of archives.
After this operation, 416 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian bullseye/main amd64 netbase all 6.3 [19.9 kB]
Get:2 http://deb.debian.org/debian bullseye/main amd64 inetutils-ping amd64 2:2.0-1+deb11u1 [245 kB]
Fetched 265 kB in 0s (904 kB/s)     
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package netbase.
(Reading database ... 7917 files and directories currently installed.)
Preparing to unpack .../archives/netbase_6.3_all.deb ...
Unpacking netbase (6.3) ...###########.......................................................................................................................................................................] 
Selecting previously unselected package inetutils-ping.####..................................................................................................................................................] 
Preparing to unpack .../inetutils-ping_2%3a2.0-1+deb11u1_amd64.deb ...
Unpacking inetutils-ping (2:2.0-1+deb11u1) ...##################################.............................................................................................................................] 
Setting up netbase (6.3) ...#########################################################################........................................................................................................] 
Setting up inetutils-ping (2:2.0-1+deb11u1) ...###############################################################################################...............................................................] 
root@web-1:/# ping web-0.nginx.default.svc.cluster.local
PING web-0.nginx.default.svc.cluster.local (10.244.84.166): 56 data bytes
64 bytes from 10.244.84.166: icmp_seq=0 ttl=63 time=0.150 ms
64 bytes from 10.244.84.166: icmp_seq=1 ttl=63 time=0.109 ms
64 bytes from 10.244.84.166: icmp_seq=2 ttl=63 time=0.073 ms
^C--- web-0.nginx.default.svc.cluster.local ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.073/0.111/0.150/0.031 ms

  

posted @ 2023-05-14 17:27  烟雨楼台,行云流水  阅读(79)  评论(0编辑  收藏  举报