kubeadm搭建K8S集群

准备

linux版本:CentOS 7.8

在VMware搭建三个集群节点,可以先装完docker直接复制虚拟机所在路径即可,必须配置好静态ip VMware搭建教程

角色 IP
master 192.168.100.100
node1 192.168.101.101
node2 192.168.102.102

关闭防火墙
systemctl stop firewalld
systemctl disable firewalld

关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久

关闭swap
echo "vm.swappiness=1">> /etc/sysctl.conf
swapoff -a
sysctl -p

在节点中分别执行
hostnamectl set-hostname master
hostnamectl set-hostname node1
hostnamectl set-hostname node2

在master添加hosts

cat >> /etc/hosts << EOF
192.168.100.100 master
192.168.100.101 node1
192.168.100.102 node2
EOF

在所有节点执行

cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

sysctl --system # 生效
yum install ntpdate -y
ntpdate time.windows.com

安装docker
sudo yum remove docker docker-common docker-selinux docker-engine

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo

sudo sed -i 's+download.docker.com+mirrors.tuna.tsinghua.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo

sudo yum makecache fast

sudo yum install docker-ce

sudo systemctl start docker

sudo docker info

sudo systemctl enable docker

vi /etc/docker/daemon.json

{
  "registry-mirrors": [
    "https://kfwkfulq.mirror.aliyuncs.com",
    "https://2lqq34jg.mirror.aliyuncs.com",
    "https://pee6w651.mirror.aliyuncs.com",
    "https://registry.docker-cn.com",
    "http://hub-mirror.c.163.com"
  ],
  "dns": [
    "8.8.8.8",
    "8.8.4.4"
  ],
  "insecure-registries": [],
  "debug": true,
  "experimental": false,
  "exec-opts":["native.cgroupdriver=systemd"]
}

k8s阿里云yum源

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

yum install -y kubelet-1.17.0 kubeadm-1.17.0 kubectl-1.17.0
systemctl enable kubelet

部署k8s master,这里的k8s版本为1.17.2【本来是1.17.0但是死活连不通】,初始化成功后会返回一串东西用于kubeadm join

kubeadm init \
  --apiserver-advertise-address=192.168.100.100 \
  --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers \
  --kubernetes-version v1.17.2 \
  --service-cidr=10.96.0.0/12 \
  --pod-network-cidr=10.244.0.0/16

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl get nodes

master安装kube-flannel.yml
由于网络死活连不通,直接到https://github.com/coreos/flannel/blob/master/Documentation/kube-flannel.yml
复制一份到本地,并更改里面的quay.io为国内镜像quay-mirror.qiniu.com。参考链接

kubectl apply -f kube-flannel.yml

节点执行master返回的一串命令
kubeadm join xxx

创建nginx集群
1、创建nginx-rc.yaml

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-controller
spec:
  replicas: 2
  selector:
    name: nginx
  template:
    metadata:
      labels:
        name: nginx
    spec:
      containers:
        - name: nginx
          image: nginx
          ports:
            - containerPort: 80

2、创建nginx-service-nodeport.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service-nodeport
spec:
  ports:
    - port: 8000
      targetPort: 80
      protocol: TCP
  type: NodePort
  selector:
    name: nginx

kubectl create -f nginx-rc.yaml
kubectl create -f nginx-service-nodeport.yaml
kubectl get pods
kubectl describe pod nginx-controller-xxxx
kubectl get service
kubectl describe service nginx-service-nodeport

查看全部pod
kubectl get pods --all-namespaces

注意节点重启时要关闭交换分区swapoff -a
tail /var/log/messages 查看报错信息

你可能感兴趣的:(kubeadm搭建K8S集群)