K8S (k8s helm)

K8S 什么是 有状态服务和 无状态服务
有状态服务(Stateful Service): 有状态服务是指在处理请求期间维护和跟踪用户状态或会话信息的服务。这意味着服务在多个请求之间保持状态&#

定期部署和扩缩容:StatefulSet支持定期部署和扩缩容。当您扩展StatefulSet 时,Kubernetes 会按照指定的顺序创建新的Pod,确保创建的第一个Pod 第一个可用。这对于一些需要保证主节点先启动、从节点启动的有状态应用非常重要,比如数据库集群中的主从复制关系。

持久存储:StatefulSet 可以与持久卷和持久卷声明结合使用,以确保有状态应用程序在Pod 重新创建期间保留数据。您可以将每个Pod 绑定到独立的持久存储卷,以保证数据的持久性和可靠性。

管理有状态应用程序:StatefulSet 提供了多种用于管理有状态应用程序的功能。例如,您可以使用rollingUpdate策略来控制有状态应用的滚动更新过程,保证更新的稳定性。此外,StatefulSet支持有状态应用的有序移除,以及与有状态应用相关的服务发现和DNS解析。

什么是 有状态服务和 无状态服务

首先写入yaml文件:

有状态的nginx-without-pvc.yaml:

apiVersion: v1 # API 版本

kind: 此资源的服务编号类型(例如Pod/Deployment .)

元数据:

name: nginx-stateful-service # 服务名称

标签:

app: nginx 有状态服务

规格:

端口:

– port: 80 # 用于访问服务的服务端口

name: Web端口

clusterIP: None # 服务不暴露在集群之外

selector: # Service选择的Pod的标签

app: nginx-stateful # 只有服务选择器可以跳过matchLabels:

apiVersion: 应用程序/v1

kind: StatefulSet # 用于有状态应用程序,是一个控制器

元数据:

name: nginx-statefulset # 有状态集名称

标签:

app: nginx 有状态

spec: #详细解释

serviceName: \’nginx-stateful-service\’ # 用于管理DNS 的服务名称,

# 必须与上面定义的服务名称相同

plicas: 3 # 所需的副本数量

selecter: # StatefulSet管理的Pod的标签

匹配标签:

app: nginx 有状态

template: # Pod 模板

元数据:

标签:

app: nginx 有状态

规格:

集装箱:

– name: nginx 容器

image: nginx:1.25.4 # 容器镜像

ports: # 容器端口,它们将被暴露

-containerPort: 80 # 容器服务使用的端口

name: Web端口

上面的yaml文件创建了两个资源

1 是有状态集部署对象

1是服务

但严格来说,上面部署的服务是无状态的。然而,在本节中,我们将首先关注创建有状态集。

为什么我仍然需要该服务?

关键是下面的服务名称标签

serviceName: \’nginx-stateful-service\’ # 用于管理DNS 的服务名称,

# 必须与上面定义的服务名相同

serviceName字段的目的是为StatefulSet中的每个pod提供稳定的网络标识符。当StatefulSet 创建Pod 时,每个Pod 都会以其索引号作为后缀,形成唯一的DNS 名称。此DNS 名称由pod-name.service-name.namespace.svc.cluster.local 组成。

指定serviceName 字段会将StatefulSet 与服务关联起来,并允许每个Pod 使用服务的名称作为其DNS 标识符。这样,每个Pod就可以通过服务名称在网络上进行通信,而无需知道其他Pod的具体名称或IP地址。

当你运行这个配置文件时,你会看到pod0被创建,然后pod1被依次创建为pod2。这就是创建pod 和部署pod 之间的巨大区别。

[gateman@manjaro-x13 有状态集]$ kubectl apply -f stateful-nginx-without-pvc.yaml

已创建服务/nginx-stateful-service

statefulset.apps/nginx-statefulset 已创建

[gateman@manjaro-x13 状态集]$ bash show.sh

+ 使用kubectl 获取PVC

在默认命名空间中找不到资源。

+ 使用kubectl 获取SVC

名称类型集群IP 外部IP 端口期限

bq-api-service-1 节点端口10.103.40.130 无32111:30604/TCP 15h

kubernetes ClusterIP 10.96.0.1 无443/TCP 76d

nginx-stateful-service ClusterIP 无无80/TCP 6s

+ 获取kubectl sts

被命名的年龄

nginx-statefulset 1/3 6s

+ 获取kubectl Pod

姓名准备状态简历年龄

bq-api-service-deployment-978b76fcf-4qcsp 1/1 运行0 5 小时20 分钟

bq-api-service-deployment-978b76fcf-7x54w 1/1 运行0 5 小时20 分钟

bq-api-service-deployment-978b76fcf-xfxcw 1/1 运行0 5 小时20 分钟

nginx-statefulset-0 1/1 运行0 7 秒

nginx-statefulset-1 0/1 容器创建0 5s

[gateman@manjaro-x13 状态集]$

[gateman@manjaro-x13 状态集]$ bash show.sh

+ 使用kubectl 获取PVC

在默认命名空间中找不到资源。

+ 使用kubectl 获取SVC

名称类型集群IP 外部IP 端口期限

bq-api-service-1 节点端口10.103.40.130 无32111:30604/TCP 15h

kubernetes ClusterIP 10.96.0.1 无443/TCP 76d

nginx-stateful-service ClusterIP 无无80/TCP 15 秒

+ 获取kubectl sts

被命名的年龄

nginx-statefulset 2/3 15 秒

+ 获取kubectl pod

姓名准备状态简历年龄

bq-api-service-deployment-978b76fcf-4qcsp 1/1 运行0 5 小时20 分钟

bq-api-service-deployment-978b76fcf-7x54w 1/1 运行0 5 小时20 分钟

bq-api-service-deployment-978b76fcf-xfxcw 1/1 运行0 5 小时20 分钟

nginx-statefulset-0 1/1 运行0 16 秒

nginx-statefulset-1 1/1 运行0 14 秒

nginx-statefulset-2 0/1 容器创建0 7s

[gateman@manjaro-x13 状态集]$ bash show.sh

+ 使用kubectl 获取PVC

在默认命名空间中找不到资源。

+ 使用kubectl 获取SVC

名称类型集群IP 外部IP 端口期限

bq-api-service-1 节点端口10.103.40.130 无32111:30604/TCP 15h

kubernetes ClusterIP 10.96.0.1 无443/TCP 76d

nginx-stateful-service ClusterIP 无无80/TCP 52 秒

+ 获取kubectl sts

被命名的年龄

nginx-statefulset 3/3 52 秒

+ 获取kubectl pod

姓名准备状态简历年龄

bq-api-service-deployment-978b76fcf-4qcsp 1/1 运行0 5 小时20 分钟

bq-api-service-deployment-978b76fcf-7x54w 1/1 运行0 5 小时20 分钟

bq-api-service-deployment-978b76fcf-xfxcw 1/1 运行0 5 小时20 分钟

nginx-statefulset-0 1/1 运行0 53 秒

nginx-statefulset-1 1/1 运行0 51 秒

nginx-statefulset-2 1/1 运行0 44 秒

重要的一点是,与上面的部署Pod 不同,部署Pod 是无序的,并且后面跟着无意义的字符串,而有状态集Pod 是严格按顺序创建的。 POD名称的后缀是索引号。

K8S 的deployment 是不是只能用于无状态服务

服务创建后

默认情况下,不能在节点内直接访问它。例如,

root@k8s-master:~# ping nginx-statefulset-0.nginx-stateful-service

ping: nginx-statefulset-0.nginx-stateful-service: 名称或服务未知

root@k8s-master:~# ping nginx-statefulset-0.nginx-stateful-service.default.svc.cluster.local

ping: nginx-statefulset-0.nginx-stateful-service.default.svc.cluster.local: 名称或服务未知

如前所述,本例中服务的功能是为Pod 提供稳定的DNS 名称。

但是无所谓

缩写:$podName.$serviceName

nginx-statefulset-0.nginx-stateful-service

或者写下整个内容:$podName.$serviceName.$nameSpace.svc.cluster.local

nginx-statefulset-0.nginx-stateful-service.default.svc.cluster.local

在k8s-master 上无法访问

原因是这些DNS 只能在k8s pod 内访问。

为了测试,使用busybox 临时创建一个pod

kubectl run -it –imagebusybox:1.28 dns-test –restart=从不/bin/sh

请注意,默认情况下,busybox 不会安装bash 和curl,但会安装ping 和nslookup。

现在你可以ping 通了

/# ping nginx-statefulset-0.nginx-stateful-service

PING nginx-statefulset-0.nginx-stateful-service (10.244.2.109): 56 个数据字节

来自10.244.2.109: 的64 个字节seq=0 ttl=64 time=14.367 ms

64 字节seq=1 ttl=64 time=0.093 ms 来自10.244.2.109:

ns查找结果

在k8s容器中,可以看到DNS服务器为10.96.0.10 kube-dns.kube-system.svc.cluster.local。

放置nginx-statefulset-0.nginx-stateful-service

10.244.2.109 解释为nginx-statefulset-0.nginx-stateful-service.default.svc.cluster.local

10.244.2.109 是pod 的IP,DNS 域名为nginx-statefulset-0.nginx-stateful-service.default.svc.cluster.local。

/# nslookup nginx-statefulset-0.nginx-stateful-service

服务器: 10.96.0.10

地址1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

name: nginx-statefulset-0.nginx-stateful-service

地址1: 10.244.2.109 nginx-statefulset-0.nginx-stateful-service.default.svc.cluster.local

/# nslookup nginx-statefulset-1.nginx-stateful-service

服务器: 10.96.0.10

地址1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

name: nginx-statefulset-1.nginx-stateful-service

地址1: 10.244.3.62 nginx-st

atefulset-1.nginx-stateful-service.default.svc.cluster.local
/ # nslookup nginx-statefulset-2.nginx-stateful-service
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: nginx-statefulset-2.nginx-stateful-service
Address 1: 10.244.1.53 nginx-statefulset-2.nginx-stateful-service.default.svc.cluster.local

在容器内用该域名是可以访问ngnix服务的

/ # wget http://nginx-statefulset-0.nginx-stateful-service.default.svc.cluster.local
Connecting to nginx-statefulset-0.nginx-stateful-service.default.svc.cluster.local (10.244.2.109:80)
index.html 100% |******************************************************************************************************************************************************************| 615 0:00:00 ETA
/ # cat index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href=\”http://nginx.org/\”>nginx.org</a>.<br/>
Commercial support is available at
<a href=\”http://nginx.com/\”>nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

StatefulSet 的scale – 扩容和缩容

命令有两种

kubectl scale statefulset \\$statefulsetName –replicas=5

# another method
kubectl patch statefulset statefulsetName -p \'{\”spec\”:{\”replicas\”:5}}\’

两种命令都可以用来扩容和缩容

例如:
用第一命令去把 replicas 从3 改成10

[gateman@manjaro-x13 statefulsets]$ kubectl scale statefulset nginx-statefulset –replicas=10
statefulset.apps/nginx-statefulset scaled
[gateman@manjaro-x13 statefulsets]$
[gateman@manjaro-x13 statefulsets]$ kubectl describe sts nginx-statefulset
Name: nginx-statefulset
Namespace: default
CreationTimestamp: Sat, 22 Jun 2024 19:48:11 +0800
Selector: app=nginx-stateful
Labels: app=nginx-stateful
Annotations: <none>
Replicas: 10 desired | 9 total
Update Strategy: RollingUpdate
Partition: 0
Pods Status: 9 Running / 1 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=nginx-stateful
Containers:
nginx-container:
Image: nginx:1.25.4
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Node-Selectors: <none>
Tolerations: <none>
Volume Claims: <none>
Events:
Type Reason Age From Message
—- —— —- —- ——-
Normal SuccessfulCreate 36s (x2 over 98s) statefulset-controller create Pod nginx-statefulset-3 in StatefulSet nginx-statefulset successful
Normal SuccessfulCreate 35s (x2 over 97s) statefulset-controller create Pod nginx-statefulset-4 in StatefulSet nginx-statefulset successful
Normal SuccessfulCreate 34s statefulset-controller create Pod nginx-statefulset-5 in StatefulSet nginx-statefulset successful
Normal SuccessfulCreate 32s statefulset-controller create Pod nginx-statefulset-6 in StatefulSet nginx-statefulset successful
Normal SuccessfulCreate 31s statefulset-controller create Pod nginx-statefulset-7 in StatefulSet nginx-statefulset successful
Normal SuccessfulCreate 29s statefulset-controller create Pod nginx-statefulset-8 in StatefulSet nginx-statefulset successful
Normal SuccessfulCreate 28s statefulset-controller create Pod nginx-statefulset-9 in StatefulSet nginx-statefulset successful

通过describe 命令 可以见 当前运行的副本的确变成10 (9 running 还有1个正在创建)
而且pods 的创建是按顺序执行的

StatefulSet 的rollingUpdate 滚动更新

跟deployment 一样, 一样可以用set image 来执行

[gateman@manjaro-x13 bq-api-service]$ kubectl set image statefulset/nginx-statefulset nginx-container=nginx:1.26.1
statefulset.apps/nginx-statefulset image updated

从describe 信息来看, rolling update 也是按照pod的顺序来执行的

Events:
Type Reason Age From Message
—- —— —- —- ——-
Normal SuccessfulCreate 13m (x2 over 23m) statefulset-controller create Pod nginx-statefulset-9 in StatefulSet nginx-statefulset successful
Normal SuccessfulCreate 13m (x2 over 23m) statefulset-controller create Pod nginx-statefulset-8 in StatefulSet nginx-statefulset successful
Normal SuccessfulDelete 13m statefulset-controller delete Pod nginx-statefulset-7 in StatefulSet nginx-statefulset successful
Normal SuccessfulCreate 13m (x2 over 23m) statefulset-controller create Pod nginx-statefulset-7 in StatefulSet nginx-statefulset successful
Normal SuccessfulDelete 12m statefulset-controller delete Pod nginx-statefulset-6 in StatefulSet nginx-statefulset successful
Normal SuccessfulCreate 12m (x2 over 23m) statefulset-controller create Pod nginx-statefulset-6 in StatefulSet nginx-statefulset successful
Normal SuccessfulCreate 12m (x2 over 24m) statefulset-controller create Pod nginx-statefulset-5 in StatefulSet nginx-statefulset successful
Normal SuccessfulDelete 12m statefulset-controller delete Pod nginx-statefulset-5 in StatefulSet nginx-statefulset successful
Normal SuccessfulDelete 12m (x2 over 24m) statefulset-controller delete Pod nginx-statefulset-4 in StatefulSet nginx-statefulset successful
Normal SuccessfulCreate 12m (x3 over 25m) statefulset-controller create Pod nginx-statefulset-4 in StatefulSet nginx-statefulset successful
Normal SuccessfulDelete 12m (x2 over 24m) statefulset-controller delete Pod nginx-statefulset-3 in StatefulSet nginx-statefulset successful
Normal SuccessfulCreate 12m (x3 over 25m) statefulset-controller create Pod nginx-statefulset-3 in StatefulSet nginx-statefulset successful
Normal SuccessfulDelete 12m statefulset-controller delete Pod nginx-statefulset-2 in StatefulSet nginx-statefulset successful
Normal SuccessfulCreate 12m (x2 over 3h) statefulset-controller create Pod nginx-statefulset-2 in StatefulSet nginx-statefulset successful

一样可以用kubectl annotate 命令去提供change cause

[gateman@manjaro-x13 bq-api-service]$ kubectl annotate statefulset/nginx-statefulset kubernetes.io/change-cause=\”downgraded to 1.26.1\”
statefulset.apps/nginx-statefulset annotated

但是很可惜, 跟deployment 不一样, 不能在revision list 里显示 change cause的annotation

[gateman@manjaro-x13 bq-api-service]$ kubectl rollout history statefulset/nginx-statefulset
statefulset.apps/nginx-statefulset
REVISION CHANGE-CAUSE
1 <none>
2 <none>

而且跟deployment 不同
statefulset的 revision 没有 replicasets 的对应

[gateman@manjaro-x13 statefulsets]$ kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
bq-api-service-deployment-6f6ffc7866 0 0 0 18h bq-api-service-container europe-west2-docker.pkg.dev/jason-hsbc/my-docker-repo/bq-api-service:1.1.7 app=bq-api-service,pod-template-hash=6f6ffc7866
bq-api-service-deployment-978b76fcf 3 3 3 8h bq-api-service-container europe-west2-docker.pkg.dev/jason-hsbc/my-docker-repo/bq-api-service:1.1.8 app=bq-api-service,pod-template-hash=978b76fcf
bq-api-service-deployment-c4979b697 0 0 0 18h bq-api-service-container europe-west2-docker.pkg.dev/jason-hsbc/my-docker-repo/bq-api-service:1.1.6 app=bq-api-service,pod-template-hash=c4979b697

只有deployment的record

#以上关于K8S 的相关内容来源网络仅供参考,相关信息请以官方公告为准!

原创文章,作者:CSDN,如若转载,请注明出处:https://www.sudun.com/ask/91639.html

(0)
CSDN的头像CSDN
上一篇 2024年6月22日
下一篇 2024年6月22日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注