IP | 角色 |
---|---|
172.28.247.137 | master |
172.28.247.138 | node1 |
172.28.247.139 | node2 |
k8s1.23.6版本需要使用对应版本的docker,我这边使用的是20.10.8的docker版本
k8s1.23.6版本需要使用对应版本的calico,我这边使用的是3.24.5的calico版
先修改主机名
注意:重启生效
hostnamectl set-hostname k8s-master
hostnamectl set-hostname k8s-node1
hostnamectl set-hostname k8s-node2
执行以下命令,配置/etc/hosts文件
cat >>/etc/hosts << EOF
172.28.247.137 k8s-master
172.28.247.138 k8s-node1
172.28.247.139 k8s-node2
EOF
# 查看配置文件
[root@k8s-master ~]# cat /etc/hosts
172.28.247.137 k8s-master
172.28.247.138 k8s-node1
172.28.247.139 k8s-node2
# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
# 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config
# 关闭交换分区
swapoff -a
# 注释
sed -ri 's/.*swap.*/#&/' /etc/fstab
或者
vim /etc/fstab
#/dev/mapper/centos-swap swap swap defaults 0 0
# 修改及其内核参数
cat >> /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
# 加载配置文件
sysctl -p
# 安装ntp服务器
yum -y install ntpdate
# 同步时间
ntpdate time.windows.com
# 配置docker的源
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
# 配置k8s的源
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
EOF
# 安装20.10.8版本的docker
yum -y install docker-ce-20.10.8
# 设置服务开机启动
systemctl start docker && systemctl enable docker.service
# 配置镜像加速和驱动
cat >> /etc/docker/daemon.json <<EOF
{
"registry-mirrors":["https://rsbud4vc.mirror.aliyuncs.com","https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn","https://dockerhub.azk8s.cn","http://hub-mirror.c.163.com"],
"exec-opts":["native.cgroupdriver=systemd"]
}
EOF
# 重启服务
systemctl daemon-reload && systemctl restart docker
kubeadm:用来初始化k8s集群
kubelet:安装在集群所有节点上,用于启动pod
kubectl:部署和管理应用,查看各种资源、创建、删除和更新各种组件
# 安装软件包
yum -y install kubelet-1.23.6 kubeadm-1.23.6 kubectl-1.23.6
#设置开机自启
systemctl enable kubelet
# 初始化k8s集群
# 第一个ip是master的ip
kubeadm init \
--apiserver-advertise-address=172.28.247.137 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version=v1.23.6 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16
上图表示初始化成功!!
master节点:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
node节点执行
kubeadm join 172.28.247.137:6443 --token y6q8st.gamnt79122wbodjs \
--discovery-token-ca-cert-hash sha256:9ef4266a855545de3837281aff154bdc2704afe385a6c5791c716827c86ac519
# 下载calico.yaml文件
curl -kO https://raw.githubusercontent.com/projectcalico/calico/v3.24.5/manifests/calico.yaml
# 修改calico.yaml文件
找到 CALICO_IPV4POOL_CIDR
- name: CALICO_IPV4POOL_CIDR
value: "10.244.0.0/16"
# 构建应用
kubectl apply -f calico.yaml
[root@k8s-master k8s]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
calico-kube-controllers-7b8458594b-bt9lf 0/1 Pending 0 15s
calico-node-kvh5n 0/1 Init:0/3 0 15s
calico-node-rncn4 0/1 Init:0/3 0 15s
calico-node-wlpkg 0/1 Init:0/3 0 15s
coredns-6d8c4cb4d-857d4 0/1 Pending 0 21m
coredns-6d8c4cb4d-t4vpq 0/1 Pending 0 21m
etcd-k8s-master 1/1 Running 0 21m
kube-apiserver-k8s-master 1/1 Running 0 21m
kube-controller-manager-k8s-master 1/1 Running 0 21m
kube-proxy-4hxjs 1/1 Running 0 21m
kube-proxy-7nbvw 1/1 Running 0 17m
kube-proxy-8f87g 1/1 Running 0 17m
kube-scheduler-k8s-master 1/1 Running 0 21m
[root@k8s-master k8s]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
calico-kube-controllers-7b8458594b-bt9lf 1/1 Running 0 13m
calico-node-kvh5n 1/1 Running 0 13m
calico-node-rncn4 1/1 Running 0 13m
calico-node-wlpkg 1/1 Running 0 13m
coredns-6d8c4cb4d-857d4 1/1 Running 0 34m
coredns-6d8c4cb4d-t4vpq 1/1 Running 0 34m
etcd-k8s-master 1/1 Running 0 34m
kube-apiserver-k8s-master 1/1 Running 0 34m
kube-controller-manager-k8s-master 1/1 Running 0 34m
kube-proxy-4hxjs 1/1 Running 0 34m
kube-proxy-7nbvw 1/1 Running 0 30m
kube-proxy-8f87g 1/1 Running 0 30m
kube-scheduler-k8s-master 1/1 Running 0 34m
[root@k8s-master k8s]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready control-plane,master 35m v1.23.6
k8s-node1 Ready <none> 31m v1.23.6
k8s-node2 Ready <none> 31m v1.23.6
# 创建nginx服务
[root@k8s-master ~]# kubectl create deployment nginx --image=nginx
deployment.apps/nginx created
# 暴露端口
[root@k8s-master ~]# kubectl expose deployment nginx --port=80 --type=NodePort
service/nginx exposed
# 查看pod以及暴露的端口
[root@k8s-master ~]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-85b98978db-7pcvd 1/1 Running 0 52s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 37m
service/nginx NodePort 10.103.83.64 <none> 80:30038/TCP 15s
访问
私网访问
公网访问
注意:云服务器安全组开放端口