linux运维、架构之路-k8s部署redis

一、创建命名空间

apiVersion: v1
kind: Namespace
metadata:
  name: dev

二、创建redis配置ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  namespace: dev
  name: redis-conf
data:
  redis.conf: |
        bind 0.0.0.0
        port 6379
        #requirepass 111111 #设置认证密码
        appendonly yes
        cluster-config-file nodes-6379.conf
        pidfile /redis/log/redis-6379.pid
        cluster-config-file /redis/conf/redis.conf
        dir /redis/data/
        logfile /redis/log/redis-6379.log
        cluster-node-timeout 5000
        protected-mode no

三、创建有状态应用StatefulSet,并把数据挂载到宿主机上

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
  namespace: dev
spec:
  replicas: 1
  serviceName: redis
  selector:
    matchLabels:
      name: redis
  template:
    metadata:
      labels:
        name: redis
    spec:
      initContainers:
      - name: init-redis
        image: hub.ityy.ultrapower.com.cn:5050/middleware/busybox:1.1.1 #初始化容器镜像
        command: ['sh', '-c', 'mkdir -p /redis/log/;mkdir -p /redis/conf/;mkdir -p /redis/data/']
        volumeMounts:
        - name: data
          mountPath: /redis/
      containers:
      - name: redis
        image: hub.ityy.ultrapower.com.cn:5050/middleware/redis:4.0.6
        imagePullPolicy: IfNotPresent
        command:
        - sh
        - -c
        - "exec redis-server /redis/conf/redis.conf"
        ports:
        - containerPort: 6379
          name: redis
          protocol: TCP
        volumeMounts:
        - name: redis-config
          mountPath: /redis/conf/
        - name: data
          mountPath: /redis/
      volumes:
      - name: redis-config
        configMap:
          name: redis-conf
      - name: data
        hostPath:
          path: /app/ #挂载宿主机目录
      nodeSelector:
        domain: dev #选择要部署的固定节点

四、创建Service对外暴露应用

kind: Service
apiVersion: v1
metadata:
  namespace: dev
  labels:
    name: redis
  name: redis
spec:
  type: NodePort
  ports:
  - name: redis
    port: 6379
    targetPort: 6379
    nodePort: 31379
  selector:
    name: redis

 

posted @ 2020-11-14 21:05  闫新江  阅读(701)  评论(0编辑  收藏  举报