K8s系列之:无状态和有状态、DaemonSet、cronjob、Secret、ConfigMap应用案例

一、无状态和有状态

无状态:

  • 认为Pod都是一样的
  • 没有顺序要求
  • 不用考虑在哪个node上运行
  • 随意进行伸缩和扩展

有状态:

  • 上面因素都需要考虑到
  • 每个Pod独立的,保持Pod启动顺序和唯一性
  • 唯一的网络标识符,持久存储
  • 有序,比如mysql主从

二、部署有状态应用

无头service:

  • ClusterIP:none
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: nginx-statefulset
  namespace: default
spec:
  serviceName: nginx
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
kubectl apply -f stats.yaml
kubectl get pods
kubectl get svc

格式:

  • 主机名称.service名称.名称空间.svc.cluster.local
  • nginx-statefulset-0.nginx.defalult.svc.cluster.local
kubectl delete statefulset --all
kubectl delete svc nginx
kubectl delete svc web

三、DaemonSet

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: ds-test
  labels:
    app: filebeat
spec:
  selector:
    matchLabels:
      app: filebeat
  template:
    metadata:
      labels:
        app: filebeat
    spec:
      containers:
      - name: logs
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - name: varlog
          mountPath: /tmp/log
      volumes:
      - name: varlog
        hostPath: 
          path: /var/log
kubectl exec -it ds-test-cbk6v bash
ls /tmp/log

四、cronjob(定时任务)

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spce:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date;echo Hello from the k8s cluster
          restartPolicy: OnFailure

五、Secret

加密数据存储在etcd里面,让Pod容器以挂载Volume方式进行访问。场景:凭证

apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  username: optics
  password: optics123
kubectl create -f secret.yaml
kubectl get secret
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: nginx
    image: nginx
    env:
      - name: SECRET_USERNAME
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: username
      - name: SECRET_PASSWORD
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: password
kubectl exec -it mypod bash

echo $SECRET_USERNAME

以Volume形式挂载到容器中

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: nginx
    image: nginx
    volumemounts:
    - name: foo
      mountPath: "/etc/foo"
      readOnly: true
  volumes:
  - name: foo
    secret:
      secretName: mysecret

六、ConfigMap

  • 存储不加密数据到etcd,让Pod以变量或者Volume挂载到容器中
  • 场景:配置文件
redis.host=127.0.0.1
redis.port=6379
redis.password=123456
kubectl configmap redis-config --from-file=redis.properties
kubectl get cm
kubectl describe cm redis-config

1.以volume形式挂载到容器pod中

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
    - name: busybox
      image: busybox
      command: ["/bin/sh","-c","cat /etc/config/redis.properties"]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: redis-config
  restartPolicy: Never

2.以变量形式挂载到pod容器中

创建yaml,声明变量信息,configmap

apiVersion: v1
kind: ConfigMap
metadata:
  name: myconfig
  namespace: default
data:
  special.level: info
  special.type: hello
kubectl apply -f myconfig.yaml
kubectl get cm
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
    - name: busybox
      image: busybox
      command: ["/bin/sh","-c","echo $(LEVEL) $(TYPE)"]
      env:
        - name: LEVEL
          valueFrom:
            configMapKeyRef:
              name: myconfig
              key: special.level
        - name: TYPE
          valueFrom:
            configMapKeyRef:
              name: myconfig
              key: special.type
  restartPolicy: Never
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Kubernetes(k8s)是一个强大的容器编排平台,提供了丰富的资源对象来定义和管理应用程序的各个方面。以下是一些常见的Kubernetes资源对象及其功能: 1. Pod:是最小的可调度和可管理的单位,用于包含一个或多个容器的组。Pod提供了一个独立的网络和存储空间,并共享同一个主机。 2. Deployment:用于定义应用程序的部署方式,管理Pod副本集的创建、升级和回滚。Deployment支持滚动更新、健康检查和自愈能力。 3. Service:用于定义一组Pod的访问方式和网络策略。Service提供了负载均衡、服务发现和连接池等功能,使得应用程序可以通过统一的入口点进行访问。 4. ReplicaSet:用于确保指定数量的Pod副本正在运行。ReplicaSet可以根据定义的副本数自动进行水平扩展或收缩,并保持指定数量的副本。 5. StatefulSet:与ReplicaSet类似,但适用于有状态应用程序。StatefulSet为每个Pod副本分配唯一标识符和稳定的网络标识,确保有状态应用程序的稳定性和顺序性。 6. DaemonSet:用于在每个节点上运行一个Pod副本,确保在整个集群中的每个节点上都有一个Pod在运行。 7. JobCronJob:用于执行一次性任务或定期任务。Job保证一个或多个Pod成功完成任务,而CronJob允许定期执行任务。 8. ConfigMap:用于存储非敏感的配置数据,如环境变量、配置文件等。ConfigMap可以被挂载到Pod中,供应用程序读取。 9. Secret:用于存储敏感的配置数据,如密码、密钥等。Secret以安全的方式存储,并提供了在Pod中使用的方法。 10. Ingress:用于暴露HTTP和HTTPS服务到集群外部。Ingress提供了路由规则和负载均衡配置,将外部请求转发到内部的Service。 这只是一小部分Kubernetes资源对象的功能介绍。Kubernetes还提供了许多其他的资源对象,如Namespace、PersistentVolume、PersistentVolumeClaim等,用于满足不同场景下的需求。通过合理使用这些资源对象,可以更好地管理和运行容器化应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

最笨的羊羊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值