前提:
1、操作系统Centos 7.4
2、通过阿里云相关镜像源安装
操作步骤:
1、修改主机名
hostnamectl set-hostname hanyu1
vi /etc/resolv.conf
nameserver 10.150.0.251
2、安装docker
安装必要的工具
yum install -y yum-utils device-mapper-persistent-data lvm2
配置官方docker源或者aliyun docker源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
或者
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
查询docker版本(否则yum install默认安装最新的版本)
yum list docker-ce --showduplicates | sort -r
安装指定版本docker
yum -y install docker-ce-18.09.3-3.el7
启动docker
systemctl enable docker && systemctl start docker
查询docker服务状态
systemctl status docker
查看docker版本
docker version
安装kubelet、kubeadm 和 kubectl
配置kubernetes.repo的源,由于官方源国内无法访问,这里使用阿里云yum源
[root@hanyu1 ~]# cat <
/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
在所有节点上安装指定版本 kubelet、kubeadm 和 kubectl(这里我安装1.16.0,目前最新版本)
yum list kubectl --showduplicates | sort -r
yum list kubeadm --showduplicates | sort -r
yum list kubelet --showduplicates | sort -r
yum install -y kubectl-1.16.0-0 kubeadm-1.16.0-0 kubelet-1.16.0-0
启动kubelet服务
systemctl enable kubelet && systemctl start kubelet
关闭swap分区
swapoff -a
初始化master节点
kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.16.0 --pod-network-cidr=10.244.0.0/16 --token-ttl 0
[root@hanyu1 ~]# kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.16.0 --pod-network-cidr=10.244.0.0/16 --token-ttl 0
[init] Using Kubernetes version: v1.16.0
[preflight] Running pre-flight checks
[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/
[WARNING Hostname]: hostname "hanyu1" could not be reached
[WARNING Hostname]: hostname "hanyu1": lookup hanyu1 on 10.150.0.251:53: server misbehaving
[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] Activating the kubelet service
[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 [hanyu1 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 10.20.0.210]
[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 [hanyu1 localhost] and IPs [10.20.0.210 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [hanyu1 localhost] and IPs [10.20.0.210 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"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[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 36.003104 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.16" 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 hanyu1 as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node hanyu1 as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: 4fz55x.x25v0mpq7370stvb
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[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
[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 -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 10.20.0.210:6443 --token 4fz55x.x25v0mpq7370stvb --discovery-token-ca-cert-hash sha256:4342810abb3e5854bc936ef87bd975632754177a726c781ee471a92ddcb856d2
按照kubeadm init成功后打印提示,继续操作:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown (id -g) $HOME/.kube/config
kubectl get nodes查询到节点处于NotReady状态,是因为网络插件还未就位,也就是这里要求运行的
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/
如果要暂时忽略让节点Ready,将如下文件的--network-plugin=cni字段去掉, (该文件是在kubeadm init或者kubeadm join过程中生成的), 修改完后重启kubelet即可(systemctl restart kubelet)
[root@hanyu2 ~]# cat /var/lib/kubelet/kubeadm-flags.env
KUBELET_KUBEADM_ARGS="--cgroup-driver=cgroupfs --network-plugin=cni --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.1"
join node
[root@hanyu2 ~]# kubeadm join 10.20.0.210:6443 --token 4fz55x.x25v0mpq7370stvb --discovery-token-ca-cert-hash sha256:4342810abb3e5854bc936ef87bd975632754177a726c781ee471a92ddcb856d2
[preflight] Running pre-flight checks
[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/
[WARNING Hostname]: hostname "hanyu2" could not be reached
[WARNING Hostname]: hostname "hanyu2": lookup hanyu2 on 10.150.0.251:53: server misbehaving
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.16" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Activating the kubelet service
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
This node has joined the cluster:
Certificate signing request was sent to apiserver and a response was received.
The Kubelet was informed of the new secure connection details.
Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
查询所有node及pod状态
[root@hanyu1 ~]# kubectl get node -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
hanyu1 Ready master 5d22h v1.16.0 10.20.0.210CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://18.9.3
hanyu2 Ready5d22h v1.16.0 10.20.0.212 CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://18.9.3
hanyu3 Ready5d19h v1.16.0 10.20.0.213 CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://18.9.3
hanyu4 Ready5d18h v1.16.0 10.20.0.214 CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://18.9.3
hanyu5 Ready4h9m v1.16.0 10.20.0.215 CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://18.9.3
hanyu6 Ready4h9m v1.16.0 10.20.0.211 CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://18.9.3
[root@hanyu1 ~]# kubectl get pod --all-namespaces -o wide
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kube-system coredns-58cc8c89f4-nx8p7 1/1 Running 3 5d22h 172.17.0.2 hanyu1
kube-system coredns-58cc8c89f4-tpd9h 1/1 Running 3 5d22h 172.17.0.3 hanyu1
kube-system etcd-hanyu1 1/1 Running 3 5d22h 10.20.0.210 hanyu1
kube-system kube-apiserver-hanyu1 1/1 Running 3 5d22h 10.20.0.210 hanyu1
kube-system kube-controller-manager-hanyu1 1/1 Running 3 5d22h 10.20.0.210 hanyu1
kube-system kube-proxy-cd6lb 1/1 Running 1 5d18h 10.20.0.214 hanyu4
kube-system kube-proxy-grp6n 1/1 Running 1 5d19h 10.20.0.213 hanyu3
kube-system kube-proxy-kthlt 1/1 Running 0 4h11m 10.20.0.211 hanyu6
kube-system kube-proxy-vpx6w 1/1 Running 1 5d22h 10.20.0.212 hanyu2
kube-system kube-proxy-wqgrc 1/1 Running 0 4h11m 10.20.0.215 hanyu5
kube-system kube-proxy-xvtjc 1/1 Running 3 5d22h 10.20.0.210 hanyu1
kube-system kube-scheduler-hanyu1 1/1 Running 3 5d22h 10.20.0.210 hanyu1