翼度科技»论坛 云主机 LINUX 查看内容

搭建自己的harbor仓库并上传和下载镜像

10

主题

10

帖子

30

积分

新手上路

Rank: 1

积分
30

1.1 搭建harbor服务器
  1. [root@rocky8 ~]$ cat /data/scripts/install_harbor.sh
  2. #!/bin/bash
  3. DOCKER_VERSION="20.10.10"
  4. UBUNTU_DOCKER_VERSION="5:${DOCKER_VERSION}~3-0~${ID}-${UBUNTU_CODENAME}"
  5. DOCKER_COMPOSE_VERSION="2.16.0"
  6. DOCKER_COMPOSE_FILE=docker-compose-Linux-x86_64
  7. HARBOR_VERSION="2.5.6"
  8. HARBOR_BASE="/usr/local/"
  9. HARBOR_NAME=harbor.yanlinux.org
  10. HARBOR_IP=`hostname -I|awk '{print $1}'`
  11. HARBOR_ADMIN_PASSWORD=123456
  12. COLOR_SUCCESS="echo -e \\033[1;32m"
  13. COLOR_FAILURE="echo -e \\033[1;31m"
  14. END="\033[m"
  15. . /etc/os-release
  16. color () {
  17.     RES_COL=60
  18.     MOVE_TO_COL="echo -en \\033[${RES_COL}G"
  19.     SETCOLOR_SUCCESS="echo -en \\033[1;32m"
  20.     SETCOLOR_FAILURE="echo -en \\033[1;31m"
  21.     SETCOLOR_WARNING="echo -en \\033[1;33m"
  22.     SETCOLOR_NORMAL="echo -en \E[0m"
  23.     echo -n "$1" && $MOVE_TO_COL
  24.     echo -n "["
  25.     if [ $2 = "success" -o $2 = "0" ] ;then
  26.         ${SETCOLOR_SUCCESS}
  27.         echo -n $"  OK  "
  28.     elif [ $2 = "failure" -o $2 = "1"  ] ;then
  29.         ${SETCOLOR_FAILURE}
  30.         echo -n $"FAILED"
  31.     else
  32.         ${SETCOLOR_WARNING}
  33.         echo -n $"WARNING"
  34.     fi
  35.     ${SETCOLOR_NORMAL}
  36.     echo -n "]"
  37.     echo
  38. }
  39. install_docker() {
  40.     if [ $ID = "centos" -o $ID = "rocky" ];then
  41.         if [ $VERSION_ID = "7" ];then
  42.             cat >  /etc/yum.repos.d/docker.repo  <<EOF
  43. [docker]
  44. name=docker
  45. gpgcheck=0
  46. #baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/
  47. baseurl=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/x86_64/stable/
  48. EOF
  49.         else     
  50.             cat >  /etc/yum.repos.d/docker.repo  <<EOF
  51. [docker]
  52. name=docker
  53. gpgcheck=0
  54. #baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/
  55. baseurl=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/8/x86_64/stable/
  56. EOF
  57.         fi
  58.         yum clean all
  59.         yum makecache
  60.         ${COLOR_FAILURE} "Docker有以下版本"${END}
  61.         yum list docker-ce --showduplicates
  62.         ${COLOR_FAILURE}"5秒后即将安装: docker-"${DOCKER_VERSION}" 版本....."${END}
  63.         sleep 5
  64.         yum -y install docker-ce-${DOCKER_VERSION} docker-ce-cli-${DOCKER_VERSION} || { color "Base,Extras的yum源失败,请检查yum源配置" 1;exit; }
  65.     else
  66.             dpkg -s docker-ce &> /dev/null && $COLOR"Docker已安装,退出" 1 && exit
  67.         apt update || { color "更新包索引失败" 1 ; exit 1; }
  68.         apt  -y install apt-transport-https ca-certificates curl software-properties-common || \
  69.             { color "安装相关包失败" 1 ; exit 2;  }
  70.         curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
  71.         add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
  72.         apt update
  73.         ${COLOR_FAILURE} "Docker有以下版本"${END}
  74.         apt-cache madison docker-ce
  75.         ${COLOR_FAILURE}"5秒后即将安装: docker-"${UBUNTU_DOCKER_VERSION}" 版本....."${END}
  76.         ${COLOR_FAILURE}"如果想安装其它Docker版本,请按ctrl+c键退出,修改版本再执行"${END}
  77.         sleep 5
  78.         apt -y  install docker-ce=${UBUNTU_DOCKER_VERSION} docker-ce-cli=${UBUNTU_DOCKER_VERSION}
  79.     fi
  80.     if [ $? -eq 0 ];then
  81.         color "安装软件包成功"  0
  82.     else
  83.         color "安装软件包失败,请检查网络配置" 1
  84.         exit
  85.     fi
  86.     mkdir -p /etc/docker
  87.     tee /etc/docker/daemon.json <<EOF
  88. {
  89.   "registry-mirrors": ["https://5lwrg1ye.mirror.aliyuncs.com"],
  90.   "insecure-registries":["harbor.yanlinux.org"]
  91. }
  92. EOF
  93.         systemctl daemon-reload
  94.     systemctl enable docker
  95.     systemctl restart docker
  96.     docker version && color "Docker 安装成功" 0 ||  color "Docker 安装失败" 1
  97.     echo 'alias rmi="docker images -qa|xargs docker rmi -f"' >> ~/.bashrc
  98.         echo 'alias rmc="docker ps -qa|xargs docker rm -f"' >> ~/.bashrc
  99.     echo 'alias dps="docker ps -a"' >> ~/.bashrc
  100.     echo 'alias dim="docker images"' >> ~/.bashrc
  101. }
  102. install_docker_compose() {
  103.     ${COLOR_SUCCESS}"开始安装 Docker compose....."${END}
  104.     sleep 5
  105.     if [ ! -e ${DOCKER_COMPOSE_FILE} ];then
  106.         curl -L https://get.daocloud.io/docker/compose/releases/download/v${DOCKER_COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m) -o /usr/bin/docker-compose
  107.     else
  108.         mv ${DOCKER_COMPOSE_FILE} /usr/bin/docker-compose
  109.     fi
  110.     chmod +x /usr/bin/docker-compose
  111.     if docker-compose --version ;then
  112.         ${COLOR_SUCCESS}"Docker Compose 安装完成"${END}
  113.     else
  114.         ${COLOR_FAILURE}"Docker compose 安装失败"${END}
  115.         exit
  116.     fi  
  117. }
  118. install_harbor() {
  119.     ${COLOR_SUCCESS}"开始安装 Harbor....."${END}
  120.     sleep 5
  121.     #下载文件
  122.     if [ ! -e harbor-offline-installer-v${HARBOR_VERSION}.tgz ];then
  123.         wget https://github.com/goharbor/harbor/releases/download/v${HARBOR_VERSION}/harbor-offline-installer-v${HARBOR_VERSION}.tgz || ${COLOR_FAILURE} "下载失败!" ${END}
  124.     fi
  125.     #[ -d ${HARBOR_BASE} ] ||  mkdir ${HARBOR_BASE}
  126.     tar xvf harbor-offline-installer-v${HARBOR_VERSION}.tgz  -C ${HARBOR_BASE}
  127.     cd ${HARBOR_BASE}/harbor
  128.     #编辑配置文件
  129.     cp harbor.yml.tmpl harbor.yml
  130.     sed -ri "/^hostname/s/reg.mydomain.com/${HARBOR_NAME}/" harbor.yml
  131.     sed -ri "/^https/s/(https:)/#\1/" harbor.yml
  132.     sed -ri "s/(port: 443)/#\1/" harbor.yml
  133.     sed -ri "/certificate:/s/(.*)/#\1/" harbor.yml
  134.     sed -ri "/private_key:/s/(.*)/#\1/" harbor.yml
  135.     sed -ri "s/Harbor12345/${HARBOR_ADMIN_PASSWORD}/" harbor.yml
  136.     sed -i 's#^data_volume: /data#data_volume: /data/harbor#' harbor.yml
  137.     ${HARBOR_BASE}/harbor/install.sh && ${COLOR_SUCCESS}"Harbor 安装完成"${END} ||  ${COLOR_FAILURE}"Harbor 安装失败"${END}
  138.     cat > /lib/systemd/system/harbor.service << EOF
  139. [Unit]
  140. Description=Harbor
  141. After=docker.service systemd-networkd.service systemd-resolved.service
  142. Requires=docker.service
  143. Documentation=http://github.com/vmware/harbor
  144. [Service]
  145. Type=simple
  146. Restart=on-failure
  147. RestartSec=5
  148. ExecStart=/usr/bin/docker-compose -f  ${HARBOR_BASE}/harbor/docker-compose.yml up
  149. ExecStop=/usr/bin/docker-compose -f ${HARBOR_BASE}/harbor/docker-compose.yml down
  150. [Install]
  151. WantedBy=multi-user.target
  152. EOF
  153.     systemctl daemon-reload
  154.     systemctl enable --now harbor &> /dev/null || ${COLOR}"Harbor已配置为开机自动启动"${END}
  155.     if [ $?  -eq 0 ];then
  156.         echo
  157.         color "Harbor安装完成!" 0
  158.         echo "-------------------------------------------------------------------"
  159.         echo -e "请访问链接: \E[32;1mhttp://${HARBOR_IP}/\E[0m"
  160.                 echo -e "用户和密码: \E[32;1madmin/${HARBOR_ADMIN_PASSWORD}\E[0m"
  161.     else
  162.         color "Harbor安装失败!" 1
  163.         exit
  164.     fi
  165. }
  166. docker info &> /dev/null && ${COLOR_FAILURE}"Docker已安装"${END} || install_docker
  167. docker-compose --version &> /dev/null && ${COLOR_FAILURE}"Docker Compose已安装"${END} || install_docker_compose
  168. install_harbor
  169. [root@rocky8 ~]$ sh /data/scripts/install_harbor.sh
复制代码
1.2 在Harbor服务器(10.0.0.18)上配置https

1.2.1 生成Harbor服务器证书

[code]#生成ca的私钥openssl genrsa -out ca.key 4096#生成ca的自签名证书openssl req -x509 -new -nodes -sha512 -days 3650 \-subj "/C=CN/ST=Jiangsu/L=Nanjing/O=example/OU=Personal/CN=yanlinux.org" \-key ca.key \-out ca.crt#生成harbor主机的私钥openssl genrsa -out harbor1.yanlinux.org.key 4096#生成harbor主机的证书申请openssl req -new -sha512 \-subj "/C=CN/ST=Jiangsu/L=Nanjing/O=example/OU=Personal/CN=harbor1.yanlinux.org" \-key harbor1.yanlinux.org.key \-out harbor1.yanlinux.org.csr#创建x509 v3扩展文件(新版新增的要求)cat > v3.txt

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具