基于centos7.9安装k8s
centos7.9安装k8s
·
centos7.9安装k8s
说明:
本文档仅供学习参考使用
1.基础准备
1.1实验配置信息
具体配置信息根据需求设置
系统版本 | 主机名 | 配置 | ip |
---|---|---|---|
centos7.9 | master | 2C+4G+30G | 192.168.10.110 |
centos7.9 | node1 | 2C+4G+30G | 192.168.10.111 |
centos7.9 | node2 | 2C+4G+30G | 192.168.10.112 |
1.2 系统初始化设置
1.2.1 固定ip配置
[root@master ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
1.2.2 设置主机名
#注意所有节点都设置,不能重名
[root@node2 ~]# hostnamectl set-hostname master
1.2.3 常用工具安装
yum install -y wget vim net-toos chrony
1.2.4 各节点时间同步设置
systemctl enable chronyd
systemctl restart chronyd
systemctl status chronyd
chronyc sources -v
timedatectl set-timezone Asia/Shanghai
timedatectl status
date
1.2.5 禁用selinux
# 将 SELinux 设置为 permissive 模式(相当于将其禁用)
sudo setenforce 0
vim /etc/selinux/config
#永久关闭需将/etc/selinux/config 中的SELINUX的值改为disabled
1.2.6 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
systemctl status firewalld
1.2.7 关闭swap
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
free
1.2.8 配置master的ssh免密登录
为了保险三台服务器都配上
echo "192.168.10.50 k8smaster" >> /etc/hosts
echo "192.168.10.51 node1" >> /etc/hosts
echo "192.168.10.52 node2" >> /etc/hosts
仅master节点操作
ssh-keygen -t rsa
ssh-copy-id node1
ssh-copy-id node2
1.2.9 配置iptables桥接功能及路由转发
sysctl -a |grep bridge #过滤bridge不存在就用下面命令加载br_netfilter
modprobe br_netfilter
cat >> /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
sysctl -p /etc/sysctl.d/k8s.conf
各节点开启转发
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.confsysctl -p
1.3 docker安装
1.3.1移除以前docker相关包
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
1.3.2 配置yum源
sudo yum install -y yum-utils
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
1.3.3 安装docker
sudo 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
sudo yum install -y docker-ce-19.03.13 docker-ce-cli-19.03.13 containerd.io-1.4.6
systemctl enable docker --now
docker version
ls
cd ~
1.3.4 配置加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://82m9ar63.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
2.k8s安装
2.1 k8s三大件安装
#(kubelet、kubeadm、kubectl)
#安装源信息写入文件
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
EOF
#下载三大件并安装
sudo yum install -y kubelet-1.20.9 kubeadm-1.20.9 kubectl-1.20.9 --disableexcludes=kubernetes
sudo systemctl enable --now kubelet
#查看状态,将停启循环
systemctl status kubelet
2.2 相关镜像获取
相关镜像信息写入文件并根据文件下载镜像
sudo tee ./images.sh <<-'EOF'
#!/bin/bash
images=(
kube-apiserver:v1.20.9
kube-proxy:v1.20.9
kube-controller-manager:v1.20.9
kube-scheduler:v1.20.9
coredns:1.7.0
etcd:3.4.13-0
pause:3.2
)
for imageName in ${images[@]} ; do
docker pull registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/$imageName
done
EOF
chmod +x ./images.sh && ./images.sh
2.3 主节点初始化
#主节点初始化(注意自己的ip地址设置)#注意所有网络范围不重叠
kubeadm init \
--apiserver-advertise-address=192.168.10.50 \
--control-plane-endpoint=k8smaster \
--image-repository registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images \
--kubernetes-version v1.20.9 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=192.168.0.0/16
初始化成功
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:
kubeadm join master:6443 --token hz33gv.8w7u29ncc0wwvcmv \
--discovery-token-ca-cert-hash sha256:b41766710bdfd85da296d48c4f4b3fc3352d38de2b2f23b34d363d2ade9b14e3 \
--control-plane
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join master:6443 --token hz33gv.8w7u29ncc0wwvcmv \
--discovery-token-ca-cert-hash sha256:b41766710bdfd85da296d48c4f4b3fc3352d38de2b2f23b34d363d2ade9b14e3
执行启动kubectl命令
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
查看nodes节点与pod情况
kubectl get nodes
kubectl get pods -A
2.4 master节点加入
kubeadm join master:6443 --token hz33gv.8w7u29ncc0wwvcmv \
--discovery-token-ca-cert-hash sha256:b41766710bdfd85da296d48c4f4b3fc3352d38de2b2f23b34d363d2ade9b14e3 \
--control-plane
2.5 worker节点加入
#使用初始化成功后的提示信息在work节点上执行,注意只有两个小时的有效时间,
kubeadm join master:6443 --token hz33gv.8w7u29ncc0wwvcmv \
--discovery-token-ca-cert-hash sha256:b41766710bdfd85da296d48c4f4b3fc3352d38de2b2f23b34d363d2ade9b14e3
#如果过期了使用以下命令重新获取
kubeadm token create --print-join-command
#查看各个节点状态,都为NotReady是正常的,需要安装网络插件后才变为ready
[root@master ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
master NotReady control-plane,master 5h51m v1.20.9
node1 NotReady <none> 15m v1.20.9
node2 NotReady <none> 5h41m v1.20.9
2.6 安装网络插件
网络插件有徐许多,如colico , kube-flannel.yml 等
#下载
curl https://docs.projectcalico.org/v3.18/manifests/calico.yaml -O
#
#安装
kubectl apply -f calico.yaml
#查看pod状态是否都启动为running状态
kubectl get pods -A
#查看nodes状态是否为ready
kubectl get nodes
#如果还有异常,状态都为running,但ready为0的就重启下服务器
reboot
至此k8s安装成功
k8s常用命令
#命名空间的操作
kubectl get namespace
kubectl create ns test
kubectl delete ns test
#通过文件创建命名空间
#vim test.yml
apiVersion: v1
kind: Namespace
metadata:
name: test1
#执行文件创建命名空间
kubectl apply -f test.yml
kubectl delete -f test.yml
kubectl run nginx --image=nginx:latest -n test1
kubectl get pod -n test1
kubectl describe pod nginx -n test1
kubectl logs -f nginx -n test1
#deployment相关
kubectl get deploy
kubectl get deploy -n test
#新增
kubectl create deployment deploy-nginx --image=daocloud.io/library/nginx:1.9.1
#删除
kubectl delete deployment deploy-nginx
#对外暴露端口
kubectl expose deployment nginx-deployment --port=8888 --target-port=80 -n test1
kubectl get service -n test1
kubectl delete service nginx-deployment -n test1
kubectl expose deployment nginx-deployment --port=8888 --target-port=80 -n test1 --type=NodePort
更多推荐
已为社区贡献1条内容
所有评论(0)