docker私用镜像仓库Harbor部署

docker私用镜像仓库Harbor部署

1. 安装docker-compose

cd /usr/local/bin

# 下载docker-compose
wget https://zwfile.yonyougov.tech/share/crux-tools/docker-compose

# 赋予docker-compose 可执行权限
chmod +x docker-compose

# 验证
docker-compose -v
# docker-compose常用命令

docker-compose up -d nginx                    # 构建建启动nignx容器
docker-compose exec nginx bash                # 登录到nginx容器中
docker-compose down                           # 删除所有nginx容器,镜像
docker-compose ps                             # 显示所有容器
docker-compose restart nginx                  # 重新启动nginx容器
docker-compose run --no-deps --rm php-fpm php -v  #在php-fpm中不启动关联容器,并容器执行php -v 执行完成后删除容器
docker-compose build nginx                    #构建镜像 。        
docker-compose build --no-cache nginx         #不带缓存的构建。
docker-compose logs  nginx                    #查看nginx的日志 
docker-compose logs -f nginx                  #查看nginx的实时日志

docker-compose config  -q                     #验证(docker-compose.yml)文件配置,当配置正确时,不输出任何内容,当文件配置错误,输出错误信息。 
docker-compose events --json nginx            #以json的形式输出nginx的docker日志
docker-compose pause nginx                    #暂停nignx容器
docker-compose unpause nginx                  #恢复ningx容器
docker-compose rm nginx                       #删除容器(删除前必须关闭容器)
docker-compose stop nginx                     #停止nignx容器
docker-compose start nginx                    #启动nignx容器

2.安装Harbor

#1 下载离线安装包
wget https://github.com/goharbor/harbor/releases/download/v1.10.4/harbor-offline-installer-v1.10.4.tgz

#2 解压压缩包
tar -zxvf harbor-offline-installer-v1.10.4.tgz

#3 修改配置文件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: manager.node  #修改主机名

# http related config
http:
  # port for http, default is 80. If https enabled, this port will redirect to https port
  port: 8089  #修改一个不会被占用的

# https related config
# https:  #暂时注掉
  # https port for harbor, default is 443
  # port: 443 #暂时注掉
  # The path of cert and key files for nginx
  # certificate: /your/certificate/path  #暂时不开https注掉
  # private_key: /your/private/key/path  #暂时不开https注掉

# 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: Harbor12345  #修改密码,暂用默认
————————————————
版权声明:本文为CSDN博主「再看我把你吃掉」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zz_aiytag/java/article/details/105702101
#4 执行prepare脚本
cd harbor
./prepare

#5 执行安装脚本
sh install.sh

#6 访问harbor
http://ip:port/harbor/

http://10.10.66.49:9081/harbor/sign-in   admin/Harbor12345 yonyou/Yonyou@1988

#7 harbor重启
 docker-compose down
 # ./prepare  # 如果修改了配置执行
 docker-compose up -d

3 docker配置harbor仓库

# 添加http harbor仓库访问
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"],
"insecure-registries": ["10.10.66.49:9081"] #添加这一行
}

4 添加用户并创建项目,把用户添加到项目并给予相应角色

# harbor admin用户登录,并创建用户,创建项目,并把用户添加到项目中

5 上传镜像到harbor

#1 在docker机器登录harbor仓库,会生成密文保存在/root/.docker/config.json
docker login 10.10.66.49:9081
username:yonyou
password:Yonyou@1988

#2 给已有镜像打tag
docker tag goharbor/redis-photon:v1.10.4 10.10.66.49:9081/sbfx/redis-photon:v1.10.4

#3 上传到harbor仓库
docker push 10.10.66.49:9081/sbfx/redis-photon:v1.10.4

6 下载harbor仓库镜像

#1 在docker机器登录harbor仓库,只需要执行一次,会生成密文保存在/root/.docker/config.json
docker login 10.10.66.49:9081
username:yonyou
password:Yonyou@1988

#2 拉取镜像文件
docker pull 10.10.66.49:9081/sbfx/redis-photon:v1.10.4 

7 rancher配置使用harbor仓库

资源 > 密文 > 镜像库凭证列表

地址:10.10.66.49:9081
用户名:yonyou
密码:Yonyou@1988

8 kubectl安装

# 下载最新版
 curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl

 # 修改权限
 chmod +x ./kubectl

 # 将二进制移到path下
 sudo mv ./kubectl /usr/local/bin/kubectl

 从rancher获取kubeconfig
 copy kubeconfig信息 到 ~/.kube/config (没有就新建,不然不能访问集群)

8.1 kubectl常用命令

# get 命令的基本输出
kubectl get services                          # 列出当前命名空间下的所有 services
kubectl get pods --all-namespaces             # 列出所有命名空间下的全部的 Pods
kubectl get pods -o wide                      # 列出当前命名空间下的全部 Pods,并显示更详细的信息
kubectl get deployment my-dep                 # 列出某个特定的 Deployment
kubectl get pods                              # 列出当前命名空间下的全部 Pods
kubectl get pod my-pod -o yaml                # 获取一个 pod 的 YAML

# describe 命令的详细输出
kubectl describe nodes my-node
kubectl describe pods my-pod

# 列出当前名字空间下所有 Services,按名称排序
kubectl get services --sort-by=.metadata.name

# 列出 Pods,按重启次数排序
kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'

# 列举所有 PV 持久卷,按容量排序
kubectl get pv --sort-by=.spec.capacity.storage

# 获取包含 app=cassandra 标签的所有 Pods 的 version 标签
kubectl get pods --selector=app=cassandra -o \
  jsonpath='{.items[*].metadata.labels.version}'

# 获取所有工作节点(使用选择器以排除标签名称为 'node-role.kubernetes.io/master' 的结果)
kubectl get node --selector='!node-role.kubernetes.io/master'

# 获取当前命名空间中正在运行的 Pods
kubectl get pods --field-selector=status.phase=Running

# 获取全部节点的 ExternalIP 地址
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'

# 列出属于某个特定 RC 的 Pods 的名称
# 在转换对于 jsonpath 过于复杂的场合,"jq" 命令很有用;可以在 https://stedolan.github.io/jq/ 找到它。
sel=${$(kubectl get rc my-rc --output=json | jq -j '.spec.selector | to_entries | .[] | "\(.key)=\(.value),"')%?}
echo $(kubectl get pods --selector=$sel --output=jsonpath={.items..metadata.name})

# 显示所有 Pods 的标签(或任何其他支持标签的 Kubernetes 对象)
kubectl get pods --show-labels

# 检查哪些节点处于就绪状态
JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}' \
 && kubectl get nodes -o jsonpath="$JSONPATH" | grep "Ready=True"

# 列出被一个 Pod 使用的全部 Secret
kubectl get pods -o json | jq '.items[].spec.containers[].env[]?.valueFrom.secretKeyRef.name' | grep -v null | sort | uniq

# 列举所有 Pods 中初始化容器的容器 ID(containerID)
# Helpful when cleaning up stopped containers, while avoiding removal of initContainers.
kubectl get pods --all-namespaces -o jsonpath='{range .items[*].status.initContainerStatuses[*]}{.containerID}{"\n"}{end}' | cut -d/ -f3

# 列出事件(Events),按时间戳排序
kubectl get events --sort-by=.metadata.creationTimestamp

# 比较当前的集群状态和假定某清单被应用之后的集群状态
kubectl diff -f ./my-manifest.yaml
=========================================================================================
# 删除pod和service  名称为eurekaserver
kubectl delete pod,service eurekaserver --namespace p-pjxvk-pipeline

# 通过标签删除pod
kubectl delete pod -l app=eurekaserver --namespace p-pjxvk-pipeline

# 通过标签删除deployment
kubectl delete deployment -l app=eurekaserver --namespace p-pjxvk-pipeline

# 指定命名空间
kubectl get pods  --namespace或-n  p-pjxvk-pipeline

# 查看pod
kubectl get pod --namespace p-pjxvk-pipeline

#创建pod
kubectl run myeureka --image=10.10.66.49:9081/sbfx/myeureka:v1.17 --namespace=p-pjxvk-pipeline

#创建deployment
kubectl create deployment myeureka --image=10.10.66.49:9081/sbfx/myeureka:v1.17 --namespace=p-pjxvk-pipeline

#创建deployment
kubectl apply -f deployment.yml

#暴露端口
kubectl expose deploy eurekaserver --port=8791 --target-port=8791 --type=NodePort

#删除deployment
kubectl delete deploy eurekaserver

#根据name删除pod
kubectl delete pod myeureka --namespace p-pjxvk-pipeline

#根据name删除deployment
kubectl delete deployment myeureka --namespace=p-pjxvk-pipeline 

#根据name删除service
kubectl delete service myeureka --namespace p-pjxvk-pipeline

# 获取 pod 日志(标准输出)
kubectl logs my-pod   
# 获取含 name=myLabel 标签的 Pods 的日志(标准输出)
kubectl logs -l name=myLabel 
# -f 跟踪日志   
kubectl logs -f my-pod    

#在pod中执行命令
kubectl exec my-pod -- ls /

#进入pod交互
kubectl exec -it  eurekaserver-5cd78d7cdb-7bks6 -- bash

# 在本地计算机上侦听端口 5000 并转发到 my-pod 上的端口 6000
kubectl port-forward eurekaserver-5cd78d7cdb-7bks6 8791:8791   

# 显示给定 Pod 和其中容器的监控数据
kubectl top pod eurekaserver-5cd78d7cdb-7bks6 --containers

# 缩放 为 Deployment, ReplicaSet, Replication Controller 或者 Job 设置一个新的副本数量
kubectl scale --replicas=1 deploy/eurekaserver

kubectl scale --replicas=1 rs/eurekaserver

# 查看pod详情
kubectl describe pod/eurekaserver-5cd78d7cdb-7bks6

# 升级镜像 滚动升级,新版本没有启动成功不会停旧版本服务
kubectl set image deployment/eurekaserver eurekaserver=myeureka:v1.0

kubectl set image deployment/eurekaserver eurekaserver=10.10.66.49:9081/sbfx/myeureka:v1.0 --record=true

# 查看镜像历史
kubectl rollout history deploy/eurekaserver

# 回滚到上一版本
kubectl rollout undo deploy/eurekaserver

8.2与资源交互

kubectl logs my-pod                                 # 获取 pod 日志(标准输出)
kubectl logs -l name=myLabel                        # 获取含 name=myLabel 标签的 Pods 的日志(标准输出)
kubectl logs -f my-pod                              # -f 跟踪日志                      

kubectl logs my-pod --previous                      # 获取上个容器实例的 pod 日志(标准输出)
kubectl logs my-pod -c my-container                 # 获取 Pod 容器的日志(标准输出, 多容器场景)
kubectl logs -l name=myLabel -c my-container        # 获取含 name=myLabel 标签的 Pod 容器日志(标准输出, 多容器场景)
kubectl logs my-pod -c my-container --previous      # 获取 Pod 中某容器的上个实例的日志(标准输出, 多容器场景)
kubectl logs -f my-pod                              # 流式输出 Pod 的日志(标准输出)
kubectl logs -f my-pod -c my-container              # 流式输出 Pod 容器的日志(标准输出, 多容器场景)
kubectl logs -f -l name=myLabel --all-containers    # 流式输出含 name=myLabel 标签的 Pod 的所有日志(标准输出)
kubectl run -i --tty busybox --image=busybox -- sh  # 以交互式 Shell 运行 Pod
kubectl run nginx --image=nginx -n mynamespace      # 在指定名字空间中运行 nginx Pod
kubectl run nginx --image=nginx                     # 运行 ngins Pod 并将其规约写入到名为 pod.yaml 的文件
  --dry-run=client -o yaml > pod.yaml

kubectl attach my-pod -i                            # 挂接到一个运行的容器中
kubectl port-forward my-pod 5000:6000               # 在本地计算机上侦听端口 5000 并转发到 my-pod 上的端口 6000
kubectl exec my-pod -- ls /                         # 在已有的 Pod 中运行命令(单容器场景)
kubectl exec my-pod -c my-container -- ls /         # 在已有的 Pod 中运行命令(多容器场景)
kubectl top pod POD_NAME --containers               # 显示给定 Pod 和其中容器的监控数据
Basic Commands (Beginner):
  create         # Create a resource from a file or from stdin.
  expose         # 使用 replication controller, service, deployment 或者 pod 并暴露它作为一个 新的
Kubernetes Service
  run            # 在集群中运行一个指定的镜像
  set            # 为 objects 设置一个指定的特征

Basic Commands (Intermediate):
  explain        #查看资源的文档
  get            #显示一个或更多 resources
  edit           #在服务器上编辑一个资源
  delete         #Delete resources by filenames, stdin, resources and names, or by resources and label selector

Deploy Commands:
  rollout        #Manage the rollout of a resource
  scale          #为 Deployment, ReplicaSet, Replication Controller 或者 Job 设置一个新的副本数量
  autoscale      #自动调整一个 Deployment, ReplicaSet, 或者 ReplicationController 的副本数量

Cluster Management Commands:
  certificate    #修改 certificate 资源.
  cluster-info   #显示集群信息
  top            #Display Resource (CPU/Memory/Storage) usage.
  cordon         #标记 node 为 unschedulable
  uncordon       #标记 node 为 schedulable
  drain          #Drain node in preparation for maintenance
  taint          #更新一个或者多个 node 上的 taints

Troubleshooting and Debugging Commands:
  describe       #显示一个指定 resource 或者 group 的 resources 详情
  logs           #输出容器在 pod 中的日志
  attach         #Attach 到一个运行中的 container
  exec           #在一个 container 中执行一个命令
  port-forward   #Forward one or more local ports to a pod
  proxy          #运行一个 proxy 到 Kubernetes API server
  cp             #复制 files 和 directories 到 containers 和从容器中复制 files 和 directories.
  auth           #Inspect authorization

Advanced Commands:
  apply          #通过文件名或标准输入流(stdin)对资源进行配置
  patch          #使用 strategic merge patch 更新一个资源的 field(s)
  replace        #通过 filename 或者 stdin替换一个资源
  wait           #Experimental: Wait for one condition on one or many resources
  convert        #在不同的 API versions 转换配置文件

Settings Commands:
  label          #更新在这个资源上的 labels
  annotate       #更新一个资源的注解
  completion     #Output shell completion code for the specified shell (bash or zsh)

Other Commands:
  alpha          #Commands for features in alpha
  api-resources  #Print the supported API resources on the server
  api-versions   #Print the supported API versions on the server, in the form of "group/version"
  config         #修改 kubeconfig 文件
  plugin         #Runs a command-line plugin
  version        #输出 client 和 server 的版本信息

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容