欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

k8s部署redis服务

程序员文章站 2024-03-12 14:01:02
...

kubernetes部署redis5.0.6单机版 自定义配置文件、密码、日志路径等

创建redis配置configmaps:

apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-conf
data:
  redis.conf: |
        bind 0.0.0.0
        port 6379
        requirepass 123456
        pidfile .pid
        appendonly yes
        cluster-config-file nodes-6379.conf
        pidfile /data/middleware-data/redis/log/redis-6379.pid
        cluster-config-file /data/middleware-data/redis/conf/redis.conf
        dir /data/middleware-data/redis/data/
        logfile "/data/middleware-data/redis/log/redis-6379.log"
        cluster-node-timeout 5000
        protected-mode no

创建configmaps pod

[[email protected] redis]# kubectl create -f redis-configmaps.yaml -n gridcloud
configmap/redis-conf created

创建StatefulSet,并把数据挂载到宿主机上:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
spec:
  replicas: 1
  serviceName: redis
  selector:
    matchLabels:
      name: redis
  template:
    metadata:
      labels:
        name: redis
    spec:
      initContainers:
      - name: init-redis
        image: busybox
        command: ['sh', '-c', 'mkdir -p /data/middleware-data/redis/log/;mkdir -p /data/middleware-data/redis/conf/;mkdir -p /data/middleware-data/redis/data/']
        volumeMounts:
        - name: data
          mountPath: /data/middleware-data/redis/
      containers:
      - name: redis
        image: redis:5.0.6
        imagePullPolicy: IfNotPresent
        command:
        - sh
        - -c
        - "exec redis-server /data/middleware-data/redis/conf/redis.conf"
        ports:
        - containerPort: 6379
          name: redis
          protocol: TCP
        volumeMounts:
        - name: redis-config
          mountPath: /data/middleware-data/redis/conf/
        - name: data
          mountPath: /data/middleware-data/redis/
      volumes:
      - name: redis-config
        configMap:
          name: redis-conf
      - name: data
        hostPath:
          path: /data/middleware-data/redis/

创建statefulset pod

[[email protected] redis]# kubectl create -f statefulset-redis.yaml -n gridcloud
statefulset.apps/redis created

创建Service:

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

创建service pod

[[email protected] redis]# kubectl create -f redis-deploy.yaml -n gridcloud
The Service "redis" is invalid: spec.ports[0].nodePort: Invalid value: 30020: provided port is already allocated
[[email protected] redis]# kubectl replace --force -f redis-deploy.yaml -n gridcloud
service "redis" deleted
service/redis replaced

在三台机器上都尝试登陆redis已验证redis是否部署成功 

[[email protected] bin]# redis-cli -h 192.168.0.155 -p 30020
192.168.0.155:30020> auth 123456
OK
192.168.0.155:30020> set a 1
OK
192.168.0.155:30020> get a
"1"
192.168.0.155:30020> 
[[email protected] bin]# redis-cli -h 192.168.0.154 -p 30020
192.168.0.154:30020> auth 123456
OK
192.168.0.154:30020> get a
"1"
192.168.0.154:30020> 
[[email protected] bin]# redis-cli -h 192.168.0.153 -p 30020
192.168.0.153:30020> auth
(error) ERR wrong number of arguments for 'auth' command
192.168.0.153:30020> auth 123456
OK
192.168.0.153:30020> 

至此redis部署完毕