Skip to content

第5章-SpringCLoud-准备工作-安装Ingress-Mysql-harbor

本章所讲内容:

5.1 Ingress 和 Ingress Controller 概述

5.2 准备安装 harbor 需要的实验环境

5.3 使用 Harbor 搭建 Docker 私有仓库

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

注意:做 springcloud 电商项目实验环境配置要求:

k8s 控制节点 4g 内存/4vCPU K8s 工作节点 8g 内存/4vCPU Harbor 节点 2g 内存/4vCPU

5.1 Ingress 和 Ingress Controller 概述

5.1.1 Ingress Controller 代理 k8s 内部应用的流程

(1) 部署 Ingress controller,我们 ingress controller 使用的是 nginx

(2) 创建 Pod 应用,可以通过控制器创建 pod

(3) 创建 Service,用来分组 pod

(4) 创建 Ingress http,测试通过 http 访问应用

(5) 创建 Ingress https,测试通过 https 访问应用客户端通过七层调度器访问后端 pod 的方式

5.1.2 安装 Nginx Ingress Controller

Ingress-controller 官网: https://github.com/kubernetes/ingress-nginx/

#把 nginx-ingress-controller_v1.1.1.tar.gz 和 kube-webhook-certgen_1.1.1.tar.gz

镜像上传到 工作节点,手动解压镜像:

[root@k8s-node01 ~]# docker load -i nginx-ingress-controller_v1.1.1.tar.gz
[root@k8s-node01 ~]# docker load -i kube-webhook-certgen_1.1.1.tar.gz

[root@k8s-node02 ~]# docker load -i nginx-ingress-controller_v1.1.1.tar.gz
[root@k8s-node02 ~]# docker load -i kube-webhook-certgen_1.1.1.tar.gz

如果 k8s 用的是 containerd 容器运行时,用如下命令解压:

[root@k8s-node01 ~]# ctr -n=k8s.io images import kube-webhook-certgen_1.1.1.tar.gz 
[root@k8s-node01 ~]# ctr -n=k8s.io images import nginx-ingress-controller_v1.1.1.tar.gz

[root@k8s-node02 ~]# ctr -n=k8s.io images import kube-webhook-certgen_1.1.1.tar.gz 
[root@k8s-node02 ~]# ctr -n=k8s.io images import nginx-ingress-controller_v1.1.1.tar.gz

Ingress-controller 官网: https://github.com/kubernetes/ingress-nginx/

#更新 yaml 文件,下面需要的 yaml 文件在课件,可上传到 xuegod63 机器上: 安装 Ingress conrtroller 需要的 yaml 所在的 github 地址: https://github.com/kubernetes/ingress-nginx/blob/main/deploy/static/provider/baremetal/deploy.yaml

[root@k8s-master01 springcloud]# kubectl apply -f deploy.yaml
[root@k8s-master01 springcloud]# kubectl create clusterrolebinding clusterrolebinding-user-3 --clusterrole=cluster-admin --user=system:serviceaccount:ingress-nginx:ingress-nginx


[root@k8s-master01 springcloud]# kubectl get pods -n ingress-nginx
NAME                                        READY   STATUS      RESTARTS   AGE
ingress-nginx-admission-create-zfl8f        0/1     Completed   0          30s
ingress-nginx-admission-patch-btjhz         0/1     Completed   0          30s
ingress-nginx-controller-56dc9c69b9-6p6kt   1/1     Running     0          30s

5.1.3 测试 Ingress HTTP 代理 tomcat

#把 tomcat-8-5.tar.gz 上传到 工作节点机器,手动解压:

[root@k8s-node01 ~]# docker load -i tomcat-8-5.tar.gz
[root@k8s-node02 ~]# docker load -i tomcat-8-5.tar.gz

1、部署后端 tomcat 服务

[root@k8s-master01 springcloud]# cat ingress-demo.yaml
apiVersion: v1
kind: Service
metadata:
  name: tomcat
  namespace: default
spec:
  selector:
    app: tomcat
    release: canary
  ports:
  - name: http
    targetPort: 8080
    port: 8080
  - name: ajp
    targetPort: 8009
    port: 8009
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-deploy
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: tomcat
      release: canary
  template:
    metadata:
      labels:
        app: tomcat
        release: canary
    spec:
      containers:
      - name: tomcat
        image: tomcat:8.5.34-jre8-alpine 
        imagePullPolicy: IfNotPresent  
        ports:
        - name: http
          containerPort: 8080
          name: ajp
          containerPort: 8009

更新 yaml 文件:

[root@k8s-master01 springcloud]# kubectl apply -f ingress-demo.yaml

查看 pod 是否部署成功:

[root@k8s-master01 springcloud]# kubectl get pod 
NAME                            READY   STATUS    RESTARTS   AGE
tomcat-deploy-b5f55d4c8-p2pmg   1/1     Running   0          13s
tomcat-deploy-b5f55d4c8-zr82q   1/1     Running   0          13s

显示如上,说明 pod 创建成功:

2、部署 ingress

(1)编写 ingress 的配置清单

如果 k8s 版本是 1.24 及之后的版本,需要先执行如下命令:

[root@k8s-master01 springcloud]# kubectl delete -A ValidatingWebhookConfiguration ingress-nginx-admission
[root@k8s-master01 springcloud]# kubectl get ingressclass
NAME    CONTROLLER             PARAMETERS   AGE
nginx   k8s.io/ingress-nginx   <none>       2m36s

查看 yaml 文件:

[root@k8s-master01 springcloud]# cat ingress-myapp.yaml 
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-myapp
  namespace: default
spec:
  ingressClassName: nginx
  rules:
  - host: tomcat.lucky.com
    http:
      paths:
      - path: /
        pathType:  Prefix
        backend:
         service:
           name: tomcat
           port:
            number: 8080

备注:ingressClassName: nginx 就是通过 kubectl get ingressclass 命令获取到的名字

更新 yaml 文件:

[root@k8s-master01 springcloud]# kubectl apply -f ingress-myapp.yaml

查看 ingress-myapp 的详细信息

[root@k8s-master01 springcloud]# kubectl describe ingress ingress-myapp
Name:             ingress-myapp
Labels:           <none>
Namespace:        default
Address:          
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
  Host              Path  Backends
  ----              ----  --------
  tomcat.lucky.com  
                    /   tomcat:8080 (10.244.58.209:8080,10.244.85.226:8080)
Annotations:        <none>
Events:
  Type    Reason  Age   From                      Message
  ----    ------  ----  ----                      -------
  Normal  Sync    51s   nginx-ingress-controller  Scheduled for sync

修改电脑本地的 host 文件,增加如下一行,下面的 ip 是 k8s 的 工作节点 ip

192.168.20.204 tomcat.lucky.com

浏览器访问 tomcat.lucky.com,出现如下:

img

把上面资源清单文件删除,防止干扰后面的实验

[root@k8s-master01 springcloud]# kubectl delete -f ingress-demo.yaml 
[root@k8s-master01 springcloud]# kubectl delete -f ingress-myapp.yaml 

5.2 准备安装harbor 需要的实验环境

新创建一台虚拟机安装 harbor,配置如下:

主机名 ip 配置

harbor 192.168.1.62 4vCPU/2G 内存/60G 硬盘注: 新机器的初始化只需要按照上面课程步骤进行初始化即可

5.2.1 harbor 简介

harbor 是私有镜像仓库,用来存储和分发镜像的

docker 还有一个官方的镜像仓库 docker hub,免费用户只能简单的使用,创建一个私有镜像仓库,存储镜像,付费用户才可以拥有更多权限,默认 docker pull 拉取镜像就是从 docker hub 上拉取,速度极慢,不利于生产环境使用。

harbor 私有镜像仓库拉取镜像速度极快,属于内网传输,功能也很强大。

互动:有什么功能?

1. 基于角色访问控制:用户与 Docker 镜像仓库通过项目进行组织管理,一个用户可以对多个镜像仓库在同一命名空间(project)里有不同的权限。

2. 镜像复制:镜像可以在多个 Registry 实例中复制(同步)。尤其适合于负载均衡,高可用,混合云和多云的场景。

3. 图形化界面:用户可以通过浏览器来浏览,检索当前 Docker 镜像仓库,管理项目和命名空间。

4. 部署简单:提供在线和离线两种安装工具

5. LDAP:Harbor 可以集成企业内部已有的 AD/LDAP,用于鉴权认证管理

5.2.2 配置静态 IP

把虚拟机或者物理机配置成静态 ip 地址,这样机器重新启动后 ip 地址也不会发生改变。在 harbor 节点配置网络

修改/etc/sysconfig/network-scripts/ifcfg-ens33 文件,变成如下:

TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no

BOOTPROTO=static IPADDR=192.168.1.62 NETMASK=255.255.255.0 GATEWAY=192.168.42.2 DNS1=192.168.42.2 DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no

IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33

DEVICE=ens33 ONBOOT=yes

修改配置文件之后需要重启网络服务才能使配置生效,重启网络服务命令如下:

[root@harbor ~]# systemctl restart network

修改主机名:

在 192.168.1.62 上:

[root@localhost ~]# hostnamectl set-hostname harbor && bash

关闭 selinux,设置永久关闭,这样重启机器 selinux 也处于关闭状态可用下面方式修改:

[root@harbor ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/'

/etc/sysconfig/selinux

[root@harbor ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g'

/etc/selinux/config

上面文件修改之后,需要重启虚拟机,如果测试环境可以用如下命令强制重启:

[root@harbor ~]# reboot -f

注:生产环境不要 reboot -f,要正常关机重启查看 selinux 是否修改成功

重启之后登录到机器上用如下命令:

[root@harbor ~]# getenforce

显示 Disabled 说明 selinux 已经处于关闭状态

5.2.3 修改 yum 源

选择一种安装 docker 的方式:1. 在线安装 或 2. 离线安装

\1. 在线安装

配置 docker-ce 国内 yum 源(阿里云)

[root@harbor ~]# yum install yum-utils -y

[root@harbor ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker- ce/linux/centos/docker-ce.repo

安装软件包

[root@harbor ~]# 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

5.2.3 配置防火墙

关闭 firewalld 防火墙, centos7 系统默认使用的是 firewalld 防火墙,停止 firewalld 防火墙, 并禁用这个服务。

[root@harbor ~]# systemctl stop firewalld && systemctl disable firewalld

5.2.4 时间同步

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

* */1 * * * /usr/sbin/ntpdate cn.pool.ntp.org 2)重启 crond 服务:

[root@harbor ~]# systemctl restart crond

5.2.5 关闭 selinux

关闭 selinux,设置永久关闭,这样重启机器 selinux 也处于关闭状态可用下面方式修改:

[root@harbor ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/'

/etc/sysconfig/selinux

[root@harbor ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g'

/etc/selinux/config

上面文件修改之后,需要重启虚拟机,如果测试环境可以用如下命令强制重启:

[root@harbor ~]# reboot -f

注:生产环境不要 reboot -f,要正常关机重启查看 selinux 是否修改成功

重启之后登录到机器上用如下命令:

[root@harbor ~]# getenforce

显示 Disabled 说明 selinux 已经处于关闭状态

5.2.6 修改内核参数

[root@harbor ~]# modprobe br_netfilter

[root@harbor ~]# 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@harbor ~]# sysctl --system

注:sysctl --system 这个会加载所有的 sysctl 配置

5.2.7 配置 hosts 文件

xuegod63、xuegod64、harbor 主机的 hosts 文件保持一致,可按如下方法修改: 在/etc/hosts 文件增加如下几行:

192.168.1.63 xuegod63

192.168.1.64 xuegod64

192.168.1.62 harbor

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

配置 xuegod63 到 harbor 无密码登陆在 xuegod63 上操作

[root@xuegod63 ~]# cd /root && ssh-copy-id -i .ssh/id_rsa.pub root@harbor #上面需要输入 yes 之后,输入密码,输入 harbor 物理机密码即可

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

Harbor 介绍

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

img官网地址:https://github.com/goharbor/harbor

harbor ['hɑ:bə] 海湾实验环境:

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

5.3.1 Harbor 自签发证书

[root@harbor ~]# mkdir /data/ssl -p [root@harbor ~]# 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]:xuegod 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]:xuegod Organizational Unit Name (eg, section) []:CA

Common Name (eg, your name or your server's hostname) []:harbor Email Address []:mk@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

查看证书是否有效:

openssl x509 -noout -text -in harbor.pem

显示如下,说明有效:

Certificate:

Data:

Version: 1 (0x0) Serial Number:

cd:21:3c:44:64:17:65:40

Signature Algorithm: sha256WithRSAEncryption

Issuer: C=CH, ST=BJ, L=BJ, O=Default Company Ltd Validity

Not Before: Dec 26 09:29:19 2020 GMT

Not After : Dec 24 09:29:19 2030 GMT

Subject: C=CN, ST=BJ, L=BJ, O=xuegod Ltd, CN=harbor Subject Public Key Info:

Public Key Algorithm: rsaEncryption Public-Key: (3072 bit)

Modulus:

00:b0:60:c3:e6:35:70:11:c8:73:83:38:9a:7e:b8:

。。。

5.3.2 安装 Harbor

安装 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"],

"insecure-registries":["192.168.40.62","harbor"]

} 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

[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

安装 docker-compose

下载二进制文件上传至 linux(课程资料已提供 docker-compose 二进制文件可直接上传)

[root@harbor ~]# rz

img

[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 实例。

安装 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

img

在自己电脑修改 hosts 文件

在 hosts 文件添加如下一行,然后保存即可

192.168.1.62 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 之后,还是访问不了,那就需要重启虚拟机

5.3.3 Harbor 图像化界面使用说明配置自己电脑的 hosts 文件:

C:\Windows\System32\drivers\etc

打开 hosts 文件,修改如下:

img

在浏览器输入:

img

https://harbor

接收风险并继续,出现如下界面,说明访问正常

img

账号:admin

密码:Harbor12345

img

输入账号密码出现如下:

所有基础镜像都会放在 library 里面,这是一个公开的镜像仓库

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

img

5.3.4 xuegod63 上测试使用 harbor 的 harbor 镜像仓库

#修改 docker 配置

[root@xuegod63 ~]# 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.1.62","harbor"]

}

[root@xuegod64~]# 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.1.62","harbor"]

}

修改配置之后使配置生效:

[root@xuegod63 ~]# systemctl daemon-reload && systemctl restart docker [root@xuegod64 ~]# systemctl daemon-reload && systemctl restart docker

#查看 docker 是否启动成功

[root@xuegod63 ~]# systemctl status docker #显示如下,说明启动成功:

Active: active (running) since Fri … ago

注意:

配置新增加了一行内容如下:

"insecure-registries":["192.168.1.62"],

上面增加的内容表示我们内网访问 harbor 的时候走的是 http,192.168.1.62 是安装 harbor 机器的 ip

登录 harbor:

[root@xuegod63]# docker login 192.168.1.62

Username:admin Password: Harbor12345

输入账号密码之后看到如下,说明登录成功了:

Login Succeeded

登录 harbor:

[root@xuegod64]# docker login 192.168.1.62

Username:admin Password: Harbor12345

输入账号密码之后看到如下,说明登录成功了:

Login Succeeded

#导入 busybox 镜像,busybox.tar.gz 在课件里

[root@xuegod63 ~]# docker load -i busybox.tar.gz

#把 tomcat 镜像打标签

[root@xuegod63 ~]# docker tag busybox:latest 192.168.1.62/test/busybox:v1 执行上面命令就会把 192.168.1.62/test/busybox:v1 上传到 harbor 里的 test 项目下[root@xuegod63 ~]# docker push 192.168.1.62/test/busybox:v1

img

执行上面命令就会把 192.168.1.62/test/tomcat:v1 上传到 harbor 里的 test 项目下

5.3.5 harbor 仓库下载镜像在 xuegod63 机器上删除镜像

[root@xuegod63 ~]# docker rmi -f 192.168.1.62/test/busybox:v1

拉取镜像

[root@xuegod63 ~]#docker pull 192.168.1.62/test/busybox:v1

[root@xuegod64 ~]# docker pull 192.168.1.62/test/busybox:v1

5.3.6 配置 k8s 控制节点和工作节点登录 harbor

1、配置 k8s 控制节点、工作节点、harbor 机器 hosts 文件,三台机器保持一致:

[root@xuegod63 ~]# cat /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.1.63 xuegod63

192.168.1.64 xuegod64

192.168.1.62 harbor

[root@xuegod64 ~]# cat /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.1.63 xuegod63

192.168.1.64 xuegod64

192.168.1.62 harbor

[root@harbor ~]# cat /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.1.63 xuegod63

192.168.1.64 xuegod64

192.168.1.62 harbor

2、修改 k8s 控制节点和工作节点的 docker 配置文件,基于 http 方式 docker login 登录

harbor,修改 daemon.json 文件:

[root@xuegod63 ~]# 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.1.62","harbor"]

}

[root@xuegod63 ~]# systemctl restart docker [root@xuegod64 ~]# 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.1.62","harbor"]

}

[root@xuegod64 ~]# systemctl restart docker

3、配置 k8s 控制节点和工作节点登录 harbor [root@xuegod63 ~]# docker login 192.168.1.62 Username: admin

Password: Harbor12345

WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

[root@xuegod64 ~]# docker login 192.168.1.62 Username: admin

Password: Harbor12345

WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

4、测试下 k8s 控制节点和工作节点能否从 harbor 拉取镜像

[root@xuegod63 ~]# docker tag calico/node:v3.18.0 192.168.1.62/library/calico:v3.18.0

[root@xuegod63 ~]# docker push 192.168.1.62/library/calico:v3.18.0

登录 harbor 验证下,镜像是否传到 harbor 镜像仓库了

img

5、测试 k8s 工作节点 xuegod64 能否从 harbor 上拉取镜像

[root@xuegod64 ~]# docker pull 192.168.1.62/library/calico:v3.18.0 [root@xuegod64 ~]# docker images |grep 192.168.1.62

192.168.1.62/library/calico v3.18.0

5a7c4970fbc2 2 years ago 172MB

docker images 可以看到 192.168.1.62/library/calico 这个镜像已经拉取到 xuegod64 上了

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

5.4.1 MySQL 简介

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

MySQL logo:

img

目前 MySQL 被广泛地应用在 Internet 上的中小型网站中。由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,使得很多公司都采用 MySQL 数据库以降低成本。

MySQL 数据库可以称得上是目前运行速度最快的 SQL 语言数据库之一。除了具有许多其他数据库所不具备的功能外,MySQL 数据库还是一种完全免费的产品,用户可以直接通过网络下载 MySQL 数据库,而不必支付任何费用。

5.4.2 安装 MySQL

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

[root@k8s-master01 springcloud]# yum install mysql* -y
[root@k8s-master01 springcloud]# yum install mariadb* -y

启动 MySQL

[root@k8s-master01 springcloud]# systemctl enable mariadb --now

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

[root@k8s-master01 springcloud]# mysqladmin -u root password "111111"

登陆数据库

[root@k8s-master01 springcloud]# mysql -uroot -p111111

创建数据库 tb_order、tb_product、tb_stock

mysql> create database tb_product; 
mysql> create database tb_stock;
mysql> create database tb_order;

5.4.3 Mysql 数据库导入数据

把相应的 sql 语句上传到 mysql 机器的 root 目录下,sql 文件分别是 order.sql、product.sql、stock.sql,这些 sql 文件在课件里大家可以搜索,按如下方法导入:

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

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

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

5.4.4 MySQL 数据库授权

MariaDB [(none)]> grant all on *.* to 'root'@'%' identified by '111111';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> exit

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

[root@k8s-master01 springcloud]# mysql -uroot -p111111
MariaDB [tb_product]> select * from tb_product.product;
+----+-----------------+----------+
| id | product_name    | price    |
+----+-----------------+----------+
|  1 | 手机            |   99.990 |
|  2 | 大彩电          |  999.000 |
|  3 | 洗衣机          |  100.000 |
|  4 | 超级大冰箱      | 9999.000 |
+----+-----------------+----------+
4 rows in set (0.00 sec)

MariaDB [tb_product]> select * from tb_stock.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.00 sec)

MariaDB [tb_product]> select * from tb_order.orders;
0 rows in set (0.00 sec)

总结:

5.1 Ingress 和 Ingress Controller 概述

5.2 准备安装 harbor 需要的实验环境

5.3 使用 Harbor 搭建 Docker 私有仓库

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