Just Do IT !

Kubernetes(K8s)入门到实践(四)----Kubernetes1.15.1配置私有仓库Harbor

字数统计: 1.5k阅读时长: 8 min
2020/04/21 Share

目录

Kubernetes(K8s)入门到实践(一)—-Kubernetes入门
Kubernetes(K8s)入门到实践(二)—-Kubernetes的基本概念和术语
Kubernetes(K8s)入门到实践(三)—-Kubernetes Centos7集群安装
Kubernetes(K8s)入门到实践(四)—-Kubernetes1.15.1配置私有仓库Harbor

前期准备

  • 需要三台K8s节点
  • Harbor虚拟机
  • docker-compose
  • harbor安装包

安装docker

1
2
3
4
5
6
7
yum install -y yum-utils device-mapper-persistent-data lvm2

yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

yum update -y && yum install -y docker-ce

安装完成后需要建立/etc/docker/daemon.json文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 启动docker
systemctl start docker && systemctl enable docker
## 创建 /etc/docker 目录
mkdir /etc/docker
# 配置 daemon.json
vim /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"insecure-registries": ["https://hub.test.com"]
}

mkdir -p /etc/systemd/system/docker.service.d
# 重启docker服务
systemctl daemon-reload && systemctl restart docker && systemctl enable docker

同理: K8s节点也需要一样修改/etc/docker/daemon.json文件

安装Harbor

下载docker-compose

1
curl -L https://github.com/docker/compose/releases/download/1.9.0/docker-compose-`uname -s`-`uname -m`> ./docker-compose

下载解压Harbor

Harbor 官方地址:https://github.com/vmware/harbor/releases

1
2
3
4
5
[root@localhost ~]# chmod a+x docker-compose 
[root@localhost ~]# mv docker-compose /usr/local/bin/
[root@localhost ~]# tar -zxvf harbor-offline-installer-v1.2.0.tgz
[root@localhost ~]# mv harbor /usr/local/
[root@localhost ~]# cd /usr/local/harbor/

配置harbor.cfg

修改为https协议,并且定义网址

1
2
hostname = hub.test.com
ui_url_protocol = https

以下为ssl证书配置文件目录 接下来配置HTTPS证书

1
2
3
4
5
ssl_cert = /data/cert/server.crt
ssl_cert_key = /data/cert/server.key

#The path of secretkey storage
secretkey_path = /data

创建https证书以及配置相关目录权限

创建cert目录,输入密码例如123456下面配置会用到

1
2
3
4
5
6
7
8
9
[root@localhost harbor]# mkdir -p /data/cert
[root@localhost harbor]# cd /data/cert/
[root@localhost cert]# openssl genrsa -des3 -out server.key 2048
Generating RSA private key, 2048 bit long modulus
...................................+++
................+++
e is 65537 (0x10001)
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:

生成服务器CSR证书请求文件,注意站点名称要一致

输入刚才设置的密码进行配置

Common Name (eg, your name or your server’s hostname) []:hub.test.com 一定要填上面配置的网址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@localhost cert]# openssl req -new -key server.key -out server.csr
Enter pass phrase for server.key:
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) []:Hebei
Locality Name (eg, city) [Default City]:sjz
Organization Name (eg, company) [Default Company Ltd]:test
Organizational Unit Name (eg, section) []:test
Common Name (eg, your name or your server's hostname) []:hub.test.com
Email Address []:test@qq.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

生成服务器认证证书

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost cert]# cp server.key server.key.org
[root@localhost cert]# openssl rsa -in server.key.org -out server.key
Enter pass phrase for server.key.org:
writing RSA key
[root@localhost cert]# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Signature ok
subject=/C=CN/ST=Hebei/L=sjz/O=test/OU=test/CN=hub.test.com/emailAddress=test@qq.com
Getting Private key
[root@localhost cert]# ls
server.crt server.csr server.key server.key.org
[root@localhost cert]# chmod a+x *
[root@localhost cert]# cd -
/usr/local/harbor

安装

1
2
3
4
5
6
7
8
9
10
11
[root@localhost harbor]# ./install.sh 
[root@localhost harbor]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c998c35434cd vmware/nginx-photon:1.11.13 "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:4443->4443/tcp nginx
b8651abbdc0f vmware/harbor-jobservice:v1.2.0 "/harbor/harbor_jobs…" 2 hours ago Up 2 hours harbor-jobservice
38cd42c3ad61 vmware/harbor-ui:v1.2.0 "/harbor/harbor_ui" 2 hours ago Up 2 hours harbor-ui
7117305239e4 vmware/harbor-adminserver:v1.2.0 "/harbor/harbor_admi…" 2 hours ago Up 2 hours harbor-adminserver
547244f64e7b vmware/harbor-db:v1.2.0 "docker-entrypoint.s…" 2 hours ago Up 2 hours 3306/tcp harbor-db
08ac3fe587c8 vmware/registry:2.6.2-photon "/entrypoint.sh serv…" 2 hours ago Up 2 hours 5000/tcp registry
a137bc1e2548 vmware/harbor-log:v1.2.0 "/bin/sh -c 'crond &…" 2 hours ago Up 2 hours 127.0.0.1:1514->514/tcp harbor-log
[root@localhost harbor]#

修改hosts文件映射

修改k8s节点与Harbor虚拟机/etc/hosts文件

1
2
3
4
192.168.0.50 k8s-master01
192.168.0.51 k8s-node01
192.168.0.52 k8s-node02
192.168.0.44 hub.test.com

本地hosts文件添加

1
192.168.0.44 hub.test.com

登录账号admin,密码Harbor12345
在这里插入图片描述
在这里插入图片描述

Harbor上传镜像

拉取镜像

这是是从我的docker hub中拉取的镜像plutoacharon/myapp:v1,也可以从docker hub中搜索拉取想要上传的镜像
docker pull plutoacharon/myapp:v1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost  ~]# docker pull plutoacharon/myapp:v1
v1: Pulling from plutoacharon/myapp
550fe1bea624: Pull complete
af3988949040: Pull complete
d6642feac728: Pull complete
c20f0a205eaa: Pull complete
fe78b5db7c4e: Pull complete
6565e38e67fe: Pull complete
Digest: sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513
Status: Downloaded newer image for plutoacharon/myapp:v1
docker.io/plutoacharon/myapp:v1
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
plutoacharon/myapp v1 d4a5e0eaa84f 2 years ago 15.5MB
[root@localhost ~]#

上传镜像

首先使用docker login https://hub.test.com登录才可以上传到Harbor中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost  ~]# docker login https://hub.test.com
Username: admin
Password:
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@localhost ~]# docker tag plutoacharon/myapp:v1 hub.test.com/library/myapp:v1
[root@localhost ~]# docker push hub.test.com/library/myapp:v1
The push refers to repository [hub.test.com/library/myapp]
a0d2c4392b06: Pushed
05a9e65e2d53: Pushed
68695a6cfd7d: Pushed
c1dc81a64903: Pushed
8460a579ab63: Pushed
d39d92664027: Pushed
v1: digest: sha256:9eeca44ba2d410e54fccc54cbe9c021802aa8b9836a0bcf3d3229354e4c8870e size: 1569

在这里插入图片描述

Kubernetes拉取运行Harbor镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@k8s-master01 ~]# kubectl run nginx-deployment --image=hub.test.com/library/myapp:v1 --port=80 --replicas=1
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx-deployment created
[root@k8s-master01 ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 1/1 1 1 25s
[root@k8s-master01 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-bdf84f685 1 1 1 39s
[root@k8s-master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-bdf84f685-pg7qk 1/1 Running 0 50s
[root@k8s-master01 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-bdf84f685-pg7qk 1/1 Running 0 65s 10.244.1.2 k8s-node01 <none> <none>

kubectl get pod -o wide可以看到nginx-deployment在node1上运行

1
2
3
4
[root@k8s-node01 ~]# docker ps | grep nginx
066e82c78200 hub.test.com/library/myapp "nginx -g 'daemon of…" 20 minutes ago Up 20 minutes k8s_nginx-deployment_nginx-deployment-bdf84f685-pg7qk_default_11af7460-37a5-4d61-b94c-5c64684110ed_0
3a0c5624068c k8s.gcr.io/pause:3.1 "/pause" 20 minutes ago Up 20 minutes k8s_POD_nginx-deployment-bdf84f685-pg7qk_default_11af7460-37a5-4d61-b94c-5c64684110ed_0
[root@k8s-node01 ~]#

1
2
3
4
5
[root@k8s-node01 ~]# curl 10.244.1.2
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-node01 ~]# curl 10.244.1.2/hostname.html
nginx-deployment-bdf84f685-pg7qk
[root@k8s-node01 ~]#
CATALOG
  1. 1. 目录
    1. 1.1. 前期准备
    2. 1.2. 安装docker
    3. 1.3. 安装Harbor
      1. 1.3.1. 下载docker-compose
      2. 1.3.2. 下载解压Harbor
      3. 1.3.3. 配置harbor.cfg
      4. 1.3.4. 创建https证书以及配置相关目录权限
      5. 1.3.5. 修改hosts文件映射
    4. 1.4. Harbor上传镜像
      1. 1.4.1. 拉取镜像
      2. 1.4.2. 上传镜像
    5. 1.5. Kubernetes拉取运行Harbor镜像