kubernetes集群系列资料04--harbor部署

一、K8S-harbor安装步骤

1、创建centos7虚拟机;
2、安装centos7系统;
3、设置系统IP、网关、DNS等;

BOOTPROTO=none
IPADDR=192.168.66.100
PREFIX=24
GATEWAY=192.168.66.1
DNS1=192.168.66.1

4、xshell连接K8S-harbor主机,进行集群部署。

hostnamectl set-hostname k8s-harbor
echo '
192.168.66.10   K8S-harbor01
192.168.66.20   k8s-node01
192.168.66.21   k8s-node02
192.168.66.100   hub.atguigu.com
' >>/etc/hosts
hostname;cat /etc/hosts

yum -y install 
conntrack-tools ntpdate ntp ipvsadm ipset jq iptables curl sysstat libseccomp wget vim-enhanced net-tools git
yum list conntrack-tools ntpdate ntp ipvsadm ipset jq iptables curl sysstat libseccomp wget vim-enhanced net-tools git

##设置防火墙为iptables并设置空规则
systemctl stop firewalld && systemctl disable firewalld;yum -y install iptables-services && systemctl start iptables && systemctl enable iptables && iptables -F && service iptables save
systemctl status firewalld;systemctl status iptables

##关闭selinux
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled' /etc/selinux/config
getenforce;grep -Ev '^#|^$' /etc/selinux/config

##关闭虚拟内存,目的:使用kubeadm安装K8S集群时会检测swap是否关闭;如果虚拟内存开启,pod就可能会放在虚拟内存中运行,会大大降低工作效率;生产环境中,K8S集群也要求关闭虚拟内存。
swapoff -a && sed -i '/ swap /s/^(.*)$/#1/g' /etc/fstab   #/ swap /^(.*)$表示匹配到swap这行的首尾为任何字符的所有内容。#1表示在该行的第一个1位置添加#;

##调整内核参数,除前3条外其他为优化项。
cat > /etc/sysctl.d/kubernetes.conf <<eof
net.bridge.bridge-nf-call-iptables=1    #前2条为开启网桥模式,必做,否则安装K8S会报错。
net.bridge.bridge-nf-call-ip6tables=1   #前2条为开启网桥模式,必做,否则安装K8S会报错。
net.ipv6.conf.all.disable_ipv6=1        #关闭IPv6,必做,否则安装K8S会报错。
net.ipv4.ip_forward=1
net.ipv4.tcp_tw_recycle=0
vm.swappiness=0                         #禁止使用swap空间,只有当系统OOM时才允许使用它;
vm.overcommit_memory=1                  #不检查物理内存是否够用;
vm.panic_on_oom=0                       #开启OOM;
fs.inotify.max_user_instances=8192
fs.inotify.max_user_watches=1048576
fs.file-max=52706963
fs.nr_open=52706963
net.netfilter.nf_conntrack_max=2310720
eof
modprobe br_netfilter;ls /proc/sys/net/bridge  #加载模块,使得/proc/sys/net/bridge/bridge-nf-call-iptables、/proc/sys/net/bridge/bridge-nf-call-ip6tables文件存在:
sysctl -p /etc/sysctl.d/kubernetes.conf

##设置系统时区
timedatectl set-timezone Asia/Shanghai
timedatectl set-local-rtc 0 #将当前UTC时间写入硬件时钟;

##重启依赖时间的服务
systemctl restart rsyslog
systemctl restart crond

##关闭系统不需要的服务
systemctl stop postfix && systemctl disable postfix
systemctl status postfix

##设置系统日志;centos7默认使用rsyslogd,但建议使用systemd journald;
mkdir /var/log/journal
mkdir /etc/systemd/journald.conf.d
cat > /etc/systemd/journald.conf.d/99-prophet.conf <<eof
[Journal]
#持久化保存到磁盘
Storage=persistent

#压缩历史日志
Compress=yes
SyncIntervalSec=5m
RateLimitInterval=30s
RateLimitBurst=1000

#最大占用空间10G
SystemMaxUse=10G

#单日志文件最大200M
SystemMaxFileSize=200M

#日志保存时间2周
MaxRetentionSec=2week

#不将日志转发到syslog,减轻系统压力
ForwardToSyslog=no
eof
# systemctl status rsyslog.service
systemctl restart systemd-journald.service

##升级系统内核为4.44;centos7.x系统自带的3.10.x内核存在一些bugs,导致运行的docker、ks运行不稳定。
# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org #导入公钥;
# yum -y install https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm #安装内核的repo源;
rpm -Uvh http://mirror.rackspace.com/elrepo/elrepo/el7/x86_64/RPMS/elrepo-release-7.0-4.el7.elrepo.noarch.rpm #安装内核4.X版本的repo源;
# yum list available | grep kernel-lt #查看可以使用的内核版本;默认只有最新版本;
# yum list --showduplicates kernel #查看可以使用的内核版本;默认只有最新版本;
yum --enablerepo=elrepo-kernel install -y kernel-lt #默认安装最新版内核(kernel-lt-5.4.123-1.el7.elrepo.x86_64);#若需安装4.44的lt版本,需指定内核参数;但repo源没有;如何安装4.4.189-1.el7.elrepo.x86_64???
grub2-set-default 'CentOs Linux(kernel-lt-5.4.123-1.el7.elrepo.x86_64) 7 (Core)'  #设置开机从新内核启动;
reboot      #重启后使内核生效;
uname -a    #查看内核是否生效;
# yum -y update #更新全部软件版本;不做会引起K8S集群不稳定工作。更新引起冲突??如何解决???
# yum -y update --skip-broken #可不升级
# grub2-set-default 'CentOs Linux(kernel-lt-5.4.123-1.el7.elrepo.x86_64) 7 (Core)'  #更新后必须再次设置开机从新内核启动;
# reboot      #重启后使内核生效;
# uname -a    #查看内核是否生效;

##kube-proxy开启ipvs的前置条件;kube-proxy主要解决SVC与POD之间的调度关系,LVS可以提高其工作效率,故应开启ipvs变更为LVS调度方式。
modprobe br_netfilter
lsmod |grep br_netfilter
cat > /etc/sysconfig/modules/ipvs.modules <<eof
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4  #无法加载模块???有什么影响么???
eof
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod |grep -e ip_vs -e nf_conntrack_ipv4

##安装docker软件
yum -y install yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum -y install docker-ce
yum list docker-ce

mkdir /etc/docker
##native.cgroupdriver=systemd表示交由systemd进行cgroup执行隔离;日志修改为json-file存储类型,大小为100M;
cat > /etc/docker/daemon.json <<eof
{
    "exec-opts":["native.cgroupdriver=systemd"],
    "log-driver":"json-file",
    "log-opts":{
        "max-size":"100m"
    }
}
eof
mkdir -p /etc/systemd/system/docker.service.d
systemctl daemon-reload && systemctl start docker && systemctl enable docker
systemctl status docker

###设置私有仓库域名没有购买的证书,故应设置insecure-registries表示该仓库是安全的。
vi /etc/docker/daemon.json
~~~~~~~~~
{
    "exec-opts":["native.cgroupdriver=systemd"],
    "log-driver":"json-file",
    "log-opts":{
        "max-size":"100m"
    },
    "insecure-registries": ["https://hub.atguigu.com"]
}
~~~~~~~~~
systemctl restart docker

##
~~~~上传docker-compose组件
mv docker-compose /usr/local/bin/
chmod a+x /usr/local/bin/docker-compose

~~~~上传harbor-offline-installer-v1.2.0.tgz
tar -xzvf harbor-offline-installer-v1.2.0.tgz
mv harbor /usr/local/  #该文件很重要,应迁移到安全目录防止被误删除;
cd /usr/local/harbor/
vi harbor.cfg
~~~~~~
hostname = hub.atguigu.com
ui_url_protocol = https
...
~~~~~~
mkdir -p /data/cert/   #按照配置文件创建证书目录;
cd /data/cert/
openssl genrsa -des3 -out server.key 2048           #生成密钥,密码为root123;openssl生成证书:Key是私用秘钥,
openssl req -new -key server.key -out server.csr    #生成证书;Csr是证书请求文件,用于申请证书。在制作csr文件时,必须使用自己的私钥来签署申,还可以设定一个密钥。国家名CN,省市CQ,组织atguigu,全名hub.atguigu.com,邮箱自定,是否需要修改密码--不修改直接enter即可。
   #crt是CA认证后的证书文,签署人用自己的key给你签署凭证。
cp server.key{,.bak}        #备份私钥,防止丢失;
openssl rsa -in server.key.bak -out server.key  #docker一般采用nginx作为前端,如果证书及密钥中有密码的话,就会出现引导不成功的问题;因此需将server.key中的密码去掉。输入密码root123进行操作。
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt  #生成的server.crt文件;crt是CA认证后的证书文,签署人用自己的key给你签署凭证。
chmod a+x server.crt #修改证书权限;
cd /usr/local/harbor/

./install.sh    #安装harbor;
docker ps -a    #查看启动进程;
docker login https://hub.atguigu.com  #本地测试harbor是否能正常工作,默认账户admin,密码Harbor12345;

二、harbor使用

1、以administrator对宿主机C:WindowsSystem32driversetchosts追加 192.168.66.100 hub.atguigu.com;
2、浏览器https://hub.atguigu.com,默认账户admin,密码Harbor12345;密码可在服务器的harbor.cfg中修改;
3、项目---》项目名称library---》镜像仓库---》推送镜像,其提示为:
  在项目中标记镜像:docker tag SOURCE_IMAGE[:TAG] hub.atguigu.com/library/IMAGE[:TAG]
  推送镜像到当前项目:docker push hub.atguigu.com/library/IMAGE[:TAG]
4、在测试节点(master或node)拉取、标记、推送镜像至harbor;

docker pull alpine
docker images #镜像名称为:alpine:latest
docker tag alpine:latest hub.atguigu.com/library/alpine:latest
docker push hub.atguigu.com/library/alpine:latest
docker pull nginx
docker tag nginx:latest hub.atguigu.com/library/nginx:latest
docker push hub.atguigu.com/library/nginx:latest

5、刷新浏览器,查看镜像是否推送至harbor;
6、更换一个测试节点测试是否能拉取镜像;

  

原文地址:https://www.cnblogs.com/chalon/p/14897563.html