阅读 169

手把手带你玩转k8s-win10的k8s上搭建开发环境服务

前言

上一篇在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工具部署。在这里就先不细说了,其实只是定义文件和发布命令略微有些区别。

项目源码地址

  • 后端

gitee.com/mldong/mldo…

  • 前端

gitee.com/mldong/mldo…

相关文章

手把手带你玩转k8s-集群创建和Hello World

手把手带你玩转k8s-ConfigMap与持久化存储

手把手带你玩转k8s-完整的发布一个可对外访问的服务

手把手带你玩转k8s-docker进阶Dockerfile与docker-compose

手把手带你玩转k8s-一键部署springboot项目

手把手带你玩转k8s-一键部署vue项目

手把手带你玩转k8s-常用对象详解

手把手带你玩转k8s-jenkins安装与流水线

手把手带你玩转k8s-jenkins流水线语法

手把手带你玩转k8s-jenkins流水线发布springboot项目

手把手带你玩转k8s-jenkins流水线发布vue项目

手把手带你玩转k8s-健康检查之存活探针与就绪探针

手把手带你玩转k8s-win10上搭建k8s集群