云原生学习

学习k8s的实验资料


docker私有镜像仓库harbor

<p>Docker容器应用的开发和运行离不开可靠的镜像管理,虽然Docker官方也提供了公共的镜像仓库,但是从安全和效率等方面考虑,部署我们私有环境内的Registry也是非常必要的。Harbor是由VMware公司开源的企业级的Docker Registry管理项目,它包括权限管理(RBAC)、LDAP、日志审核、管理界面、自我注册、镜像复制和中文支持等功能。 官网地址:<a href="https://github.com/goharbor/harbor">https://github.com/goharbor/harbor</a></p> <h1>搭建Harbor</h1> <p>搭建Harbor的机器至少需要2G内存</p> <h2>为Harbor自签发证书</h2> <pre><code class="language-shell">[root@localhost ~]# hostnamectl set-hostname harbor &amp;amp;&amp;amp; bash [root@harbor ~]# mkdir /opt/harbor/ssl -p [root@harbor ~]# cd /opt/harbor/ssl #生成ca证书: #生成一个3072位的key,也就是私钥 [root@harbor ssl]# openssl genrsa -out ca.key 3072 #生成一个数字证书ca.pem,3650表示证书的有效时间是3年,按箭头提示填写即可,没有箭头标注的为空 [root@harbor ssl]# openssl req -new -x509 -days 3650 -key ca.key -out ca.pem</code></pre> <p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=55c4d4aed5edc099f68b75a57b526d89&amp;amp;file=file.png" alt="" /></p> <pre><code class="language-shell">#生成域名的证书: #生成一个3072位的key,也就是私钥 [root@harbor ssl]# openssl genrsa -out harbor.key  3072 #生成一个证书请求,一会签发证书时需要的,标箭头的按提示填写,没有箭头标注的为空: [root@harbor ssl]# openssl req -new -key harbor.key -out harbor.csr</code></pre> <p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=7f5dc8f1002a746d14c5a7b1ef5c69f8&amp;amp;file=file.png" alt="" /> 签发证书: <code>openssl x509 -req -in harbor.csr -CA ca.pem -CAkey ca.key -CAcreateserial -out harbor.pem -days 3650</code> 显示如下,说明证书签发好了: <img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=71da7c79428c8e64ffc3f754d653c09b&amp;amp;file=file.png" alt="" /></p> <h2>安装Harbor</h2> <h3>安装docker</h3> <h4>系统环境配置</h4> <pre><code class="language-shell">#关闭防火墙 [root@ harbor ~]# systemctl stop firewalld &amp;amp;&amp;amp; systemctl disable firewalld #关闭iptables防火墙 [root@ harbor ~]# yum install iptables-services -y  #安装iptables #禁用iptables [root@ harbor ~]# service iptables stop   &amp;amp;&amp;amp; systemctl disable iptables #清空防火墙规则 [root@ harbor ~]# iptables -F  #关闭selinux [root@ harbor ~]# setenforce 0 [root@harbor ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config #注意:修改selinux配置文件之后,重启机器,selinux才能永久生效 #配置时间同步 [root@harbor ~]# yum install -y ntp ntpdate [root@xianchaomaster1 ~]# ntpdate cn.pool.ntp.org  #编写定时同步时间计划任务 [root@harbor ~]# crontab -e  * */1 * * * /usr/sbin/ntpdate   cn.pool.ntp.org #重启crond服务使配置生效: [root@harbor ~]# systemctl restart crond #配置hosts文件 [root@harbor ~]# cat /etc/hosts 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6 10.5.146.44 harbor #更换yum源 #备份旧yum源 [root@harbor ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup #下载新yum源 [root@harbor ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo #更新yum缓存 [root@harbor ~]# yum clean all [root@harbor ~]# yum makecache #**注意:** #更新yum缓存的时候可能无法获取对应版本的source,可用 sed -i 's/$releasever/7/g' Centos-7.repo 直接替换可用版本的source #安装基础软件包 [root@ harbor ~]# yum install -y  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</code></pre> <h4>安装docker-ce</h4> <pre><code class="language-shell">#配置docker-ce国内yum源(阿里云) [root@ harbor ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #安装docker依赖包 [root@ harbor ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 #安装docker-ce [root@ harbor ~]# yum install docker-ce -y #启动docker服务 [root@ harbor ~]# systemctl start docker &amp;amp;&amp;amp; systemctl enable docker [root@ harbor ~]# systemctl status docker ● docker.service - Docker Application Container Engine    Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)    Active: active (running) since Thu 2021-07-01 21:29:18 CST; 30s ago      Docs: https://docs.docker.com #查看Docker 版本信息 [root@ harbor ~]# docker version    </code></pre> <h3>开启包转发功能和修改内核参数</h3> <p>内核参数修改:br_netfilter模块用于将桥接流量转发至iptables链,br_netfilter内核参数需要开启转发。 &gt; ```shell [root@ harbor~]# modprobe br_netfilter [root@ harbor~]# cat &gt; /etc/sysctl.d/docker.conf &lt;&lt;EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 EOF [root@harbor ~]# sysctl -p /etc/sysctl.d/docker.conf</p> <pre><code>若执行 modprobe br_netfilter 报错:modprobe: FATAL: Module br_netfilter not found,解决方案如下。 安装br_netfilter该模块: yum install bridge-utils -y echo br_netfilter &amp;gt; /etc/modules-load.d/br_netfilter.conf 重新执行加载命令: modprobe br_netfilter 此时若还是原来的报错,则需要重启服务器,再执行lsmod | grep br_netfilter命令查看网桥过滤模块是否加载成功 注意: Docker 安装后出现:WARNING: bridge-nf-call-iptables is disabled 的解决办法: net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1: 将Linux系统作为路由或者VPN服务就必须要开启IP转发功能。当linux主机有多个网卡时一个网卡收到的信息是否能够传递给其他的网卡 如果设置成1 的话 可以进行数据包转发,可以实现VxLAN 等功能。不开启会导致docker部署应用无法访问。 重启docker ```shell [root@xianchaomaster1 ~]# systemctl restart docker   [root@xianchaomaster1 ~]# scp /etc/docker/daemon.json 192.168.40.181:/etc/docker/ [root@harbor ~]# systemctl daemon-reload [root@harbor ~]# systemctl restart docker</code></pre> <h3>安装harbor</h3> <pre><code class="language-shell">#创建安装目录 [root@harbor ssl]# mkdir /opt/harbor/install -p [root@harbor ssl]# cd /opt/harbor/install/</code></pre> <p>/opt/harbor/ssl目录下有如下文件: ca.key  ca.pem  ca.srl  harbor.csr  harbor.key  harbor.pem <img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=38512f236d42ec9f727e62ce4bf109ec&amp;amp;file=file.png" alt="" /> 把harbor的离线包harbor-offline-installer-v2.3.0-rc3.tgz上传到这个目录,离线包在课件里提供了 下载harbor离线包的地址:<a href="https://github.com/goharbor/harbor/releases/tag/">https://github.com/goharbor/harbor/releases/tag/</a></p> <pre><code class="language-shell">#解压 [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,跟上面签发的证书域名保持一致 hostname:  harbor   #协议用https certificate: /data/ssl/harbor.pem private_key: /data/ssl/harbor.key</code></pre> <p>邮件和ldap不需要配置,在harbor的web界面可以配置 其他配置采用默认即可 注:harbor默认的账号密码:admin/Harbor12345</p> <h4>安装docker-compose</h4> <p>docker-compose项目是Docker官方的开源项目,负责实现对Docker容器集群的快速编排。docker-compose可以管理多个docker实例。 Docker-Compose的工程配置文件默认为docker-compose.yml,Docker-Compose运行目录下的必要有一个docker-compose.yml。 安装harbor需要的离线镜像包docker-harbor-2-3-0.tar.gz在课件,可上传到harbor机器,通过docker load -i解压</p> <pre><code class="language-shell">#上传课件里的docker-compose-Linux-x86_64文件到harbor机器 [root@harbor harbor]# mv docker-compose-Linux-x86_64.64 /usr/bin/docker-compose [root@harbor harbor]# chmod +x /usr/bin/docker-compose [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</code></pre> <p>看到下面内容,说明安装成功 <img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=e5f80f91c946eafd73f2d1a73052b6ef&amp;amp;file=file.png" alt="" /></p> <p>在自己电脑修改hosts文件:在hosts文件添加 10.5.146.44 harbor 然后保存即可</p> <p><strong>扩展</strong> 停掉harbor <code>[root@harbor harbor]# cd /data/install/harbor</code> <code>[root@harbor harbor]# docker-compose stop</code> 启动harbor: <code>[root@harbor harbor]# cd /data/install/harbor</code> <code>[root@harbor harbor]# docker-compose start</code> 如果docker-compose start启动harbor之后,还是访问不了,那就需要重启虚拟机</p> <h1>Harbor 图形化界面使用说明</h1> <p>在浏览器输入:<a href="https://harbor">https://harbor</a> 账号:admin 密码:Harbor12345 输入账号密码出现如下 <img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=98c6255996d9e260b1a54f3692c59b0a&amp;amp;file=file.png" alt="" /> 所有基础镜像都会放在library里面,这是一个公开的镜像仓库 新建项目-&gt;起个项目名字test(把访问级别公开那个选中,让项目才可以被公开使用) <img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=266746912ead480479c8251889b13713&amp;amp;file=file.png" alt="" /></p> <h1>测试使用harbor私有镜像仓库</h1> <pre><code class="language-shell">#修改docker配置  [root@karen ~]# vim /etc/docker/daemon.json { &amp;quot;registry-mirrors&amp;quot;: [&amp;quot;https://rsbud4vc.mirror.aliyuncs.com&amp;quot;,&amp;quot;https://registry.docker-cn.com&amp;quot;,&amp;quot;https://docker.mirrors.ustc.edu.cn&amp;quot;,&amp;quot;https://dockerhub.azk8s.cn&amp;quot;,&amp;quot;http://hub-mirror.c.163.com&amp;quot;], &amp;quot;insecure-registries&amp;quot;: [&amp;quot;10.5.146.44&amp;quot;,&amp;quot;harbor&amp;quot;] } #修改配置之后使配置生效: [root@karen ~]# systemctl daemon-reload &amp;amp;&amp;amp; systemctl restart docker #查看docker是否启动成功 [root@karen ~]# systemctl status docker</code></pre> <p><strong>注意:</strong>配置新增加了一行内容:&quot;insecure-registries&quot;: [&quot;10.5.146.44&quot;,&quot;harbor&quot;] 表示我们内网访问harbor的时候走的是http,10.5.146.44是安装harbor机器的ip</p> <h2>登录harbor:</h2> <pre><code class="language-shell">[root@karen ~]# docker login 10.5.146.44 Username:admin  Password:  Harbor12345 #输入账号密码之后看到如下,说明登录成功了: Login Succeeded</code></pre> <h2>导入tomcat镜像,tomcat.tar.gz在课件里</h2> <pre><code class="language-shell">[root@karen ~]# docker load -i tomcat.tar.gz #把tomcat镜像打标签 [root@karen ~]# docker tag tomcat:latest  10.5.146.44/test/tomcat:v1 #上传镜像 [root@karen ~]# docker push 10.5.146.44/test/tomcat:v1 #执行上面命令就会把192.168.40.181/test/tomcat:v1上传到harbor里的test项目下</code></pre> <h2>从harbor仓库下载镜像</h2> <p>在karen机器上删除镜像 <code>[root@karen ~]# docker rmi -f 192.168.40.181/test/tomcat:v1</code> 拉取镜像 <code>[root@karen ~]#docker pull 192.168.40.181/test/tomcat:v1</code></p>

页面列表

ITEM_HTML