K8s集群使用容器镜像仓库Harbor

这篇具有很好参考价值的文章主要介绍了K8s集群使用容器镜像仓库Harbor。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Kubernetes集群使用容器镜像仓库Harbor

一、容器镜像仓库Harbor部署

1.1 在docker主机部署harbor

1.1.1 docker-ce安装

1.1.1.1 获取YUM源

使用阿里云开源软件镜像站。

# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
1.1.1.2 安装并设置启动及开机自启动
# yum -y install docker-ce
# systemctl enable --now docker

1.1.2 docker compose安装

下载docker-compose二进制文件
# wget https://github.com/docker/compose/releases/download/1.25.0/docker-compose-Linux-x86_64
查看已下载二进制文件
# ls
docker-compose-Linux-x86_64
移动二进制文件到/usr/bin目录,并更名为docker-compose
# mv docker-compose-Linux-x86_64 /usr/bin/docker-compose
为二进制文件添加可执行权限
# chmod +x /usr/bin/docker-compose
安装完成后,查看docker-compse版本
# docker-compose version
docker-compose version 1.25.0, build 0a186604
docker-py version: 4.1.0
CPython version: 3.7.4
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019

1.1.3 获取harbor安装文件

下载harbor离线安装包
# wget https://github.com/goharbor/harbor/releases/download/v2.5.1/harbor-offline-installer-v2.5.1.tgz
查看已下载的离线安装包
# ls
harbor-offline-installer-v2.5.1.tgz

1.1.4 修改配置文件

# tar xf harbor-offline-installer-v2.5.1.tgz	#解压harbor离线安装包
# ls											#查看解压出来的目录
harbor 
# ls harbor										#查看harbor目录
common.sh  harbor.v2.5.1.tar.gz  harbor.yml.tmpl  install.sh  LICENSE  prepare
创建配置文件
# cd harbor/
# mv harbor.yml.tmpl harbor.yml
修改配置文件内容
# vim harbor.yml
# Configuration file of Harbor
# The IP address or hostname to access admin UI and registry service.
# DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external clients.
hostname: 192.168.10.200

# http related config
http:
  # port for http, default is 80. If https enabled, this port will redirect to https port
  port: 80

# https related config
# https:
  # https port for harbor, default is 443
#  port: 443
  # The path of cert and key files for nginx
#  certificate: /root/harbor/6864844_kubemsb.com.pem 
#  private_key: /root/harbor/6864844_kubemsb.com.key
# # Uncomment following will enable tls communication between all harbor components
# internal_tls:
#   # set enabled to true means internal tls is enabled
#   enabled: true
#   # put your cert and key files on dir
#   dir: /etc/harbor/tls/internal
# Uncomment external_url if you want to enable external proxy
# And when it enabled the hostname will no longer used
# external_url: https://reg.mydomain.com:8433
# The initial password of Harbor admin
# It only works in first time to install harbor
# Remember Change the admin password from UI after launching Harbor.
harbor_admin_password: 12345 访问密码
......

1.1.5 执行预备脚本

# ./prepare
prepare base dir is set to /root/harbor
Clearing the configuration file: /config/portal/nginx.conf
Clearing the configuration file: /config/log/logrotate.conf
Clearing the configuration file: /config/log/rsyslog_docker.conf
Generated configuration file: /config/portal/nginx.conf
Generated configuration file: /config/log/logrotate.conf
Generated configuration file: /config/log/rsyslog_docker.conf
Generated configuration file: /config/nginx/nginx.conf
Generated configuration file: /config/core/env
Generated configuration file: /config/core/app.conf
Generated configuration file: /config/registry/config.yml
Generated configuration file: /config/registryctl/env
Generated configuration file: /config/registryctl/config.yml
Generated configuration file: /config/db/env
Generated configuration file: /config/jobservice/env
Generated configuration file: /config/jobservice/config.yml
Generated and saved secret to file: /data/secret/keys/secretkey
Successfully called func: create_root_cert
Generated configuration file: /compose_location/docker-compose.yml
Clean up the input dir

1.1.6 执行安装脚本

# ./install.sh
[Step 0]: checking if docker is installed ...

Note: docker version: 20.10.12

[Step 1]: checking docker-compose is installed ...

Note: docker-compose version: 1.25.0

[Step 2]: loading Harbor images ...

[Step 3]: preparing environment ...

[Step 4]: preparing harbor configs ...
prepare base dir is set to /root/harbor

[Step 5]: starting Harbor ...
Creating network "harbor_harbor" with the default driver
Creating harbor-log ... done
Creating harbor-db     ... done
Creating registry      ... done
Creating registryctl   ... done
Creating redis         ... done
Creating harbor-portal ... done
Creating harbor-core   ... done
Creating harbor-jobservice ... done
Creating nginx             ... done
✔ ----Harbor has been installed and started successfully.----

1.1.7 验证运行情况

# docker ps
CONTAINER ID   IMAGE                                COMMAND                  CREATED              STATUS                        PORTS                                                                            NAMES
71c0db683e4a   goharbor/nginx-photon:v2.5.1         "nginx -g 'daemon of…"   About a minute ago   Up About a minute (healthy)   0.0.0.0:80->8080/tcp, :::80->8080/tcp, 0.0.0.0:443->8443/tcp, :::443->8443/tcp   nginx
4e3b53a86f01   goharbor/harbor-jobservice:v2.5.1    "/harbor/entrypoint.…"   About a minute ago   Up About a minute (healthy)                                                                                    harbor-jobservice
df76e1eabbf7   goharbor/harbor-core:v2.5.1          "/harbor/entrypoint.…"   About a minute ago   Up About a minute (healthy)                                                                                    harbor-core
eeb4d224dfc4   goharbor/harbor-portal:v2.5.1        "nginx -g 'daemon of…"   About a minute ago   Up About a minute (healthy)                                                                                    harbor-portal
70e162c38b59   goharbor/redis-photon:v2.5.1         "redis-server /etc/r…"   About a minute ago   Up About a minute (healthy)                                                                                    redis
8bcc0e9b06ec   goharbor/harbor-registryctl:v2.5.1   "/home/harbor/start.…"   About a minute ago   Up About a minute (healthy)                                                                                    registryctl
d88196398df7   goharbor/registry-photon:v2.5.1      "/home/harbor/entryp…"   About a minute ago   Up About a minute (healthy)                                                                                    registry
ed5ba2ba9c82   goharbor/harbor-db:v2.5.1            "/docker-entrypoint.…"   About a minute ago   Up About a minute (healthy)                                                                                    harbor-db
dcb4b57c7542   goharbor/harbor-log:v2.5.1           "/bin/sh -c /usr/loc…"   About a minute ago   Up About a minute (healthy)   127.0.0.1:1514->10514/tcp                                                        harbor-log

1.1.8 访问harbor UI界面

k8s使用harbor,k8s,kubernetes,docker,容器

k8s使用harbor,k8s,kubernetes,docker,容器

1.2 在kubernetes集群中部署harbor

二、Kubernetes集群使用harbor仓库

2.1 通过secret使用harbor仓库

2.1.1 新建一个harbor私有仓库

k8s使用harbor,k8s,kubernetes,docker,容器

k8s使用harbor,k8s,kubernetes,docker,容器

2.1.2 kubernetes集群所有节点配置harbor仓库

# vim /etc/docker/daemon.json

{							
  "insecure-registries": ["http://192.168.10.200"]			
}

# systemctl restart docker

2.1.3 上传nginx测试镜像到harbor

docker-compose down停止harbor,修改后再启动。

[root@harbor ~]# cat /etc/docker/daemon.json
{
        "insecure-registries": ["http://192.168.10.200"]
}
[root@harbor ~]# systemctl restart docker
[root@harbor ~]# docker pull nginx:1.15-alpine


[root@harbor ~]# docker login 192.168.10.200
Username: admin
Password:							这里密码是12345
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

[root@harbor ~]# docker tag nginx:1.15-alpine 192.168.10.200/test/nginx:v1
[root@harbor ~]# docker push 192.168.10.200/test/nginx:v1

k8s使用harbor,k8s,kubernetes,docker,容器

2.1.4 创建docker-registry类型secret

[root@k8s-master1 ~]# kubectl create secret docker-registry harbor-secret --docker-server=192.168.10.200 --docker-username=admin --docker-password=12345

说明:

  • 类型为docker-registry
  • –docker-server指定harbor仓库的IP
  • –docker-username指定harbor仓库的登录用户名
  • –docker-password指定harbor仓库的登录密码

验证查看

[root@k8s-master1 ~]# kubectl get secret |grep harbor-secret
harbor-secret                        kubernetes.io/dockerconfigjson        1      19s
[root@k8s-master1 ~]# kubectl describe secret harbor-secret
Name:         harbor-secret
Namespace:    default
Labels:       <none>
Annotations:  <none>

Type:  kubernetes.io/dockerconfigjson

Data
====
.dockerconfigjson:  94 bytes

2.1.5 创建pod并使用secret

[root@k8s-master1 ~]# vim pod-harbor.yml
apiVersion: v1
kind: Pod
metadata:
  name: pod-harbor
spec:
  containers:
  - name: c1
    image: 192.168.10.200/test/nginx:v1
  imagePullSecrets:                     # 定义镜像下载使用的secrets
  - name: harbor-secret					# 与上面的secret一致
[root@k8s-master1 ~]# kubectl apply -f pod-harbor.yml
pod/pod-harbor created

2.1.6 验证pod

[root@k8s-master1 ~]# kubectl describe pod pod-harbor
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  16s   default-scheduler  Successfully assigned default/pod-harbor to k8s-worker1
  Normal  Pulling    15s   kubelet            Pulling image "192.168.10.200/test/nginx:v1"
  Normal  Pulled     14s   kubelet            Successfully pulled image "192.168.10.250/test/nginx:v1" in 630.869309ms
  Normal  Created    14s   kubelet            Created container c1
  Normal  Started    14s   kubelet            Started container c1

可以看到是从192.168.10.200/test/nginx:v1拉取的镜像

2.2 通过serviceaccout使用harbor仓库

2.2.1 设为serviceaccount默认规则

如果每次编写yaml文件都需要添加imagePullSecrets这2行配置,有点麻烦, 有没有在不需要添加这2行配置就可以实现下载harbor仓库里面的镜像呢?答案是有的,可以把secret配置到serviceAccount中即可。

2.2.2 创建serviceaccount及应用过程

  1. 创建serviceaccount
[root@k8s-master1 ~]# vim serviceaccount-harbor-sa.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: harbor-sa
  namespace: default
[root@k8s-master1 ~]# kubectl apply -f serviceaccount-harbor-sa.yaml
serviceaccount/harbor-sa created
[root@k8s-master1 ~]# kubectl get sa |grep harbor-sa
harbor-sa                1         14s
  1. 修改serviceaccount添加使用harbor-secret
[root@k8s-master1 ~]# kubectl describe serviceaccount harbor-sa
Name:                harbor-sa
Namespace:           default
Labels:              <none>
Annotations:         <none>
Image pull secrets:  <none>
Mountable secrets:   harbor-sa-token-thxwq
Tokens:              harbor-sa-token-thxwq
Events:              <none>
[root@k8s-master1 ~]# kubectl patch serviceaccount harbor-sa -n default  -p '{"imagePullSecrets": [{"name": "harbor-secret"}]}'
serviceaccount/harbor-sa patched
[root@k8s-master1 ~]# kubectl describe serviceaccount harbor-sa
Name:                harbor-sa
Namespace:           default
Labels:              <none>
Annotations:         <none>
Image pull secrets:  harbor-secret
Mountable secrets:   harbor-sa-token-thxwq
Tokens:              harbor-sa-token-thxwq
Events:              <none>

[root@k8s-master1 ~]# kubectl get serviceaccount harbor-sa -o yaml
apiVersion: v1
imagePullSecrets:
- name: harbor-secret					   # 确认,通过patch方式更新了
kind: ServiceAccount
......
  1. 修改yaml使用serviceAccount
[root@k8s-master1 ~]# vim pod-harbor.yml
apiVersion: v1
kind: Pod
metadata:
  name: pod-harbor
spec:
  serviceAccount: harbor-sa					# 原来的2句换成使用harbor-sa这个serviceAccount
  containers:
  - name: c1
    image: 192.168.10.200/test/nginx:v1
  1. 删除先前的重新创建pod验证
[root@k8s-master1 ~]# kubectl delete pod pod-harbor
pod "pod-harbor" deleted

[root@k8s-master1 ~]# kubectl apply -f pod-harbor.yml
pod/pod-harbor created

[root@k8s-master1 ~]# kubectl get pods
NAME         READY   STATUS    RESTARTS   AGE
pod-harbor   1/1     Running   0          8s
[root@k8s-master1 ~]# kubectl describe pods pod-harbor
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  13s   default-scheduler  Successfully assigned default/pod-harbor to k8s-worker2
  Normal  Pulling    13s   kubelet            Pulling image "192.168.10.200/test/nginx:v1"
  Normal  Pulled     12s   kubelet            Successfully pulled image "192.168.10.200/test/nginx:v1" in 731.788686ms
  Normal  Created    12s   kubelet            Created container c1
  Normal  Started    12s   kubelet            Started container c1

补充: serviceAccount可以实现不同namespace下载镜像使用访问harbor账号的不同。文章来源地址https://www.toymoban.com/news/detail-788368.html

到了这里,关于K8s集群使用容器镜像仓库Harbor的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • k8s实战-如何使用私有镜像仓库

    本文介绍如何通过创建Secret来拉取私有镜像仓库的镜像,从而完成Deployment的创建。 拉取镜像时需要认证 ​ 使用私有仓库时需要通过用户名和密码进行认证。所以,若是直接配置镜像仓库的地址,无法拉取到镜像文件,会报拉取镜像的错误。 可能由于网络问题导致镜像拉取

    2024年02月17日
    浏览(24)
  • k8s使用私有镜像仓库的访问凭据配置

    k8s创建私有仓库凭据有两种方式 1.使用kubectl命令创建secret 2.使用docker凭证作为k8s的凭据。 name:凭据名称 DOCKER_REGISTRY_SERVER:私有服务地址 DOCKER_USER:登录账号 DOCKER_PASSWORD:密码 DOCKER_EMAIL:邮箱 1.首先登录私有服务 2.查看auth文件内容 3.基于auth文件创建secret

    2024年02月06日
    浏览(26)
  • ctr-k8s镜像管理命令,将k8s正在使用的镜像推送仓库

    ​ 一.k8s镜像管理命令 查出k8s中pod在使用的镜像 kubectl get -o wide deploy -n yxyw-uat |awk ‘{print $7}’ 查出镜像地址,勾选正则开头配上镜像推送命令 gem-acr-p-a01-registry-vpc.cn-shenzhen.cr.aliyuncs.com/osale/gyx-admin:yxyw-pre-2023-06-05-11-18-10 ​ 二.将pod在使用的镜像推送到阿里云镜像仓库 三.cric

    2024年02月13日
    浏览(35)
  • k8s使用helm部署Harbor镜像仓库并启用SSL

    参照:https://zhaoll.blog.csdn.net/article/details/128155767 有多种安装方式,根据自己的k8s版本选择合适的helm版本 参考:https://blog.csdn.net/qq_30614345/article/details/131669319 我们这里使用helm安装harbor,项目地址:https://github.com/goharbor/harbor-helm (1)拉取项目文件 (2)解压并修改配置 tar -z

    2024年02月16日
    浏览(28)
  • ​k8s 1.24 1.25 集群使用docker作为容器

    背景 在新版本Kubernetes环境(1.24以及以上版本)下官方不在支持docker作为容器运行时了,若要继续使用docker 需要对docker进行配置一番。需要安装cri-docker作为Kubernetes容器 查看当前容器运行时 安装docker 安装cri-docker 为kubelet配置容器运行时 关于 https://www.oiox.cn/ https://www.oiox.cn

    2024年02月12日
    浏览(45)
  • k8s基础:使用kubectl set image命令更新Deployment中容器的镜像

    在Kubernetes中,使用 kubectl 更新Deployment中容器的镜像,可以使用以下命令: 例如,如果你有一个名为 myapp 的 Deployment,其中包含一个名为 mycontainer 的容器,你想将镜像从 myregistry/myimage:v1 更新到 myregistry/myimage:v2 ,可以执行: 这条命令将会触发一个滚动更新,根据你的Deploy

    2024年04月26日
    浏览(27)
  • k8s harbor镜像仓库搭建

    1.前言 Harbor 是一个开源的云原生镜像仓库,用于存储和分发 Docker 镜像。它提供了一些安全性和管理方面的功能,使得用户可以更好地管理和共享 Docker 镜像 2.配置harbor搭建环境 harbor的搭建需要用到docker、docker-compose服务 docker搭建参考:部署docker-ce_Apex Predator的博客-CSDN博客

    2024年02月16日
    浏览(32)
  • 准备k8s集群镜像

    一、在安装kubernetes集群之前,必须要提前准备好集群需要的镜像,所需平面镜像可以通过下面命令查看 kubeadm config images list 二、由于网络原因registry.k8s.io这个镜像仓库源访问不了,就需要变通一下切换镜像仓库地址为阿里云的地址,并打上镜像标签 kubeadm init | Kubernetes 三、

    2024年02月03日
    浏览(27)
  • K8s拉取Harbor私有仓库镜像

    提示:需要先部署Harbor私有镜像库。 insecure-registries对应可信任的镜像服务地址,如果有多个地址,还可以用“,”隔开,配置多个。 提示:每个k8s节点都需要配置,完成之后需要重启docker服务。 选择需要的命名空间,创建密文。 如果没有密文,后面的配置,都会明文数据

    2024年02月16日
    浏览(32)
  • k8s容器集群管理

    k8s容器集群管理 k8s简介 容器技术的发展 使用kubeadm安装k8s kubectl Kubernetes 架构 k8s节点 节点与控制面之间的通信 控制器 k8s容器 Kubernetes 对象 Kubernetes 对象管理 Kubernetes 对象管理 指令式命令 Kubernetes 对象管理 指令式对象配置 k8s对象管理 声明式对象配置 使用 Kustomize 对 Kubern

    2024年02月10日
    浏览(24)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包