kebeadm 搭建k8s笔记

在所有节点上设置SELINUX为permissive模式
# 修改配置
$ vi /etc/selinux/config
SELINUX=permissive

$ setenforce 0


所有节点设置/etc/hosts主机名,请根据实际情况进行配置
$ cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.161.150 k8s-master1
192.168.161.151 k8s-master2
192.168.161.152 k8s-master3
192.168.161.170 k8s-node1
192.168.161.171 k8s-node2
192.168.161.172 k8s-node3
192.168.161.160 k8s-vip


在所有节点上禁用swap
$ swapoff -a

# 禁用fstab中的swap项目
$ vi /etc/fstab
#/dev/mapper/centos-swap swap swap defaults 0 0

# 确认swap已经被禁用
$ cat /proc/swaps
Filename Type Size Used Priority

# 重启主机
$ reboot

设置docker-ce的安装yum源
# 安装yum管理工具
$ yum install -y yum-utils

# 添加阿里云的yum源
$ yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 安装docker-ce
$ yum install -y 3:docker-ce-18.09.3-3.el7.x86_64

# 启动docker服务
$ systemctl enable docker && systemctl start docker

设置kubernetes安装yum源
# 配置kubernetes软件yum源
$ cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/apt/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/apt/doc/rpm-package-key.gpg
EOF


$ cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
repo_gpgcheck=0
EOF


安装kubernetes
yum install -y kubeadm-1.14.1-0.x86_64 kubelet-1.14.1-0.x86_64 kubectl-1.14.1-0.x86_64


拉取镜像
images=(
kube-apiserver:v1.14.1
kube-controller-manager:v1.14.1
kube-scheduler:v1.14.1
kube-proxy:v1.14.1
pause:3.1
etcd:3.3.10
coredns:1.3.1
)

for imageName in ${images[@]} ; do
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName k8s.gcr.io/$imageName
done


使用yum方式安装flannel
yum install -y flannel

在所有节点上设置iptables参数
# 所有节点配置ip转发
$ cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

# 让配置生效
$ sysctl --system


echo "1" >/proc/sys/net/bridge/bridge-nf-call-iptables


初始化master节点
kubeadm init --kubernetes-version=v1.14.1 --pod-network-cidr=10.244.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

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 192.168.161.150:6443 --token y4nlq5.enr99ie4axofcrs1 \
--discovery-token-ca-cert-hash sha256:887c079a03e3dcc311b4a9ec3fcac3c970ee2ab21067c2510704eda5dc0854c7


需要部署网络插件,部署flannel
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml

查看结果: 这里失败了
kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-fb8b8dccf-p6m45 0/1 Pending 0 8m33s
coredns-fb8b8dccf-qknld 0/1 Pending 0 8m33s
etcd-k8s-master1 1/1 Running 0 7m30s
kube-apiserver-k8s-master1 1/1 Running 0 7m37s
kube-controller-manager-k8s-master1 1/1 Running 0 7m55s
kube-flannel-ds-amd64-842j7 0/1 Init:0/1 0 2m26s
kube-proxy-qjdt4 1/1 Running 0 8m33s
kube-scheduler-k8s-master1 1/1 Running 0 7m44s


解决办法是 wget https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml
cat 这个文件 然后看插件镜像名称:docker pull quay.io/coreos/flannel:v0.11.0-amd64
在所有节点上都需要这个,失败的手动pull docker pull quay.io/coreos/flannel:v0.11.0-amd64


kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-fb8b8dccf-p6m45 0/1 ContainerCreating 0 17m
coredns-fb8b8dccf-qknld 0/1 ContainerCreating 0 17m
etcd-k8s-master1 1/1 Running 0 15m
kube-apiserver-k8s-master1 1/1 Running 0 16m
kube-controller-manager-k8s-master1 1/1 Running 0 16m
kube-flannel-ds-amd64-842j7 1/1 Running 0 10m
kube-proxy-qjdt4 1/1 Running 0 17m
kube-scheduler-k8s-master1 1/1 Running 0 16m

node加入master(这个根据初始化完成后的实际情况来)
kubeadm join 192.168.161.150:6443 --token y4nlq5.enr99ie4axofcrs1 \
--discovery-token-ca-cert-hash sha256:887c079a03e3dcc311b4a9ec3fcac3c970ee2ab21067c2510704eda5dc0854c7

kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-fb8b8dccf-p6m45 1/1 Running 0 21m
coredns-fb8b8dccf-qknld 1/1 Running 0 21m
etcd-k8s-master1 1/1 Running 0 20m
kube-apiserver-k8s-master1 1/1 Running 0 20m
kube-controller-manager-k8s-master1 1/1 Running 0 20m
kube-flannel-ds-amd64-842j7 1/1 Running 0 14m
kube-flannel-ds-amd64-ltlvk 1/1 Running 0 94s
kube-flannel-ds-amd64-m69s6 1/1 Running 0 91s
kube-flannel-ds-amd64-tsq2j 1/1 Running 0 3m23s
kube-proxy-4jwgn 1/1 Running 0 91s
kube-proxy-8bqk9 1/1 Running 0 3m23s
kube-proxy-mmsb4 1/1 Running 0 94s
kube-proxy-qjdt4 1/1 Running 0 21m
kube-scheduler-k8s-master1 1/1 Running 0 20m

猜你喜欢

转载自www.cnblogs.com/gytangyao/p/10841865.html