在一些公司安装K8S集群或者rancher等软件,都是没有网络的,在这种情况下,需要自己想办法安装!这里给大家介绍在没有网络的情况下,怎么安装rancher和K8S集群,最后在用rancher管理K8S集群部署服务!这里讲解1台机器安装rancher和K8S,然后在部署服务

rancher2.4和K8S离线安装包下载地址:
链接:https://pan.baidu.com/s/1eM40irs3JKprcwroPqh9Zw?pwd=MAQQ 提取码:MAQQ
–来自百度网盘的分享

docker离线安装包下载:

条件:

1台机器linux(centos7系统),没有网络不能连接外网

1.基础信息性能优化

hostnamectl set-hostname master &&  bash
cat >> /etc/hosts << EOF
192.168.0.3 master
EOF
systemctl stop firewalld && systemctl disable firewalld  # 关闭防火墙
sed -i 's/enforcing/disabled/' /etc/selinux/config  && setenforce 0  #关闭selinux
swapoff -a && sed -i 's/.*swap.*/#&/' /etc/fstab ##关闭swap
##将桥接的IPv4 流量传递到iptables 的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system # 生效

2.离线安装docker

2.1 上传docker离线安装包
docker-19.03.1.tar.gz
docker-20.10.2.tgz
docker.service
docker-compose-Linux-x86_64

2.2 解压 (两个版本根据自己选择一种就可以了)
tar -xvf docker-19.03.9.tgz
tar -xvf docker-20.10.2.tgz

2.3 将解压出来的docker文件内容移动到 /usr/bin/ 目录下

cp docker/*  /usr/bin/ #复制解压下的二进制文件到bin下

2.4 将docker注册为service

cp  docker.service /etc/systemd/system/docker.service

2.5 启动

chmod +x /etc/systemd/system/docker.service 
systemctl daemon-reload
systemctl start docker
systemctl enable docker.service

2.6 安装docker-compose

sudo cp docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

cat > /etc/docker/daemon.json << EOF
{
  "registry-mirrors": ["https://qj799ren.mirror.aliyuncs.com"],
   "insecure-registries": ["192.168.0.3:5000"],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
EOF

systemctl restart docker  && systemctl status docker

3.安装仓库

注意:这里是关键,所有需要的镜像都是离线导入到系统的,不然安装会失败!

cd /opt #代码上传到这里
mkdir -p /opt/docker
docker load -i registry.tar.gz
tar zxvf docker-registry.tar.gz -C /opt
rm -fr registry.tar.gz docker-registry.tar.gz
docker run -d -p 5000:5000 --restart=always --name registry -v /opt/docker-registry:/var/lib/registry registry:2

4.安装rancher2.4

#启动rancher服务,172.17.0.1:5000是内网的仓库地址,请修改
docker run -d --restart=unless-stopped \
  -p 8080:80 -p 8443:443 \
  -e CATTLE_SYSTEM_DEFAULT_REGISTRY=192.168.0.3:5000 \
  -e CATTLE_SYSTEM_CATALOG=bundled \
  -v /opt/rancher:/var/lib/rancher \
  --name rancher2 192.168.0.3:5000/rancher/rancher:v2.4.17

5. 登录rancher

稍等几分钟访问
我是华为云服务器内网是192.168.0.3,外网是123.249.36.136
所以需要用外网访问:https://123.249.36.136:8443

5.1 设置密码

用户是:admin

k8s集群部署ingress nginx k8s集群部署rancher_离线

5.2 设置IP(有公网设置公网IP,无公网就设置为本地IP)

k8s集群部署ingress nginx k8s集群部署rancher_离线_02


5.3 选择中文页面

k8s集群部署ingress nginx k8s集群部署rancher_离线_03

k8s集群部署ingress nginx k8s集群部署rancher_docker_04

6. 加入节点

k8s集群部署ingress nginx k8s集群部署rancher_离线_05

k8s集群部署ingress nginx k8s集群部署rancher_nginx_06

k8s集群部署ingress nginx k8s集群部署rancher_nginx_07

k8s集群部署ingress nginx k8s集群部署rancher_离线_08


k8s集群部署ingress nginx k8s集群部署rancher_docker_09

7. 加入成功后

k8s集群部署ingress nginx k8s集群部署rancher_离线_10

k8s集群部署ingress nginx k8s集群部署rancher_kubernetes_11

8. 离线部署nginx服务

8.1 利用yaml离线部署

k8s集群部署ingress nginx k8s集群部署rancher_离线_12


k8s集群部署ingress nginx k8s集群部署rancher_kubernetes_13

这里的master主机的hostname不要在意,我是第二次修改博客,没有变更,但是环境条件是一致的,保证没有网络,是离线的部署和测试。

k8s集群部署ingress nginx k8s集群部署rancher_kubernetes_14


注意这里需要用vi编辑,直接不能写入

#由于是离线,需要提前导入nginx的镜像,我这里提前做过了就过了
cat > nginx.yaml << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx
  name: nginx
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
EOF

k8s集群部署ingress nginx k8s集群部署rancher_kubernetes_15


在rancher界面也看到了

k8s集群部署ingress nginx k8s集群部署rancher_linux_16


k8s集群部署ingress nginx k8s集群部署rancher_linux_17

8.2 利用rancher离线部署httpd

需要提前把httpd的镜像准备好,我这里已经准备过了

k8s集群部署ingress nginx k8s集群部署rancher_docker_18


k8s集群部署ingress nginx k8s集群部署rancher_linux_19


k8s集群部署ingress nginx k8s集群部署rancher_nginx_20

k8s集群部署ingress nginx k8s集群部署rancher_nginx_21

欢迎在使用中有任何问题可以给我留言,我随时回复大家!