目录

例子1:(配置文件映射、文件夹映射、端口映射)

例子2:(持久化)

例子3:(容器时间)

例子4:(mysql最大连接数)


以下几个例子包括三个模板:ConfigMap、Deployment、Service,ConfigMap是用来添加程序启动配置文件的,Deployment定义容器主程序(包括镜像、挂载文件、容器内所用端口等),Service定义了容器端口映射,其中 targetPort 指定容器内与宿主机间端口映射关系, nodePort 指定与宿主机外网间的端口映射关系。

例子1:(配置文件映射、文件夹映射、端口映射)

里边涉及到配置文件映射、文件夹映射、端口映射

apiVersion: v1
kind: ConfigMap    #configmap文件(主要用来映射文本文件的)
metadata:
  name: media-sipgw-config    #configmap名字,后边Deployment用configmap时,直接用此名字来指代它
data:
  sipgw.ini: |+             #要映射的配置文件名字+内容
    [SRV]
    server_id =200
    local_ip = 127.0.0.1
    nvr_svr = 1
    opensips_port = 30111
    sipgw_password = 123456
    sdp_number = 0
    [SIP]
    sip_ip = 192.168.105.131
    sip_port = 5061
    rtp_port = 30133
    sip_id = 34020000002000000001
    call_use_tcp = 0
    [REDIS]
    redis_ip = redis
    redis_port = 6379
    redis_pass =
    [RECORD]
    record_ip = 0.0.0.0
    record_port = 8383
    http_ip = 0.0.0.0
    http_port = 18081 
    [LOGINFO]
    #LOGCRIT = 0, LOGERROR=1, LOGWARNING=2, LOGINFO=3, LOGDEBUG=4, LOGDEBUG2=5, LOGALL=6
    log_level = 4
    [ERROR_JUDGE]
    allow_time = 2
    allow_invite_times = 4
    [RECYCLE]
    auto_recycle_span = 60 
    max_streams = 100
  visualipc.ini: |+
    [SRV]
    server_id =200
    local_ip = 127.0.0.1
    rtp_server_ip_wan = 172.21.138.154
    rtp_server_port = 30122
    nvr_svr = 0 
    max_pushers = 10 
    usleep_goon_span = 40
    usleep_stop_span = 1
    memory_length = 100000
    [SIP]
    sip_ip = 172.21.138.154
    sip_port = 5062
    sip_id = 34020000001110000001
    [REDIS]
    redis_ip = 172.21.138.150 
    redis_port = 30004
    redis_pass =
    [RECORD]
    record_ip = 0.0.0.0
    record_port = 8383
    http_ip = 0.0.0.0
    http_port = 18088 
    [LOGINFO]
    #LOGCRIT = 0, LOGERROR=1, LOGWARNING=2, LOGINFO=3, LOGDEBUG=4, LOGDEBUG2=5, LOGALL=6
    log_level = 4
    [ERROR_JUDGE]
    allow_time = 2
    allow_invite_times = 4
    [RECYCLE]
    auto_recycle_span = 0
    max_streams = 500
  run.sh: |+                        #启动脚本
    #!/bin/bash
    cd /opt/monit
    cp ./config/sipgw.ini .
    cp ./config/visualipc.ini .
    chmod 777 *
    #nohup ./visualipc >/dev/null 2>&1 &
    ./sipgw
    tail -f /dev/null
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: media-sipgw
  labels:
    app: media-sipgw
spec:
  replicas: 1
  selector:
    matchLabels:
      app: media-sipgw
  template:
    metadata:
      labels:
        app: media-sipgw
    spec:
      containers:
      - name: media-sipgw
        image: local.harbor.com/library/sipgw:dev-2.4
        imagePullPolicy: IfNotPresent    #镜像拉取规则
        command: ["sh", "/opt/monit/config/run.sh"]                    #启动命令
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 18081           #容器内部所用到的端口
        - containerPort: 30122
        volumeMounts:
        - name: media-sipgw-config
          mountPath: "/opt/monit/config"                   #配置文件要映射到的目录(如果容器由此目录,会直接覆盖)
        - name: media-sipgw-mount
          mountPath: "/home/mount"                        #挂载文件夹(容器内部的文件夹)可以实现将主机上一文件夹挂载到容器内部
        resources:   #资源限制
          requests:
            cpu: "500m"
            memory: "1000Mi"
          limits:
            cpu: "500m"
            memory: "1000Mi"
      volumes:
      - name: media-sipgw-config
        configMap:
          name: media-sipgw-config                           #ConfigMap的名字
      - name: media-sipgw-mount
        hostPath:
          path: /home/media-mount/sipgw                    #挂载文件夹所在宿主机文件夹(此方法能够实现持久化)
      nodeSelector:
        kubernetes.io/hostname: "global-node-02"      #指定宿主机,global-node-02是其节点名称
---
apiVersion: v1
kind: Service     #端口映射
metadata:
  name: media-sipgw
  labels:
    app: media-sipgw
spec:
  type: NodePort
  ports:
  - port: 18081
    protocol: TCP
    nodePort: 30121       #映射到主机外的端口
    targetPort: 18081      #容器间的端口
    name: sipgw1-1
  - port: 30122
    protocol: UDP
    nodePort: 30122
    targetPort: 30122
    name: sipgw1-2
  selector:
    app: media-sipgw

例子2:(持久化)

mysql 模板,可实现持久化

apiVersion: apps/v1
kind: Deployment
metadata:
  name: media-mysql
  labels:
    app: media-mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: media-mysql
  template:
    metadata:
      labels:
        app: media-mysql
    spec:
      nodeSelector:
        kubernetes.io/hostname: "global-node-02"
      containers:
      - name: media-mysql
        image: local.harbor.com/library/mysql:8.0.11
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD        #设置 mysql 密码的环境变量,更多可以从docker habor上查找
          value: "1234.com"     #密码
        volumeMounts:
        - mountPath: /var/lib/mysql      #docker内路径(mysql默认存储路径,尽量不要修改)
          name: data
      volumes:
      - name: data
        hostPath:
          path: /home/media-mount/mysql       #宿主
---
apiVersion: v1
kind: Service
metadata:
  name: media-mysql
  labels:
    app: media-mysql
spec:
  type: NodePort
  ports: 
  - port: 3306
    targetPort: 3306
    nodePort: 30151
  selector:
    app: media-mysql 
---   #若仅仅是容器内部访问,则可以用ClusterIP
apiVersion: v1
kind: Service
metadata:
  name: media-mysql
  labels:
    app: media-mysql
spec:
  type: ClusterIP
  ports: 
  - port: 3306
    targetPort: 3306
  selector:
    app: media-mysql 

k8s命令:

创建: kubectl create -f ***.yaml
删除:kubectl delete -f ***.yaml
查看deployment:kubectl get deploy
查看service--可以查看映射端口:kubectl get svc
查看service--可以查看映射端口:kubectl get svc
进入容器:kubectl exec -it *** bash/sh
查看pod启动日志:kubectl describe pod ***
查看容器打印在控制台的日志:kubectl logs podname

若想telnet测试容器端口连通性,需要进到busybox内,用telnet测试

例子3:(容器时间)

设置容器时间与宿主机一致

apiVersion: v1
kind: ConfigMap
metadata:
  name: media-warning-config
data:
  config_warning.ini: |+
    [server]
    port=8135
    callback= http://23.166.0.179/command/video/callback
    statusurl = http://23.166.0.179/async/vpaas/equipment/command/updatePlatformStatus
    querycallback = http://23.166.0.179/command/video/callback
    controlcallback = http://23.166.0.179/command/video/callback
    qualitycallback = http://23.166.0.179/command/video/callback
    rtspwarning = http://23.166.0.179/command/video/callback
    platreport = http://23.166.0.179/command/video/callback/%d/%s
    catlogurl = http://23.166.0.179/async/vpaas/equipment/command/callback
    platrtspurl = http://23.166.0.179/async/vpaas/vedio/command/callback
    [redis]
    host = redis-media
    port = 6379
    password= vpaas123
    db = 0
    [mysql]
    host =media-mysql-5-7
    port =3306
    db= fengdu-broacast
    username = root
    password = 1234.com
    [sipgw]
    sipgwHost = media-sipgw
    [time_limit]
    callback_time_limit = 10
  config.ini: |+
    [server]
    port=8136
    hlssrv=23.166.0.179:8005
    downloadurl=http://23.166.0.179/mp4/
    callbackurl =http://23.166.0.179/vpaas/media/pushDownloadStatus/
    [ffmpeg]
    path=/usr/sbin/ffmpeg
    outpath=/data/wwwroot/23.166.0.179/web/mp4/
  run.sh: |+
    #!/bin/bash
    cd /data/warning
    cp ./config/config_warning.ini .
    cp ./config/config.ini .
    chmod 777 *
    #nohup ./convert >/dev/null 2>&1 &
    ./warning
    #tail -f /dev/null
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: media-warning
  labels:
    app: media-warning
spec:
  replicas: 1
  selector:
    matchLabels:
      app: media-warning
  template:
    metadata:
      labels:
        app: media-warning
    spec:
      containers:
      - name: media-warning
        image: local.registry.com/media/warning:dev-3.2
        command: ["sh", "/data/warning/config/run.sh"]
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8135
        - containerPort: 8136
        volumeMounts:
        - name: media-warning-config
          mountPath: "/data/warning/config"
        - name: media-warning-mount
          mountPath: "/home/mount"
        - name: host-time
          readOnly: true
          mountPath: /etc/localtime
      volumes:
      - name: media-warning-config
        configMap:
          name: media-warning-config
      - name: media-warning-mount
        hostPath:
          path: /home/media-mount/warning
      - name: host-time
        hostPath:
          path: /etc/localtime
          type: ''
---
apiVersion: v1
kind: Service
metadata:
  name: media-warning
  labels:
    app: media-warning
spec:
  type: NodePort
  ports:
  - port: 8135
    protocol: TCP
    nodePort: 30143
    targetPort: 8135
    name: warning-1
  - port: 8136
    protocol: TCP
    nodePort: 30144
    targetPort: 8136
    name: warning-2
  selector:
    app: media-warning
 

设置宿主机或容器的时区可以通过更改  /etc/localtime 内容来实现的,比如直接在宿主机执行 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtimedate即可

这个例子是直接将宿主机上的  /etc/localtime 文件映射到容器内部的  /etc/localtime,这样就完成容器与宿主机时间同步的问题

例子4:(mysql最大连接数)

设置mysql 的最大连接数(大部分与例子3的一致,只是增加了mysql.cnf文件的挂载)

apiVersion: v1
kind: ConfigMap
metadata:
  name: media-mysql-config
data:
  mysql.cnf: |+
    [mysqld]
    max_connections=1000
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: media-mysql-5-7
  labels:
    app: media-mysql-5-7
spec:
  replicas: 1
  selector:
    matchLabels:
      app: media-mysql-5-7
  template:
    metadata:
      labels:
        app: media-mysql-5-7
    spec:
      containers:
      - name: media-mysql-5-7
        image: local.registry.com/media/mysql:5.7
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "1234.com"
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: data
        - name: host-time
          readOnly: true
          mountPath: /etc/localtime
        - name: media-mysql-config
          mountPath: "/etc/mysql/conf.d"
      volumes:
      - name: data
        hostPath:
          path: /home/media-mount/mysql-5.7
      - name: host-time
        hostPath:
          path: /etc/localtime
          type: ''
      - name: media-mysql-config
        configMap:
          name: media-mysql-config
      nodeSelector:
        kubernetes.io/hostname: "master02-test"
---
apiVersion: v1
kind: Service
metadata:
  name: media-mysql-5-7
  labels:
    app: media-mysql-5-7
spec:
  type: NodePort
  ports: 
  - port: 3306
    nodePort: 30152
  selector:
    app: media-mysql-5-7
 

查看mysql当前最大连接数:在mysql里边运行  show variables like '%max_connections%';(参考:https://my.oschina.net/yoblue/blog/1154196

一般更改mysql最大连接数是将 /etc/mysql/conf.d/mysql.cnf 的内容改为:(参考:https://blog.csdn.net/wangmiaoyan/article/details/103676232

[mysqld]
max_connections=1000

所以yaml文件直接将此文件映射到了 mysql 容器对应的目录了

加host:(与containers对应)

hostAliases:
      - hostnames:
        - rsf-qos.kodo.zywl.cn
        - io-qos.kodo.zywl.cn
        - api-qos.kodo.zywl.cn
        - up-qos.kodo.zywl.cn
        - video-dev.kodo.zywl.cn
        - rs-qos.kodo.zywl.cn
        ip: 192.168.105.80

 

Logo

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

更多推荐