k8s-应用部署
该demo主要作为一个dubbo项目通过maven自动化docker打包插件发布到镜像仓库样例工程。该wiki后面同时会提供k8s部署zk,mysql,应用包的整个过程。该项目大体功能:zk作为注册中心,服务端发布服务,消费端订阅服务,在调用过程中访问mysql数据库。
环境准备:k8s集群,以及配置好kubectl客户端工具.
项目源码:https://github.com/HushAsy/demo
1.provider,consumer镜像制作:
关于自动化镜像发布,可以参考我的另一篇文章:https://www.cnblogs.com/HushAsy/p/9896196.html
此处贴出Dockerfile文件:
FROM registry.cn-hangzhou.aliyuncs.com/hush/basecontainer:20180929 #添加本地jar包 RUN mkdir -p /home/admin/app/ RUN mkdir -p /home/admin/tomcat/ ENV CATALINA_HOME /home/admin/tomcat/ # ARG JAR_FILE ADD target/${JAR_FILE} /home/admin/app/ # # # # 将启动命令写入启动脚本 start.sh RUN echo "$JAVA_HOME/bin/java -jar $JAVA_OPTS -Ddubbo.address.ip=$POD_IP -Djava.security.egd=file:/dev/./urandom /home/admin/app/${JAR_FILE} --spring.profiles.active=prod" > /home/admin/start.sh && chmod +x /home/admin/start.sh WORKDIR $CATALINA_HOME ENTRYPOINT ["/bin/bash", "/home/admin/start.sh"]
2.mysql-app.yaml
#mysql-app.yaml apiVersion: apps/v1 kind: Deployment metadata: name: mysql labels: app: mysql spec: replicas: 1 selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: containers: - image: registry-vpc.cn-hangzhou.aliyuncs.com/hush/dubbo_mysql name: mysql env: - name: MYSQL_ROOT_PASSWORD value: "123456" ports: - containerPort: 3306 name: mysql
#在客户端提交yaml文件创建pod
kubectl create -f mysql-app.yaml
#创建成功后,执行下面命名可以看到当前pod执行情况
kubectl get pods
3.mysql service yaml配置
#mysql-srv.yaml
apiVersion: v1 kind: Service metadata: name: mysql spec: ports: - port: 3306 selector: app: mysql
4.zk.yaml配置
--- kind: Deployment apiVersion: extensions/v1beta1 metadata: name: zk-0 spec: replicas: 1 template: metadata: labels: app: zk node: node0 spec: hostname: zk-0 volumes: - name: zk-data hostPath: path: /data/zk-cluster/zk-data-0 - name: zk-logs hostPath: path: /data/zk-cluster/zk-logs-0 containers: - name: zk-0 image: registry-vpc.cn-hangzhou.aliyuncs.com/hush/dubbo_zk imagePullPolicy: IfNotPresent volumeMounts: - name: zk-data readOnly: false mountPath: "/data/zk-data" - name: zk-logs readOnly: false mountPath: "/data/zk-logs" ports: - containerPort: 2181 - containerPort: 2888 - containerPort: 3888 #command: ['tail', '-f', '/etc/hosts'] env: - name: ZOO_MY_ID value: '0' - name: ZOO_SERVERS # 注意!!k8s使用到virtual ip,因此,本机必须使用0.0.0.0 ip地址,否则本机zk启动会异常: # ERROR [zk1/10.0.0.251:3888:QuorumCnxManager$Listener@547] - Exception while listening # java.net.BindException: Address not available (Bind failed)错误 value: server.0=0.0.0.0:2888:3888 server.1=zk-1:2888:3888 server.2=zk-2:2888:3888 - name: ZOO_DATA_DIR value: '/data/zk-data' - name: ZOO_DATA_LOG_DIR value: '/data/zk-logs' --- kind: Deployment apiVersion: extensions/v1beta1 metadata: name: zk-1 spec: replicas: 1 template: metadata: labels: app: zk node: node1 spec: hostname: zk-1 volumes: - name: zk-data hostPath: path: /data/zk-cluster/zk-data-1 - name: zk-logs hostPath: path: /data/zk-cluster/zk-logs-1 containers: - name: zk-1 image: registry-vpc.cn-hangzhou.aliyuncs.com/hush/dubbo_zk imagePullPolicy: IfNotPresent volumeMounts: - name: zk-data readOnly: false mountPath: "/data/zk-data" - name: zk-logs readOnly: false mountPath: "/data/zk-logs" ports: - containerPort: 2181 - containerPort: 2888 - containerPort: 3888 #command: ['tail', '-f', '/etc/hosts'] env: - name: ZOO_MY_ID value: '1' - name: ZOO_SERVERS # 注意!!k8s使用到virtual ip,因此,本机必须使用0.0.0.0 ip地址,否则本机zk启动会异常: # ERROR [zk1/10.0.0.251:3888:QuorumCnxManager$Listener@547] - Exception while listening # java.net.BindException: Address not available (Bind failed)错误 value: server.0=zk-0:2888:3888 server.1=0.0.0.0:2888:3888 server.2=zk-2:2888:3888 - name: ZOO_DATA_DIR value: '/data/zk-data' - name: ZOO_DATA_LOG_DIR value: '/data/zk-logs' --- kind: Deployment apiVersion: extensions/v1beta1 metadata: name: zk-2 spec: replicas: 1 template: metadata: labels: app: zk node: node2 spec: hostname: zk-2 volumes: - name: zk-data hostPath: path: /data/zk-cluster/zk-data-2 - name: zk-logs hostPath: path: /data/zk-cluster/zk-logs-2 containers: - name: zk-2 image: registry-vpc.cn-hangzhou.aliyuncs.com/hush/dubbo_zk imagePullPolicy: IfNotPresent volumeMounts: - name: zk-data readOnly: false mountPath: "/data/zk-data" - name: zk-logs readOnly: false mountPath: "/data/zk-logs" ports: - containerPort: 2181 - containerPort: 2888 - containerPort: 3888 #command: ['tail', '-f', '/etc/hosts'] env: - name: ZOO_MY_ID value: '2' - name: ZOO_SERVERS # 注意!!k8s使用到virtual ip,因此,本机必须使用0.0.0.0 ip地址,否则本机zk启动会异常: # ERROR [zk1/10.0.0.251:3888:QuorumCnxManager$Listener@547] - Exception while listening # java.net.BindException: Address not available (Bind failed)错误 value: server.0=zk-0:2888:3888 server.1=zk-1:2888:3888 server.2=0.0.0.0:2888:3888 - name: ZOO_DATA_DIR value: '/data/zk-data' - name: ZOO_DATA_LOG_DIR value: '/data/zk-logs'
5.zk service yaml配置
--- kind: Service apiVersion: v1 metadata: name: zk-0 labels: app: zk node: node0 spec: type: LoadBalancer ports: - name: port-2181 port: 2181 - name: port-2888 port: 2888 - name: port-3888 port: 3888 selector: app: zk node: node0 --- kind: Service apiVersion: v1 metadata: name: zk-1 labels: app: zk node: node1 spec: type: LoadBalancer ports: - name: port-2181 port: 2181 - name: port-2888 port: 2888 - name: port-3888 port: 3888 selector: app: zk node: node1 --- kind: Service apiVersion: v1 metadata: name: zk-2 labels: app: zk node: node2 spec: type: LoadBalancer ports: - name: port-2181 port: 2181 - name: port-2888 port: 2888 - name: port-3888 port: 3888 selector: app: zk node: node2
6.部署provider yaml
apiVersion: apps/v1 kind: Deployment metadata: name: provider labels: app: provider spec: replicas: 1 selector: matchLabels: app: provider template: metadata: labels: app: provider spec: containers: - name: provider image: <镜像地址> ports: - containerPort: 20880
7.consumer deployment yaml
apiVersion: apps/v1 kind: Deployment metadata: name: consumer labels: app: consumer spec: replicas: 1 selector: matchLabels: app: consumer template: metadata: labels: app: consumer spec: containers: - name: consumer image: <镜像地址> ports: - containerPort: 8080
8.consumer service yaml
kind: Service apiVersion: v1 metadata: name: consumer_svc spec: selector: app: consumer ports: - protocol: TCP port: 8080
9. 前端访问控制 ingress 可以瞅瞅这位哥写的博客:https://mritd.me/2017/03/04/how-to-use-nginx-ingress/
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: test annotations: nginx.ingress.kubernetes.io/rewrite-target: / spec: rules: - host: <host> http: paths: - path: /index backend: serviceName: consumer_svc servicePort: 8080
通过对应域名或ip访问 完美