本地Kubernetes部署架构

Staffjoy部署至Kubernetes - 图1

校验Kubernetes安装

1、启动minikube作为Kubernetes Server端

  1. minikube start

2、校验Kubernetes版本

  1. $ kubectl version
  2. Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.1", GitCommit:"5e58841cce77d4bc13713ad2b91fa0d961e69192", GitTreeState:"clean", BuildDate:"2021-05-12T14:18:45Z", GoVersion:"go1.16.4", Compiler:"gc", Platform:"linux/amd64"}
  3. Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.2", GitCommit:"faecb196815e248d3ecfb03c680a4507229c2a56", GitTreeState:"clean", BuildDate:"2021-01-13T13:20:00Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}

image.png

3、kubectl config current-context

  1. $ kubectl config current-context
  2. minikube

image.png

4、检查集群信息

  1. $ kubectl cluster-info
  2. Kubernetes control plane is running at https://192.168.49.2:8443
  3. KubeDNS is running at https://192.168.49.2:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
  4. To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

image.png

5、检查节点信息

  1. $ kubectl get nodes
  2. NAME STATUS ROLES AGE VERSION
  3. minikube Ready control-plane,master 16d v1.20.2

image.png

安装和访问Kubernetes Dashboard

1、安装Kubernetes Dashboard

https://github.com/kubernetes/dashboard

  1. kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.3.1/aio/deploy/recommended.yaml

:::danger 如果未部署成功并且多次执行了apply命令,可能会出现如下报错:
The ClusterRoleBinding "kubernetes-dashboard" is invalid: roleRef: Invalid value: rbac.RoleRef{APIGroup:"rbac.authorization.k8s.io", Kind:"ClusterRole", Name:"kubernetes-dashboard"}: cannot change roleRef
image.png
原因是已经存在重复的ClusterRoleBinding,使用命令kubectl delete clusterrolebinding kubernetes-dashboard删除重复的绑定即可。
然后再执行该命令则不会出现问题
image.png :::

  1. kubectl get pods --namespace=kubernetes-dashboard

:::tips 注意:这里的命令行参数--namespace后面的值要改为对应的YAML文件里面的Namespace的值
image.png ::: image.png

2、启动Kube Proxy

  1. kubectl proxy
  2. # 如果kubernetes部署在服务器通过公网IP访问则使用下面的命令,否则默认绑定的是127.0.0.1
  3. kubectl proxy --address='0.0.0.0'

image.png

3、生成访问令牌

  1. kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')

image.png

4、访问Dashboard

http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/
image.png

Staffjoy部署到本地Kubernetes环境

1、镜像构建(可选)

  1. mvn clean package -DskipTests
  2. docker-compose build
  3. docker images

服务容器化和Docker compose部署

2、部署MySQL数据库(可选)

  • staffjoy_account
  • staffjoy_company

    授予ip访问权限

    ```bash [root@iZuf68qk0bio1l786kylamZ ~]# docker exec -it mysql bash root@c4c2e8d188de:/# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 39 Server version: 8.0.26 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A

Database changed mysql> select user, host from user; +—————————+—————-+ | user | host | +—————————+—————-+ | root | % | | mysql.infoschema | localhost | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +—————————+—————-+ 5 rows in set (0.00 sec)

mysql> grant all privileges on . to root@127.0.0.1 identified by ‘123456’ with grant options;

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/396745/1632370673875-48b4f565-91ef-4c78-af27-ce6cd831bebc.png#clientId=u77fbdc3a-4907-4&from=paste&height=600&id=u9bef36f7&margin=%5Bobject%20Object%5D&name=image.png&originHeight=1801&originWidth=2259&originalType=binary&ratio=1&size=1405341&status=done&style=shadow&taskId=u05d90b3f-6dc2-4527-8113-36573abf6c4&width=753)
  2. <a name="sp0ra"></a>
  3. ### 3、部署Staffjoy(K8s/test)
  4. ```bash
  5. kubectl apply -f config.yml

image.png

  1. # 指定test目录下的所有yaml文件
  2. kubectl apply -f test

image.png

4、端口转发

  1. # 查询faraday pod名
  2. kubectl get pods
  3. kubectl poft-forward faraday-svc-deployment-8584d9c74d-v92wt 80:80

image.png

5、启动SwitchHosts

6、命令行校验

  1. kubectl get pods -o wide
  2. kubectl get services
  3. kubectl get deployments

image.png

7、K8s Dashboard校验

8、Staffjoy校验

9、清理

  1. kubectl delete deployment --all
  2. kubectl delete service --all
  3. kubectl delete configmaps --all

image.png

Staffjoy部署到阿里云Kubernetes架构

Staffjoy部署至Kubernetes - 图18

阿里云Kubernetes拓扑结构

https://developer.aliyun.com/article/88526
fbf3d81e025b172496c49eb688d302414efac1ba.png

阿里云Kubernetes环境创建

1、创建VPC

2、创建RDS数据库

加IP白名单
更新JDBC连接字符串
创建ROOT帐号
创建数据库和表

3、创建共享版Kubernetes集群

4、校验

  1. kubectl config current-context
  2. kubectl cluster-info
  3. kubectl get nodes

Staffjoy部署

1、部署Staffjoy(Kubernetes/UAT)

  1. kubectl apply -f config
  2. kubectl apply -f uat

2、阿里云Dashboard校验

3、命令行校验

  1. kubectl get pods -o wide
  2. kubectl get services
  3. kubectl get deployments

4、启用SwitchHosts

更新ELB IP地址

5、Staffjoy校验

6、删除阿里云Kubernetes和RDS