k8s使用Harbor仓库


title: kubeadm安装k8s 1.15版
image: /images/theme/k8s.jpg


前提:
1、安装 k8s 的节点必须是大于 1 核心的 CPU
2、阿里云的repo 和base源

curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

1、设置系统主机名以及host解析

hostnamectl  set-hostname  k8s-master
10.0.0.20 k8s-node01
10.0.0.21 k8s-node02  
10.0.0.10 k8s-master

2、安装依赖包

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

3、设置防火墙为 Iptables 并设置空规则

systemctl  stop firewalld  &&  systemctl  disable firewalld
 yum -y install iptables-services  &&  systemctl  start iptables  &&  systemctl  enable iptables  &&  iptables -F  &&  service iptables save

4、关闭selinux 关闭swap
开启虚拟内存时 如果pod在swap中运行 那么kubernetes出现报错

   swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
   setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

5、调整内核参数,对于 K8S

   cat > kubernetes.conf <<EOF
   net.bridge.bridge-nf-call-iptables=1    #开启网桥模式
   net.bridge.bridge-nf-call-ip6tables=1    #开启网桥模式
   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.ipv6.conf.all.disable_ipv6=1   #关闭ipv6
   net.netfilter.nf_conntrack_max=2310720
   EOF
   cp kubernetes.conf  /etc/sysctl.d/kubernetes.conf  开机调用
   sysctl -p /etc/sysctl.d/kubernetes.conf    立刻生效

6、调整系统时区

   # 设置系统时区为 中国/上海
   timedatectl set-timezone Asia/Shanghai
   # 将当前的 UTC 时间写入硬件时钟
   timedatectl set-local-rtc 0
   # 重启依赖于系统时间的服务
   systemctl restart rsyslog 
   systemctl restart crond

7、关闭系统不需要服务

   systemctl stop postfix && systemctl disable postfix

8、设置 rsyslogd 和 systemd journald
使用日志为 journald 方式

   mkdir /var/log/journal # 持久化保存日志的目录
   mkdir /etc/systemd/journald.conf.d  创建配置文件存放目录

创建journal 的日志配置文件

  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

9、重启服务

  systemctl restart systemd-journald

10、升级系统内核为 4.44

CentOS 7.x 系统自带的 3.10.x 内核存在一些 Bugs,导致运行的 Docker、Kubernetes 不稳定,例如: rpm -Uvh
http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm

升级

   rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
      # 安装完成后检查 /boot/grub2/grub.cfg 中对应内核 menuentry 中是否包含 initrd16 配置,如果没有,再安装
   一次!
   yum --enablerepo=elrepo-kernel install -y kernel-lt
   # 设置开机从新内核启动
   grub2-set-default 'CentOS Linux (4.4.226-1.el7.elrepo.x86_64) 7 (Core)' && reboot
   
   
   uname  -r  

11、kube-proxy开启ipvs的前置条件

修改调度方式为ipvs,解决pod和service的调度方式

  modprobe 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

12、安装 Docker 软件

  yum install -y yum-utils device-mapper-persistent-data lvm2

yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

 yum update -y && yum install -y docker-ce
 systemctl restart  docker  && systemctl  enable  docker
 uname  -r
   ## 创建 /etc/docker 目录
   mkdir /etc/docker
    
   # 配置 daemon.
   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

13、 # 重启docker服务

   systemctl daemon-reload && systemctl restart docker && systemctl enable docker

修改/etc/docker/daemon.json的文件 并且发送到其他节点

{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "insecure-registries":["https://hub.atguigu.com"]
}

 scp  /etc/docker/daemon.json   10.0.0.10:/etc/docker/daemon.json
  scp  /etc/docker/daemon.json   10.0.0.20:/etc/docker/daemon.json
  scp  /etc/docker/daemon.json   10.0.0.21:/etc/docker/daemon.json 

重启docker(所有节点)

systemctl restart docker systemctl restart docker

导入docker-compose文件 并赋予执行权限

  mv docker-compose /usr/local/
  cd /usr/local/
  ll docker-compose 
 mv docker-compose   /usr/local/bin/
  chmod  a+x   /usr/local/bin/docker-compose 

链接:https://pan.baidu.com/s/1H5zIMIoFCzip9T34mEe1mQ
提取码:xdfg
复制这段内容后打开百度网盘手机App,操作更方便哦

导入包,解压并修改文件
链接:https://pan.baidu.com/s/12fmXkfSV7Ar-bNAlHyLINQ
提取码:85q0

tar zxvf  harbor-offline-installer-v1.2.0.tgz 
  vim harbor.cfg 
  修改内容
  hostname = hub.atguigu.com
  ui_url_protocol = https

创建存放证书的目录  
  
  mkdir  /data/cert/
  mkdir  /data/cert/ -p
cd /data/cert/

openssl genrsa -des3  -out server.key 2048

cat server.key 
openssl  req -new  -key  server.key  -oout  server.csr
 openssl  req -new  -key  server.key  -out  server.csr
cp  server.key  server.key.org
openssl rsa -in server.key.org  -out  server.key
openssl  x509 -req -days  365  -in server.csr -signkey server.key -out server.crt
chmod a+x *
./install.sh  执行脚本

等到脚本执行完成 在本地win中添加解析

10.0.0.100 hub.atguigu.com

docker登录测试

[root@localhost harbor]# docker login  https://hub.atguigu.com
Username: admin
Password: 
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

Login Succeeded
密码:
Harbor12345

在这里插入图片描述
(哔哩哔哩观看尚硅谷视频学习时写的博客)

docker pull wangyanglinux/myapp:v1
下载一个镜像(汪洋的)
push到harbor中

docker tag  wangyanglinux/myapp:v1  hub.atguigu.com/library/myapp:v1 
docker push   hub.atguigu.com/library/myapp:v1

在这里插入图片描述

在kubernetes中启动一个deployment进行测试镜像是否能被调用
命令行方式启动一个deployment

kubectl   run nginx-deployment  --image=hub.atguigu.com/library/myapp:v1 --port=80 --replicas=1
 kubectl   run 
nginx-deployment    指定deployment的名字
--image=hub.atguigu.com/library/myapp:v1   指定使用的镜像
--port=80   指定暴露的端口(其实不指定也没事;因为是在扁平化的网络中)
--replicas=1   指定副本数为1个
查看deployment
[root@k8s-master ~]# kubectl  get deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   0/1     1            0           16s
查看rs (deployment 通过rs控制pod)
[root@k8s-master ~]# kubectl  get rs
NAME                         DESIRED   CURRENT   READY   AGE
nginx-deployment-85756b779   1         1         1       23s
[root@k8s-master ~]# 
查看pod
[root@k8s-master ~]# kubectl  get pod
NAME                               READY   STATUS    RESTARTS   AGE
nginx-deployment-85756b779-bqjqg   1/1     Running   0          29s
[root@k8s-master ~]# kubectl  get pod -o wide
NAME                               READY   STATUS    RESTARTS   AGE   IP           NODE         NOMINATED NODE   READINESS GATES
nginx-deployment-85756b779-bqjqg   1/1     Running   0          41s   10.244.2.2   k8s-node02   <none>           <none>
访问一下 (通过扁平化的网络方式)
[root@k8s-master ~]# curl 10.244.2.2
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master ~]# curl 10.244.2.2/hostname.html
nginx-deployment-85756b779-bqjqg
[root@k8s-master ~]# 
命令行方式添加deployment的副本数
kubectl  scale --replicas=3 deployment/nginx-deployment

如何访问这三个nginx的pod
通过svc
创建一个svc

kubectl   expose  deployment  nginx-deployment  --port=30000 --target-port=80
kubectl   expose  
deployment   指定类型为deployment
nginx-deployment    指定deployment的名字
--port=30000   指定宿主机的端口
--target-port=80   指定pod的端口 
创建svc
[root@k8s-master ~]# kubectl   expose  deployment  nginx-deployment  --port=30000 --target-port=80
service/nginx-deployment exposed
查看svc的地址
[root@k8s-master ~]# kubectl  get  svc  -o wide
NAME               TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)     AGE    SELECTOR
kubernetes         ClusterIP   10.96.0.1       <none>        443/TCP     7h6m   <none>
nginx-deployment   ClusterIP   10.105.52.122   <none>        30000/TCP   2m4s   run=nginx-deployment
访问测试
[root@k8s-master ~]# curl 10.105.52.122:30000
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master ~]# 
默认轮询的方式进行负载

查看地址规则

yum install -y  ipvsadm-1.27-8.el7.x86_64
[root@k8s-master ~]# ipvsadm -Ln |grep  10.105.52.122 
TCP  10.105.52.122:30000 rr

调度的机制是使用调度ipvs模块来实现负载均衡

这是内部地址 不能被外部访问

[root@k8s-master ~]# kubectl  get  svc
NAME               TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)     AGE
kubernetes         ClusterIP   10.96.0.1       <none>        443/TCP     7h14m
nginx-deployment   ClusterIP   10.105.52.122   <none>        30000/TCP   9m50s

修改svc的方式为

kubectl   edit  svc  nginx-deployment
修改  ClusterIP    方式为  NodePort
  type: NodePort
[root@k8s-master ~]# kubectl  get  svc
NAME               TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)           AGE
kubernetes         ClusterIP   10.96.0.1       <none>        443/TCP           7h17m
nginx-deployment   NodePort    10.105.52.122   <none>        30000:30048/TCP   12m

查看端口

[root@k8s-master ~]# netstat  -anpt |grep  :30048
tcp6       0      0 :::30048                :::*                    LISTEN      100907/kube-proxy   
[root@k8s-master ~]# 

所有节点都暴露这个端口 30048

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章