查看系统版本
[root@localhost ~]# cat /etc/centos-release
CentOS Linux release 8.2.2004 (Core)
配置网络
[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:e0:df:ff brd ff:ff:ff:ff:ff:ff
inet 192.168.17.139/24 brd 192.168.17.255 scope global dynamic noprefixroute ens160
valid_lft 1002sec preferred_lft 1002sec
inet6 fe80::6797:3823:326a:ac6b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
192.168.17.100、192.168.17.101、192.168.17.102
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens160
YPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens160"
UUID="c050f003-e673-47c0-af25-a36a6947fd67"
DEVICE="ens160"
ONBOOT="yes"
IPADDR="192.168.17.100"
NETMASK="255.255.255.0"
GATEWAY="192.168.17.2"
DNS1="192.168.17.2"
配置主机名
[root@localhost ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.17.100 master01
192.168.17.101 node01
192.168.17.102 node02
199.232.68.133 raw.githubusercontent.com
关闭防火墙:
[root@master01 ~]# systemctl stop firewalld
[root@master01 ~]# systemctl disable firewalld
关闭selinux:
[root@master01 ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config
[root@master01 ~]# setenforce 0
关闭swap,注释swap分区
[root@master01 ~]# swapoff -a
[root@master01 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Jul 3 23:57:20 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/cl-root / xfs defaults 0 0
UUID=ee7e9877-466a-4a7b-bc9e-f02b0a21d99a /boot ext4 defaults 1 2
#/dev/mapper/cl-swap swap swap defaults 0 0
配置内核参数,将桥接的IPv4流量传递到iptables的链
[root@master01 ~]# cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
安装常用包
[root@master01 ~]# yum install vim bash-completion net-tools gcc -y
添加阿里源
[root@localhost ~]# rm -rfv /etc/yum.repos.d/*
已删除'/etc/yum.repos.d/CentOS-AppStream.repo'
已删除'/etc/yum.repos.d/CentOS-Base.repo'
已删除'/etc/yum.repos.d/CentOS-centosplus.repo'
已删除'/etc/yum.repos.d/CentOS-CR.repo'
已删除'/etc/yum.repos.d/CentOS-Debuginfo.repo'
已删除'/etc/yum.repos.d/CentOS-Devel.repo'
已删除'/etc/yum.repos.d/CentOS-Extras.repo'
已删除'/etc/yum.repos.d/CentOS-fasttrack.repo'
已删除'/etc/yum.repos.d/CentOS-HA.repo'
已删除'/etc/yum.repos.d/CentOS-Media.repo'
已删除'/etc/yum.repos.d/CentOS-PowerTools.repo'
已删除'/etc/yum.repos.d/CentOS-Sources.repo'
已删除'/etc/yum.repos.d/CentOS-Vault.repo'
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2595 100 2595 0 0 18941 0 --:--:-- --:--:-- --:--:-- 18941
使用aliyun源安装docker-ce
[root@master01 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@master01 ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
添加仓库自:https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@master01 ~]# yum -y install docker-ce
[root@master01 ~]# systemctl enable docker
安装docker-ce如果出现以下错
[root@master01 ~]# yum -y install docker-ce
Docker CE Stable - x86_64 118 kB/s | 25 kB 00:00
错误:
问题: package docker-ce-3:19.03.12-3.el7.x86_64 requires containerd.io >= 1.2.2-3, but none of the providers can be installed
- cannot install the best candidate for the job
- package containerd.io-1.2.10-3.2.el7.x86_64 is filtered out by modular filtering
- package containerd.io-1.2.13-3.1.el7.x86_64 is filtered out by modular filtering
- package containerd.io-1.2.13-3.2.el7.x86_64 is filtered out by modular filtering
- package containerd.io-1.2.2-3.3.el7.x86_64 is filtered out by modular filtering
- package containerd.io-1.2.2-3.el7.x86_64 is filtered out by modular filtering
- package containerd.io-1.2.4-3.1.el7.x86_64 is filtered out by modular filtering
- package containerd.io-1.2.5-3.1.el7.x86_64 is filtered out by modular filtering
- package containerd.io-1.2.6-3.3.el7.x86_64 is filtered out by modular filtering
(尝试添加 '--skip-broken' 来跳过无法安装的软件包 或 '--nobest' 来不只使用最佳选择的软件包)
解决方法先安装containerd.io 再安装docker-ce即可成功
[root@master01 ~]# wget https://download.docker.com/linux/centos/7/x86_64/edge/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm
[root@master01 ~]# yum install containerd.io-1.2.6-3.3.el7.x86_64.rpm
添加阿里云配置镜像加速器 https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
添加阿里kubernetes源
[root@master01 ~]# 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/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
安装kubectl、kubelet、kubeadm
yum list kubelet --showduplicates | sort -r
查看哪个版本的kubernetes可用
[root@master01 ~]# yum -y install kubectl-1.18.0-0 kubelet-1.18.0-0 kubeadm-1.18.0-0
[root@master01 ~]# systemctl enable kubelet
初始化k8s集群
[root@master01 ~]# kubeadm init --kubernetes-version=1.18.0 \
--apiserver-advertise-address=192.168.17.100 \
--image-repository registry.aliyuncs.com/google_containers \
--service-cidr=10.10.0.0/16 --pod-network-cidr=10.122.0.0/16
pod 的网段为: 10.122.0.0/16, api server地址就是master本机IP。
–image-repository指定阿里云镜像仓库地址。
集群初始化成功后返回如下信息:
W0708 20:31:52.067159 32265 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
[init] Using Kubernetes version: v1.18.0
[preflight] Running pre-flight checks
[WARNING FileExisting-tc]: tc not found in system path
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [master01 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.10.0.1 192.168.17.100]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [master01 localhost] and IPs [192.168.17.100 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [master01 localhost] and IPs [192.168.17.100 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
W0708 20:33:27.685427 32265 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
[control-plane] Creating static Pod manifest for "kube-scheduler"
W0708 20:33:27.687021 32265 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 15.002560 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.18" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node master01 as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node master01 as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: 837kdg.xjz50s6zhlgrufkr
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
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.17.100:6443 --token y27bk7.6soqfhk5bnt01v4f \
--discovery-token-ca-cert-hash sha256:f397ea2d0a5d17dced55e359d4a59bf52109e958accac73da953f73036f7f871
根据提示创建kubectl
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
查看节点,pod
[root@master01 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
master01.weidyg.com Ready master 7m16s v1.18.5
[root@master01 ~]# kubectl get pod --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-7ff77c879f-wn6qm 1/1 Running 0 7m12s
kube-system coredns-7ff77c879f-zkjtm 1/1 Running 0 7m12s
kube-system etcd-master01.weidyg.com 1/1 Running 0 7m28s
kube-system kube-apiserver-master01.weidyg.com 1/1 Running 0 7m28s
kube-system kube-controller-manager-master01.weidyg.com 1/1 Running 0 7m28s
kube-system kube-proxy-4sdfb 1/1 Running 0 7m12s
kube-system kube-scheduler-master01.weidyg.com 1/1 Running 0 7m28s
安装calico网络
kubectl delete -f calico.yaml
wget https://docs.projectcalico.org/manifests/calico.yaml
sed -i 's@image: quay.io/calico/@image: registry.cn-shanghai.aliyuncs.com/gcr-k8s/calico-@g' calico.yaml
kubectl apply -f calico.yaml
[root@master01 ~]# kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
configmap/calico-config created
customresourcedefinition.apiextensions.k8s.io/bgpconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/bgppeers.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/blockaffinities.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/clusterinformations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/felixconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworksets.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/hostendpoints.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamblocks.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamconfigs.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamhandles.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ippools.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/kubecontrollersconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networksets.crd.projectcalico.org created
clusterrole.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrolebinding.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrole.rbac.authorization.k8s.io/calico-node created
clusterrolebinding.rbac.authorization.k8s.io/calico-node created
daemonset.apps/calico-node created
serviceaccount/calico-node created
deployment.apps/calico-kube-controllers created
serviceaccount/calico-kube-controllers created
解决 raw.githubusercontent.com 无法访问问题
[root@master01 ~]# vi /etc/hosts
199.232.68.133 raw.githubusercontent.com
安装kubernetes-dashboard
官方部署dashboard的服务没使用nodeport,将yaml文件下载到本地,在service里添加nodeport
[root@master01 ~]# wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml
[root@master01 ~]# vim recommended.yaml
---
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
type: NodePort
ports:
- port: 443
targetPort: 8443
nodePort: 30000
selector:
k8s-app: kubernetes-dashboard
---
[root@master01 ~]# kubectl apply -f recommended.yaml
namespace/kubernetes-dashboard created
serviceaccount/kubernetes-dashboard created
service/kubernetes-dashboard created
secret/kubernetes-dashboard-certs created
secret/kubernetes-dashboard-csrf created
secret/kubernetes-dashboard-key-holder created
configmap/kubernetes-dashboard-settings created
role.rbac.authorization.k8s.io/kubernetes-dashboard created
clusterrole.rbac.authorization.k8s.io/kubernetes-dashboard created
rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard created
clusterrolebinding.rbac.authorization.k8s.io/kubernetes-dashboard created
deployment.apps/kubernetes-dashboard created
service/dashboard-metrics-scraper created
deployment.apps/dashboard-metrics-scraper created
创建service account并绑定默认cluster-admin管理员集群角色:
[root@master01 ~]# kubectl create serviceaccount dashboard-admin -n kube-system
[root@master01 ~]# kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
[root@master01 ~]# kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
Name: dashboard-admin-token-r4v2h
Namespace: kube-system
Labels: <none>
Annotations: kubernetes.io/service-account.name: dashboard-admin
kubernetes.io/service-account.uid: 262846b3-e9c6-4c3b-bd64-78e1fcb95e37
Type: kubernetes.io/service-account-token
Data
====
namespace: 11 bytes
token: eyJhbGciOiJSUzI1NiIsImtpZCI6ImhLb0J5d0pscUJqNVUxQmJSUmpQN21wTmlMcjZ5eUgzSlRzRS1iTG04eGsifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4tNWN4cHIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiMTczMGI0YzItYjM0MS00OWQ5LWIzZDQtNzdjMzEwNTZlMjMxIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZC1hZG1pbiJ9.ahVFluprwHUiazcaiYkWm4US29TsSnPtJ9AUvjUvpYxg4HFhUCH3jbCSHcsqJkJnbo3nlgL6cSBWJJTy1U4xXHH9dgDCLKX4dXDFp5uCvZO20ZfIWNnkJprU2G5y6r-KfQiGmxYYf5NPuEpre4dWPvyFra-LYfNTezjgB_a8qmRaSX20N67pBu3w8CnYok4kSlg7Yeq42eVgb2EqLcqRe08QSAN7RxHx2cJPszu3DOygGak5wtyPIkhjfMoHisnJaheLbbCjAMyEZI9NSkCL-BXRXjuQtl8ShVgeMG6zncW7A8YFJhgbNLeYa-aXlaBiwFGVdfaUAEoffrX14DgfPA
ca.crt: 1025 bytes
#卸载服务
kubeadm reset
#删除rpm包
rpm -qa|grep kube*|xargs rpm --nodeps -e
#删除容器及镜像
docker images -qa|xargs docker rmi -f
[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
修改或创建/etc/docker/daemon.json,加入下述内容:
{
"exec-opts": ["native.cgroupdriver=systemd"]
}
重启docker:
systemctl restart docker
查看修改后的状态:
docker info | grep Cgroup