K8S部署Redis/Mysql并暴露出服务

目录

一.Redis

1.redis-pvc.yaml

2.redis-configmap.yaml

3.redis-service.yaml

4.redis.yaml

5.开始部署redis

二.Mysql

1.mysql-pv.yaml

2.mysql-deployment.yaml

3.mysql-nodeport.yaml

4.依次执行如下命令


一.Redis

1.redis-pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-6379-pvc
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  storageClassName: "nfs-provisioner"  # 这里指定storageClass名称

2.redis-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-cm
data:
    redis_6379.conf: |
      protected-mode no
      port 6379
      tcp-backlog 511
      timeout 0
      tcp-keepalive 300
      daemonize no
      supervised no
      pidfile /var/run/redis_6379.pid
      loglevel notice
      logfile "/data/redis_6379.log"
      databases 16
      always-show-logo yes
      # requirepass {{ $name.redis.env.passwd }}
      save 900 1
      save 300 10
      save 60 10000
      stop-writes-on-bgsave-error yes
      rdbcompression yes
      rdbchecksum yes
      dbfilename dump_6379.rdb
      dir  /data
      replica-serve-stale-data yes
      replica-read-only yes
      repl-diskless-sync no
      repl-diskless-sync-delay 5
      repl-disable-tcp-nodelay no
      replica-priority 100
      lazyfree-lazy-eviction no
      lazyfree-lazy-expire no
      lazyfree-lazy-server-del no
      replica-lazy-flush no
      appendonly no
      appendfilename "appendonly.aof"
      appendfsync everysec
      no-appendfsync-on-rewrite no
      auto-aof-rewrite-percentage 100
      auto-aof-rewrite-min-size 64mb
      aof-load-truncated yes
      aof-use-rdb-preamble yes
      lua-time-limit 5000
      slowlog-log-slower-than 10000
      slowlog-max-len 128
      latency-monitor-threshold 0
      notify-keyspace-events ""
      hash-max-ziplist-entries 512
      hash-max-ziplist-value 64
      list-max-ziplist-size -2
      list-compress-depth 0
      set-max-intset-entries 512
      zset-max-ziplist-entries 128
      zset-max-ziplist-value 64
      hll-sparse-max-bytes 3000
      stream-node-max-bytes 4096
      stream-node-max-entries 100
      activerehashing yes
      client-output-buffer-limit normal 0 0 0
      client-output-buffer-limit replica 256mb 64mb 60
      client-output-buffer-limit pubsub 32mb 8mb 60
      hz 10
      dynamic-hz yes
      aof-rewrite-incremental-fsync yes
      rdb-save-incremental-fsync yes
      rename-command FLUSHALL SAVEMORE16
      rename-command FLUSHDB  SAVEDB16
      rename-command CONFIG   UPDATEC16
      rename-command KEYS     NOALL16

3.redis-service.yaml

apiVersion: v1
kind: Service
metadata:
 name: redis-service
 labels:
   name: redis-service
spec:
 type: NodePort
 ports:
 - port: 6379
   protocol: TCP
   targetPort: 30301
   name: redis-6379
 selector:
   name: redis   # 这里填写Deployment/metadata/name 的值

4.redis.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: redis
  name: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      name: redis
  template:
    metadata:
      labels:
        name: redis
    spec:
      containers:
      - name: redis-6379
        image: redis:5.0
        volumeMounts:
        - name: configmap-volume
          mountPath: /usr/local/etc/redis/redis_6379.conf
          subPath: redis_6379.conf
        - name: redis-6379
          mountPath: "/data"
        command:
          - "redis-server"
        args:
          - /usr/local/etc/redis/redis_6379.conf
      volumes:
      - name: configmap-volume
        configMap:
          name: redis-cm
          items:
          - key: redis_6379.conf
            path: redis_6379.conf
      - name: redis-6379
        persistentVolumeClaim:
          claimName: redis-6379-pvc  # 这里填写pvc的名称

5.开始部署redis

依次执行如下命令

kubectl apply -f redis-pvc.yaml
kubectl apply -f redis-configmap.yaml
kubectl apply -f redis-service.yaml
kubectl apply -f redis.yaml

二.Mysql

1.mysql-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  hostPath: #得挂载一个空目录
    path: "/Users/joy/mysqlSVC"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi

2.mysql-deployment.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306 #如果这里的port和pod的containerPort不一致,则需要通过targetPort显式指定,否则无法建立映射
  selector:
    app: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.6
        imagePullPolicy: IfNotPresent
        name: mysql
        env:
          # Use secret in real usage
        - name: MYSQL_ROOT_PASSWORD
          value: t1234
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

3.mysql-nodeport.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql-nodeport
spec:
  type: NodePort
  ports:
  - port: 3306
    protocol: TCP
    targetPort: 30036
    name: mysql
  selector:
    name: mysql-nodeport

4.依次执行如下命令

kubectl apply -f mysql-pv.yaml
kubectl apply -f mysql-deployment.yaml
kubectl apply -f mysql-nodeport.yaml

你可能感兴趣的:(K8S,mysql,redis,kubernetes,云原生)