# 1.临时关闭防火墙,selinux [root@zjs ~]# systemctl stop firewalld [root@zjs ~]# systemctl disable firewalld [root@zjs ~]# setenforce 0 # 2.临时关闭swap [root@zjs ~]# swapoff -a # 临时 # 3.传递IPv4流量给iptables链 [root@zjs ~]# cat > /etc/sysctl.d/k8s.conf << EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF # 4.时间同步 [root@zjs ~]# yum install ntpdate -y [root@zjs ~]# ntpdate time.windows.com二.安装docker,参考前一篇swarm安装
# 1.卸载旧版本
apt-get remove docker docker-engine docker.io containerd runc
# 2.安装前提依赖
apt update
apt-get install ca-certificates curl gnupg lsb-release
# 3.安装GPG证书
add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# 4.写入软件源信息
add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# 5.安装新版本docker
apt-get install docker-ce docker-ce-cli containerd.io
# 6.配置用户组
groupadd docker
#7.启动docker
systemctl start docker
#8.docker换源
# 修改 /etc/docker/daemon.json (如果该文件不存在,则创建)
{
"registry-mirrors": [
"https://hub-mirror.c.163.com"
]
}
# 9.安装必要系统工具
apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# 10.配置docker
# 添加 docker 配置 /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"registry-mirrors": ["docker镜像仓库国内加速"]
}
# 11.重启docker
service docker restart
三.Kubernetes单机版安装和使用
1.添加kubernetes的yum源
[root@zjs ~]# 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
2. 安装kubeadm,kubelet 和kubectl
[root@zjs ~]# yum -y install kubelet-1.21.5-0 kubeadm-1.21.5-0 kubectl-1.21.5-0 [root@zjs ~]# systemctl enable kubelet
3. 部署Kubernetes Master
[root@zjs ~]# kubeadm init --apiserver-advertise-address=222.190.67.94 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.21.5 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=all 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/ Then you can join any number of worker nodes by running the following on each as root: kubeadm join 222.190.67.94:6443 --token 7aj43b.lsaj1n8xg2kqhs6q --discovery-token-ca-cert-hash sha256:b1ac091af23de4ecf339f7ea37973452d53e6e04a28c531bb6534032ea9b6ef7
参数说明:
–apiserver-advertise-address:master主机IP地址
–image-repository registry.aliyuncs.com/google_containers 这个是镜像地址
–kubernetes-version=v1.21.5 这个参数是下载的k8s软件版本号
4. 部署任务
单机版kubernetes可以先去除污点之后再部署任务
# 1.检查污点并去除 [root@zjs ~]# kubectl get node -o yaml | grep taint -A 5 [root@zjs ~]# kubectl taint nodes --all node-role.kubernetes.io/master- # 2.安装补全命令的包 [root@zjs ~]# yum -y install bash-completion [root@zjs ~]# kubectl completion bash [root@zjs ~]# source /usr/share/bash-completion/bash_completion [root@zjs ~]# kubectl completion bash >/etc/profile.d/kubectl.sh [root@zjs ~]# source /etc/profile.d/kubectl.sh [root@zjs ~]# cat >> /root/.bashrc <443/TCP 88m service/nginx NodePort 10.103.251.155 80:30582/TCP 26s



