k8s 部署 redis

创建部署文件

bash 复制代码
vim redis.yaml

添加如下内容:

bash 复制代码
apiVersion: v1
kind: Namespace
metadata:
  name: redis
---
apiVersion: v1
kind: Secret
metadata:
  name: redis-password
  namespace: redis
type: Opaque
data:
  password: d2d3cmhnZWE= # 建议生产环境使用更复杂的密码
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-config
  namespace: redis
data:
  redis.conf: |
    # 这里放置其他Redis配置,但移除了requirepass行
    # 密码将通过命令行动态传入
    maxmemory 256mb
    maxmemory-policy allkeys-lru
    appendonly yes
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  namespace: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - name: redis
          image: redis:6.2
          ports:
            - containerPort: 6379
          env:
            - name: REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: redis-password
                  key: password
          # 主要修改点在这里:通过命令行参数传递密码
          command: 
            - "redis-server"
            - "/usr/local/etc/redis/redis.conf"
            - "--requirepass"
            - "$(REDIS_PASSWORD)" # 这里会正确解析环境变量
          volumeMounts:
            - name: redis-config
              mountPath: /usr/local/etc/redis/redis.conf
              subPath: redis.conf
          resources:
            requests:
              memory: "64Mi"
              cpu: "100m"
            limits:
              memory: "256Mi"
              cpu: "500m"
          readinessProbe:
            exec:
              command:
                - redis-cli
                - -a
                - $(REDIS_PASSWORD)
                - ping
            initialDelaySeconds: 5
            periodSeconds: 10
      volumes:
        - name: redis-config
          configMap:
            name: redis-config
---
apiVersion: v1
kind: Service
metadata:
  name: redis-service
  namespace: redis
spec:
  selector:
    app: redis
  ports:
    - protocol: TCP
      port: 6379
      targetPort: 6379
  # type: ClusterIP 是默认值,如果只在集群内部访问,无需修改
  # 如果要从集群外部访问,可以改为 NodePort 或 LoadBalancer
  # type: LoadBalancer

启动服务

bash 复制代码
kubectl apply -f redis.yaml

查看服务

bash 复制代码
kubectl get pods -n redis
相关推荐
XUHUOJUN3 天前
AKS 不是安装在 Windows Server 上,而是运行在 Windows Server 之上的 Azure 平台能力
windows·架构·k8s·azure local·azure stack
Geek-Chow3 天前
Connecting kubectl to a Private EKS Cluster Over an Internal Domain
kubernetes·k8s·aws
spider_xcxc5 天前
Docker Compose 容器通信详解:同一个 Compose 文件才能互通吗?
docker·容器·k8s·容器化·容器网络
欢醉10 天前
k8s调用服务异常cannot allocate memory
kubernetes·k8s
万里侯13 天前
GitOps 漂移检测:集群里改了配置,Git 还以为一切正常
微服务·容器·k8s
spider_xcxc14 天前
云上网络基石(阿里云):深入解读阿里云VPC及其应用生态
网络·阿里云·云计算·k8s·运维开发·虚拟化
weixin_4399306415 天前
Kubernetes(K8s)入门实践
云原生·容器·kubernetes·k8s
AOwhisky17 天前
kubernetes(K8s)学习笔记:第十期与第十一期核心知识点自测与详解
运维·笔记·云原生·kubernetes·云计算·k8s·hpa
赵丙双18 天前
美团 Leaf-snowflake 分布式 ID 生成器 k8s 改造的想法
k8s·snowflake·分布式id生成器·美团 leaf
AOwhisky18 天前
Kubernetes(K8s)学习笔记(第十四期):集群存储与有状态应用(下篇):StatefulSet 有状态应用管理
redis·笔记·mysql·云原生·kubernetes·云计算·k8s