原文

1.部署configmap

redis-config.yaml

---
kind: ConfigMap
apiVersion: v1
metadata:
  name: redis-config
  namespace: dev
  labels:
    app: redis
data:
  redis.conf: |-
    dir /srv
    port 6379
    bind 0.0.0.0
    appendonly yes
    daemonize no
    #protected-mode no
    requirepass 123456
    pidfile /srv/redis-6379.pid

创建

kubectl create -f redis-config.yaml

如果redis数据需要持久化 可以用存储

2.部署deploy

redis-deploy.yaml

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  namespace: dev
  labels:
    app: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis:5.0.8
        command:
          - "sh"
          - "-c"
          - "redis-server /usr/local/redis/redis.conf"
        ports:
        - containerPort: 6379
        resources:
          limits:
            cpu: 100m
            memory: 100Mi
          requests:
            cpu: 50m
            memory: 30Mi
        livenessProbe:
          tcpSocket:
            port: 6379
          initialDelaySeconds: 300
          timeoutSeconds: 1
          periodSeconds: 10
          successThreshold: 1
          failureThreshold: 3
        readinessProbe:
          tcpSocket:
            port: 6379
          initialDelaySeconds: 5
          timeoutSeconds: 1
          periodSeconds: 10
          successThreshold: 1
          failureThreshold: 3
        volumeMounts:
        - name: config
          mountPath:  /usr/local/redis/redis.conf
          subPath: redis.conf
      volumes:
      - name: config
        configMap:
          name: redis-config

创建

kubectl create -f redis-deploy.yaml

测试

# 查看pod的ip
kubectl get pods -n dev -o wide
# 运行一个自删除的redis 容器 测试pod的连通性
docker run -it  --rm  redis:3.0.7 /bin/bash
# 或者
kubectl exec -it -n dev redis-99c765c65-7xp7g -- bash
root@redis-99c765c65-7xp7g:/data# redis-cli 
127.0.0.1:6379> auth 123456 

# 使用reids-cli命令连接测试

3.部署service

redis-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: redis-service
  namespace: dev
  labels:
    app: redis
spec:
  type: NodePort
  ports:
    - name: tcp
      port: 6379
  selector:
    app: redis

创建

kubectl create -f redis-service.yaml

然后外部连接测试

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐