[kubernetes]-k8s部署单节点redis

小灰灰 2022-12-22 06:12 537阅读 0赞

导语:项目需要部署在k8s里 需要运行一个3.0.7的redis 保存session

首先创建redis.conf对应的cm文件

redis-config.yaml

  1. ---
  2. kind: ConfigMap
  3. apiVersion: v1
  4. metadata:
  5. name: redis-config
  6. labels:
  7. app: redis
  8. data:
  9. redis.conf: |-
  10. dir /srv
  11. port 6379
  12. bind 0.0.0.0
  13. appendonly yes
  14. daemonize no
  15. #protected-mode no
  16. requirepass Hangzhou@123
  17. pidfile /srv/redis-6379.pid
  18. kubectl apply -f redis-config.yaml -n devops

如果redis数据需要持久化 可以用存储。我是session 丢了就丢了呗。

redis-deploy.yaml

  1. ---
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: redis
  6. labels:
  7. app: redis
  8. spec:
  9. replicas: 1
  10. selector:
  11. matchLabels:
  12. app: redis
  13. template:
  14. metadata:
  15. labels:
  16. app: redis
  17. spec:
  18. containers:
  19. - name: redis
  20. image: redis:3.0.7
  21. command:
  22. - "sh"
  23. - "-c"
  24. - "redis-server /usr/local/redis/redis.conf"
  25. ports:
  26. - containerPort: 6379
  27. resources:
  28. limits:
  29. cpu: 1000m
  30. memory: 1024Mi
  31. requests:
  32. cpu: 1000m
  33. memory: 1024Mi
  34. livenessProbe:
  35. tcpSocket:
  36. port: 6379
  37. initialDelaySeconds: 300
  38. timeoutSeconds: 1
  39. periodSeconds: 10
  40. successThreshold: 1
  41. failureThreshold: 3
  42. readinessProbe:
  43. tcpSocket:
  44. port: 6379
  45. initialDelaySeconds: 5
  46. timeoutSeconds: 1
  47. periodSeconds: 10
  48. successThreshold: 1
  49. failureThreshold: 3
  50. volumeMounts:
  51. - name: config
  52. mountPath: /usr/local/redis/redis.conf
  53. subPath: redis.conf
  54. volumes:
  55. - name: config
  56. configMap:
  57. name: redis-config

创建configmap和deployment

  1. kubectl apply -f redis-config.yaml --force -n devops
  2. kubectl apply -f redis-deploy.yaml -n devops --force

7d18dd08f4ed2e356cec6edcb402a24f.png

  1. # 查看pod的ip
  2. kubectl get pods -n devops -o wide
  3. # 运行一个自删除的redis 容器 测试pod的连通性
  4. docker run -it --rm redis:3.0.7 /bin/bash
  5. # 使用reids-cli命令连接测试

27d295ef50defcdc1fd0e27ad3896899.png

测试密码设置没问题

redis-svc.yaml

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: redis
  5. namespace: devops
  6. labels:
  7. app: redis
  8. spec:
  9. type: NodePort
  10. ports:
  11. - name: tcp
  12. port: 6379
  13. nodePort: 30379
  14. selector:
  15. app: redis

创建svc

  1. kubectl apply -f redis-svc.yaml -n devops

测试连接node的端口测试

441fade740411e9c029b1016189e6119.png

连接成功。

发表评论

表情:
评论列表 (有 0 条评论,537人围观)

还没有评论,来说两句吧...

相关阅读