一 创建nfs(maste节点操作,两个node节点也需要安装)
1 首先安装一个nfs服务器,配置共享目录,
[yx@tidb-tidb-02 ~]$ cat /etc/exports
/home/yx/hnf *(rw,no_root_squash)
然后启动nfs
2 然后在master上面创建一个nfs pv的动态供给,需要三个文件class.yaml deployment.yaml rbac.yaml
这三个文件去网上下载 https://github.com/kubernetes-incubator/external-storage/tree/master/nfs-client/deploy
rbac是让nfs有权限访问apiserver,其中需要更改的地方是deploynment.yaml里面的nfsip地址和nfs所共享的目录
kubectl create -f rbac.yaml
kubectl create -f class.yaml
kubectl create -f deployment.yaml #第一次创建的时候回提示存在,不知道什么原因,然后删除,再次创建即可
最后查看是否创建成功
[yx@tidb-tidb-03 nfs]$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nfs-client-provisioner-89bb89db6-gwdhn 1/1 Running 0 4m46s
二 自己搭建dockerhub私有仓库,或者注册公有仓库,下面的操作在node节点上操作
1拉取项目代码
1 拉取代码,通过git
git clone https://github.com/lizhenliang/tomcat-java-demo.git
2 编译,要进入到项目那个目录里面
yum install maven 安装编译所需要的命令
mvn clean package
第一次需要很长的时间
3 镜像打包
需要先登录 docker login 用户名和密码就是你注册时候的用户名和密码
sudo docker build -t huningfei/tomcat .
sudo docker push huningfei/tomcat:latest #上传到dockerhub的公有仓库
三 创建java项目的yaml文件(master上面)
1 创建命名空间 namespace
namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: test
2 创建deployment 先创建secret认证 ,否则不能下载镜像
因为我的镜像上传到了dockerhub的公有仓库,如果是私有的,后面还需要跟 --docker-server=dockerhub-ip地址
认证:
kubectl create secret docker-registry registry-pull-secret --docker-username=huningfei --docker-password=password [email protected] -n test
## 查看是否创建成功
[yx@tidb-tidb-03 java-demo]$ kubectl get secret -n test
NAME TYPE DATA AGE
default-token-t2xwp kubernetes.io/service-account-token 3 3m4s
registry-pull-secret kubernetes.io/dockerconfigjson 1 34s
deployment.yaml 创建指定的pod
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: tomcat-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: registry-pull-secret
containers:
- name: tomcat
image: huningfei/tomcat:latest
imagePullPolicy: Always
ports:
- containerPort: 8080
name: web
protocol: TCP
resources:
requests:
cpu: 0.5
memory: 1Gi
limits:
cpu: 1
memory: 2Gi
livenessProbe:
httpGet:
path: /
port: 8080
initialDelaySeconds: 60
timeoutSeconds: 20
readinessProbe:
httpGet:
path: /
port: 8080
initialDelaySeconds: 60
timeoutSeconds: 20
创建deployment
kubectl create -f deployment.yaml #创建
[yx@tidb-tidb-03 java-demo]$ kubectl get pods -n test #查看是否创建成功
NAME READY STATUS RESTARTS AGE
tomcat-java-demo-755456cdd6-49rcc 0/1 Running 0 61s
tomcat-java-demo-755456cdd6-khvsc 0/1 Running 0 61s
tomcat-java-demo-755456cdd6-n67dt 0/1 Running 0 61s
发现三个tomct容器创建成功了
3 创建service,把端口映射出去
kubectl create -f service.yaml
apiVersion: v1
kind: Service
metadata:
name: tomcat-java-demo
namespace: test
spec:
selector:
project: www
app: java-demo
ports:
- name: web
port: 80
targetPort: 8080
4 创建ingress,为了使用域名访问(创建这个文件的是要提前部署好ingress mandatory.yaml)
kubectl create -f ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: tomcat-java-demo
namespace: test
spec:
rules:
- host: java.ctnrs.com
http:
paths:
- path: /
backend:
serviceName: tomcat-java-demo
servicePort: 80
5 最后查看pod和svc
kubectl get pod,svc -n test
NAME READY STATUS RESTARTS AGE
pod/tomcat-java-demo-755456cdd6-49rcc 1/1 Running 0 6m13s
pod/tomcat-java-demo-755456cdd6-khvsc 1/1 Running 0 6m13s
pod/tomcat-java-demo-755456cdd6-n67dt 1/1 Running 0 6m13s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/tomcat-java-demo ClusterIP 10.0.0.98 80/TCP 48s
#查看ingress在那个node上面
[yx@tidb-tidb-03 java-demo]$ kubectl get pods -n ingress-nginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
nginx-ingress-controller-7d8dc989d6-sfqcd 1/1 Running 0 2d5h 192.168.18.105 192.168.18.105
6 创建mysql,数据库没必要让外网访问,所以不产生ip
mysql.yaml
kubectl create -f mysql.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: test
spec:
ports:
- port: 3306
name: mysql
clusterIP: None
selector:
app: mysql-public
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: db
namespace: test
spec:
serviceName: "mysql"
template:
metadata:
labels:
app: mysql-public
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
- name: MYSQL_DATABASE
value: test
ports:
- containerPort: 3306
volumeMounts:
- mountPath: "/var/lib/mysql"
name: mysql-data
volumeClaimTemplates:
- metadata:
name: mysql-data
spec:
accessModes: ["ReadWriteMany"]
storageClassName: "managed-nfs-storage"
resources:
requests:
storage: 2Gi
7 查看pv
kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-73253e56-b425-11e9-b2ea-000c29bfd03f 2Gi RWX Delete Bound test/mysql-data-db-0 managed-nfs-storage 26m
8 进入数据库,导入表结构
kubectl exec -it db-0 bash -n test
然后把sql文件导入到数据库中,scp -r /home/yx/tomcat-java-demo/db/tables_ly_tomcat.sql 到maste上面
然后用kubeclt 直接拷贝到容器里面
kubectl cp /tmp/tables_ly_tomcat.sql db-0:/ -n test #db-0是pod的名字
最后在登陆到数据库中,source即可导入
9 更改数据库的ip,也就是mysql pod的名字
vim tomcat-java-demo/src/main/resources/application.yml
前面是pod的名字,然后是service名字(serviceName),最后是namespace命名空间
10 改完之后需要重新构建镜像
mvn clean package
然后sudo docker build -t huningfei/tomcat:1.0 .
推送 sudo docker push huningfei/tomcat:1.0 也可以指定别的版本,如2.0
11 滚动更新pod
如果镜像名字有变化,需要先更改deployment.yaml
没有则直接 kubectl apply -f deployment.yaml
然后查看是否在滚动更新
kubectl get pods -n test
下面这种情况说明在滚动更新,等最下面那个启动完成之后,原来的就删除掉一个
12 最后绑定域名,浏览器查看java.ctnrs.com
#查看域名
[yx@tidb-tidb-03 java-demo]$ kubectl get ingress -n test
NAME HOSTS ADDRESS PORTS AGE
tomcat-java-demo java.ctnrs.com 80 139m
最后访问域名即可,测试功能是否正常