侧边栏壁纸
博主头像
qingtian博主等级

喜欢是一件细水流长的事,是永不疲惫的双向奔赴~!

  • 累计撰写 104 篇文章
  • 累计创建 48 个标签
  • 累计收到 1 条评论

将应用向Kubernetes迁移

qingtian
2022-12-17 / 0 评论 / 0 点赞 / 54 阅读 / 6,139 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-12-17,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

Kubernetes生产落地实践_06

StatefulSet部署 Mysql

apiVersion: v1
kind: Service
metadata:
  name: mysql57
  labels:
    app: mysql
spec:
  ports:
  - port: 3306
    name: mysql
    nodePort: 30306
  type: NodePort
  selector:
    app: mysql
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql # has to match .spec.template.metadata.labels
  serviceName: "mysql"
  replicas: 1 # by default is 1
  template:
    metadata:
      labels:
        app: mysql # has to match .spec.selector.matchLabels
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: mysql
        image: registry.cn-beijing.aliyuncs.com/qingfeng666/mysql:5.7
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: host-path
          mountPath: /var/lib/mysql
        env:
          - name: MYSQL_ROOT_PASSWORD
            value: "password"
      volumes:
        - name: host-path
          hostPath:
              path: /tmp/mysql
              type: DirectoryOrCreate

部署xxl-job

apiVersion: v1
kind: Service
metadata:
  name: xxl-job
spec:
  selector:
    app: xxl-job
  type: NodePort
  ports:
  - port: 9080
    targetPort: 8080
    nodePort: 30002
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: xxl-job
spec:
  selector:
    matchLabels:
      app: xxl-job
  template:
    metadata:
      labels:
        app: xxl-job
    spec:
      containers:
      - name: xxl-job
        image: xxl-job-admin:2.3.0
        resources:
          limits: 
            memory: "512Mi"
            cpu: "500m"
        ports:
        - containerPort: 8080
        env:
          - name: MYSQL_PORT
            value: "30306"
          - name: MYSQL_SERVER
            value: "192.168.86.129"
          - name: MYSQL_USER_TEST
            value: "root"
          - name: MYSQL_PASSWORD_TEST
            value: "password"

使用私有镜像中心拉取镜像

创建secret配置私有镜像仓库

kubectl create secret docker-registry regcred-local --docker-server=art.local:8081 --docker-username=admin --docker-password=123456789gk --docker-email=xxxxx@qq.com
apiVersion: v1
kind: Service
metadata:
  name: xxl-job
spec:
  selector:
    app: xxl-job
  type: NodePort
  ports:
  - port: 9080
    targetPort: 8080
    nodePort: 30002
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: xxl-job
spec:
  selector:
    matchLabels:
      app: xxl-job
  template:
    metadata:
      labels:
        app: xxl-job
    spec:
      containers:
      - name: xxl-job
        image: art.local:8081/docker-local/xxl-job-admin:2.3.0
        resources:
          limits: 
            memory: "512Mi"
            cpu: "500m"
        ports:
        - containerPort: 8080
        env:
          - name: MYSQL_PORT
            value: "30306"
          - name: MYSQL_SERVER
            value: "192.168.86.129"
          - name: MYSQL_USER_TEST
            value: "root"
          - name: MYSQL_PASSWORD_TEST
            value: "password"
      imagePullSecrets: #让k8s使用本地的secret配置来拉取私有镜像仓库的镜像
      - name: regcred-local

为应用进行配置分离

为MYSQL创建secret

kubectl create secret generic  db-xxl-job --from-literal=mysql_server=192.168.86.129 --from-literal=mysql_port=30306 --from-literal=mysql_username=root --from-literal=mysql_password=password
apiVersion: v1
kind: Service
metadata:
  name: xxl-job
spec:
  selector:
    app: xxl-job
  type: NodePort
  ports:
  - port: 9080
    targetPort: 8080
    nodePort: 30002
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: xxl-job
spec:
  selector:
    matchLabels:
      app: xxl-job
  template:
    metadata:
      labels:
        app: xxl-job
    spec:
      containers:
      - name: xxl-job
        image: art.local:8081/docker-local/xxl-job-admin:2.3.0
        resources:
          limits: 
            memory: "512Mi"
            cpu: "500m"
        ports:
        - containerPort: 8080
        env:
          - name: MYSQL_PORT
            valueFrom:
              secretKeyRef:
                name: db-xxl-job
                key: mysql_port
          - name: MYSQL_SERVER
            valueFrom:
              secretKeyRef:
                name: db-xxl-job
                key: mysql_server
          - name: MYSQL_USER_TEST
            valueFrom:
              secretKeyRef:
                name: db-xxl-job
                key: mysql_username
          - name: MYSQL_PASSWORD_TEST
            valueFrom:
              secretKeyRef:
                name: db-xxl-job
                key: mysql_password
          - name: MYSQL_DB_NAME
            value: "xxl_job_1"
      imagePullSecrets:
      - name: regcred-local

使用Helm

安装Helm

  • Kubernetes 中的包管理工具

  • 用 Yaml 管理多个应用同时部署

  • 实现了部署的版本管理

  • 应用和配置分离

优点

  • 管理复杂的多容器部署(微服务发布)

  • 版本更新更加容易

  • 共享版本更加方便

  • 一键回滚多个容器版本

下载helm

wget  https://get.helm.sh/helm-v3.7.1-linux-amd64.tar.gz -O helm-v3.7.1-linux-amd64.tar.gz
# 解压
tar -zxvf helm-v3.7.1-linux-amd64.tar.gz
mv  linux-amd64/helm  /usr/local/bin/helm
helm version

配置国内阿里云镜像源

helm repo remove stable
helm repo add stable http://mirror.azure.cn/kubernetes/charts/
helm repo update

Helm使用命令

1、 create

创建一个 chart 并指定名字

2、 dependency

管理 chart 依赖

3、get

下载一个 release。可用子命令:all、hooks、manifest、notes、values

4、history

获取 release 历史

5、install

安装一个 chart

6、list

列出 release

7、package

将 chart 目录打包到 chart 存档文件中

8、pull

从远程仓库中下载 chart 并解压到本地 # helm pull stable/mysql --untar

9、repo

添加,列出,移除,更新和索引 chart 仓库。可用子命令:add、index、list、remove、update

10、rollback

从之前版本回滚

11、search

根据关键字搜索 chart。可用子命令:hub、repo

12、show

查看 chart 详细信息。可用子命令:all、chart、readme、values

13、status

显示已命名版本的状态

14、template

本地呈现模板

15、uninstall

卸载一个 release

16、upgrade

更新一个 release

17、version

查看 helm 客户端版本

配置本地Helm仓库

image-20221213114816276

helm repo add helm http://192.168.86.128:8081/artifactory/helm --username admin --password AP9UhjFFA7xGrMTA5VQCXLqLiso

创建xxl-job Helm Chart

image-20221217192837410

value.yaml

# Default values for xxl-job.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

image:
  repository: art.local:8081/docker-local/xxl-job-admin
  pullPolicy: IfNotPresent
  # Overrides the image tag whose default is the chart appVersion.
  tag: "2.3.0"

service:
  type: NodePort
  targetPort: 8080
  port: 9080
  nodePort: 30002

mysql:
  port: 30306
  server: 192.168.86.129
  user: root
  password: password

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: {{ .Release.Name }}
spec:
  selector:
    app: {{ .Release.Name }}
  type: NodePort
  ports:
  - port: {{ .Values.service.port }}
    targetPort: {{ .Values.service.targetPort }}
    nodePort: {{ .Values.service.nodePort }}

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}
spec:
  selector:
    matchLabels:
      app: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}
    spec:
      containers:
      - name: {{ .Release.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        resources:
          limits: 
            memory: "512Mi"
            cpu: "500m"
        ports:
        - containerPort: 8080
        env:
          - name: MYSQL_PORT
            value: "{{ .Values.mysql.port }}"
          - name: MYSQL_SERVER
            value: "{{ .Values.mysql.server }}"
          - name: MYSQL_USER_TEST
            value: "{{ .Values.mysql.user }}"
          - name: MYSQL_PASSWORD_TEST
            value: "{{ .Values.mysql.password }}"
          - name: MYSQL_DB_NAME
            value: "xxl_job_1"
      imagePullSecrets:
      - name: regcred-local

# 打包
helm package xxl-job xxl-job
# 安装
helm install xxl-job xxl-job-0.1.0.tgz

HelmChart上传至JCR

# 上传至私有仓库
curl -uadmin:AP9UhjFFA7xGrMTA5VQCXLqLiso -T xxl-job-0.1.0.tgz "http://192.168.86.128:8081/artifactory/helm/xxl-job-0.1.0.tgz"
helm repo update
# 搜索xxl-job
helm search repo xxl-job
# 使用helm镜像部署
helm install xxl-job helm/xxl-job

使用Helm进行升级和回滚

# 部署指定版本
helm install xxl-job helm/xxl-job --version 0.1.1
# 升级
helm upgrade xxl-job helm/xxl-job --version 0.1.2
# 回滚 每次helm进行升级都会变更version
helm rollback xxl-job 1

在不同的环境部署HelmChart

helm install kubeblog xxl-job-0.1.1.tgz -f xxl-job/values-test.yaml -n test
0

评论区