美文网首页
k8s之容器迁移 [jenkins为例]

k8s之容器迁移 [jenkins为例]

作者: watson168 | 来源:发表于2017-11-14 16:38 被阅读720次

    迁移步骤

    • 容器内数据目录备份
    • 修改yaml配置文件
    • 重新创建容器
    • 将备份文件拷贝到容器中

    一、数据备份
    1.找到需要迁移的容器名称


    待迁移的容器

    2.找到迁移容器的数据目录

    [root@k8s-master01 jenkins]# cat jenkins-deployment.yaml 
    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: jenkins
    ...
     volumeMounts:
            - name: jenkins-home
              mountPath: /var/jenkins_home
    ...
    
    1. 备份容器中数据目录到本机

    [root@k8s-master01 jenkins]# kubectl -n kube-public cp jenkins-4163869451-kkxpc:/var/jenkins_home jenkins-dir/

    二、修改yaml配置文件

    1. pvc 修改点:namespace、storageClassName
    [root@k8s-master01 jenkins]# cat jenkins-pvc.yaml 
    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
      name: jenkins-pvc
      namespace: pp100
    spec:
      storageClassName: heketi-usage
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 50Gi
    
    1. deployment.yaml修改点:namespace
    [root@k8s-master01 jenkins]# cat jenkins-deployment.yaml 
    kind: Deployment
    metadata:
      name: jenkins
      namespace: pp100
      labels:
        app-name: jenkins
    spec:
      replicas: 1
      strategy:
        type: RollingUpdate
        rollingUpdate:
          maxSurge: 1
          maxUnavailable: 0
      template:
        metadata:
          labels:
            app-name: jenkins
        spec:
          containers:
          - name: jenkins
            image: docker.io/jenkins:latest
            imagePullPolicy: IfNotPresent
            ports:
            - containerPort: 8080
              name: web
              protocol: TCP
            - containerPort: 50000
              name: agent
              protocol: TCP
            volumeMounts:
            - name: jenkins-home
              mountPath: /var/jenkins_home
            env:
            - name: JAVA_OPTS
              value: "-Duser.timezone=Asia/Shanghai -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -XX:MaxRAMFraction=1 -Dhudson.slaves.NodeProvisioner.MARGIN=50 -Dhudson.slaves.NodeProvisioner.MARGIN0=0.85"
          volumes:
          - name: jenkins-home
            persistentVolumeClaim:
              claimName: jenkins-pvc
          dnsPolicy: ClusterFirst
    

    3.svc.yaml 主要修改点:namespace

    [root@k8s-master01 jenkins]# cat jenkins-service.yaml 
    kind: Service
    apiVersion: v1
    metadata:
      labels:
        app-name: jenkins
      name: jenkins
      namespace: pp100
    spec:
      ports:
      - port: 8080
        targetPort: 8080
        name: web
      - port: 50000
        targetPort: 50000
        name: agent
      selector:
        app-name: jenkins
    

    4.ingress 的修改点:namespace、host

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: jenkins
      namespace: pp100
    spec:
      rules:
      - host: k8s-jenkins100.pp100.net
        http:
          paths:
          - path: /
            backend:
              serviceName: jenkins
              servicePort: 8080
    

    三、创建一个新的容器
    1.创建并查看状态

    [root@k8s-master01 jenkins]# kubectl create -f jenkins-pvc.yaml    
    [root@k8s-master01 jenkins]# kubectl create -f jenkins-deployment.yaml      
    [root@k8s-master01 jenkins]# kubectl create -f jenkins-service.yaml         
    [root@k8s-master01 jenkins]# kubectl create -f jenkins-ingress.yaml
    [root@k8s-master01 jenkins]# kubectl get pod -n pp100  -o  wide 
    [root@k8s-master01 jenkins]# kubectl get pvc  -n pp100  -o  wide 
    [root@k8s-master01 jenkins]# kubectl get svc   -n pp100  -o  wide 
    [root@k8s-master01 jenkins]# kubectl get ing   -n pp100  -o  wide 
    

    四、登录到容器中清空数据文件

    [root@k8s-master01 jenkins]# kubectl -n pp100  exec -it  jenkins-4163869451-kkxpc  bash
    

    五、拷贝备份的数据文件到容器中

    [root@k8s-master01 jenkins]# kubectl -n pp100   cp ./jenkins-dir/   jenkins-4163869451-3r1nx:/var/jenkins_home/
    

    六、重启容器

    [root@k8s-master01 jenkins]# kubectl -n pp100 delete  pod jenkins-4163869451-kkxpc 
    

    七、根据实际情况修改容器中目录权限等问题

    相关文章

      网友评论

          本文标题:k8s之容器迁移 [jenkins为例]

          本文链接:https://www.haomeiwen.com/subject/gxsxvxtx.html