k8s的部署脚本是什么

发布时间:2022-03-01 13:40:13 作者:iii
来源:亿速云 阅读:153

本篇内容主要讲解“k8s的部署脚本是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“k8s的部署脚本是什么”吧!

浅谈docker

docker可以类比为jvm,jvm也是虚拟机,然后docker的image可以类比为jar包,jar运行在jvm里面,image当然运行在docker容器里,然后jar包是通过maven 的pom.xml定义构建的产物,那docker也有一个Dockerfile来定义打包的镜像。在没有容器化之前,我们开发提供的最终产物是jar,容器化之后我们需要提供docker的镜像,docker的镜像会包含我们的jar,然后提供一个稳定的可移植的运行环境。下面是最简单的一个docker镜像构建:

k8s的部署脚本是什么

通过 docker build -t kl/sales . 就可以构建一个可运行的镜像

浅谈k8s的部署脚本

其实k8s的脚本没什么可说的,本身k8s的概念非常之多,具体可参考官方的文档,下面贴出我们的部署文件

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: sales-app
  namespace: #namespace
spec:
  replicas: 2
  minReadySeconds: 130  # 从容器启动到应用正常提供服务
  strategy:  # k8s更新策略
      type: RollingUpdate #滚动更新
      rollingUpdate:
        maxSurge: 2  # 更新时允许最大激增的容器数,默认 replicas 的 1/4 向上取整
        maxUnavailable: 1  # 更新时允许最大 unavailable 容器数,默认 replicas 的 1/4 向下取整
  template:
    metadata:
      labels:
        app: sales-app
    spec:
      imagePullSecrets: #镜像拉取秘钥
      - name: xx-xx-xx
      containers:
      - name: sales
        image: xxx-vpc.cn-xx.aliyuncs.com/keking/sales:#Tag
        imagePullPolicy: Always
        env:
        - name: runShell
          value: #runShell
        ports:
        - containerPort: 6002
          name: web
          protocol: TCP
        resources:
          limits:
            cpu: 600m
            memory: 4Gi
          requests:
            cpu: 300m
            memory: 100Mi
        livenessProbe: #检查是否存活
          httpGet:
            path: /health
            port: 6002
          initialDelaySeconds: 240 #首次检查在多少时间后
          periodSeconds: 180 #检查周期
          timeoutSeconds: 3 #超时时间
          failureThreshold: 3 #失败的阈值,连续三次
        readinessProbe: #检查是否成功启动
          httpGet:
            path: /health
            port: 6002
          initialDelaySeconds: 140 #首次检查在多少时间后
          periodSeconds: 300 #检查周期
          timeoutSeconds: 3 #超时时间

---
apiVersion: v1
kind: Service
metadata:
  name: sales-app
  namespace: #namespace
  labels:
    app: sales-app
spec:
  selector:
    app: sales-app
  ports:
  - name: web
    port: 80
    targetPort: 6002
    nodePort: 31942
  type: NodePort

最后聊聊jenkins pipeline

和一般的jenkins pipeline定义一样,容器化后就多了两个步骤,一个是在jar生成后,根据前面的讲的Dockerfile文件构建docker的镜像,这个要求jenkins所在主机必须有docker的环境。第二个步骤是发送k8s的部署脚本到k8s容器,这个是通过jenkins kuberneetes插件来完成的,详见下面的jenkins脚本

podTemplate(label: 'jnlp-slave', cloud: 'kubernetes', containers: [
        containerTemplate(
                name: 'jnlp',
                image: 'xx-vpc.cn-xx.aliyuncs.com/keking/jenkins-slave:latest',
                alwaysPullImage: true
        ),
],
        volumes: [
                hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
                hostPathVolume(mountPath: '/usr/bin/docker', hostPath: '/usr/bin/docker'),
                hostPathVolume(mountPath: '/usr/local/jdk', hostPath: '/usr/local/jdk'),
                hostPathVolume(mountPath: '/usr/local/maven', hostPath: '/usr/local/maven'),
        ],
        imagePullSecrets: ['registry-pull-secret'],
)
        {
            node("jnlp-slave") {
                stage('Git Checkout') {
                    checkout([$class: 'GitSCM', branches: [[name: "*/${branch}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'xx', url: 'http://xx.xx.xx/xx/yudian-salesplatform-boot.git']]])
                }
                stage('Maven Build') {
                    script {
                        if ('true' == "${build}") {
                            sh "mvn clean install -U  -P${profile} -Dmaven.test.skip=true"
                        } else {
                            echo "跳过maven build"
                        }
                    }
                }
                stage('Docker Build ') {
                    script {
                        if ('true' == "${build}") {
                            sh '''
                                docker login -u it_sh@keking-group -p x#xxx-vpc.cn-xx.aliyuncs.com
                                docker build -t xx-vpc.cn-xx.aliyuncs.com/keking/sales:${Tag} .
                                docker push xx-vpc.cn-xx.aliyuncs.com/keking/sales:${Tag}
                               '''
                        } else {
                            echo "跳过docker build"
                        }
                    }

                }
                stage('Deploy to K8s') {
                    script {
                        if('true' == "${deploy}"){
                            if ('prod' == "${profile}") {
                                sh '''
                                       sed -i "s/#namespace/prod/g" deploy.yaml
                                       sed -i "s%#runShell%java -Xmx2G -Xms2G -Xmn960M -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/dump.log -Xloggc:/tmp/gc.log -verbose:gc -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:MaxMetaspaceSize=128m -XX:MetaspaceSize=128m  -jar -Ddev_meta=http://conf.x.ops:8801/ -Denv=DEV -Didc=prod app.jar%g" deploy.yaml
                                   '''
                            }
                            if ('uat' == "${profile}") {
                                sh '''
                                       sed -i "s/#namespace/uat/g" deploy.yaml
                                       sed -i "s%#runShell%java -Xmx1536M -Xms1536M -Xmn960M -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/dump.log -Xloggc:/tmp/gc.log -verbose:gc -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:MaxMetaspaceSize=128m -XX:MetaspaceSize=128m  -jar -Ddev_meta=http://conf.keking.ops:8801/ -Denv=DEV -Didc=sales_docker app.jar%g" deploy.yaml                      
                                   '''
                            }
                            sh '''
                               sed -i "s/#Tag/${Tag}/g" deploy.yaml
                            '''
                            kubernetesDeploy configs: 'deploy.yaml', kubeConfig: [path: ''], kubeconfigId: '9c1da035-60b9-4bd8-aeae-1a31666280ce', secretName: '', ssh: [sshCredentialsId: '*', sshServer: ''], textCredentials: [certificateAuthorityData: '', clientCertificateData: '', clientKeyData: '', serverUrl: 'https://']
                        }else {
                            echo "跳过Deploy k8s"
                        }
                    }
                }
            }
        }

到此,相信大家对“k8s的部署脚本是什么”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. K8S——ETCD部署
  2. K8S 1.16.2 部署

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

k8s

上一篇:Mysql索引失效场景有哪些

下一篇:web中如何根据具体属性值选择

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》