一、harbor安装
参考:https://github.com/vmware/harbor/blob/master/docs/installation_guide.md
Harbor 的所有组件都在 Dcoker 中部署,所以 Harbor 可使用 Docker Compose 快速部署。
环境准备
安装docker-compose
- 方法一:
安装pip(pip is already installed if you are using Python 2 >=2.7.9 or Python 3 >=3.4)
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
使用pip安装docker-compose
pip install docker-compose
- 方法二:
1.从github上下载docker-compose二进制文件安装
下载最新版的docker-compose文件
$ sudo curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-\`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
添加可执行权限
$ sudo chmod +x /usr/local/bin/docker-compose
二、安装harbor
1.下载安装安装资源
wget [http://harbor.orientsoft.cn/harbor-v1.4.0/harbor-offline-installer-v1.4.0.tgz](http://harbor.orientsoft.cn/harbor-v1.4.0/harbor-offline-installer-v1.4.0.tgz)
2.创建安装脚本
cat <EOF > install-harbor.sh
\#!/bin/bash
\#sudo su - root
mkdir -p /data/harbor/storage #创建存储数据目录
cd /data/harbor
wget http://harbor.orientsoft.cn/harbor-v1.4.0/harbor-offline-installer-v1.4.0.tgz
tar xf harbor-offline-installer-v1.4.0.tgz
mv harbor/* .
\#change harbor.cfg
cp harbor.cfg harbor.cfg.bak
sed -i -e "s#hostname\ =.*#hostname\ =\ harbor\-ali\.abc\.com#" \
-e "s#ssl_cert\ =.*#ssl_cert\ =\ /data/harbor/storage/cert/server\.crt#" \
-e "s#ssl_cert_key\ =.*#ssl_cert_key\ =\ /data/harbor/storage/cert/server\.key#" \
-e "s#secretkey_path\ =.*#secretkey_path\ =\ /data/harbor/storage#" \
-e "s#harbor_admin_password\ =.*#harbor_admin_password\ =\ harbor123" \
harbor.cfg
\#change docker-compose.yml
cp docker-compose.yml docker-compose.yml.bak
sed -i "s#\-\ \/data#\-\ \/data\/harbor\/storage#g" docker-compose.yml
./install.sh
EOF
3.执行安装脚本
./install-harbor.sh
4.打开浏览器访问harbor
输入用户名密码访问验证是否正常运行
三、harbor关联k8s
1. master尝试登陆harbor
出现错误,harbor默认需要用证书验证,生产最好要配置证书,这里也可以配置忽略证书登录
只需要在/etc/docker/daemon加上以下参数就可以了
[root@master-01 ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["http://hub-mirror.c.163.com"],
"exec-opts": ["native.cgroupdriver=systemd"],
"insecure-registries": ["http://harbor-ali.abc.com"]
}
重启docker
systemctl restart docker
再次登录就可以了
- 注意:因为harbor-ali.abc.com是测试用的域名,需要配置本机配hosts才能访问
2. 在k8s master上创建secret关联harbor
kubectl create secret docker-registry rmyegsecret --docker-server=harbor-ali.abc.com --docker-username=admin --docker-password=harbor123 --docker-email=1398569257@qq.com
验证是否创建成功
接下来创建一个pod应用去下载harbor的资源就可以了
3.下载一个测试镜像myapp
[root@master-01 k8s]# docker pull ikubernetes/myapp:v1
v1: Pulling from ikubernetes/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 ikubernetes/myapp:v1
docker.io/ikubernetes/myapp:v1
重新打标签把它push到harbor中(我这里push之前要在harbor创建k8s_img镜像目录,每个镜像都应该创建目录来区分不同用途的镜像)
可以看到镜像已经推送到harbor上了
4.接下来创建pod应用的时候就可以使用了(在imagePullSecrets引用)
[root@master-01 base]# cat myapp.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: myapp
release: canary
template:
metadata:
labels:
app: myapp
release: canary
spec:
imagePullSecrets:
- name: rmyregsecret
hostAliases:
- ip: "10.1.1.5"
hostnames:
- "harbor-ali.abc.com"
containers:
- name: myapp
image: "harbor-ali.abc.com/k8s_img/myapp:v1"
imagePullPolicy: Always
ports:
- name: http
containerPort: 80
验证是否创建成功
以后创建应用时就可以这样引用了
这里遇到个坑在本地master 用docker login可以正常登录harbor,但是在pod创建是一直提示镜像pull失败
解决方法:在node主机的hosts也要做域名harbor-ali.abc.com解析。