Kubernetes安装与配置
准备环境
kubernetes-master | ubuntu 18.04 | 10.10.0.197 |
kubernetes-node1 | centos 7 | 10.10.0.164 |
kubernets-master配置apt源
配置kubernets源
apt-get update && apt-get install -y apt-transport-https curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - cat <<EOF >/etc/apt/sources.list.d/kubernetes.list deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main EOF apt-get update
配置docker-ce源
# step 1: 安装必要的一些系统工具 sudo apt-get update sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common # step 2: 安装GPG证书 curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - # Step 3: 写入软件源信息 sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" # Step 4: 更新并安装 Docker-CE sudo apt-get -y update
kubernets-master安装重要组件
# apt-get install docker-ce kubelet kubeadm kubectl
确保如下两个文件中的值为1
root@kubernetes-mast:/etc/apt# cat /proc/sys/net/bridge/bridge-nf-call-iptables 1 root@kubernetes-mast:/etc/apt# cat /proc/sys/net/bridge/bridge-nf-call-ip6tables 1
将kubelet&docker加入开机自启动
root@kubernetes-mast:~# systemctl enable kubelet root@kubernetes-mast:~# systemctl enable docker Synchronizing state of docker.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable docker
对kubeadm进行初始化
root@kubernetes-mast:~# kubeadm init --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 W0823 10:14:10.059009 16899 version.go:98] could not fetch a Kubernetes version from the internet: unable to get URL "https://dl.k8s.io/release/stable-1.txt": Get https://dl.k8s.io/release/stable-1.txt: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers) W0823 10:14:10.059106 16899 version.go:99] falling back to the local client version: v1.15.3 [init] Using Kubernetes version: v1.15.3 [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 SystemVerification]: this Docker version is not on the list of validated versions: 19.03.1. Latest validated version: 18.09 [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 "etcd/ca" certificate and key [certs] Generating "etcd/server" certificate and key [certs] etcd/server serving cert is signed for DNS names [kubernetes-mast localhost] and IPs [10.10.0.197 127.0.0.1 ::1] [certs] Generating "etcd/peer" certificate and key [certs] etcd/peer serving cert is signed for DNS names [kubernetes-mast localhost] and IPs [10.10.0.197 127.0.0.1 ::1] [certs] Generating "apiserver-etcd-client" certificate and key [certs] Generating "etcd/healthcheck-client" certificate and key [certs] Generating "ca" certificate and key [certs] Generating "apiserver-kubelet-client" certificate and key [certs] Generating "apiserver" certificate and key [certs] apiserver serving cert is signed for DNS names [kubernetes-mast kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 10.10.0.197] [certs] Generating "front-proxy-ca" certificate and key [certs] Generating "front-proxy-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 19.002255 seconds [upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace [kubelet] Creating a ConfigMap "kubelet-config-1.15" 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 kubernetes-mast as control-plane by adding the label "node-role.kubernetes.io/master=''" [mark-control-plane] Marking the node kubernetes-mast as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule] [bootstrap-token] Using token: r0m213.enxdgnvynkemidca [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 -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 10.10.0.197:6443 --token r0m213.enxdgnvynkemidca --discovery-token-ca-cert-hash sha256:741411b0e864eb3452aff1c6e6153c35ff01fe86907cb9f50ecfa8af2c556808
初始化完后,执行如下命令;
root@kubernetes-mast:~# mkdir -p $HOME/.kube root@kubernetes-mast:~# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config root@kubernetes-mast:~# chown $(id -u):$(id -g) $HOME/.kube/config
查看kubernetes-master节点状态
root@kubernetes-mast:~# kubectl get cs NAME STATUS MESSAGE ERROR controller-manager Healthy ok scheduler Healthy ok etcd-0 Healthy {"health":"true"} root@kubernetes-mast:~# kubectl get nodes NAME STATUS ROLES AGE VERSION kubernetes-mast NotReady master 57m v1.15.3
部署网络插件flannel
参考链接:https://github.com/coreos/flannel/blob/master/README.md
root@kubernetes-mast:~# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
podsecuritypolicy.policy/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds-amd64 created
daemonset.apps/kube-flannel-ds-arm64 created
daemonset.apps/kube-flannel-ds-arm created
daemonset.apps/kube-flannel-ds-ppc64le created
daemonset.apps/kube-flannel-ds-s390x created
kubernets-node1配置yum源
配置kubernetes源
apt-get update && apt-get install -y apt-transport-https curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - cat <<EOF >/etc/apt/sources.list.d/kubernetes.list deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main EOF apt-get update
配置docker-ce源
# step 1: 安装必要的一些系统工具 sudo yum install -y yum-utils device-mapper-persistent-data lvm2 # Step 2: 添加软件源信息 sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # Step 3: 更新并安装 Docker-CE sudo yum makecache fast
kubernets-node1安装重要组件
yum install -y docker-ce kubeadm kubectl kubelet
将docker和kubelet加入开启自启动
[root@kubernetes-node1 ~]# systemctl enable docker kubelet Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service. Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service. [root@kubernetes-node1 ~]#
启动docker
[root@kubernetes-node1 ~]# systemctl start docker
将kubernetes-node1节点加入到kubernetes-master节点下
1 [root@kubernetes-node1 ~]# kubeadm join 10.10.0.197:6443 --token r0m213.enxdgnvynkemidca --discovery-token-ca-cert-hash sha256:741411b0e864eb3452aff1c6e6153c35ff01fe86907cb9f50ecfa8af2c556808 2 3 [preflight] Running pre-flight checks 4 [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/ 5 [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 19.03.1. Latest validated version: 18.09 6 [preflight] Reading configuration from the cluster... 7 [preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml' 8 [kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.15" ConfigMap in the kube-system namespace 9 [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml" 10 [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env" 11 [kubelet-start] Activating the kubelet service 12 [kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap... 13 14 This node has joined the cluster: 15 * Certificate signing request was sent to apiserver and a response was received. 16 * The Kubelet was informed of the new secure connection details. 17 18 Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
在master节点查看kubernetes-node1节点是否加入成
root@kubernetes-mast:~# kubectl get nodes NAME STATUS ROLES AGE VERSION kubernetes-mast Ready master 150m v1.15.3 kubernetes-node1 Ready <none> 3m36s v1.15.3
最近加了一个node2节点,发现报如下错误
1 [root@kubernetes-node2 yum.repos.d]# kubeadm join 10.10.0.197:6443 --token r0m213.enxdgnvynkemidca --discovery-token-ca-cert-hash sha256:741411b0e864eb3452aff1c6e6153c35ff01fe86907cb9f50ecfa8af2c556808 2 [preflight] Running pre-flight checks 3 [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/ 4 [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 19.03.1. Latest validated version: 18.09 5 [preflight] Reading configuration from the cluster... 6 [preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml' 7 error execution phase preflight: unable to fetch the kubeadm-config ConfigMap: failed to get config map: Unauthorized
请到master上检查你所使用的token是否有效,kubeadm token list
1 root@kubernetes-mast:~# kubeadm token list 2 TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS 3 r0m213.enxdgnvynkemidca <invalid> 2019-08-24T10:15:03+08:00 authentication,signing The default bootstrap token generated by 'kubeadm init'. system:bootstrappers:kubeadm:default-node-token
发现是token过期了,生成不过期的token
1 root@kubernetes-mast:~# kubeadm token create --ttl 0 --print-join-command 2 kubeadm join 10.10.0.197:6443 --token i5ih8a.iu7kvotjmvf01a2k --discovery-token-ca-cert-hash sha256:741411b0e864eb3452aff1c6e6153c35ff01fe86907cb9f50ecfa8af2c556808
接下来用生成新的token将node2节点加入到master节点
1 [root@kubernetes-node2 yum.repos.d]# kubeadm join 10.10.0.197:6443 --token i5ih8a.iu7kvotjmvf01a2k --discovery-token-ca-cert-hash sha256:741411b0e864eb3452aff1c6e6153c35ff01fe86907cb9f50ecfa8af2c556808 2 [preflight] Running pre-flight checks 3 [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/ 4 [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 19.03.1. Latest validated version: 18.09 5 [preflight] Reading configuration from the cluster... 6 [preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml' 7 [kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.15" ConfigMap in the kube-system namespace 8 [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml" 9 [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env" 10 [kubelet-start] Activating the kubelet service 11 [kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap... 12 13 This node has joined the cluster: 14 * Certificate signing request was sent to apiserver and a response was received. 15 * The Kubelet was informed of the new secure connection details. 16 17 Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
再次通过master查看node2节点是否正确加入master节点
root@kubernetes-mast:~# kubectl get nodes NAME STATUS ROLES AGE VERSION kubernetes-mast Ready master 5d6h v1.15.3 kubernetes-node1 Ready <none> 5d3h v1.15.3 kubernetes-node2 Ready <none> 79s v1.15.3