部署docker

1.卸载旧版本

yum remove docker \           

                  docker-client \

                  docker-client-latest \

                  docker-common \

                  docker-latest \

                  docker-latest-logrotate \

                  docker-logrotate \

                  docker-engine

2:下载需要的安装包

yum install -y yum-utils

3.下载镜像(2选1)

yum-config-manager \

    --add-repo \

    https://download.docker.com/linux/centos/docker-ce.repo  #国外的地址

   

    # 设置阿里云的Docker镜像仓库

yum-config-manager \

    --add-repo \

    https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo  #国外的地址

4:更新yum软件包索引

yum makecache fast

5:安装docker相关的配置docker-ce 是社区版,docker-ee 企业版-出现了completed即安装成功。

yum install docker-ce docker-ce-cli containerd.io

6.启动docker

systemctl start docker

# 查看当前版本号,是否启动成功

docker version

# 设置开机自启动

systemctl enable docker

#运行测试docker是否安装成功

docker run hello-world

#查看运行的docker镜像

docker images

docker部署完成。

k8s部署

1、基础环境

# 将 SELinux 设置为 permissive 模式(相当于将其禁用)
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

#关闭swap
swapoff -a  
sed -ri 's/.*swap.*/#&/' /etc/fstab

#允许 iptables 检查桥接流量
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system

2、安装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=0
repo_gpgcheck=0
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

#kubelet 现在每隔几秒就会重启,因为它陷入了一个等待 kubeadm 指令的死循环

2、使用kubeadm引导集群

1、下载各个机器需要的镜像

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、初始化主节点

#所有机器添加master域名映射,以下需要修改为自己的
echo "192.168.119.128  cluster-endpoint" >> /etc/hosts

#主节点初始化
kubeadm init \
--apiserver-advertise-address=192.168.119.128 \
--control-plane-endpoint=cluster-endpoint \
--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 cluster-endpoint:6443 --token urdolj.4t4ee90con391v5v \
    --discovery-token-ca-cert-hash sha256:74ad8bd443b2f790b3e952728ca3a97e2f33d8519f9ab962d1b44a18d37564f9 \
    --control-plane
 

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join cluster-endpoint:6443 --token urdolj.4t4ee90con391v5v \
    --discovery-token-ca-cert-hash sha256:74ad8bd443b2f790b3e952728ca3a97e2f33d8519f9ab962d1b44a18d37564f9
 

*/

#查看集群所有节点
kubectl get nodes

# 运行中的应用在docker里面叫容器,在k8s里面叫Pod
kubectl get pods -A

#根据配置文件,给集群创建资源
#kubectl apply -f xxxx.yaml

#安装网络组件

#calico官网:Install Calico networking and network policy for on-premises deployments (tigera.io)

curl https://docs.projectcalico.org/manifests/calico.yaml -O

#curl https://docs.projectcalico.org/archive/v3.21/manifests/calico.yaml -O

kubectl apply -f calico.yaml

/*

#主节点初始化改了网段需要修改配置文件:

cat calico.yaml |grep 192.168(这一行的注释去掉,ip改为自己的)

节点状态没就绪的多等一会。

*/

#加入node节点

kubeadm join cluster-endpoint:6443 --token urdolj.4t4ee90con391v5v \
    --discovery-token-ca-cert-hash sha256:74ad8bd443b2f790b3e952728ca3a97e2f33d8519f9ab962d1b44a18d37564f9
 

#验证集群节点状态,需等待所有节点准备就绪(kubectl get pods -A)

kubectl get nodes

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐