kubernetes(十三) k8s 业务上线流程(手动版)

时间:2020-08-02 10:02:16   收藏:0   阅读:26
k8s 实战

传统部署与k8s部署的区别

技术图片

技术图片

技术图片

制作镜像

技术图片

控制器管理POD

Pod数据持久化

技术图片

暴露应用

使用Service ClusterIP类型暴露集群内部的应用访问

技术图片

对外发布应用

技术图片

部署Java/PHP项目

部署Java项目

$ mkdir ~/base_env/ && cd ~/base_env
$ vim Dockerfile
FROM centos:7
LABEL maintainer="[email protected]"
ENV tomcat_version="8.5.57"
$  vim Dockerfile 
FROM hub.cropy.cn/base/tomcat:v1
LABEL maintainer="[email protected]"
RUN rm -rf /usr/local/tomcat/webapps/*
COPY target/*.war /usr/local/tomcat/webapps/ROOT.war

$ docker build -t hub.cropy.cn/demo/java-demo:v1 .
$ docker push hub.cropy.cn/demo/java-demo:v1
$ helm install java-demo-db --set persistence.storageClass="managed-nfs-storage" stable/mysql
$ MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default java-demo-db-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo)
$ echo $MYSQL_ROOT_PASSWORD    #获取数据库密码
$ mysql -h java-demo-db-mysql -p     #-h 这里的host是mysql的svc
E2szJIvuhI
$ kubectl get pods
NAME                                      READY   STATUS    RESTARTS   AGE
java-demo-db-mysql-745b9688b8-7f5fn       1/1     Running   0          93s
$ kubectl cp db/tables_ly_tomcat.sql java-demo-db-mysql-745b9688b8-7f5fn:/  #拷贝sql文件到数据库容器,然后导入数据
$ kubectl exec -it java-demo-db-mysql-745b9688b8-7f5fn -- bash
[email protected]:/# mysql -uroot -pE2szJIvuhI  
mysql> source /tables_ly_tomcat.sql;
$ vim src/main/resources/application.yml 
server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://java-demo-db-mysql.default:3306/test?characterEncoding=utf-8
    username: root
    password: E2szJIvuhI
    driver-class-name: com.mysql.jdbc.Driver
  freemarker:
    allow-request-override: false
    cache: true
    check-template-location: true
    charset: UTF-8
    content-type: text/html; charset=utf-8
    expose-request-attributes: false
    expose-session-attributes: false
    expose-spring-macro-helpers: false
    suffix: .ftl
    template-loader-path:
      - classpath:/templates/
$ mvn clean package -D maven.test.skip=true     #重新打包
$ docker build -t hub.cropy.cn/demo/java-demo:v2 . #重新构建
$ docker push hub.cropy.cn/demo/java-demo:v2     #镜像上传
$ vim k8s-yaml/deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: java-demo
  namespace: test
spec:
  replicas: 3
  selector:
    matchLabels:
      project: www
      app: java-demo
  template:
    metadata:
      labels:
        project: www
        app: java-demo
    spec:
      imagePullSecrets:
      - name: "docker-regsitry-auth"
      containers:
      - image: hub.cropy.cn/demo/java-demo:v2
        name: java-demo
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
          name: web
          protocol: TCP
        resources:
          requests:
            cpu: 0.5
            memory: 0.5Gi
          limits:
            cpu: 1
            memory: 2Gi
        livenessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 60
          timeoutSeconds: 20
        readinessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 60
          timeoutSeconds: 20
$ kubectl apply -f k8s-yaml/deployment.yaml
$ kubectl rollout history deploy/java-demo -n test    #查看历史
$ kubectl rollout undo deploy/java-demo -n test       #回滚至上一版本

如果项目不能正常写入数据,可以到myqsl添加授权即可

授权root有所有远程权限:

grant all on java.* to ‘root‘@‘%‘ identified by ‘E2szJIvuhI‘;
加入客户端主机名解析,然后浏览器直接访问即可

192.168.56.14 java.cropy.cn

部署php项目

$ git clone http://192.168.56.19:9999/root/php-demo.git
$ vim Dockerfile
FROM lizhenliang/nginx-php:latest
LABEL maintainer="php.cropy.cn"
ADD . /usr/local/nginx/html 
$ docker build -t hub.cropy.cn/demo/php-demo:v1 .
$ docker push hub.cropy.cn/demo/php-demo:v1
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!