kubernetes实战篇之nexus oss服务器部署及基于nexus的docker镜像仓库搭建

系列目录html

Nexus oss仓库管理平台搭建

Nexus是一款仓库管理工具,支持Npm,bower,maven,nuget,apt,yum甚至docker,helm等各类仓库,说的通俗如下,就是私服镜像仓库.而且有免费版,OSS版即为免费版,下载的时候请注意版本的选择.java

下载地址nginx

往下面滚动页面,能够看到Choose your Nexus,有osx,windows和unix版本可供选择.这里我选择的是windows版本.web

下载的内容是一个压缩包,下载完成后把压缩包解压.解压后有两个文件夹,咱们进入nexus-3.x.x文件夹下的bin目录,咱们在当前目录下打开cmd或者从任意位置打开cmd,而后cd到上述目录.执行nexus.exe /run来安装nexusdocker

注册服务

安装完成之后,执行nexus.exe /install Nexus Service来注册服务shell

启动服务

执行命令nexus.exe /start Nexus Service来启动服务json

经过以上操做,其实是往windows服务里面添加了一个名字叫做nexus service的服务,咱们能够经过powerhsell命令Get-Service "nexus service"来找到这个服务,经过Restart-Service "nexus service"来重启它.ubuntu

以上执行完成之后,就能够经过打开浏览器,输入localhost:8081来访问它,也能够经过其它局域网主机使用IP:8081方式来访问它.windows

img

经过首页,咱们能够看到Nexus罗列出来的支持的仓库类型,很是丰富.centos

注意,你们可能看到我访问的端口号并非8081而是8443,这是由于我设置的http自动跳转到https,而且指定的https端口为8443,这里之因此要启用https是由于下面即将讲到的docker镜像上传须要使用https.这部份内容将在下一节讲解.

登录

注意,若是没有登录,Nexus并非以显著的方式提示你登录,可是非登录用户功能受限.要想实现一些管理功能,必须登录.登录须要点击右上角sign in按钮.默认登录用户名为admin密码为admin123.

Nexus搭建docker镜像仓库

上一节咱们讲了如何在windows下安装nexus,本节咱们讲解如何来搭建nexus来搭建docker镜像仓库.

咱们进入web管理界面,点击齿轮图标,而后点击Repositories.以下图示

img

注意,以上操做须要登录.登录方式为点击右上角sign in,而后用默认admin,admin123登录.详细请见上一节内容

而后点击create repository按钮.以下图操做

img

页面出来了很是多的仓库类型供选择,因为咱们要建立的是docker仓库,这里选择的是docker hosted

img

点击后出现以下界面

img

配置上面填写的内容,填写完成后咱们点击左下角create repository来建立仓库

nexus服务器支持https配置

因为咱们使用的是https方式通信,所以咱们须要对nexus服务器进行配置,以支持https.因为咱们的证书是本身生成的,所以docker客户端也需添加对本身生成key的信任.

服务端开启监听ssl协议端口

进到nexus-xxx-xxx目录下(前面一节提到的从官网下载的nexus压缩包解压后的一个目录),进到ect目录下,里面有一个nexus-default.properties文件,用文本编辑器打开它

# Jetty section注释下面添加一行,内容为application-port-ssl=8443

nexus-args一行尾部添加,${jetty.etc}/jetty-http-redirect-to-https.xml,以指示服务器自动将http跳转到https

生成证书秘钥

网上有不少生成密码证书的博客文章,使用的工具也不尽相同.这里,咱们使用java jkd自带的keytool.exe来生成ssl证书.

进入到nexus-xxx-xxx目录下的ect/ssl目录下,执行如下命令:

keytool -genkeypair -keystore keystore.jks -storepass nexus3 -keypass nexus3 -alias jetty -keyalg RSA -keysize 2048 -validity 5000 -dname "CN=${NEXUS_DOMAIN}, OU=Example, O=Sonatype, L=Unspecified, ST=Unspecified, C=US" -ext "SAN=DNS:${NEXUS_DOMAIN},IP:${NEXUS_IP_ADDRESS}" -ext "BC=ca:true"

其中${NEXUS_IP_ADDRESS}替换为你的实际ip地址,${NEXUS_DOMAIN}若是没有域名能够随便填写

须要注意的是,若是因为环境变量配置问题致使keytool命令找不到,则须要显式指定keytool.exe完整路径,而后后面带上要执行的命令.

修改jetty-https.xml

进入到nexus-xxx-xxx目录下的etc/jetty目录下,用文本编辑器打开jetty-https.xml文件

把里面对应的内容修改成以下配置:

<Set name="KeyStorePath"><Property name="karaf.etc"/>/ssl/keystore.jks</Set>
    <Set name="KeyStorePassword">nexus3</Set>
    <Set name="KeyManagerPassword">nexus3</Set>
    <Set name="TrustStorePath"><Property name="karaf.etc"/>/ssl/keystore.jks</Set>
    <Set name="TrustStorePassword">nexus3</Set>

注意以上内容全是修改,而不是新增,修改的时候找到相同key的修改便可.

重启nexus服务器

能够经过上一节介绍的powershell命令Restart-Service "nexus service来重启服务,以使配置生效.

须要注意的是,执行以上命令须要以管理员身份启动powershell.除了使用命令之外,也能够进入到服务管理里面找到名为nexus service的服务,而后手动重启它.

执行完以上配置之后,咱们再打开浏览器访问https://..localhost:8443或者http://localhost:8081

注意,重启服务之后,若是以上内容不能立刻响应,能够等一会时间再观察看看.

配置客户端信任ca证书

个人docker服务器安装在192.168.122.65上面.下面介绍centos和ubuntu下如何添加证书信任

centos下添加证书信任

#生成cert文件
[root@localhost ~]# keytool -printcert -sslserver 192.168.124.43:8443 -rfc >nexus.crt
[root@localhost ~]# yum install ca-certificates
[root@localhost ~]# update-ca-trust force-enable
# 还能够放在/etc/docker/certs.d/192.168.124.43:8443目录下
[root@localhost ~]# mv nexus.crt /etc/pki/ca-trust/source/anchors/nexus.crt
[root@localhost ~]# update-ca-trust
[root@localhost ~]# service docker restart
[root@localhost ~]# docker login -u admin -p admin123 192.168.124.43:8002
Login Succeeded

ubuntu下添加信任证书

对于Ubuntu系统来讲certificate的存放路径是 /usr/local/share/ca-certificates

### 生成cert文件
[root@localhost ~]# keytool -printcert -sslserver 192.168.124.43:8443 -rfc >nexus.crt
# 还能够放在/etc/docker/certs.d/192.168.124.43:8443目录下
[root@localhost ~]# mv nexus.crt /usr/local/share/ca-certificates/nexus.crt
[root@localhost ~]# update-ca-certificates
[root@localhost ~]# service docker restart
[root@localhost ~]# docker login -u admin -p admin123 192.168.124.43:8002

在redhat系统中,若是报Unkonw authority错误,修改/etc/pki/tls/openssl.cnf。在其中的[ v3_ca]部分,添加subjectAltName选项

[ v3_ca ]  
subjectAltName = IP:192.168.124.43

而后再执行如下命令

[root@localhost ~]# service docker restart
[root@localhost ~]# docker login -u admin -p admin123 192.168.124.43:8002
Login Succeeded

注,网上有很多介绍使用http访问的方法,可是却没有执行成功,你们能够尝试一下

[root@localhost centos]# vi /etc/docker/daemon.json
{
  "insecure-registries": [
    "192.168.124.43:8003"
  ],
  "disable-legacy-registry": true

上传镜像到docker仓库

  • 为镜像打标签
docker tag 镜像名:tag名 192.168.124.43:8002/镜像名:tag名

我里,我没有本身建立镜像,而是随便找了一个已以存在的镜像,而后打上标签,以下

192.168.124.43:8002/rancher/nginx-ingress-controller-defaultbackend

经过执行docker push命令把它推到远程私服仓库

[root@localhost ~]# docker push 192.168.124.43:8002/rancher/nginx-ingress-controller-defaultbackend
The push refers to a repository [192.168.124.43:8002/rancher/nginx-ingress-controller-defaultbackend]
d62604d5d244: Pushed
1.4: digest: sha256:f63ced70bc85ca753e715c93b0adc3115510a6575129102383236c30112379e5 size: 528
[root@localhost ~]#

注意,推送前是须要先登录的,登录的方法上面也介绍过了

咱们登录nexus web管理界面,点击左侧Broswe,能够看到已经有内容push进来了

img

咱们执行docker search命令

[root@localhost ~]# docker search 192.168.124.43:8002/rancher/nginx-ingress-controller-defaultbackend
NAME                                                                      DESCRIPTION   STARS     OFFICIAL   AUTOMATED
192.168.124.43:8002/rancher/nginx-ingress-controller-defaultbackend:1.4                 0
[root@localhost ~]#

能够看到可以从远程服务器上搜索到它.

拉取上传的镜像

实际环境中,生成docker镜像和服务器和使用docker镜像的服务器每每不是同一台服务器.这里咱们使用另外一个服务器192.168.124.59来拉取刚刚上传的镜像

须要注意的是,这里基于的前提是用于拉取镜像的服务器已经安装了docker,docker的安装能够参考其它章节或者网络上的教程

  • 执行keytool -printcert -sslserver 192.168.59.1:8443 -rfc >nexus.crt

  • 执行yum install ca-certificates来安装

-紧接着执行update-ca-trust force-enable

  • 执行mv nexus.crt /etc/pki/ca-trust/source/anchors/nexus.crt

  • 执行update-ca-trust
  • service docker restart
  • 执行login docker login -u admin -p admin123 192.168.124.43:8002

[root@k8s-master ~]# docker login -u admin -p admin123 192.168.124.43:8002
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

而后咱们执行docker pull

[root@k8s-master ~]# docker pull 192.168.124.43:8002/rancher/nginx-ingress-controller-defaultbackend:1.4
1.4: Pulling from rancher/nginx-ingress-controller-defaultbackend
5990baa43a5e: Pull complete
Digest: sha256:f63ced70bc85ca753e715c93b0adc3115510a6575129102383236c30112379e5
Status: Downloaded newer image for 192.168.124.43:8002/rancher/nginx-ingress-controller-defaultbackend:1.4
[root@k8s-master ~]#

注意pull的时候要带上版本的tag

以上,咱们从一台服务器(192.168.122.65)推送镜像到私服,而后用另外一台服务器拉取(192.168.124.59).须要注意的是,因为服务端使用了自已生成的https证书,所以docker镜像生产端和消费端都要添加ca证书信任

参考资料1

参考资料2

相关文章
相关标签/搜索