K8S环境一键部署脚本,无坑版

 一、安装基础环境

#!/bin/bash
yum -y install wget
yum -y install vim
yum -y install net-tools
#关闭交换分区
swapoff -a
sed -i '/swap/s/^/#/' /etc/fstab
#关闭selinux
setenforce 0
sed -ri '/^SELINUX=/s/SELINUX=.+/SELINUX=disabled/' /etc/selinux/config
#关闭系统防火墙
systemctl stop firewalld
systemctl disable firewalld
#开启转发
cd /etc/
cat >>sysctl.conf<<EOF
net.ipv4.ip_forward = 1
EOF
#启用br_netfilter模块
modprobe br_netfilter
#配置内核参数
echo -e 'net.bridge.bridge-nf-call-ip6tables = 1\nnet.bridge.bridge-nf-call-iptables = 1' > /etc/sysctl.d/k8s.conf
sysctl --system
#安装时间同步服务
yum install -y chrony
rm -rf /etc/chrony.conf
cd /etc
cat >>chrony.conf<<EOF
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
server ntp4.aliyun.com iburst
EOF
systemctl start chronyd
systemctl enable chronyd
#启用IPVS模块
modprobe -- ip_vs;modprobe -- ip_vs_rr;modprobe -- ip_vs_wrr;modprobe -- ip_vs_sh;modprobe -- nf_conntrack_ipv4
#安装IPSET和IPVSADM
yum install -y ipset ipvsadm
#重启服务器是保证IPVS模块启用
cd /usr/lib/systemd/system/
rm -rf /usr/lib/systemd/system/containerd.service
cat >>containerd.service<<EOF
#   Copyright 2018-2020 Docker Inc.

#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at

#       http://www.apache.org/licenses/LICENSE-2.0

#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target

[Service]
ExecStartPre=-/sbin/modprobe overlay
ExecStartPre=-/sbin/modprobe ip_vs
ExecStartPre=-/sbin/modprobe ip_vs_rr
ExecStartPre=-/sbin/modprobe ip_vs_wrr
ExecStartPre=-/sbin/modprobe ip_vs_sh
ExecStartPre=-/sbin/modprobe nf_conntrack_ipv4
ExecStart=/usr/bin/containerd
KillMode=process
Delegate=yes
LimitNOFILE=1048576
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
#安装DOCKER镜像源
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
#安装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 install -y docker-ce-18.09.7
#启动DOCKER
systemctl enable docker
systemctl start docker
#配置镜像下载仓库
cd /etc/docker/
cat >>daemon.json<<EOF
{
"registry-mirrors": ["https://i4xomte7.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
#安装K8S工作节点
yum install -y kubeadm-1.17.14 kubelet-1.17.14 kubectl-1.17.14
#启动K8S工作节点
systemctl enable kubelet
#加入主节点
#每次初始化生成的token不同,自行更改添加

 二、初始化集群

单节点:

kubeadm init  --apiserver-advertise-address=10.0.1.203  --image-repository registry.aliyuncs.com/google_containers  --kubernetes-version v1.17.14  --service-cidr=10.96.0.0/12  --pod-network-cidr=10.244.0.0/16

注:--apiserver-advertise-address= 是指定master主机IP

多节点:

1.方法一
kubeadm init --control-plane-endpoint "10.4.7.59:6443" --pod-network-cidr 172.16.0.0/16 --service-cidr 10.96.0.0/16  --image-repository registry.aliyuncs.com/google_containers --upload-cert


2.方法二
[root@localhost~]#vim kubeadm-config.yml         #创建初始化文件
apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
  - system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef
  ttl: 24h0m0s
  usages:
  - signing
  - authentication
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: 1.2.3.4
  bindPort: 6443
nodeRegistration:
  criSocket: /var/run/dockershim.sock
  name: wq126
  taints:
  - effect: NoSchedule
    key: node-role.kubernetes.io/master
---
apiServer:
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns:
  type: CoreDNS
etcd:
  local:
    dataDir: /var/lib/etcd
imageRepository: k8s.gcr.io
kind: ClusterConfiguration
kubernetesVersion: v1.17.0
networking:
  dnsDomain: cluster.local
  serviceSubnet: 10.96.0.0/12
scheduler: {}






[root@localhost~]# kubeadm init --config=kubeadm-config.yml --experimental-upload-certs | tee kubeadm-init.log            #开始初始化,中途会下载镜像时间较长耐心等待


加入工作节点和管理节点的命令在初始化日志里面,日志文件为kubeadm-init.log



注:初始化文件可用
kubeadm config print init-defaults > kubeadm-config.yml  #生成初始化文件

三、部署GUI界面

方法一:

[root@localhost~]# kubectl apply -f https://addons.kuboard.cn/kuboard/kuboard-v3.yaml

访问:http://your-node-ip-address:30080

                用户:admin        密码:Kuboard123

卸载GUI:

kubectl delete -f https://addons.kuboard.cn/kuboard/kuboard-v3.yaml
rm -rf /usr/share/kuboard

方法二:

        部署kubedashboard:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml


kubectl  patch svc kubernetes-dashboard -n kubernetes-dashboard -p '{"spec":{"type":"NodePort","ports":[{"port":443,"targetPort":8443,"nodePort":30443}]}}'

        配置登录用户:

[root@localhost~]# cat > dashboard-adminuser.yaml << EOF
apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kubernetes-dashboard

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: admin-user
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: admin-user
  namespace: kubernetes-dashboard  
EOF


[root@localhost~]# kubectl apply -f dashboard-adminuser.yaml   #应用创建用户的文件

[root@localhost~]# kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')

 访问dashboard:        https://<any_node_ip>:30443

使用上面生成的token登录

卸载K8S:

kubeadm reset -f
modprobe -r ipip
lsmod
rm -rf ~/.kube/
rm -rf /etc/kubernetes/
rm -rf /etc/systemd/system/kubelet.service.d
rm -rf /etc/systemd/system/kubelet.service
rm -rf /usr/bin/kube*
rm -rf /etc/cni
rm -rf /opt/cni
rm -rf /var/lib/etcd
rm -rf /var/etcd
yum clean all
yum remove kube*

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
k8s离线一键部署Kubesphere需要以下步骤: 1. 准备离线环境:首先需要将Kubesphere部署所需的软件包和依赖项下载到离线环境中。这包括Kubernetes、Helm、ETCD、Metrics Server等等。可以通过访问Kubesphere官网或者软件源网站下载所需软件包。 2. 安装Kubesphere离线包:将下载好的Kubesphere离线包上传至离线环境中,并进行解压。在解压后的目录中,运行安装脚本,如`./install.sh`。这会自动安装和配置Kubesphere所需要的组件和服务。 3. 配置集群信息:一键部署脚本通常会提供一个配置文件,用于指定Kubernetes集群的相关信息。在安装过程中,需要根据实际情况修改配置文件中的参数,例如指定Master和Worker节点的IP地址、集群网络配置、存储类型等等。 4. 启动Kubesphere部署:在配置完成后,执行启动命令如`./install.sh start`,脚本将自动开始部署Kubesphere。可以通过查看日志来跟踪安装进度,如`./install.sh log`。 5. 验证Kubesphere部署:一旦脚本执行完毕,可以使用Kubectl命令来检查Kubesphere是否已经成功部署。运行`kubectl get pods -n kubesphere-system`命令,可以查看Kubesphere系统命名空间中的所有Pod状态。确保所有Pod都处于运行中的状态。 6. 访问Kubesphere控制台:在部署成功后,可以通过浏览器访问通过配置文件指定的URL地址,例如`https://<Your_Kubesphere_IP>`。输入默认的管理员用户名和密码,即可登录到Kubesphere控制台。从控制台中,可以管理和监控Kubernetes集群,创建和管理应用程序等等。 通过以上步骤,就可以在离线环境中使用一键部署脚本部署Kubesphere。这个过程可能因系统和配置而略有差异,但总体流程是相似的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值