k8s-部署java+php业务网站项目案例+ELK Stack收集K8S平台日志+Prometheus全方位监控K8
一。准备环境
1.1 拉去gitlab代码 关于gitlab安装可以查看我以前博文
https://www.cnblogs.com/zhaobin-diray/p/13221556.html
mkdir /root/gitlab/
cd /root/gitlab/
git clone http://172.17.17.84:85/java/java.git #克隆代码
1.2 harbor镜像仓库搭建可以查看我以前博文
新建项目demon----
https://www.cnblogs.com/zhaobin-diray/p/13554765.html
docker login reg.ctnrs.com
账号 admin
密码 harbor12345
docker push reg.ctnrs.com/demo/java-demon:v1 #推送打好的tag镜像
1.3 代码编译,需要安装jdk.maven环境
yum install java-1.8.0-openjdk maven -y
14.修改国内mav源
vim /etc/maven/settings.xml 添加到 </mirrors> 上面
<mirror> <id>central</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>https://maven.aliyun.com/repository/public</url> </mirror>
1.5执行编译 在java项目里 执行构建
mvn clean package -D maven.test.skip=true
编译完成后当前目录多了target目录
docker build -t reg.ctnrs.com/demo/java-demon:v1 . 做镜像并且把镜像传到demon项目里
二。k8s编排部署项目
2.1创建命名空间
kubectl create ns test
为了防止每次登陆harbor可以创建secret来保存harbor账号密码,k8s里面调用方便,-n是指定给哪个命名空间所用
kubectl create secret docker-registry docker-regsitry-auth --docker-username=admin --docker-password=Harbor12345 --docker-server=reg.ctnrs.com -n test
2.2 使用deployment来部署项目
kubectl apply -f 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: reg.ctnrs.com/demo/java-demon:v1 name: java-demo imagePullPolicy: Always ports: - containerPort: 8080 name: web protocol: TCP resources: requests: cpu: 0.5 memory: 1.8Gi limits: cpu: 2 memory: 3Gi livenessProbe: httpGet: path: / port: 8080 initialDelaySeconds: 60 timeoutSeconds: 20 readinessProbe: httpGet: path: / port: 8080 initialDelaySeconds: 60 timeoutSeconds: 20
kubectl apply -f service.yaml
apiVersion: v1 kind: Service metadata: labels: app: java-demo name: java-demo namespace: test spec: ports: - port: 80 protocol: TCP targetPort: 8080 nodePort: 30018 selector: project: www app: java-demo type: NodePort
kubectl get svc -n test
kubectl get ep -n test
网页访问 就可以了 http://172.17.17.85:30018/
2.3 使用ingress来暴露项目
kubectl apply -f ingress-controller.yaml 需要有ingress-controller的支持
kubectl apply -f .
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: java-demo namespace: test spec: rules: - host: java.ctnrs.com http: paths: - path: / backend: serviceName: java-demo servicePort: 80
2.4 创建mysql数据库-用helm
helm install java-demon-db --set persistence.storageClass="managed-nfs-storage" azure/mysql
kubectl cp db/tables_ly_tomcat.sql java-demon-db-mysql-dc4bcf7fd-df42g:/ #copy数据表
kubectl exec -it java-demon-db-mysql-dc4bcf7fd-df42g bash #进入mysql容器
mysql -uroot -p$MYSQL_ROOT_PASSWORD 可以直接进入mysql
create databases java;
use java;
source /tables_ly_tomcat.sql;
grant all on java.* to 'root'@'%' identifiel by 'yuPML5qCdK';
CREATE TABLE `user` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(100) NOT NULL COMMENT '名字', `age` INT(3) NOT NULL COMMENT '年龄', `sex` CHAR(1) DEFAULT NULL COMMENT '性别', PRIMARY KEY (`id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8;
vim src/main/resources/application.yml
url: jdbc:mysql://java-demon-db-mysql.default:3306/java?characterEncoding=utf-8 username: root password: yuPML5qCdK
[root@k8s-master java]# mvn clean package -D maven.test.skip=true
[root@k8s-master k8s-yaml]# docker build -t reg.ctnrs.com/demo/java-demon:v2 .
[root@k8s-master k8s-yaml]# vim deployment.yaml #修改为新镜像
- image: reg.ctnrs.com/demo/java-demon:v2
[root@k8s-master java]# kubectl apply -f .
重新访问即可
三。 上传PHP-demon
[root@k8s-master k8s-yaml]# helm install php-demon-db --set persistence.storageClass="managed-nfs-storage" azure/mysql -n test
[root@k8s-master k8s-yaml]# MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default php-demon-db-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo)
[root@k8s-master php-demo]# echo $MYSQL_ROOT_PASSWORD #qOaSMpDX4v打印出来的密码 根据实际去填写一会
[root@k8s-master php-demo]# vim wp-config.php
define('DB_NAME', 'wp'); /** MySQL数据库用户名 */ define('DB_USER', 'root'); /** MySQL数据库密码 */ define('DB_PASSWORD', 'qOaSMpDX4v'); /** MySQL主机 */ define('DB_HOST', 'php-demon-db-mysql.default');
[root@k8s-master php-demo]# docker build -t reg.ctnrs.com/demo/php-demo:v1 . #打tag
[root@k8s-master php-demo]# docker push reg.ctnrs.com/demo/php-demo:v1
[root@k8s-master k8s-yaml]# kubectl get secret -n test 查看设置的变量docker-regsitry-auth
[root@k8s-master k8s-yaml]# vim deployment.yaml 修改下面2项
- name: docker-regsitry-auth
containers:
- name: nginx
image: reg.ctnrs.com/demo/php-demo:v1
[root@k8s-master k8s-yaml]# kubectl apply -f .
[root@k8s-master k8s-yaml]# kubectl exec -it php-demon-db-mysql-76bd66ff4d-nmglm bash 对面的是数据库的名字
root@php-demon-db-mysql-76bd66ff4d-nmglm:/# mysql -uroot -p$MYSQL_ROOT_PASSWORD
mysql> create databases wp;
页面访问 http://172.17.17.85:32144/