centos7虚拟机部署k8s集群
Published in -
22-03-15 15:24
- 下载centos镜像
https://www.centos.org/download/
- 虚拟机安装centos镜像
安装完成
- 修改hostname
vi /etc/hostname
- 配置网络
vi /etc/sysconfig/network-scripts/ifcfg-eth0
# 修改以下配置
BOOTPROTO=static # 从dhcp改成static
# 从no改成yes。系统将在启动时自动开启该接口。
ONBOOT=yes
# 设置IP地址
IPADDR=192.168.0.200
# 设置子网掩码
NETMASK=255.255.255.0
# 设置网关
GATEWAY=192.168.0.1
# 设置DNS
DNS1=211.167.230.100
DNS2=211.167.230.200
# 表示该接口将通过该配置文件进行设置,而不是通过网络管理器进行管理
NM_CONTROLLED=no
- 重启网络
service network restart
systemctl restart network
- 更新系统
yum update -y
- 安装git
yum install http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-2.noarch.rpm
yum -y install git
git version
- 安装常用库
yum install zsh lrzsz vim wget
- 安装zsh的插件
sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"
- 配置hosts
cat >> /etc/hosts << EOF
192.168.0.200 master
192.168.0.201 node1
192.168.0.202 node2
192.168.0.203 nfs
EOF
- 把hosts文件复制到node1\2(在msster上运行,如果node和nfs是master的副本则不需要执行这步)
scp /etc/hosts root@node1:/etc/hosts
scp /etc/hosts root@node2:/etc/hosts
scp /etc/hosts root@nfs:/etc/hosts
- 关闭防火墙(在3台机运行)
systemctl stop firewalld && systemctl disable firewalld
- 关闭selinux(在3台机运行)
sed -i 's/enforcing/disabled/' /etc/selinux/config && setenforce 0
- 关闭swap(在3台机运行)
swapoff -a && sed -ri 's/.*swap.*/- &/' /etc/fstab
- 时间同步(在3台机运行)
yum install ntpdate -y && timedatectl set-timezone Asia/Shanghai && ntpdate time.windows.com
- 安装docker
Step 1: 安装必要的一些系统工具
yum install -y yum-utils device-mapper-persistent-data lvm2
Step 2: 添加软件源信息
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Step 3: 更新并安装Docker-CE
yum makecache fast
yum -y install docker-ce
Step 4: 开启Docker服务
systemctl start docker && systemctl enable docker
Step 5: 配置源
tee /etc/docker/daemon.json <<-'EOF'
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors": ["https://j6wjrsg1.mirror.aliyuncs.com"]
}
EOF
- [选装]安装docker compose v2
DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
mkdir -p $DOCKER_CONFIG/cli-plugins
curl -SL https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose
chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose
docker compose version
- 安装k8s
- 配置安装源
cat <<EOF > /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
EOF
- 安装k8s
yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
systemctl enable kubelet && systemctl start kubelet
- 查看k8s所需镜像
kubeadm config images list
k8s.gcr.io/kube-apiserver:v1.23.4
k8s.gcr.io/kube-controller-manager:v1.23.4
k8s.gcr.io/kube-scheduler:v1.23.4
k8s.gcr.io/kube-proxy:v1.23.4
k8s.gcr.io/pause:3.6
k8s.gcr.io/etcd:3.5.1-0
k8s.gcr.io/coredns/coredns:v1.8.6
- 下载镜像
# 使用阿里云下载镜像
docker pull registry.aliyuncs.com/google_containers/kube-apiserver:v1.23.4
docker pull registry.aliyuncs.com/google_containers/kube-controller-manager:v1.23.4
docker pull registry.aliyuncs.com/google_containers/kube-scheduler:v1.23.4
docker pull registry.aliyuncs.com/google_containers/kube-proxy:v1.23.4
docker pull registry.aliyuncs.com/google_containers/pause:3.6
docker pull registry.aliyuncs.com/google_containers/etcd:3.5.1-0
docker pull registry.aliyuncs.com/google_containers/coredns:v1.8.6
# 打tag方式修改名称
docker tag registry.aliyuncs.com/google_containers/kube-apiserver:v1.23.4 k8s.gcr.io/kube-apiserver:v1.23.4
docker tag registry.aliyuncs.com/google_containers/kube-controller-manager:v1.23.4 k8s.gcr.io/kube-controller-manager:v1.23.4
docker tag registry.aliyuncs.com/google_containers/kube-scheduler:v1.23.4 k8s.gcr.io/kube-scheduler:v1.23.4
docker tag registry.aliyuncs.com/google_containers/kube-proxy:v1.23.4 k8s.gcr.io/kube-proxy:v1.23.4
docker tag registry.aliyuncs.com/google_containers/pause:3.6 k8s.gcr.io/pause:3.6
docker tag registry.aliyuncs.com/google_containers/etcd:3.5.1-0 k8s.gcr.io/etcd:3.5.1-0
docker tag registry.aliyuncs.com/google_containers/coredns:v1.8.6 k8s.gcr.io/coredns/coredns:v1.8.6
# 删除原镜像
docker rmi registry.aliyuncs.com/google_containers/kube-apiserver:v1.23.4 registry.aliyuncs.com/google_containers/kube-controller-manager:v1.23.4 registry.aliyuncs.com/google_containers/kube-scheduler:v1.23.4 registry.aliyuncs.com/google_containers/kube-proxy:v1.23.4 registry.aliyuncs.com/google_containers/pause:3.6 registry.aliyuncs.com/google_containers/etcd:3.5.1-0 registry.aliyuncs.com/google_containers/coredns:v1.8.6
- 复制node/nfs副本
修改IP和hostname,重启
- master初始化
设置免登录(在msster上运行)
ssh-keygen
ssh-copy-id root@node1
ssh-copy-id root@node2
ssh-copy-id root@nfs
初始化
kubeadm init \
--apiserver-advertise-address=192.168.220.200 \
--kubernetes-version=v1.23.4 \
--pod-network-cidr=10.244.0.0/16
如果初始化失败
kubeadm reset
初始化成功的输出
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:
# 下面的代码后面node节点加入master集群时会用到
kubeadm join 192.168.0.200:6443 --token seu44z.2mwjds6cggi5kwep \
--discovery-token-ca-cert-hash sha256:1c0da8c9cba3d007a8598ad7e4f29e4d33e2ab62b01ad499ea5ff95a6fa79ac8
安装网络插件
kubectl apply -f kube-flannel.yml
kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
- node节点加入到集群
kubeadm join 192.168.220.200:6443 --token cw1jk8.tupno2et89kktv0d \
--discovery-token-ca-cert-hash sha256:a9c0a06b84d08cc82358f0f07eb4fd2b34e3491d1351bd2e16f2aeb2df50a7ef
- 查看节点
kubectl get nodes
- 查看pod
kubectl get pods --all-namespaces
- 查看网络
kubectl get svc
- 查看pods
watch kubectl get pods --all-namespaces
- 查看单个pod的详情
kubectl describe pods NAME -n namespaces
- 监控
watch kubectl get pods --all-namespaces -o wide
Next
^_^ Be the first to comment.
Comment
Nickname
Email (Reply notice)
Website
Content (can use Markdown syntax)