k8s环境实现springcloud电商项目(一)之环境准备(Ingress-Mysql-harbor安装)

k8s环境实现springcloud电商项目(一)之环境准备(Ingress-Mysql-harbor安装)

基于 springcloud 的电商平台功能图

ingress及ingress controller的部署可参考上一篇文章https://www.cnblogs.com/fengdejiyixx/p/16344502.html

1. 环境准备

准备安装harbor 需要的实验环境
新创建一台虚拟机安装 harbor,配置如下:
主机名 ip 配置
harbor 192.168.7.51 4vCPU/4G 内存/60G 硬盘

1.1 修改 yum 源

选择一种安装 docker 的方式:1. 在线安装 或 2. 离线安装
1.在线安装
配置 docker-ce 国内 yum 源(阿里云)
[root@k8s-node1 ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

安装软件包

[root@k8s-node1 ~]# yum -y install wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo ntp libaio-devel wget vim ncurses-devel autoconf automake zlib-devel python-devel epel-release openssh-server socat ipvsadm conntrack ntpdate yum-utils device-mapper-persistent-data lvm2 telnet

1.2 配置防火墙

关闭 firewalld 防火墙, centos7 系统默认使用的是 firewalld 防火墙,停止 firewalld 防火墙,并禁用这个服务。
[root@k8s-node1 ~]# systemctl stop firewalld && systemctl disable firewalld

1.3 时间同步

[root@k8s-node1 ~]# ntpdate cn.pool.ntp.org编辑计划任务,每小时做一次同步
1)
[root@k8s-node1 ~]# crontab -e

  • */1 * * * /usr/sbin/ntpdate cn.pool.ntp.org
    2)重启 crond 服务:
    [root@k8s-node1 ~]# systemctl restart crond

1.4 关闭 selinux

关闭 selinux,设置永久关闭,这样重启机器 selinux 也处于关闭状态可用下面方式修改:
[root@k8s-node1 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/'
/etc/sysconfig/selinux
[root@k8s-node1 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g'
/etc/selinux/config
上面文件修改之后,需要重启虚拟机,如果测试环境可以用如下命令强制重启:
[root@k8s-node1 ~]# reboot -f
注:生产环境不要 reboot -f,要正常关机重启

查看 selinux 是否修改成功
重启之后登录到机器上用如下命令:
[root@k8s-node1 ~]# getenforce
显示 Disabled 说明 selinux 已经处于关闭状态

1.5 修改内核参数

[root@k8s-node1 ~]# modprobe br_netfilter
[root@k8s-node1 ~]# cat < /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
[root@k8s-node1 ~]# sysctl --system

注:sysctl --system 这个会加载所有的 sysctl 配置
修改主机名
在 192.168.7.51 上:
[root@k8s-node1 ~]# hostnamectl set-hostname harbor && bash
5.2.8配置 hosts 文件
k8s-master1、k8s-node2、harbor 主机的 hosts 文件保持一致,可按如下方法修改:在/etc/hosts 文件增加如下几行:
192.168.7.20k8s-master1
192.168.7.23k8s-node2
192.168.7.51 harbor

1.6 配置主机之间无密码登陆

配置 k8s-master1 到 harbor 无密码登陆在 k8s-master1 上操作
[root@k8s-master1 ~]# cd /root && ssh-copy-id -i .ssh/id_rsa.pub root@harbor1
#上面需要输入 yes 之后,输入密码,输入 harbor 物理机密码即可

2. 实战:使用 Harbor 搭建Docker 私有仓库

Harbor 介绍
Docker 容器应用的开发和运行离不开可靠的镜像管理,虽然 Docker 官方也提供了公共的镜像仓库,但是从安全和效率等方面考虑,部署我们私有环境内的 Registry 也是非常必要的。Harbor 是由
VMware 公司开源的企业级的 Docker Registry 管理项目,它包括权限管理(RBAC)、LDAP、日志审核、管理界面、自我注册、镜像复制和中文支持等功能。
官网地址:https://github.com/goharbor/harbor
实验环境:

harbor 机器需要的内存至少要 4G

2.1 为 Harbor 自签发证书

[root@harbor ~]# mkdir /data/ssl -p && cd /data/ssl/
生成 ca 证书:
[root@harbor ssl]# openssl genrsa -out ca.key 3072
#生成一个 3072 位的 key,也就是私钥
[root@harbor ssl]# openssl req -new -x509 -days 3650 -key ca.key -out ca.pem
#生成一个数字证书 ca.pem,3650 表示证书的有效时间是 10 年,按箭头提示填写即可,没有箭头标注的为空:

[root@harbor ssl]# openssl req -new -x509 -days 3650 -key ca.key -out ca.pem You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank
For some fields there will be a default value, If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:guangdong Locality Name (eg, city) [Default City]: shenzhen
Organization Name (eg, company) [Default Company Ltd]:xxxx Organizational Unit Name (eg, section) []:CA
Common Name (eg, your name or your server's hostname) []:harbor Email Address []:mk@163.com
\#生成域名的证书:
[root@harbor ssl]# openssl genrsa -out harbor.key   3072
\#生成一个 3072 位的 key,也就是私钥
[root@harbor ssl]# openssl req -new -key harbor.key -out harbor.csr
\#生成一个证书请求,一会签发证书时需要的,标箭头的按提示填写,没有箭头标注的为空: 
[root@harbor ssl]#   openssl req -new -key harbor.key -out harbor.csr
You are about to be asked to enter information that will be incorporated into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank
For some fields there will be a default value, If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:guangdong Locality Name (eg, city) [Default City]: shenzhen
Organization Name (eg, company) [Default Company Ltd]:xxxx Organizational Unit Name (eg, section) []:CA
Common Name (eg, your name or your server's hostname) []:harbor Email Address []:qq@163.com

Please enter the following 'extra' attributes to be sent with your certificate request
A challenge password []:
An optional company name []:
 
签发证书:
[root@harbor ssl]# openssl x509 -req -in harbor.csr -CA ca.pem -CAkey ca.key - CAcreateserial -out harbor.pem -days 3650
![image](https://img2022.cnblogs.com/blog/1279547/202206/1279547-20220602161552214-636373560.png)

查看证书是否有效:
openssl x509 -noout -text -in harbor.pem

显示如下,说明有效:image

2.2 安装 Docker

安装 docker

[root@harbor~]# yum install docker-ce  -y
\#启动 docker 服务
[root@harbor ~]# systemctl start docker && systemctl enable docker
配置镜像加速器
[root@harbor~]# tee /etc/docker/daemon.json << 'EOF'
{
"registry-mirrors":["https://rsbud4vc.mirror.aliyuncs.com","https://registry.docker- cn.com","https://docker.mirrors.ustc.edu.cn","https://dockerhub.azk8s.cn","http://hub- mirror.c.163.com","http://qtid6917.mirror.aliyuncs.com","https://rncxm540.mirror.aliyuncs. com","https://e9yneuy4.mirror.aliyuncs.com"]
} EOF
重启 docker 服务使配置生效
[root@harbor~]# systemctl daemon-reload   && systemctl restart docker
#查看 docker 状态
[root@harbor ~]# systemctl status docker
创建安装目录
[root@harbor ssl]# mkdir /data/install -p [root@harbor ssl]# cd /data/install/
安装 harbor
/data/ssl 目录下有如下文件:
ca.key   ca.pem   ca.srl   harbor.csr   harbor.key    harbor.pem

2.3 安装配置harbor

[root@harbor install]# cd /data/install/
#把 harbor 的离线包 harbor-offline-installer-v2.3.0-rc3.tgz 上传到这个目录
下载 harbor 离线包的地址:
https://github.com/goharbor/harbor/releases/tag/
解压:
[root@harbor install]# tar zxvf harbor-offline-installer-v2.3.0-rc3.tgz
[root@harbor install]# cd harbor
[root@harbor harbor]# cp harbor.yml.tmpl harbor.yml 
[root@harbor harbor]# vim harbor.yml
修改配置文件:
hostname:   harbor
#修改 hostname,跟上面签发的证书域名保持一致
#协议用 https
certificate: /data/ssl/harbor.pem
private_key: /data/ssl/harbor.key

邮件和 ldap 不需要配置,在 harbor 的 web 界面可以配置其他配置采用默认即可
修改之后保存退出
注:harbor 默认的账号密码:admin/Harbor12345

2.4 安装 docker-compose

方法 1:离线上传 docker-compose 到服务器上
下载二进制文件上传至此目录
[root@harbor ~]# mv docker-compose-Linux-x86_64.64 /usr/local/bin/docker-compose
添加执行权限
[root@harbor ~]# chmod +x /usr/local/bin/docker-compose
注: docker-compose 项目是 Docker 官方的开源项目,负责实现对 Docker 容器集群的快速编排。Docker-Compose 的工程配置文件默认为 docker-compose.yml,Docker-Compose 运行目录下的必要有一个 docker-compose.yml。docker-compose 可以管理多个 docker 实例。

方法 2:在线安装: [root@harbor ~]# curl -L
https://github.com/docker/compose/releases/download/1.26.2/docker-compose-`uname - s`-`uname -m` > /usr/local/bin/docker-compose
添加执行权限
[root@harbor ~]# chmod +x /usr/local/bin/docker-compose

安装 harbor 需要的离线镜像包 docker-harbor-2-3-0.tar.gz,可上传到 harbor,通过 docker load -i 解压
[root@harbor install]# docker load -i docker-harbor-2-3-0.tar.gz 
[root@harbor install]# cd /data/install/harbor
[root@harbor harbor]# ./install.sh

image
在自己电脑修改 hosts 文件
在 hosts 文件添加如下一行,然后保存即可
192.168.7.51 harbor
扩展:
如何停掉 harbor:
[root@harbor harbor]# cd /data/install/harbor
[root@harbor harbor]# docker-compose stop
如何启动 harbor:
[root@harbor harbor]# cd /data/install/harbor
[root@harbor harbor]# docker-compose start
如果 docker-compose start 启动 harbor 之后,还是访问不了,那就需要重启虚拟机
浏览器访问harbor,
账号:admin
密码:Harbor12345image
所有基础镜像都会放在 library 里面,这是一个公开的镜像仓库

新建项目->起个项目名字 test(把访问级别公开那个选中,让项目才可以被公开使用)image

2.5 在 k8s-master1 上测试使用 harbor 的 harbor 镜像仓库

#修改 docker 配置

[root@k8s-master1 ~]# vim /etc/docker/daemon.json
{
"registry-mirrors":["https://vh3bm52y.mirror.aliyuncs.com","https://registry.docker-
cn.com","https://docker.mirrors.ustc.edu.cn","https://dockerhub.azk8s.cn","http://hub- mirror.c.163.com"],
"exec-opts": ["native.cgroupdriver=systemd"], "insecure-registries":["192.168.7.51","harbor"]
}
![image](https://img2022.cnblogs.com/blog/1279547/202206/1279547-20220604143814539-80687132.png)

修改配置之后使配置生效:
[root@k8s-master1 ~]# systemctl daemon-reload && systemctl restart docker
查看 docker 是否启动成功
[root@k8s-master1 ~]# systemctl status docker
显示如下,说明启动成功:
Active: active (running) since Fri … ago
注意:
配置新增加了一行内容如下:
"insecure-registries":["192.168.7.51"],
上面增加的内容表示我们内网访问 harbor 的时候走的是 http,192.168.7.51 是安装 harbor 机器的 ip

登录 harbor:

[root@k8s-master1]# docker login 192.168.7.51
Username:admin 
Password:   Harbor12345
输入账号密码之后看到如下,说明登录成功了:
Login Succeeded
#导入 busybox 镜像,busybox.tar.gz 在课件里
[root@k8s-master1 ~]# docker load -i busybox.tar.gz
#把 tomcat 镜像打标签
[root@k8s-master1 ~]# docker tag busybox:latest 192.168.7.51/test/busybox:v1
[root@k8s-master1 ~]# docker push 192.168.7.51/test/busybox:v1
执行上面命令就会把 192.168.7.51/test/tomcat:v1 上传到 harbor 里的 test 项目下

2.5.1 从 harbor 仓库上测试拉取镜像

在 k8s-master1 机器上删除镜像
[root@k8s-master1 ~]# docker rmi -f 192.168.7.51/test/busybox:v1
拉取镜像
[root@k8s-master1 ~]#docker pull 192.168.7.51/test/busybox:v1

3. 安装和配置数据存储仓库 MySQL

3.1 MySQL 简介

MySQL 是一款安全、跨平台、高效的,并与 PHP、Java 等主流编程语言紧密结合的数据库系统。该数据库系统是由瑞典的 MySQL AB 公司开发、发布并支持,由 MySQL 的初始开发人员 David
Axmark 和 Michael Monty Widenius 于 1995 年建立的。MySQL 的象征符号是一只名为 Sakila
的海豚,代表着 MySQL 数据库的速度、能力、精确和优秀本质。
MySQL logo:image

目前 MySQL 被广泛地应用在 Internet 上的中小型网站中。由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,使得很多公司都采用 MySQL 数据库以降低成本。
MySQL 数据库可以称得上是目前运行速度最快的 SQL 语言数据库之一。除了具有许多其他数据库所不具备的功能外,MySQL 数据库还是一种完全免费的产品,用户可以直接通过网络下载 MySQL 数据库,而不必支付任何费用。

3.2 MySQL 特点

1)功能强大
MySQL 中提供了多种数据库存储引擎,各引擎各有所长,适用于不同的应用场合,用户可以选择最合适的引擎以得到最高性能,可以处理每天访问量超过数亿的高强度的搜索 Web 站点。MySQL5 支持事务、视图、存储过程、触发器等。
2)支持跨平台
MySQL 支持至少 20 种以上的开发平台,包括 Linux、Windows、FreeBSD 、IBMAIX、
AIX、FreeBSD 等。这使得在任何平台下编写的程序都可以进行移植,而不需要对程序做任何的修改。
3)运行速度快
高速是 MySQL 的显著特性。在 MySQL 中,使用了极快的 B 树磁盘表(MyISAM)和索引压缩;通过使用优化的单扫描多连接,能够极快地实现连接;SQL 函数使用高度优化的类库实现,运行速度极快。
4)支持面向对象
PHP 支持混合编程方式。编程方式可分为纯粹面向对象、纯粹面向过程、面句对象与面向过程混合3 种方式。
5)安全性高
灵活和安全的权限与密码系统,允许基本主机的验证。连接到服务器时,所有的密码传输均采用加密形式,从而保证了密码的安全。
6)成本低
MySQL 数据库是一种完全免费的产品,用户可以直接通过网络下载。
7)支持各种开发语言
MySQL 为各种流行的程序设计语言提供支持,为它们提供了很多的 API 函数,包括 PHP、ASP.NET、Java、Eiffel、Python、Ruby、Tcl、C、C++、Perl 语言等。
8)数据库存储容量大
MySQL 数据库的最大有效表尺寸通常是由操作系统对文件大小的限制决定的,而不是由 MySQL内部限制决定的。InnoDB 存储引擎将 InnoDB 表保存在一个表空间内,该表空间可由数个文件创建,表空间的最大容量为 64TB,可以轻松处理拥有上千万条记录的大型数据库。
9)支持强大的内置函数
PHP 中提供了大量内置函数,几乎涵盖了 Web 应用开发中的所有功能。它内置了数据库连接、文件上传等功能,MySQL 支持大量的扩展库,如 MySQLi 等,可以为快速开发 Web 应用提供便利。

3.3 安装 MySQL

用 k8s-master1 机器复用即可:在 k8s-master1 上操作:

[root@k8s-master1 ~]# yum install mysql* mariadb* -y
启动 MySQL
[root@k8s-master1 ~]# systemctl start mariadb.service

mysql 安装成功后,默认的 root 用户密码为空,你可以使用以下命令来创建 root 用户的密码,密码设置成 111111

[root@k8s-master1 ~]# mysqladmin -u root password "111111"
登陆数据库
[root@k8s-master1 ~]# mysql -uroot -p111111
创建数据库 tb_order、tb_product、tb_stock 
mysql> create database tb_product; 
mysql> create database tb_stock;
mysql> create database tb_order;

3.4 在 Mysql 数据库导入数据

把准备好的相应的 sql 语句上传到 mysql 机器的 root 目录下,sql 文件分别是 order.sql、product.sql、 stock.sql,按如下顺序导入:

mysql> use tb_order
mysql>  source  /root/k8s/springcloud/order.sql

mysql> use tb_stock
mysql> source /root/k8s/springcloud/stock.sql

mysql> use tb_product
mysql> source /root/k8s/springcloud/product.sql

3.5 对 MySQL 数据库授权

mysql> grant all on *.* to 'root'@'10.244.%.%' identified by '111111'; 
mysql> grant all on *.* to 'root'@'192.168.%.%' identified by '111111'; 
mysql> grant all on *.* to 'root'@'%' identified by '111111';
mysql> flush privileges;
mysql> exit

3.5.1 验证数据库导入数据是否正确

[root@k8s-master1 ~]# mysql -uroot -p111111 
mysql> use tb_stock
mysql> select * from stock;
+----+---------+-------------+------------+
| id | prod_id | sales_stock | real_stock |
+----+---------+-------------+------------+
|   1 |	1 |	99 |	99 |
|   2 |	2 |	88 |	88 |
|   3 |	3 |	77 |	77 |
|   4 |	4 |	66 |	66 |
+----+---------+-------------+------------+
4 rows in set (0.01 sec)

mysql> use tb_product
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 * from product;
+----+-----------------+----------+
| id | product_name	| price	|
+----+-----------------+----------+
|   1 | 手机	|	99.990 |
|   2 | 大彩电	|   999.000 |
|   3 | 洗衣机	|   100.000 |
|   4 | 超级大冰箱	| 9999.000 |
+----+-----------------+----------+
4 rows in set (0.01 sec) 
mysql> use tb_order
Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A
mysql> select * from orders; Empty set (0.00 sec)

到此我们的基础环境都已经配置好了,后面开始搭建电商平台

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值