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
相关推荐
阿乐艾官1 天前
一个完整的创建Pod流程
k8s
j200103223 天前
Prometheus
k8s·prometheus
淡泊if4 天前
K8s 网络排障:从抓包开始,一步步定位诡异“502”
网络·kubernetes·k8s
WAIT_TIME8 天前
RKE2 + KubeSphere 部署方案
k8s·kubesphere·rancher·rke2
稀样9 天前
常见的学习资料网站
k8s·jenkins
老友@11 天前
云计算的统一心智模型
开发语言·ci/cd·docker·云计算·k8s·perl
j200103221 个月前
Gateway—— 高级流量路由
gateway·k8s
西门吹雪分身1 个月前
K8S之Ingress
java·容器·kubernetes·k8s
小义_1 个月前
【Kubernetes】(二)k8s基础
linux·云原生·k8s
小义_1 个月前
【Kubernetes】(一)k8s基础
云原生·k8s