前言

上一篇在win10系统上安装了docker并启用了其集成的k8s的环境,同时也以nginx为例在k8s上跑了一个完整的实例。本文还是在该k8s集群上操作,安装我们在开发过程中使用的服务,如:mysql/redis/rabbitmq等。

image-20200809102946049

持久化

上一篇为了演示方便并没有使用持久化存储,本文这里要安装开发环境中的服务,持久化是不可缺少的环节。

共享目录

  1. 创建目录(git bash)

    mkdir -p /d/mldong/docker-data/
    
  2. 设置目录权限(git bash)

    chmod 777 -R /d/mldong/docker-data/
    
  3. 设置文件共享

image-20200808220255919

image-20200808220312675

详细说明可看documentation.

案例演示

配置后,这里先以nginx为例进行演示

  1. 创建目录(git bash)
mkdir -p /d/mldong/docker-data/nginx/html
  1. 新增文件(git bash)
echo "hello world" > /d/mldong/docker-data/nginx/html/index.html
  1. 运行nginx实例(PowerShell)

    在这里使用git bash的话,会无效,所以使用powershell的方式。

docker run -p 81:80 -v /d/mldong/docker-data/nginx/html:/usr/share/nginx/html -d --name mynginx nginx
  1. 浏览器访问

image-20200809124614572

  1. 清理(git bash)
# 停止刚运行的名称为mynginx的实例
docker ps -a | grep mynginx | awk '{print $1}' | xargs docker stop
# 删除刚运行的名称为mynginx的实例
docker ps -a | grep mynginx | awk '{print $1}' | xargs docker rm

image-20200808221721973

安装服务

安装Mysql

  1. 初始化目录

    mkdir -p /d/mldong/docker-data/mysql/data
    
  2. 定义文件

    cat << EOF > /d/mldong/docker-data/mysql/mysql.yaml
    apiVersion: v1
    kind: Namespace
    metadata:
      name: mysql-dev
    ---
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: mysql-cm
      namespace: mysql-dev
    data:
      my.cnf: |-
        [mysqld]
        user=mysql
        wait_timeout=2880000
        interactive_timeout = 2880000
        max_allowed_packet = 256M
        default-storage-engine=INNODB
        character-set-server=utf8mb4
        default-time_zone = '+8:00'
        [client]
        default-character-set=utf8mb4
        [mysql]
        default-character-set=utf8mb4
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: db
      namespace: mysql-dev
    spec:
      selector:
        matchLabels:
          app: db
      replicas: 1
      template:
        metadata:
          annotations:
            version/config: V0001
          labels:
            app: db
        spec:
          containers:
            - name: db
              image: mysql:5.7.28
              imagePullPolicy: IfNotPresent
              ports:
                - containerPort: 3306
                  name: port
                  protocol: TCP
              env:
                - name: MYSQL_ROOT_PASSWORD
                  value: "123456"
                - name: MYSQL_DATABASE
                  value: "mldong"
                - name: MYSQL_USER
                  value: "u_mldong"
                - name: MYSQL_PASSWORD
                  value: "mldong@123456"
              volumeMounts:
                - name: mysql-cm
                  mountPath: "/etc/mysql/mysql.conf.d"
                - name: mysql-data
                  mountPath: "/var/lib/mysql"
          volumes:
            - name: mysql-cm
              configMap:
                name: mysql-cm
            - name: mysql-data
              hostPath:
                path: /d/mldong/docker-data/mysql/data
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: db-nodeport
      namespace: mysql-dev
    spec:
      type: NodePort
      ports:
      - port: 3306
        targetPort: 3306
        nodePort: 32306 # 这里不指定就会随机(30000-32767)
      selector:
        app: db
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: db
      namespace: mysql-dev
    spec:
      type: ClusterIP
      ports:
      - port: 3306
        protocol: TCP
        targetPort: 3306
      selector:
        app: db
    
    EOF
    
  3. 拉取镜像

    先拉取的话,第一次发布时不用等

    docker pull mysql:5.7.28
    
  4. 开始发布

    kubectl apply -f /d/mldong/docker-data/mysql/mysql.yaml
    
  5. 查看服务

    kubectl get pods -n mysql-dev
    kubectl get svc -n mysql-dev
    

    image-20200809101828862

    image-20200809101909405

  6. 测试连接

    image-20200809102044273

    image-20200809102127286

    1. 清理

      kubectl delete -f /d/mldong/docker-data/mysql/mysql.yaml
      

安装Redis

  1. 初始化目录

    mkdir -p /d/mldong/docker-data/redis/data
    
  2. 定义文件

    cat << EOF > /d/mldong/docker-data/redis/redis.yaml
    apiVersion: v1
    kind: Namespace
    metadata:
      name: redis-dev
    ---
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: redis-cm
      namespace: redis-dev
    data:
      redis.conf: |-
        #daemonize yes
        pidfile /data/redis.pid
        port 6379
        tcp-backlog 30000
        timeout 0
        tcp-keepalive 10
        loglevel notice
        logfile /data/redis.log
        databases 16
        #save 900 1
        #save 300 10
        #save 60 10000
        stop-writes-on-bgsave-error no
        rdbcompression yes
        rdbchecksum yes
        dbfilename dump.rdb
        dir /data
        slave-serve-stale-data yes
        slave-read-only yes
        repl-diskless-sync no
        repl-diskless-sync-delay 5
        repl-disable-tcp-nodelay no
        slave-priority 100
        requirepass 123456
        maxclients 30000
        appendonly yes
        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
        lua-time-limit 5000
        slowlog-log-slower-than 10000
        slowlog-max-len 128
        latency-monitor-threshold 0
        notify-keyspace-events KEA
        hash-max-ziplist-entries 512
        hash-max-ziplist-value 64
        list-max-ziplist-entries 512
        list-max-ziplist-value 64
        set-max-intset-entries 1000
        zset-max-ziplist-entries 128
        zset-max-ziplist-value 64
        hll-sparse-max-bytes 3000
        activerehashing yes
        client-output-buffer-limit normal 0 0 0
        client-output-buffer-limit slave 256mb 64mb 60
        client-output-buffer-limit pubsub 32mb 8mb 60
        hz 10
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: redis-pod
      namespace: redis-dev
    spec:
      selector:
        matchLabels:
          app: redis-pod
      replicas: 1
      template:
        metadata:
          annotations:
            version/config: V0001
          labels:
            app: redis-pod
        spec:
          containers:
            - name: redis-pod
              image: redis:5.0.4
              imagePullPolicy: IfNotPresent
              ports:
                - containerPort: 6379
                  name: port
                  protocol: TCP
              env:
              	- name: RABBITMQ_DEFAULT_USER
              	  value: admin
                - name: RABBITMQ_DEFAULT_PASS
                  value: "123456"
              volumeMounts:
                - name: redis-cm
                  mountPath: "/usr/local/etc/redis"
                - name: redis-data
                  mountPath: "/data"
              command: ["/bin/sh"]
              args: ["-c", "redis-server /usr/local/etc/redis/redis.conf"]
          volumes:
            - name: redis-cm
              configMap:
                name: redis-cm
            - name: redis-data
              hostPath:
                path: /d/mldong/docker-data/redis/data
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: redis-nodeport
      namespace: redis-dev
    spec:
      type: NodePort
      ports:
      - port: 6379
        targetPort: 6379
        nodePort: 32379 # 这里不指定就会随机(30000-32767)
      selector:
        app: redis-pod
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: redis
      namespace: redis-dev
    spec:
      type: ClusterIP
      ports:
      - port: 6379
        protocol: TCP
        targetPort: 6379
      selector:
        app: redis-pod
    
    EOF
    
  3. 拉取镜像

    docker pull redis:5.0.4
    
  4. 开始发布

    kubectl apply -f /d/mldong/docker-data/redis/redis.yaml
    
  5. 查看服务

    kubectl get pods -n redis-dev
    kubectl get svc -n redis-dev
    

    image-20200809105301042

  6. 测试连接

    image-20200809105451126

    image-20200809110044146

    1. 清理

      kubectl delete -f /d/mldong/docker-data/redis/redis.yaml
      

    image-20200809110110145

安装Rabbitmq

  1. 初始化目录

    mkdir -p /d/mldong/docker-data/rabbitmq
    
  2. 定义文件

    cat << EOF > /d/mldong/docker-data/rabbitmq/rabbitmq.yaml
    apiVersion: v1
    kind: Namespace
    metadata:
      name: rabbitmq-dev
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: rabbitmq-pod
      namespace: rabbitmq-dev
    spec:
      selector:
        matchLabels:
          app: rabbitmq-pod
      replicas: 1
      template:
        metadata:
          annotations:
            version/config: V0001
          labels:
            app: rabbitmq-pod
        spec:
          containers:
            - name: rabbitmq-pod
              image: rabbitmq:management
              imagePullPolicy: IfNotPresent
              ports:
                - containerPort: 15672
                  name: port
                  protocol: TCP
                - containerPort: 5672
                  name: management-port
                  protocol: TCP
              env:
                - name: RABBITMQ_DEFAULT_USER
                  value: admin
                - name: RABBITMQ_DEFAULT_PASS
                  value: "123456"
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: rabbitmq-nodeport
      namespace: rabbitmq-dev
    spec:
      type: NodePort
      ports:
      - port: 5672
        targetPort: 5672
        nodePort: 32672 # 这里不指定就会随机(30000-32767)
      selector:
        app: rabbitmq-pod
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: management-nodeport
      namespace: rabbitmq-dev
    spec:
      type: NodePort
      ports:
      - port: 15672
        targetPort: 15672
        nodePort: 31672 # 这里不指定就会随机(30000-32767)
      selector:
        app: rabbitmq-pod
    EOF
    
  3. 拉取镜像

    docker pull rabbitmq:management
    
  4. 开始发布

    kubectl apply -f /d/mldong/docker-data/rabbitmq/rabbitmq.yaml
    
  5. 查看服务

    kubectl get pods -n rabbitmq-dev
    kubectl get svc -n rabbitmq-dev
    

    image-20200809114936972

  6. 测试连接-管理端

image-20200809115018570

image-20200809115332593

  1. 连接测试-springboot

    略。需要注意的是,这里暴露的端口为32672。

  2. 清理

    kubectl delete -f /d/mldong/docker-data/rabbitmq/rabbitmq.yaml
    

小结

本文使用Docker for Windows上的k8s集群搭建本地开发服务。部署服务的思路和在阿里云上的差不多,不过因为本地环境的特殊性,这里使用了hostpath的方式去配置持久化,中间少了pv/pvc/ingress的环节。当然,除了部署到k8s集群上,我们还可以使用原生命令或docker-compose工具部署。在这里就先不细说了,其实只是定义文件和发布命令略微有些区别。

Logo

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

更多推荐