首页 > 其他分享 >k8s——statefulset

k8s——statefulset

时间:2024-05-22 12:51:33浏览次数:24  
标签:web statefulset kubectl nginx master k8s root

statefulset基础模版

[root@master statefulset]# cat web.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     # statefulset 类型的资源
metadata:    
  name: web   # statefulset 对象的名字
spec:
  serviceName: "nginx"   # 使用哪个service来管理dns
  replicas: 2      
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:   # 容器内部要暴露的端口
        - containerPort: 80  # 具体暴露的端口号
          name: web  #该端口配置的名字


// 先不写下面的,因为涉及到别的知识点

        volumeMounts:   # 加载数据卷
        - name: www    #指定加载哪个数据卷
          mountPath: /usr/share/nginx/html  # 加载到容器中的哪个目录
  volumeClaimTemplates:    # 数据卷模版
  - metadata:    # 数据卷描述
      name: www   # 数据卷的名称
      annotations:   # 数据卷的注解
        volume.alpha.kubernetes,io.storage-class: anything
    spec:   # 数据卷的规约
      accessModes: [ "ReadWriteOnce" ]   # 访问模式
      resources:
        requests:
          storage: 1Gi   # 需要1G 的存储资源
[root@master statefulset]# 

[root@master statefulset]# kubectl create -f web.yaml 
service/nginx created
statefulset.apps/web created
[root@master statefulset]# kubectl get sts
NAME   READY   AGE
web    0/2     5s
[root@master statefulset]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   5d18h
nginx        ClusterIP   None         <none>        80/TCP    8s
[root@master statefulset]# kubectl get pvc  //注释掉之后这个是没有的
NAME        STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
www-web-0   Pending                                                     11s

//修改后
[root@master statefulset]# kubectl create -f web.yaml 
service/nginx created
statefulset.apps/web created
[root@master statefulset]# kubectl get  sts
NAME   READY   AGE
web    2/2     9s
[root@master statefulset]# kubectl get  svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   5d19h
nginx        ClusterIP   None         <none>        80/TCP    15s
[root@master statefulset]# 
#  有一个疑问,k8s集群内部`kubectl get po`并没有这么多pod
[root@master ~]# kubectl get  po
NAME         READY   STATUS    RESTARTS       AGE
my-pod       1/1     Running   3 (4m8s ago)   5d18h
nginx-demo   1/1     Running   4 (4m8s ago)   5d23h
nginx-po     1/1     Running   4 (4m8s ago)   5d22h
web-0        1/1     Running   2 (4m8s ago)   20h
web-1        1/1     Running   2 (4m8s ago)   20h
[root@master ~]# 

# python 中列出pod ,列出的是所有的pod 等价于kubectl get -A po 
[root@master ~]# kubectl get -A po
NAMESPACE         NAME                               READY   STATUS    RESTARTS        AGE
default           my-pod                             1/1     Running   3 (3m12s ago)   5d18h
default           nginx-demo                         1/1     Running   4 (3m12s ago)   5d23h
default           nginx-po                           1/1     Running   4 (3m12s ago)   5d22h
default           web-0                              1/1     Running   2 (3m12s ago)   20h
default           web-1                              1/1     Running   2 (3m12s ago)   20h
kube-flannel      kube-flannel-ds-bkvjm              1/1     Running   4 (3m12s ago)   5d23h
kube-flannel      kube-flannel-ds-hg6bh              1/1     Running   5 (3m56s ago)   6d14h
kube-system       coredns-66f779496c-c5rmz           1/1     Running   4 (3m56s ago)   6d15h
kube-system       coredns-66f779496c-jswxz           1/1     Running   4 (3m56s ago)   6d15h
kube-system       etcd-master                        1/1     Running   4 (3m56s ago)   6d15h
kube-system       kube-apiserver-master              1/1     Running   4 (3m56s ago)   6d15h
kube-system       kube-controller-manager-master     1/1     Running   5 (3m56s ago)   6d15h
kube-system       kube-proxy-jr5m5                   1/1     Running   4 (3m56s ago)   6d15h
kube-system       kube-proxy-tcpzh                   1/1     Running   4 (3m12s ago)   5d23h
kube-system       kube-scheduler-master              1/1     Running   5 (3m57s ago)   6d15h
tigera-operator   tigera-operator-55585899bf-s28bt   1/1     Running   8 (3m19s ago)   6d14h

拉取指定镜像测试

[root@master statefulset]# kubectl run -it --image busybox:1.28.4 dns-test  /bin/sh
If you don't see a command prompt, try pressing enter.
/ # ping web-0.nginx
PING web-0.nginx (10.244.1.45): 56 data bytes
64 bytes from 10.244.1.45: seq=0 ttl=64 time=0.048 ms
64 bytes from 10.244.1.45: seq=1 ttl=64 time=0.054 ms
^C
--- web-0.nginx ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.048/0.051/0.054 ms
/ # nslookup web-0.nginx    # sts的名字-第几个.service的名字 是statefulset的访问格式
Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name:      web-0.nginx
Address 1: 10.244.1.45 web-0.nginx.default.svc.cluster.local
/ # 

扩容和缩容

# 扩容
[root@master statefulset]# kubectl scale sts web --replicas=6
statefulset.apps/web scaled

# 缩容
[root@master statefulset]# kubectl scale sts web --replicas=3
statefulset.apps/web scaled
[root@master statefulset]# kubectl get sts
NAME   READY   AGE
web    3/3     20h
[root@master statefulset]# 

灰度更新

[root@master statefulset]# kubectl scale sts web --replicas=6
statefulset.apps/web scaled
[root@master statefulset]# kubectl get sts
NAME   READY   AGE
web    6/6     20h

[root@master statefulset]# kubectl  edit sts web 
statefulset.apps/web edited
# 进去之后修改
---
  updateStrategy:
    rollingUpdate:
      partition: 3   # 把0修改为3
    # 修改partition的值可以模拟灰度更新,就是说更新的时候先更新编号大于3的pod
---

# 查看
[root@master statefulset]# kubectl describe po web-1|grep nginx:
  nginx:
    Image:          nginx:1.7.9
  Normal   Pulled                  20h                kubelet            Container image "nginx:1.7.9" already present on machine
  Normal   Pulled                  12h                kubelet            Container image "nginx:1.7.9" already present on machine
  Normal   Pulled                  48m                kubelet            Container image "nginx:1.7.9" already present on machine
[root@master statefulset]# kubectl describe po web-5|grep nginx:
  nginx:
    Image:          nginx:1.9.1
  Normal  Pulled     95s   kubelet            Container image "nginx:1.9.1" already present on machine
[root@master statefulset]# 

# 确认新版本没有问题之后,在修改partition的值为0,来更新所有
[root@master statefulset]# kubectl  edit sts web 
statefulset.apps/web edited
[root@master statefulset]# kubectl describe po web-5|grep nginx:
  nginx:
    Image:          nginx:1.9.1
  Normal  Pulled     6m13s  kubelet            Container image "nginx:1.9.1" already present on machine
[root@master statefulset]# kubectl describe po web-0|grep nginx:
  nginx:
    Image:          nginx:1.9.1
  Normal  Pulled     4s    kubelet            Container image "nginx:1.9.1" already present on machine
[root@master statefulset]# 

OnDelete策略更新

[root@master statefulset]# kubectl edit sts web 
statefulset.apps/web edited
---
  updateStrategy:
    type: OnDelete   #修改更新策略
---


[root@master statefulset]# kubectl edit  sts web 
statefulset.apps/web edited
---
    spec:
      containers:
      - image: nginx:1.7.9  #修改镜像
        imagePullPolicy: IfNotPresent
        name: nginx
        ports:
---
[root@master statefulset]# kubectl describe po web-0 |grep nginx:
  nginx:
    Image:          nginx:1.9.1
  Normal  Pulled     13m   kubelet            Container image "nginx:1.9.1" already present on machine
[root@master statefulset]# kubectl describe po web-1 |grep nginx:
  nginx:
    Image:          nginx:1.9.1
  Normal  Pulled     14m   kubelet            Container image "nginx:1.9.1" already present on machine
[root@master statefulset]# kubectl delete pod web-1
pod "web-1" deleted  # 看似删除实则更新
[root@master statefulset]# 
[root@master statefulset]# kubectl describe po web-1 |grep nginx:
  nginx:
    Image:          nginx:1.7.9
  Normal  Pulled     3s    kubelet            Container image "nginx:1.7.9" already present on machine
[root@master statefulset]# 

删除sts

rs是deployment的东西,所以sts没有rs
删除sts和headless service的时候分为两种,级联删除和非级联删除

级联删除

# 删除sts时候,会同时删除pods
[root@master statefulset]# kubectl get sts
NAME   READY   AGE
web    3/3     23h
[root@master statefulset]# kubectl delete sts web 
statefulset.apps "web" deleted
[root@master statefulset]# kubectl get sts
No resources found in default namespace.
[root@master statefulset]# kubectl get po
NAME       READY   STATUS    RESTARTS       AGE
dns-test   1/1     Running   1 (160m ago)   162m
[root@master statefulset]# 


# 删除sts的时候,直接删除pod

非级联删除

[root@master statefulset]# kubectl get sts
NAME   READY   AGE
web    2/2     25s
[root@master statefulset]# kubectl get po
NAME       READY   STATUS    RESTARTS       AGE
dns-test   1/1     Running   1 (166m ago)   168m
web-0      1/1     Running   0              27s
web-1      1/1     Running   0              26s
[root@master statefulset]# kubectl delete  sts web --cascade=false
warning: --cascade=false is deprecated (boolean value) and can be replaced with --cascade=orphan.
statefulset.apps "web" deleted
[root@master statefulset]# kubectl get po
NAME       READY   STATUS    RESTARTS       AGE
dns-test   1/1     Running   1 (166m ago)   168m
web-0      1/1     Running   0              74s
web-1      1/1     Running   0              73s
[root@master statefulset]# kubectl get sts
No resources found in default namespace.
[root@master statefulset]# 
# 非级联删除没有连带删除pod

标签:web,statefulset,kubectl,nginx,master,k8s,root
From: https://www.cnblogs.com/humlogs/p/18203601

相关文章

  • minikube 搭建 k8s 单机环境
    准备linux环境uname-acat/etc/os-release查看linux环境查看linux发行版本,可以在/etc目录下找到以release结尾的文件,这个一般就是记录发行版本的文件准备docker环境需要有一个镜像打包的工具安装dockersudoyuminstalldocker如果提示没有找到软件,那么需要配置一下软......
  • 从需求角度介绍PasteSpider(K8S平替部署工具适合于任何开发语言)
             你是否被K8S的强大而吸引,我相信一部分人是被那复杂的配置和各种专业知识而劝退,应该还有一部分人是因为K8S太吃资源而放手!    这里介绍一款平替工具PasteSpider,PasteSpider是一款使用c#编写的linux容器部署工具(使用PasteSpider和自己用啥语言开发没关......
  • k8s——deployment
    创建deployment[root@masterdeploy]#kubectlcreatedeploynginx-deploy--image=nginx:1.7.9deployment.apps/nginx-deploycreated//kubectlcreate资源类型资源名称(deployment的名称)指定镜像//都是在动态的更新pod副本的数量deployment和replicaSet是嵌套关......
  • K8S多master节点更换证书
    以下命令master节点均需要执行1.备份cp-a/etc/kubernetes{,.bak}cp-a/var/lib/kubelet{,.bak}cp-a/var/lib/etcd/var/lib/etcd.bak2.生成kubeadm-configkubectl-nkube-systemgetcmkubeadm-config-oyaml>kubeadm-config-20240521.yaml3.刷新证书到期时间再......
  • k8s安全权限配置
     https://blog.csdn.net/qq_51545656/article/details/136627284一、安全机制Kubernetes的安全机制非常关键,因为它确保了集群的稳定性和数据的安全。三个主要安全机制是Kubernetes安全架构的核心组成部分:认证(Authentication):这是安全机制的第一道防线。它负责确认请求者的......
  • k8s常用命令
    aliask=kubectl操作kubeconfig#查看contextkconfigget-contexts#切换contextkconfiguse-contextcontextName#使用context访问k--context=contextNamegetpod操作k8s资源#设置节点标签klabelnodenodeNamekey=value--overwrite#设置节点污点ktaintn......
  • rancher添加k8s节点时显示节点已添加
    由于几台服务器都用相同的外部externalip,而添加k8s节点时有外部ip先使用外部ip,所以会显示节点已添加,无法添加成功,此时需要用--address参数来指定内部ip,这样节点就由内部ip来添加如下添加k8s节点的命令:sudodockerrun-d--privileged--restart=unless-stopped--net=ho......
  • k3s和k8s对比,应该选择哪个
    k3s和k8s都是Kubernetes的发行版,但在一些关键特性上存在差异。以下是两者的对比及选择建议:1.特性与功能:k8s(Kubernetes)是一个功能完善的容器编排系统,具有自动装箱、自我修复、水平扩展、服务发现和负载均衡等特性。它支持复杂的部署、扩展和管理操作,适用于大规模生产环境。k3s......
  • K8S下应用异常卡顿问题的分析与学习
    K8S下应用异常卡顿问题的分析与学习背景周二自己在处理申威服务器的问题时,被同事拉进一个群聊.告知客户现场有一个特殊情况:服务晚上重启,上午速度还可以,但是到了下午就开始变的非常卡顿.因为当时正在车上也看不到具体信息.晚上九点上会进行了一次简单查看.发现......
  • 放弃k8s,拥抱ECI;水很深,一切都是为了钱,没有钱的动力,云服务商会发展k8s生态???
    学习k8s前鄙视eci,学习完后决定放弃k8s,拥抱ECI;从架构图可以看到,以后ECI不会是方向,只是一些压测,弹性伸缩的业务;2.eci不能融入k8s生态,导致企业不愿意转,这也是eci一直想融入k8s生态,向k8s生态靠拢的原因,就是说是eci什么k8sserverless版本退出,底层基于eci,没有k8s生态eci就是一......