侧边栏壁纸
博主头像
进击的码农博主等级

新年新气象,开搞开搞

  • 累计撰写 32 篇文章
  • 累计创建 11 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

记一次没完成的K8S实践

wosperry
2021-07-08 / 0 评论 / 0 点赞 / 9 阅读 / 10899 字
#####################################################################################
# 配置 ----------------------------------------------------------START
# 主节点
HOST="192.168.2.151"
HOST_NAME="master"
# 从节点 
declare -A SLAVES
SLAVES=(
[slave1]="192.168.2.152" 
[slave2]="192.168.2.153"
)
# 如果是Harbor,则是配置的Host
DOCKERREPOHOST="wosperry.com.cn:8704"  #必须先 docker login 192.168.124.5 登录自己仓库#
DOCKERREPONAME="google_containers"
# 配置 -----------------------------------------------------------END
#####################################################################################

# 随便新建一个文件夹 -----------------------------------------------START
mkdir /k8s
cd /k8s
# 随便新建一个文件夹 -----------------------------------------------END


# 同步时间 -------------------------------------------------------START
yum -y install chrony
systemctl start chronyd && systemctl enable chronyd
chronyc sources
# 同步时间 -------------------------------------------------------END


# 设置 Host Name ------------------------------------------------START
hostnamectl set-hostname $HOST_NAME
# 设置 Host Name ------------------------------------------------END


# 调整 hosts ----------------------------------------------------START
TMP=""
TMP=`echo -e "${TMP}\n${HOST} ${HOST_NAME}"`
for key in $(echo ${!SLAVES[*]})
do 
  TMP=`echo -e "${TMP}\n${SLAVES[$key]} ${key}"`
done
cat << EOF >> /etc/hosts
${TMP}
EOF
# 查看HOST
cat /etc/hosts
# 调整 hosts ----------------------------------------------------END


# 禁用SELinux和Swap ---------------------------------------------START
# 关闭swap分区【虚拟内存】并且永久关闭虚拟内存
swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
# 关闭selinux
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
# 禁用SELinux和Swap ----------------------------------------------END


# 安装依赖软件 ---------------------------------------------------START
yum install -y conntrack ipvsadm ipset jq iptables curl sysstat libseccomp wget vim net-tools git iproute lrzsz bash-completion tree nfs-utils unzip bind-utils gcc
# 安装依赖软件 ---------------------------------------------------END


# 放行端口 ------------------------------------------------------START
firewall-cmd --zone=public --add-port=6443/tcp --permanent
firewall-cmd --zone=public --add-port=2379-2380/tcp --permanent
firewall-cmd --zone=public --add-port=10250-10252/tcp --permanent
firewall-cmd --zone=public --add-port=30000-32767/tcp --permanent
# 重启
firewall-cmd --reload 
# 放行端口 ------------------------------------------------------END
# 置空iptables --------------------------------------------------START
yum -y install iptables-services && systemctl start iptables && systemctl enable iptables && iptables -F && service iptables save
# 置空iptables --------------------------------------------------END

# 安装 docker-ce ------------------------------------------------START
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce && systemctl enable docker && systemctl start docker
docker --version
# 安装 docker-ce ------------------------------------------------END


# 配置 /etc/docker/daemon.json ----------------------------------START
mkdir -p /etc/docker 

cat > /etc/docker/daemon.json <<EOF
{
  "registry-mirrors": [
    "https://4534gx72.mirror.aliyuncs.com",
    "https://1nj0zren.mirror.aliyuncs.com",
    "https://docker.mirrors.ustc.edu.cn",
    "http://f1361db2.m.daocloud.io",
    "https://registry.docker-cn.com"
  ],
  "insecure-registries": [
    "$DOCKERREPOHOST"
  ]
}
EOF

systemctl daemon-reload && systemctl restart docker 
# 配置 /etc/docker/daemon.json ----------------------------------END


# 调整内核参数 ---------------------------------------------------START
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
vm.overcommit_memory=1
vm.panic_on_oom=0
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
net.netfilter.nf_conntrack_max=2310720
EOF

# 将优化内核文件拷贝到/etc/sysctl.d/文件夹下,这样优化文件开机的时候能够被调用
cp kubernetes.conf /etc/sysctl.d/kubernetes.conf 
# 手动刷新,让优化文件立即生效
sysctl -p /etc/sysctl.d/kubernetes.conf
# 调整内核参数 ---------------------------------------------------END


# 创建路径 ------------------------------------------------------START
# 创建保存日志的目录
mkdir /var/log/journal
# 创建配置文件存放目录
mkdir /etc/systemd/journald.conf.d
# 创建路径 ------------------------------------------------------END

# 创建配置文件 ---------------------------------------------------START
cat > /etc/systemd/journald.conf.d/99-prophet.conf <<EOF
[Journal]
Storage=persistent
Compress=yes
SyncIntervalSec=5m
RateLimitInterval=30s
RateLimitBurst=1000
SystemMaxUse=10G
SystemMaxFileSize=200M
MaxRetentionSec=2week
ForwardToSyslog=no
EOF
# 创建配置文件 ---------------------------------------------------END


# 重启systemd journald 的配置 ------------------------------------START
systemctl restart systemd-journald
# 重启systemd journald 的配置 ------------------------------------END


# 打开文件数调整 --------------------------------------------------START
echo "* soft nofile 65536" >> /etc/security/limits.conf
echo "* hard nofile 65536" >> /etc/security/limits.conf
# 打开文件数调整 --------------------------------------------------END


# kube-proxy 开启 ipvs 前置条件 -----------------------------------START
modprobe br_netfilter
 
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF
# 使用lsmod命令查看这些文件是否被引导
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4
# kube-proxy 开启 ipvs 前置条件 -----------------------------------END 


# 国内镜像配置(国内建议配置) ----------------------------------------START
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=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
# 国内镜像配置(国内建议配置) ----------------------------------------END


# 安装Kubelet ----------------------------------------------------START
yum -y install kubelet kubectl kubeadm
systemctl enable kubelet && systemctl start kubelet
# 安装Kubelet ----------------------------------------------------END


# 以下私服拉取镜像,不一定要这样执行,可以尝试 
# kubeadm config print init-defaults > kubeadm-init.yaml
# 生成的文件
# 先尝试 kubeadm config images pull --config kubeadm-init.yaml
# 直接拉取镜像试试,如果不行再走私服
# 我遇到装不了的情况,仓库的coredns的tag不带v,而使用的带v


# 不使用生成的文件
# kubeadm config print init-defaults > kubeadm-init.yaml
# 创建 kubeadm-init.yaml------------------------------------------START


####################################################################################################
# 私服开始
####################################################################################################
cat << EOF > kubeadm-init.yaml
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: ${HOST}
  bindPort: 6443
nodeRegistration:
  criSocket: /var/run/dockershim.sock
  name: master
  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: ${DOCKERREPOHOST}/${DOCKERREPONAME}
kind: ClusterConfiguration
kubernetesVersion: v1.21.0
networking:
  dnsDomain: cluster.local
  serviceSubnet: 10.96.0.0/12
scheduler: {}
EOF

# 创建 kubeadm-init.yaml------------------------------------------END


docker login $DOCKERREPOHOST

# 拉取镜像并推送到自己仓库 -------------------------------------------START

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.21.0 
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.21.0 ${DOCKERREPOHOST}/${DOCKERREPONAME}/kube-apiserver:v1.21.0 
docker push ${DOCKERREPOHOST}/${DOCKERREPONAME}/kube-apiserver:v1.21.0

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.21.0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.21.0 ${DOCKERREPOHOST}/${DOCKERREPONAME}/kube-controller-manager:v1.21.0 
docker push ${DOCKERREPOHOST}/${DOCKERREPONAME}/kube-controller-manager:v1.21.0

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.21.0 
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.21.0 ${DOCKERREPOHOST}/${DOCKERREPONAME}/kube-scheduler:v1.21.0 
docker push ${DOCKERREPOHOST}/${DOCKERREPONAME}/kube-scheduler:v1.21.0

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.21.0 
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.21.0 ${DOCKERREPOHOST}/${DOCKERREPONAME}/kube-proxy:v1.21.0 
docker push ${DOCKERREPOHOST}/${DOCKERREPONAME}/kube-proxy:v1.21.0
kubeadm config images pull --config kubeadm-init.yaml

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0 
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0 ${DOCKERREPOHOST}/${DOCKERREPONAME}/etcd:3.4.13-0 
docker push ${DOCKERREPOHOST}/${DOCKERREPONAME}/etcd:3.4.13-0

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 ${DOCKERREPOHOST}/${DOCKERREPONAME}/pause:3.4.1 
docker push ${DOCKERREPOHOST}/${DOCKERREPONAME}/pause:3.4.1

docker pull coredns/coredns:latest
docker tag coredns/coredns:latest ${DOCKERREPOHOST}/${DOCKERREPONAME}/coredns:v1.8.0 
docker push ${DOCKERREPOHOST}/${DOCKERREPONAME}/coredns:v1.8.0
 
# 拉取镜像并推送到自己仓库 -------------------------------------------END

####################################################################################################
# 私服结束
####################################################################################################
kubeadm config images pull --config kubeadm-init.yaml

#--kubernetes-version="v1.21.0" --pod-network-cidr=10.0.244.0/16 
#--image-repository ${DOCKERREPOHOST}/${DOCKERREPONAME}/google_containers 

kubeadm init --config kubeadm-init.yaml

kubectl get nodes

0

评论区