角色 | IP | 系统版本 | 主机名 | 安装组件 |
Master | 192.168.40.100 | Centos 7.9 | master1 | apiserver、controller-manager、scheduler、etcd、kube-proxy、docker、 calico |
Node | 192.168.40.101 | Centos 7.9 | node1 | kubelet、kube-proxy、docker、calico、coredns |
[root@localhost ~]# hostnamectl set-hostname master1 && bash
[root@master1 ~]# hostnamectl set-hostname node1 && bash
Master节点
[root@master1 ~]# vim /etc/hosts
#添加如下内容
192.168.40.100 master1
192.168.40.101 node1
Node节点
[root@node1 ~]# vim /etc/hosts
#添加如下内容
192.168.40.100 master1
192.168.40.101 node1
关闭SELinux
#Master节点
[root@master1 ~]# vim /etc/selinux/config
#修改如下内容
SELINUX=disabled
#重启服务器
[root@master1 ~]# reboot
#Node节点
[root@node1 ~]# vim /etc/selinux/config
#修改如下内容
SELINUX=disabled
#重启服务器
关闭防火墙
[root@master1 ~]# systemctl stop firewalld && systemctl disable firewalld
[root@node1 ~]# systemctl stop firewalld && systemctl disable firewalld
关闭swap交换分区
#Master节点
[root@master1 ~]# vim /etc/fstab
#注释下面这一行
#/dev/mapper/centos-swap swap swap defaults 0 0
#重启服务器
[root@master1 ~]# reboot
#Node节点
[root@node1 ~]# vim /etc/fstab
#注释下面这一行
#/dev/mapper/centos-swap swap swap defaults 0 0
#重启服务器
[root@node1 ~]# reboot
#加载 br_netfilter 内核模块
[root@master1 ~]# modprobe br_netfilter
[root@master1 ~]# echo "modprobe br_netfilter" >> /etc/profile
[root@master1 ~]# vim /etc/sysctl.d/k8s.conf
#添加以下内容
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
#加载配置参数
[root@master1 ~]# sysctl -p /etc/sysctl.d/k8s.conf
#加载 br_netfilter 内核模块
[root@node1 ~]# modprobe br_netfilter
[root@node1 ~]# echo "modprobe br_netfilter" >> /etc/profile
[root@node1 ~]# vim /etc/sysctl.d/k8s.conf
#添加以下内容
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
#加载配置参数
[root@node1 ~]# sysctl -p /etc/sysctl.d/k8s.conf
安装 yum-utils
软件包,添加镜像源
[root@master1 ~]# yum install yum-utils -y
[root@master1 ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@master1 ~]# vim /etc/yum.repos.d/kubernetes.repo
#添加如下内容
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
[root@node1 ~]# yum install yum-utils -y
[root@node1 ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@node1 ~]# vim /etc/yum.repos.d/kubernetes.repo
#添加如下内容
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
7、开启IPVS
[root@master1 ~]# vim /etc/sysconfig/modules/ipvs.modules
#添加如下内容
#!/bin/bash
ipvs_modules="ip_vs ip_vs_lc ip_vs_wlc ip_vs_rr ip_vs_wrr ip_vs_lblc ip_vs_lblcr ip_vs_dh ip_vs_sh ip_vs_nq ip_vs_sed ip_vs_ftp nf_conntrack"
for kernel_module in ${ipvs_modules}; do
/sbin/modinfo -F filename ${kernel_module} > /dev/null 2>&1
if [ 0 -eq 0 ]; then
/sbin/modprobe ${kernel_module}
fi
done
[root@master1 ~]# bash /etc/sysconfig/modules/ipvs.modules
[root@node1 ~]# vim /etc/sysconfig/modules/ipvs.modules
#添加如下内容
#!/bin/bash
ipvs_modules="ip_vs ip_vs_lc ip_vs_wlc ip_vs_rr ip_vs_wrr ip_vs_lblc ip_vs_lblcr ip_vs_dh ip_vs_sh ip_vs_nq ip_vs_sed ip_vs_ftp nf_conntrack"
for kernel_module in ${ipvs_modules}; do
/sbin/modinfo -F filename ${kernel_module} > /dev/null 2>&1
if [ 0 -eq 0 ]; then
/sbin/modprobe ${kernel_module}
fi
done
[root@node1 ~]# bash /etc/sysconfig/modules/ipvs.modules
#安装docker
[root@master1 ~]# yum install docker-ce -y
#启动docker并设置开机自启动
[root@master1 ~]# systemctl start docker && systemctl enable docker.service
#添加docker镜像加速器
[root@master1 ~]# tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors": ["https://ffunzkst.mirror.aliyuncs.com"]
> }
> EOF
#重载配置并重新启动
[root@master1 ~]# systemctl daemon-reload && systemctl restart docker && systemctl status docker
#安装docker
[root@node1 ~]# yum install docker-ce -y
#启动docker并设置开机自启动
[root@node1 ~]# systemctl start docker && systemctl enable docker.service
#添加docker镜像加速器
[root@node1 ~]# tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors": ["https://ffunzkst.mirror.aliyuncs.com"]
> }
> EOF
#重载配置并重新启动
[root@node1 ~]# systemctl daemon-reload && systemctl restart docker && systemctl status docker
#安装软件包
[root@master1 ~]# yum install -y kubelet-1.23.1 kubeadm-1.23.1 kubectl-1.23.1
#为kubelet设置开机自启动
[root@master1 ~]# systemctl enable kubelet
#安装软件包
[root@node1 ~]# yum install -y kubelet-1.23.1 kubeadm-1.23.1 kubectl-1.23.1
#为kubelet设置开机自启动
[root@node1 ~]# systemctl enable kubelet
#加载镜像
[root@master1 ~]# docker load -i k8s-images-v1.23.1.tar.gz
[root@node1 ~]# docker load -i k8s-images-v1.23.1.tar.gz
#初始化集群
[root@master1 ~]# kubeadm init --kubernetes-version=1.23.1 --apiserver-advertise-address=192.168.40.100 --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=SystemVerification
#执行以下命令
[root@master1 ~]# mkdir -p $HOME/.kube
[root@master1 ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@master1 ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
#查看集群
[root@master1 ~]# kubectl get node
#打印加入k8s集群的命令
[root@master1 ~]# kubeadm token create --print-join-command
#复制命令到node节点执行
[root@node1 ~]# kubeadm join 192.168.40.100:6443 --token dc41lr.7cxmkrsrt4l17tcm --discovery-token-ca-cert-hash sha256:b619364d63e2d21f4a27d7120e9222a82e1511cf7bf6f3808d78b5c672e3d790
#回到master节点,查看node节点是否加入到集群中
[root@master1 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
master1 NotReady control-plane,master 5m35s v1.23.1
node1 NotReady 30s v1.23.1
#修改Node节点的role
[root@master1 ~]# kubectl label node node1 node-role.kubernetes.io/worker=worker
#查看修改结果
[root@master1 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
master1 NotReady control-plane,master 7m31s v1.23.1
node1 NotReady worker 2m26s v1.23.1
上传calico的yaml文件,文件可以到k8s官网去下载
#创建资源
[root@master1 ~]# kubectl apply -f calico.yaml
#查看Pod状态,如果全部为running,则安装成功
[root@master1 ~]# kubectl get pod -n kube-system -owide
#在控制节点上,创建一个yaml文件
[root@master1 ~]# vim deploy.yaml
#添加如下内容
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
#创建资源
[root@master1 ~]# kubectl apply -f deploy.yaml
#查看Pod状态,首次创建需要拉取镜像,要等一会状态才会为running
[root@master1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-9456bbbf9-n7z66 1/1 Running 0 16s
#在控制节点创建一个service的yaml文件
[root@master1 ~]# vim service.yaml
#添加如下内容
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: NodePort
selector:
app: nginx
ports:
- port: 80
targetPort: 80
nodePort: 30080
#创建service
[root@master1 ~]# kubectl apply -f service.yaml
#验证结果
[root@master1 ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 21m
my-service NodePort 10.108.221.141 80:30080/TCP 7s
在浏览器地址栏输入http://192.168.40.100:30080,如果能访问到nginx,则服务启动成功
至此,k8s集群安装完成