主机 | 操作系统 | ip地址 |
---|---|---|
k8s01(master) | Centos 7.3 | 172.16.1.30 |
k8s02(node01) | Centos 7.3 | 172.16.1.31 |
k8s03(node02) | Centos 7.3 | 172.16.1.32 |
可以选择任意一台服务器,这里选择k8s集群中的master作为harboar私有仓库。
1)安装必要的系统工具
[root@master ~]# yum -y install yum-utils device-mapper-persistent-data lvm2
2)安装docker-compose
github下载地址:https://github.com/docker/compose/releases ,可选择下载对应的版本,例如下载1.25.0版本:
[root@master ~]# curl -L https://github.com/docker/compose/releases/download/1.25.0-rc4/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
#添加可执行的权限:
[root@master ~]# chmod +x /usr/local/bin/docker-compose
#查看compose版本:
[root@master ~]# docker-compose -v
docker-compose version 1.25.0-rc4, build 8f3c9c58
3)下载harbor安装包并解压:
下载地址:https://github.com/goharbor/harbor/releases
例如下载v1.7.4版本。
#选择离线安装模式(offline):
[root@master harbor]# wget https://storage.googleapis.com/harbor-releases/release-1.7.0/harbor-offline-installer-v1.7.4.tgz
[root@master harbor]# tar xf harbor-offline-installer-v1.7.4.tgz
[root@master harbor]# cd harbor/
[root@master harbor]# ls
common docker-compose.notary.yml harbor.v1.7.4.tar.gz open_source_license
docker-compose.chartmuseum.yml docker-compose.yml install.sh prepare
docker-compose.clair.yml harbor.cfg LICENSE
4)配置harbor
#修改harbor配置文件:
[root@master harbor]# vim harbor.cfg
修改“hostname”为本机ip地址或域名,其他参数暂时保持默认即可,如果实际需求时可再做修改,配置文件详细参数如下:
# hostname设置访问地址,可以使用ip、域名,不可以设置为127.0.0.1或localhost
hostname = 172.16.1.30
# 访问协议,默认是http,也可以设置https,如果设置https,则nginx ssl需要设置on
ui_url_protocol = http
# mysql数据库root用户默认密码root123,实际使用时修改下
db_password = root123
max_job_workers = 3
customize_crt = on
ssl_cert = /data/cert/server.crt
ssl_cert_key = /data/cert/server.key
secretkey_path = /data
admiral_url = NA
# 邮件设置,发送重置密码邮件时使用
email_identity =
email_server = smtp.mydomain.com
email_server_port = 25
email_username = sample_admin@mydomain.com
email_password = abc
email_from = admin <sample_admin@mydomain.com>
email_ssl = false
# 启动Harbor后,管理员UI登录的密码,默认是Harbor12345
harbor_admin_password = Harbor12345
# 认证方式,这里支持多种认证方式,如LADP、本次存储、数据库认证。默认是db_auth,mysql数据库认证
auth_mode = db_auth
# LDAP认证时配置项
ldap_url = ldaps://ldap.mydomain.com
ldap_searchdn = uid=searchuser,ou=people,dc=mydomain,dc=com
ldap_search_pwd = password
ldap_basedn = ou=people,dc=mydomain,dc=com
ldap_filter = (objectClass=person)
ldap_uid = uid
ldap_scope = 3
ldap_timeout = 5
# 是否开启自注册
self_registration = on
# Token有效时间,默认30分钟
token_expiration = 30
# 用户创建项目权限控制,默认是everyone(所有人),也可以设置为adminonly(只能管理员)
project_creation_restriction = everyone
verify_remote_cert = on
5)安装harbor
只需要执行安装脚本即可:
[root@master harbor]# ./install.sh #注意在harbor目录下
6)登录harbor web界面,访问URL:http://172.16.1.30
#默认用户名:admin,密码:Harbor12345
//以上信息可以在harbor配置文件中查看到:
//登陆界面如下:
#新建一个项目:
#新建的项目可作为一个仓库,目前该仓库中还没有任何镜像,可任意上传镜像:
7)Harbor的启动与停止
//停止harbor:
[root@master harbor]# docker-compose stop
Stopping nginx ... done
Stopping harbor-portal ... done
Stopping harbor-jobservice ... done
Stopping harbor-core ... done
Stopping harbor-adminserver ... done
Stopping registryctl ... done
Stopping redis ... done
Stopping registry ... done
Stopping harbor-db ... done
Stopping harbor-log ... done
//启动harbor:
[root@master harbor]# docker-compose start
Starting log ... done
Starting registry ... done
Starting registryctl ... done
Starting postgresql ... done
Starting adminserver ... done
Starting core ... done
Starting portal ... done
Starting redis ... done
Starting jobservice ... done
Starting proxy ... done
//重启harbor
重启harbor一般建议先stop停止,再start开启,直接使用restart可能会有报错。
8)修改Harbor的默认登陆密码
#使用默认登陆的密码,可能会考虑到安全性。所以在Harbor的web界面中,已经为我们提供了修改密码的菜单选项,操作如下:
//修改admin的密码:
#修改docker配置文件:
[root@master harbor]# vim /usr/lib/systemd/system/docker.service
#重新加载并重启docker:
[root@master harbor]# systemctl daemon-reload
[root@master harbor]# systemctl restart docker
#重启harbor:
[root@master harbor]# docker-compose stop
[root@master harbor]# docker-compose start
2) 客户端登陆harbor:
[root@master harbor]# docker login -uadmin -p Harbor12345 172.16.1.30:80
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
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
#将需要上传到harbor仓库的镜像进行push,例如nginx镜像
#注意:上传至harbor中已存在的仓库
[root@master harbor]# docker tag nginx:latest 172.16.1.30:80/harbor/nginx:v1.0
[root@master harbor]# docker push 172.16.1.30:80/harbor/nginx:v1.0
The push refers to repository [172.16.1.30:80/harbor/nginx]
12fdf55172df: Pushed
002a63507c1c: Pushed
1c95c77433e8: Pushed
v1.0: digest: sha256:099019968725f0fc12c4b69b289a347ae74cc56da0f0ef56e8eb8e0134fc7911 size: 948
#在harbor仓库中可查看到上传的镜像
#如果需要删除镜像,可用在线进行删除:
3)其他用户共享harbor仓库
#将master上的docker配置文件拷贝至其他节点(node01,node02):
[root@master harbor]# scp /usr/lib/systemd/system/docker.service root@node01:/usr/lib/systemd/system/
docker.service 100% 1634 1.6KB/s 00:00
[root@master harbor]# scp /usr/lib/systemd/system/docker.service root@node02:/usr/lib/systemd/system/
docker.service 100% 1634 1.6KB/s 00:00
#重新加载docker:
[root@node01 ~]# systemctl daemon-reload
[root@node01 ~]# systemctl restart docker
[root@node02 ~]# systemctl daemon-reload
[root@node02 ~]# systemctl restart docker
2)登陆harbor私有仓库(以node01为例):
[root@node01 ~]# docker login -u admin -p Harbor12345 172.16.1.30:80
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
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
3)从harbor仓库中拉取镜像:
[root@node01 ~]# docker pull 172.16.1.30:80/harbor/nginx:v1.0
v1.0: Pulling from harbor/nginx
1ab2bdfe9778: Pull complete
a17e64cfe253: Pull complete
e1288088c7a8: Pull complete
Digest: sha256:099019968725f0fc12c4b69b289a347ae74cc56da0f0ef56e8eb8e0134fc7911
Status: Downloaded newer image for 172.16.1.30:80/harbor/nginx:v1.0
[root@node01 ~]# docker images | grep nginx
172.16.1.30:80/harbor/nginx v1.0 5a3221f0137b 6 months ago 126MB
4)以harbor私有仓库中的镜像,简单部署nginx服务
[root@master harbor]# cat nginx.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx
spec:
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: 172.16.1.30:80/harbor/nginx:v1.0 #下载镜像指定harbor仓库中的镜像
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
spec:
type: NodePort
selector:
app: web
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30000
//创建nginx服务,并访问nginx网页:
[root@master harbor]# kubectl create -f nginx.yaml
deployment.extensions/nginx created
service/nginx-svc created
[root@master harbor]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-7559d56464-tqc2g 1/1 Running 0 63s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 113d
service/nginx-svc NodePort 10.105.18.41 <none> 80:30000/TCP 63s
K8S在默认情况下只能拉取Harbor仓库中的公有镜像,拉取私有镜像会报错:ErrImagePull 或 ImagePullBackOff,如下图(node02上没有pull镜像到本地,所以会报错)
为了解决这个问题,我们需要创建认证登陆密钥来拉取私有镜像。
核心思路:拉取私有仓库镜像需要配置私有仓库的登陆信息,用Secret存储,并且定义Deployment或者Pod时,指定imagePullSecret为保存了私有仓库登陆信息的Secret名。
注意:需要事先在客户端登录harbor,才能够拉取harbor仓库中的私有镜像。
#有两种创建Secret的方法:
1,docker credential创建Secret
1)先在服务器上登陆Harbor仓库:
[root@master harbor]# docker login -uadmin -pHarbor12345 172.16.1.30:80
2)登陆成功后会在当前用户下生成~/.docker/config.json文件:
[root@master harbor]# cat ~/.docker/config.json
{
"auths": {
"172.16.1.30:80": {
"auth": "YWRtaW46SGFyYm9yMTIzNDU="
}
},
"HttpHeaders": {
"User-Agent": "Docker-Client/18.09.0 (linux)"
}
#将config.json文件进行base64加密:
[root@master harbor]# cat ~/.docker/config.json | base64 -w 0
ewoJImF1dGhzIjogewoJCSIxNzIuMTYuMS4zMDo4MCI6IHsKCQkJImF1dGgiOiAiWVdSdGFXNDZTR0Z5WW05eU1USXpORFU9IgoJCX0KCX0sCgkiSHR0cEhlYWRlcnMiOiB7CgkJIlVzZXItQWdlbnQiOiAiRG9ja2VyLUNsaWVudC8xOC4wOS4wIChsaW51eCkiCgl9Cn0=
3)创建secret
[root@master harbor]# vim login-secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: login
type: kubernetes.io/dockerconfigjson
data:
.dockerconfigjson: ewoJImF1dGhzIjogewoJCSIxNzIuMTYuMS4zMDo4MCI6IHsKCQkJImF1dGgiOiAiWVdSdGFXNDZTR0Z5WW05eU1USXpORFU9IgoJCX0KCX0sCgkiSHR0cEhlYWRlcnMiOiB7CgkJIlVzZXItQWdlbnQiOiAiRG9ja2VyLUNsaWVudC8xOC4wOS4wIChsaW51eCkiCgl9Cn0=
[root@master harbor]# kubectl create -f login-secret.yaml
secret/login created
[root@master harbor]# kubectl get secrets
NAME TYPE DATA AGE
default-token-wswg2 kubernetes.io/service-account-token 3 113d
login kubernetes.io/dockerconfigjson 1 36s
2,用户名密码创建Secret(一条命令解决,推荐使用)
#使用私有仓库的用户名密码,直接创建Secret:
[root@master harbor]# kubectl create secret docker-registry login-new --docker-server=172.16.1.30:80 --docker-username=admin --docker-password=Harbor12345
secret/login-new created
[root@master harbor]# kubectl get secrets
NAME TYPE DATA AGE
default-token-wswg2 kubernetes.io/service-account-token 3 113d
login kubernetes.io/dockerconfigjson 1 38m
login-new kubernetes.io/dockerconfigjson 1 11m
##部署应用,拉取私有镜像(以上边的nginx服务为例):
[root@master harbor]# cat nginx.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx
spec:
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: 172.16.1.30:80/harbor/nginx:v1.0
ports:
- containerPort: 80
imagePullSecrets: #添加imagePullSecrets字段
- name: login
---
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
spec:
type: NodePort
selector:
app: web
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30000
注:需要在创建容器时指定imagePullSecrets字段(指定刚才创建的密钥)
#重新创建nginx服务:
[root@master harbor]# kubectl delete -f nginx.yaml
[root@master harbor]# kubectl apply -f nginx.yaml
#再次查看pod,成功拉取harbor仓库中的私有镜像
##测试第二种方法创建的Secret,部署应用:
#重新创建nginx服务:
注:先将node节点上原有拉取在本地的镜像给删除掉
[root@master harbor]# kubectl apply -f nginx.yaml
deployment.extensions/nginx configured
service/nginx-svc unchanged
#查看pod,同样能够成功拉取Harbor仓库中的私有镜像:
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。