微信公众号:运维开发故事,作者:double冬

对k8s刚入门的朋友而言,光搭建k8s集群是不够的,我们需要更多的理论加实战,才能更好的掌握k8s的好处,当我们成功部署一个k8s集群之后,我们需要在实际项目中进行应用,本文简单的介绍了当前比较主流的PHP/Java网站的部署

传统部署与K8S部署区别

通常使用传统的部署的时候,我们一个web项目,网站的搭建,往往使用的如下的一种整体架构,可能有的公司在某一环节使用的东西是不一样,但是大体的框架流程是都是差不多的
图片
使用K8S部署,便于弹性伸缩,节约资源,发布周期快,整体框架如下:
图片

准备环境

192.168.73.138 k8s-Master

192.168.73.139 k8s-node01

192.168.73.140 k8s-node02

192.168.73.136 Harbor镜像仓库

1 php项目部署流程

当我们把项目迁移到K8S平台上时,首先我们需要了解的是整个部署的流程,按照这个流程部署,才能避免出现问题,也方便大家理解
图片
github项目地址https://github.com/zhangdongdong7/php-demo.git

1.1 制作镜像

使用Dockerfile制作镜像,把应用程序、运行环境、文件系统一起打包成一个镜像,然后推送到Harbor镜像仓库中
首先在k8s的master节点进行操作

[root@k8s-master ]# git clone https://github.com/zhangdongdong7/php-demo.git
[root@k8s-master ]# cd php-demo[root@k8s-master php-demo]# ls
deployment.yaml  ingress.yaml  mysql.yaml  namespace.yaml  README.md  service.yaml  wordpress

使用wordpress创建一个博客网站,打开wordpress,编写Dockerfile构建镜像,然后推送到一个harbor镜像仓库中,可以看前面章节,harbor镜像的搭建,这里是使用的harbor镜像仓库地址为192.168.73.136

[root@k8s-master php-demo]cd wordpress
[root@k8s-master wordpress]# vim Dockerfile 
FROM lizhenliang/nginx-php:latest
MAINTAINER www.ctnrs.com
ADD . /usr/local/nginx/html
[root@k8s-master wordpress]docker login 192.168.73.136
[root@k8s-master wordpress]docker build -t 192.168.73.136/test/php-demo:latest . 
[root@k8s-master wordpress]docker push 192.168.73.136/test/php-demo:latest

1.2 创建控制器管理pod

回到php-demo目录编写yaml,首先部署一个test的命令空间

[root@k8s-master wordpress]# cd ../
[root@k8s-master php-demo]# vim namespace.yaml 
apiVersion: v1
kind: Namespace
metadata:
  name: test
[root@k8s-master php-demo]# kubectl apply -f  namespace.yaml

创建认证

[root@k8s-master php-demo]# kubectl create secret docker-registry registry-pull-secret —docker-username=admin —docker-password=Harbor12345 —docker-email=123@qq.com —docker-server=192.168.73.136 -n test

编写deployment.yaml控制器,这里需要把image进行修改成刚才推送到Harbor镜像仓库中的地址

[root@k8s-master php-demo]# vim deployment.yaml 
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: php-demo
  namespace: test
spec:
  replicas: 2
  selector:
    matchLabels:
      project: www
      app: php-demo
  template:
    metadata:
      labels:
        project: www
        app: php-demo
    spec:
      imagePullSecrets:
      - name: registry-pull-secret
      containers:
      - name: nginx
        image: 192.168.73.136/test/php-demo:v3
        imagePullPolicy: Always
        ports:
        - containerPort: 80
          name: web
          protocol: TCP
        resources:
          requests:
            cpu: 0.5
            memory: 256Mi
          limits:
            cpu: 1
            memory: 1Gi
        resources:
          requests:
            cpu: 0.5
            memory: 256Mi
          limits:
            cpu: 1
            memory: 1Gi
        livenessProbe:
          httpGet:
            path: /status.php
            port: 80

1.3 pod数据持久化

这里演示的是一个静态的web网站,基本不需要做持久化,直接把代码打包到了镜像中

1.4 暴露应用

创建一个service来暴露应用,直接使用的了ingress控制器的方式暴露应用了

[root@k8s-master php-demo]# vim service.yaml
apiVersion: v1
kind: Service
metadata:
  name: php-demo
  namespace: test
spec:
  selector:
    project: www
    app: php-demo
  ports:
  - name: web
    port: 80
    targetPort: 80
[root@k8s-master php-demo]# kubectl apply -f service.yaml
[root@k8s-master php-demo]# kubectl get pods,svc -n test -o wide
NAME                                    READY   STATUS    RESTARTS   AGE   IP            NODE         NOMINATED NODE   READINESS GATES
pod/php-demo-66d9c64968-4r4vn           1/1     Running   0          24h   10.244.1.73   k8s-node01   <none>           <none>
pod/php-demo-66d9c64968-8zw9s           1/1     Running   0          24h   10.244.2.43   k8s-node02   <none>           <none>
pod/tomcat-java-demo-5f4f64dd4b-tcmtv   1/1     Running   0          24h   10.244.2.42   k8s-node02   <none>           <none>
pod/tomcat-java-demo-5f4f64dd4b-vvx5x   1/1     Running   0          24h   10.244.1.72   k8s-node01   <none>           <none>

NAME                       TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE   SELECTOR
service/php-demo           NodePort    10.1.136.96   <none>        80:32625/TCP   23h   app=php-demo,project=www
service/tomcat-java-demo   ClusterIP   10.1.198.15   <none>        80/TCP         24h   app=java-demo,project=www
[root@k8s-master php-demo]#

1.5 创建ingress对外发布应用

编写yaml,首先创建ingress控制器,创建ingress,最后可以查看pod,svc,ingress的状态,全部都正常可以开始下一步,如果有异常可以使用kubectl describe命令查看日志进行排错

[root@k8s-master java-demo]# kubectl apply -f mandatory.yaml
[root@k8s-master php-demo]# vim ingress.yaml 
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: php-demo
  namespace: test
spec:
  rules:
    - host: php.ctnrs.com
      http:
        paths:
        - path: /
          backend:
            serviceName: php-demo
            servicePort: 80
[root@k8s-master php-demo]# kubectl apply -f ingress.yaml 
[root@k8s-master java-demo]# kubectl get pod,svc,ingress -n test -o wide
NAME                                    READY   STATUS    RESTARTS   AGE   IP            NODE         NOMINATED NODE   READINESS GATES
pod/php-demo-66d9c64968-4r4vn           1/1     Running   0          24h   10.244.1.73   k8s-node01   <none>           <none>
pod/php-demo-66d9c64968-8zw9s           1/1     Running   0          24h   10.244.2.43   k8s-node02   <none>           <none>
pod/tomcat-java-demo-5f4f64dd4b-tcmtv   1/1     Running   0          24h   10.244.2.42   k8s-node02   <none>           <none>
pod/tomcat-java-demo-5f4f64dd4b-vvx5x   1/1     Running   0          24h   10.244.1.72   k8s-node01   <none>           <none>

NAME                       TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE   SELECTOR
service/php-demo           NodePort    10.1.136.96   <none>        80:32625/TCP   24h   app=php-demo,project=www
service/tomcat-java-demo   ClusterIP   10.1.198.15   <none>        80/TCP         24h   app=java-demo,project=www

NAME                                  HOSTS            ADDRESS   PORTS   AGE
ingress.extensions/php-demo           php.ctnrs.com              80      24h
ingress.extensions/tomcat-java-demo   java.ctnrs.com             80      24h
[root@k8s-master java-demo]#            

可以在集群之外找一个虚机创建数据库,我是在harbor镜像仓库安装

docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
docker exec -it mysql:5.7 bash
mysql -uroot -p$MYSQL_ROOT_PASSWORDcreate 
database wp;

2.6 绑定本机hosts,访问域名验证

windows系统,hosts文件地址:C:\Windows\System32\drivers\etc,Mac系统sudo vi /private/etc/hosts 编辑hosts文件,在底部加入域名和ip,用于解析,这个ip地址为node节点ip地址
加入如下命令,然后保存

192.168.73.139 php.ctnrs.com

在浏览器中,输入php.ctnrs.com,会跳转到初始化设置界面,设置对应的账号,然后安装,登录,然后就可以编辑文章发布了,一个简单的WordPress的php网站搭建完成
图片
图片

2 Java项目部署流程

部署流程如下,跟php的网址一样
图片
github项目地址https://github.com/zhangdongdong7/java-demo.git

2.1 制作镜像

使用Dockerfile制作镜像,把应用程序、运行环境、文件系统一起打包成一个镜像,然后推送到Harbor镜像仓库中

  • 首先在k8s的master节点进行操作
[root@k8s-master ]# git clone https://github.com/zhangdongdong7/java-demo.git
[root@k8s-master java-demo]# cd java-demo
[root@k8s-master java-demo]# ls
deployment.yaml  ingress.yaml    mysql.yaml      README.md     tomcat-java-demo-master.zipdeploy.yml       mandatory.yaml  namespace.yaml  service.yaml
[root@k8s-master java-demo]# unzip tomcat-java-demo-master.zip
[root@k8s-master java-demo]# cd tomcat-java-demo-master/
  • 安装环境
[root@k8s-master tomcat-java-demo-master]# yum install java-1.8.0-openjdk maven -y
  • 编译构建
    如果maven构建慢的话,默认是官方源,可以换阿里云的源vim /etc/maven/settings.xml,大概在(159-164行),更换为如下代码
[root@k8s-master tomcat-java-demo-master]# vim /etc/maven/settings.xml    
    ...
    <mirror>
      <id>central</id>
      <mirrorOf>central</mirrorOf>
      <name>aliyun maven</name>
      <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
    ...
[root@k8s-master tomcat-java-demo-master]# ls
db  Dockerfile  LICENSE  pom.xml  README.md  src  target
  • Dockerfile已经打包好了,ls可以查看目录中已经存在,可以查看一下
[root@k8s-master tomcat-java-demo-master]# cat Dockerfile 
FROM lizhenliang/tomcat 
LABEL maintainer www.ctnrs.com
RUN rm -rf /usr/local/tomcat/webapps/
ADD target/.war /usr/local/tomcat/webapps/ROOT.war
  • 在tomcat目录下构建镜像
[root@k8s-master tomcat-java-demo-master]# docker login 192.168.73.136
Authenticating with existing credentials…WARNING! 
Your password will be stored unencrypted in /root/.docker/config.json.Configure a credential helper to remove this warning. 
Seehttps://docs.docker.com/engine/reference/commandline/login/#credentials-storeLogin 
Succeeded
[root@k8s-master tomcat-java-demo-master]docker build -t 192.168.73.136/test/java-demo:latest .
[root@k8s-master tomcat-java-demo-master]docker push 192.168.73.136/test/java-demo:latest

  • 回到上一层java-demo目录中
[root@k8s-master tomcat-java-demo-master]# cd ../
[root@k8s-master java-demo]# ls
db               deploy.yml    mandatory.yaml  namespace.yaml  service.yaml             tomcat-java-demo-master.zip
deployment.yaml  ingress.yaml  mysql.yaml      README.md       tomcat-java-demo-master
[root@k8s-master java-demo]#
  • 创建一个test的命名空间
[root@k8s-master java-demo]# cat namespace.yaml 
apiVersion: v1
kind: Namespace
metadata:
  name: test namespace.yaml
[root@k8s-master java-demo]# kubectl apply -f namespace.yaml  
  • 创建一个harbor仓库的拉取镜像认证,如果搭建php网站的时候创建过认证,这一步可以不用再创建,跳过
[root@k8s-master java-demo]# kubectl create secret docker-registry registry-pull-secret —docker-username=admin —docker-password=Harbor12345 —docker-email=123@qq.com —docker-server=192.168.73.136 -n test

2.2 创建控制器管理pod

  • 编写deployment.yaml,创建pods,这里需要把image进行修改成刚才推送到Harbor镜像仓库中的地址
[root@k8s-master java-demo]# vim deployment.yaml 
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: tomcat-java-demo
  namespace: test
spec:
  replicas: 2
  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: 192.168.73.136/test/java-demo:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
          name: web
          protocol: TCP
        resources:
          requests:
            cpu: 0.25
            memory: 1Gi
          limits:
            cpu: 1
            memory: 2Gi
        livenessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 60
          timeoutSeconds: 20
        readinessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 60
[root@k8s-master java-demo]# kubectl adpply -f deployment.yaml 

2.3 pod数据持久化

这里演示的是一个静态的web网站,基本不需要做持久化,直接把代码打包到了镜像中

2.4 暴露应用

创建一个service来暴露应用,直接使用的了ingress控制器的方式暴露应用了

[root@k8s-master java-demo]# cat 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
[root@k8s-master java-demo]# kubectl apply -f service.yaml  

2.5 创建ingress对外发布应用

编写yaml,因为刚才php项目创建过ingress控制器,因此可以不用创建,直接创建ingress,最后可以查看pod,svc,ingress的状态,全部都正常可以开始下一步,如果有异常可以使用kubectl describe命令查看日志进行排错

[root@k8s-master java-demo]# cat 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
[root@k8s-master java-demo]# kubectl apply -f ingress.yaml
[root@k8s-master java-demo]# kubectl get pod,svc,ingress -n test -o wide
NAME                                    READY   STATUS    RESTARTS   AGE   IP            NODE         NOMINATED NODE   READINESS GATES
pod/php-demo-66d9c64968-4r4vn           1/1     Running   0          24h   10.244.1.73   k8s-node01   <none>           <none>
pod/php-demo-66d9c64968-8zw9s           1/1     Running   0          24h   10.244.2.43   k8s-node02   <none>           <none>
pod/tomcat-java-demo-5f4f64dd4b-tcmtv   1/1     Running   0          24h   10.244.2.42   k8s-node02   <none>           <none>
pod/tomcat-java-demo-5f4f64dd4b-vvx5x   1/1     Running   0          24h   10.244.1.72   k8s-node01   <none>           <none>

NAME                       TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE   SELECTOR
service/php-demo           NodePort    10.1.136.96   <none>        80:32625/TCP   24h   app=php-demo,project=www
service/tomcat-java-demo   ClusterIP   10.1.198.15   <none>        80/TCP         24h   app=java-demo,project=www

NAME                                  HOSTS            ADDRESS   PORTS   AGE
ingress.extensions/php-demo           php.ctnrs.com              80      24h
ingress.extensions/tomcat-java-demo   java.ctnrs.com             80      24h
[root@k8s-master java-demo]#            

2.6 绑定本机hosts,访问域名验证

windows系统,hosts文件地址:C:\Windows\System32\drivers\etc,Mac系统sudo vi /private/etc/hosts 编辑hosts文件,在底部加入域名和ip,用于解析,这里的ip是node的ip地址
加入如下命令,然后保存

192.168.73.139 java.ctnrs.com

在浏览器中输入java.ctnrs.com访问
图片

图片
如有不对的地方和问题,欢迎指出和交流

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐