主机名 公网ip eth0内网ip 服务
    k8s-master 180.76.97.148 192.168.48.9 master
    k8s-node1 180.76.147.43 192.168.48.10 node1
    k8s-node2 180.76.159.209 192.168.48.11 node2
    k8s-etcd 180.76.116.214 192.168.48.12 etcd harbor
    ljsd-test-14 60.1.1.14 docker客户端
    1. #进入60.1.1.14
    2. #账号: root xxxxxxx
    3. #从git拉取gm项目的代码
    4. [root@ljsd-test-14 ~]# git clone http://60.1.1.230/backend/gm_tw_admin.git
    5. [root@ljsd-test-14 ~]# cd gm_tw_admin/
    6. #mvn编译打包,出来的jar在./target/gm_tw_admin-1.0-SNAPSHOT.jar
    7. [root@ljsd-test-14 gm_tw_admin]# mvn clean install
    8. [INFO] Scanning for projects...
    9. [INFO]
    10. [INFO] ------------------------------------------------------------------------
    11. [INFO] Building gm_tw_admin 1.0-SNAPSHOT
    12. [INFO] ------------------------------------------------------------------------
    13. [INFO]
    14. [INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ gm_tw_admin ---
    15. [INFO] Deleting /root/gm_tw_admin/target
    16. [INFO]
    17. [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ gm_tw_admin ---
    18. [INFO] Using 'UTF-8' encoding to copy filtered resources.
    19. [INFO] Copying 1 resource
    20. [INFO] Copying 1115 resources
    21. [INFO]
    22. [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ gm_tw_admin ---
    23. [INFO] Changes detected - recompiling the module!
    24. [INFO] Compiling 86 source files to /root/gm_tw_admin/target/classes
    25. [WARNING] /root/gm_tw_admin/src/main/java/com/jmfy/controller/RecoverItemController.java: Some input files use unchecked or unsafe operations.
    26. [WARNING] /root/gm_tw_admin/src/main/java/com/jmfy/controller/RecoverItemController.java: Recompile with -Xlint:unchecked for details.
    27. [INFO]
    28. [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ gm_tw_admin ---
    29. [INFO] Using 'UTF-8' encoding to copy filtered resources.
    30. [INFO] skip non existing resourceDirectory /root/gm_tw_admin/src/test/resources
    31. [INFO]
    32. [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ gm_tw_admin ---
    33. [INFO] No sources to compile
    34. [INFO]
    35. [INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ gm_tw_admin ---
    36. [INFO] No tests to run.
    37. [INFO]
    38. [INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ gm_tw_admin ---
    39. [INFO] Building jar: /root/gm_tw_admin/target/gm_tw_admin-1.0-SNAPSHOT.jar
    40. [INFO]
    41. [INFO] --- spring-boot-maven-plugin:1.4.3.RELEASE:repackage (default) @ gm_tw_admin ---
    42. [INFO]
    43. [INFO] --- maven-install-plugin:2.5.2:install (default-install) @ gm_tw_admin ---
    44. [INFO] Installing /root/gm_tw_admin/target/gm_tw_admin-1.0-SNAPSHOT.jar to /root/.m2/repository/com/deshen/gm_tw_admin/1.0-SNAPSHOT/gm_tw_admin-1.0-SNAPSHOT.jar
    45. [INFO] Installing /root/gm_tw_admin/pom.xml to /root/.m2/repository/com/deshen/gm_tw_admin/1.0-SNAPSHOT/gm_tw_admin-1.0-SNAPSHOT.pom
    46. [INFO] ------------------------------------------------------------------------
    47. [INFO] BUILD SUCCESS
    48. [INFO] ------------------------------------------------------------------------
    49. [INFO] Total time: 3.828 s
    50. [INFO] Finished at: 2019-05-06T17:52:19+08:00
    51. [INFO] Final Memory: 44M/437M
    52. [INFO] ------------------------------------------------------------------------
    53. #编写Dockerfile文件
    54. [root@ljsd-test-14 gm_tw_admin]# cat Dockerfile
    55. FROM primetoninc/jdk:1.8
    56. ADD ./target/gm_tw_admin-1.0-SNAPSHOT.jar /data/gm/gm_tw_admin-1.0-SNAPSHOT.jar
    57. ADD ./conf/ /data/gm/conf/
    58. EXPOSE 8888
    59. WORKDIR /data/gm/
    60. CMD ["java","-Dspring.config.location=./conf/application.properties","-jar","./gm_tw_admin-1.0-SNAPSHOT.jar"]
    61. #按照Dockerfile文件进行打包成镜像
    62. [root@ljsd-test-14 gm_tw_admin]# docker build -t gm:latest .
    63. Sending build context to Docker daemon 103.9 MB
    64. Step 1/6 : FROM primetoninc/jdk:1.8
    65. ---> f4b4fccc65bb
    66. Step 2/6 : ADD ./target/gm_tw_admin-1.0-SNAPSHOT.jar /data/gm/gm_tw_admin-1.0-SNAPSHOT.jar
    67. ---> 6eae187655db
    68. Removing intermediate container 8c5365b86b7c
    69. Step 3/6 : ADD ./conf/ /data/gm/conf/
    70. ---> 0c386e0e4204
    71. Removing intermediate container 9b37d0bfaa2a
    72. Step 4/6 : EXPOSE 8888
    73. ---> Running in 2e219c84522c
    74. ---> a4ef2a9c419c
    75. Removing intermediate container 2e219c84522c
    76. Step 5/6 : WORKDIR /data/gm/
    77. ---> 75829ad402f6
    78. Removing intermediate container 36cc1c3be630
    79. Step 6/6 : CMD java -Dspring.config.location=./conf/application.properties -jar ./gm_tw_admin-1.0-SNAPSHOT.jar
    80. ---> Running in b6489177e685
    81. ---> 436802adbf94
    82. Removing intermediate container b6489177e685
    83. Successfully built 436802adbf94
    84. #查看打包好的镜像
    85. [root@ljsd-test-14 gm_tw_admin]# docker images
    86. REPOSITORY TAG IMAGE ID CREATED SIZE
    87. gm latest 436802adbf94 29 seconds ago 708 MB
    88. #给刚打包好的镜像打tag
    89. [root@ljsd-test-14 gm_tw_admin]# docker tag gm:latest reg.kt007.com/ljsd/gm:latest
    90. #将tag镜像推送至harbor仓库
    91. [root@ljsd-test-14 gm_tw_admin]# docker push reg.kt007.com/ljsd/gm:latest
    92. #进入180.76.97.148 k8s-master
    93. [root@k8s-master ~]# cd gm/
    94. [root@k8s-master gm]# ls
    95. deployment_gm.yaml ingress-gm.yaml services_gm.yaml
    96. #编写yaml文件
    97. [root@k8s-master gm]# cat deployment_gm.yaml
    98. apiVersion: apps/v1
    99. kind: Deployment
    100. metadata:
    101. name: ysj-gm
    102. labels:
    103. app: ysj-tw-gm
    104. spec:
    105. replicas: 3
    106. selector:
    107. matchLabels:
    108. app: ysj-tw-gm
    109. template:
    110. metadata:
    111. labels:
    112. app: ysj-tw-gm
    113. spec:
    114. imagePullSecrets:
    115. - name: registry-pull-secret
    116. containers:
    117. - name: tomcat
    118. image: reg.kt007.com/ljsd/gm:latest
    119. imagePullPolicy: Always
    120. ports:
    121. - containerPort: 8888
    122. [root@k8s-master gm]#
    123. [root@k8s-master gm]# cat services_gm.yaml
    124. apiVersion: v1
    125. kind: Service
    126. metadata:
    127. name: ysj-gm
    128. namespace: default
    129. spec:
    130. selector:
    131. app: ysj-tw-gm
    132. ports:
    133. - name: http
    134. port: 8888
    135. targetPort: 8888
    136. [root@k8s-master gm]#
    137. [root@k8s-master gm]# cat ingress-gm.yaml
    138. apiVersion: extensions/v1beta1
    139. kind: Ingress
    140. metadata:
    141. name: ingress-gm
    142. namespace: default
    143. annotations:
    144. kubernets.io/ingress.class: "nginx"
    145. spec:
    146. rules:
    147. - host: gm.ljsd.com
    148. http:
    149. paths:
    150. - path:
    151. backend:
    152. serviceName: ysj-gm
    153. servicePort: 8888
    154. [root@k8s-master gm]#
    155. #创建gm deployment
    156. [root@k8s-master gm]# kubectl create -f deployment_gm.yaml
    157. #创建gm services
    158. [root@k8s-master gm]# kubectl create -f services_gm.yaml
    159. #使用ingress 发布
    160. [root@k8s-master gm]# kubectl create -f ingress-gm.yaml
    161. #查看ingress端口号是32080
    162. [root@k8s-master gm]# kubectl get svc -n ingress-nginx
    163. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    164. ingress-nginx NodePort 10.244.6.129 <none> 80:32080/TCP,443:32443/TCP 6d
    165. 编辑C:\Windows\System32\drivers\etc\hosts
    166. 添加一条
    167. 180.76.159.209 gm.ljsd.com
    168. #然后浏览器访问http://gm.ljsd.com:32080/login
    169. admin
    170. 123
    171. #存在一个问题,登陆进去后,页面提示又要登陆,问了开发,登陆那块做了session,k8s默认负载均衡策略是rr,第一次登陆在第一个pod,第二次登陆到第二个pod,session就会丢失.
    172. 解决方案:
    173. 1.kube-proxy默认使用的是iptables,默认是rr轮询.可以切换ipvs负载均衡,使用ip_hash.
    174. 2.yaml文件replicas的值设置成1
    175. 3.使用StatefulSet部署,而不使用Deployments部署.
    176. StatefulSet是为了解决有状态服务的问题(对应DeploymentsReplicaSets是为无状态服务而设计)