k8s 1.18版本
单master 两个节点 2核4G 20GB
192.168.83.134 | 主节点 master |
---|---|
192.168.83.135 | node1 |
192.168.83.130 | node2 |
免密登录设置
ssh-keygen -t rsa ssh-copy-id 172.17.68.221
1.基础设置
1.1 关闭防火墙 selinux swap
shell
# 关闭防火墙 全部操作
systemctl stop firewalld
systemctl disable firewalld
# 关闭selinux 永久关闭
sed -i 's/enforcing/disabled/' /etc/selinux/config
# 关闭 swap
swapoff -a # 临时关闭
sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久关闭
# 设置主机名称
# 192.168.83.134
hostnamectl set-hostname master
#192.168.83.135
hostnamectl set-hostname node1
#192.168.83.130
hostnamectl set-hostname node2
1.2 添加 hosts
shell
cat >> /etc/hosts << EOF
192.168.83.134 master
192.168.83.135 node1
192.168.31.130 node2
EOF
1.3 IPv4 流量传递到 iptables
shell
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
#
sysctl --system
1.4 时间同步
shell
yum install ntpdate -y
ntpdate time.windows.com
1.5 docker 安装
shell
# wget
yum install wget -y
#
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
# install
yum -y install docker-ce-18.06.1.ce-3.el7
#
mkdir -p /etc/docker
# docker 加速
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://m38560ti.mirror.aliyuncs.com"]
}
EOF
# 开机启动
systemctl enable docker && systemctl start docker
#
docker --version
1.6 kubernetes 镜像添加
shell
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
# 安装工具 指定版本
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
# 开机启动
systemctl enable kubelet
1.7 kubernetes 设置
shell
# --apiserver-advertise-address master ip 其余不动
kubeadm init --apiserver-advertise-address=192.168.83.134 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.18.0 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16
# 在主节点设置如下信息
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# 记下 命令行语句
# 查看集群节点
kubectl get nodes
-- 创建token 防止 token 过期
kubeadm token create --print-join-command
shell
# 从节点服务器上执行如下 kubeadm join master ip
kubeadm join 192.168.83.134:6443 --token tydnw0.7mzn9hwi928a2f0a --discovery-token-ca-cert-hash sha256:1116605c731e1ed3840a097397ab3630ac4de6f2c1c3deacc22ef1665e6f75f8
1.8 配置网络插件
shell
# 下载插件
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# 执行网络插件应用
kubectl apply -f kube-flannel.yml
# 查看pods
kubectl get pods -n kube-system
# 查看 ndoes
kubectl get nodes
实例
shell
# 创建nginx
kubectl create deployment nginx --image=nginx
# 暴露端口
kubectl expose deployment nginx --port=80 --type=NodePort
# 查看 端口 暴露的
kubectl get pod,svc
# 可以在从节点 任意节点 通过 http://nodeIp:port/ 访问
1.9 k8s 更新证书
sh
## 1.18 版本的 k8s 检查有效期
kubeadm alpha certs check-expiration
## 更新证书有效期
kubeadm alpha certs renew all