kubeadm中如何安装Kubernetes集群
kubeadm中如何安装Kubernetes集群,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
创新互联是专业的播州网站建设公司,播州接单;提供成都做网站、网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行播州网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
1,修改主机名
//master 192.168.2.211 hostnamectl set-hostname kube-master //node1 192.168.2.212 hostnamectl set-hostname kube-node1 //node1 192.168.2.213 hostnamectl set-hostname kube-node2
2, 关闭防火墙
systemctl stop firewalld && systemctl disable firewalld
3,关闭selinux,必须修改允许容器访问主机文件系统
//临时 setenforce 0 //永久 sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux
4,关闭swap,否则会报错
//临时 swapoff -a //永久 vi /etc/fstab 注释掉/dev/mapper/cl-swap那一行
如图
修改永久关闭的配置,需要重启系统
5,安装docker
官方文档说已验证的docker版本为1.11.1, 1.12.1, 1.13.1, 17.03, 17.06, 17.09, 18.06(https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG-1.13.md#external-dependencies),但是在测试中使用1.13之前版本时遇到很多问题,所以这里使用目前最新版18。
sudo yum install -y yum-utils sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo sudo yum install -y docker-ce systemctl start docker && systemctl enable docker
版本:
[root@kube-master ~]# docker version Client: Version: 18.09.0 API version: 1.39 Go version: go1.10.4 Git commit: 4d60db4 Built: Wed Nov 7 00:48:22 2018 OS/Arch: linux/amd64 Experimental: false Server: Docker Engine - Community Engine: Version: 18.09.0 API version: 1.39 (minimum version 1.12) Go version: go1.10.4 Git commit: 4d60db4 Built: Wed Nov 7 00:19:08 2018 OS/Arch: linux/amd64 Experimental: false
6,安装kubeadm,kubelet,kubectl
cat </etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-\$basearch enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg EOF yum install -y kubelet kubeadm kubectl systemctl enable kubelet && systemctl start kubelet
官方提供的安装地址在墙外,如果服务器在国内,可以使用阿里的源(https://opsx.alibaba.com/mirror?lang=zh-cn)。
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 yum install -y kubelet kubeadm kubectl systemctl enable kubelet && systemctl start kubelet
版本:
这一步我没遇到,但是官方文档说某些 RHEL/CentOS 7系统会出现由于iptables被绕过,流量被错误地路由的问题。需要确保sysctl配置中net.bridge.bridge-nf-call-iptables的值为1:
cat </etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sysctl --system
在测试时发现老版本docker驱动不是cgroupfs,导致kubernetes报错。查看驱动版本:
docker info | grep -i cgroup
如果不是需要修改kubernetes配置:
vi /etc/default/kubelet KUBELET_EXTRA_ARGS=--cgroup-driver=改为当前驱动 systemctl daemon-reload systemctl restart kubelet
7,下载服务所需镜像
可以跳过,初始化会自动下载,但是因为有墙,所以需要尽量提前准备好。首先设置docker代理,
mkdir /etc/systemd/system/docker.service.d cat </etc/systemd/system/docker.service.d/http-proxy.conf [Service] Environment="HTTP_PROXY=http://192.168.2.100:1080" EOF systemctl daemon-reload systemctl restart docker
下载镜像
kubeadm config images pull --kubernetes-version v1.13.0
得到镜像如下
8,初始化master
kubeadm init --apiserver-advertise-address=192.168.2.211 --kubernetes-version=v1.13.0 --pod-network-cidr=10.244.0.0/16
--apiserver-advertise-address:api-server服务监听地址,即master的ip
--kubernetes-version:指定要部署的kubernetes版本,此处为1.13.0
--pod-network-cidr:指定pod网络分配地址范围。需要与下一步要使用的插件的配置一致,此处使用Flannel
[init] Using Kubernetes version: v1.13.0 [preflight] Running pre-flight checks [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 18.09.0. Latest validated version: 18.06 [WARNING Hostname]: hostname "kube-master" could not be reached [WARNING Hostname]: hostname "kube-master": lookup kube-master on 192.168.2.1:53: no such host [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 "front-proxy-ca" certificate and key [certs] Generating "front-proxy-client" certificate and key [certs] Generating "etcd/ca" certificate and key [certs] Generating "etcd/peer" certificate and key [certs] etcd/peer serving cert is signed for DNS names [kube-master localhost] and IPs [192.168.2.211 127.0.0.1 ::1] [certs] Generating "etcd/healthcheck-client" certificate and key [certs] Generating "apiserver-etcd-client" certificate and key [certs] Generating "etcd/server" certificate and key [certs] etcd/server serving cert is signed for DNS names [kube-master localhost] and IPs [192.168.2.211 127.0.0.1 ::1] [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 [kube-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.2.211] [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 23.502901 seconds [uploadconfig] storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace [kubelet] Creating a ConfigMap "kubelet-config-1.13" in namespace kube-system with the configuration for the kubelets in the cluster [patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "kube-master" as an annotation [mark-control-plane] Marking the node kube-master as control-plane by adding the label "node-role.kubernetes.io/master=''" [mark-control-plane] Marking the node kube-master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule] [bootstrap-token] Using token: thmy85.7ahn8zezyt6m39yy [bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles [bootstraptoken] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials [bootstraptoken] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token [bootstraptoken] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster [bootstraptoken] creating the "cluster-info" ConfigMap in the "kube-public" namespace [addons] Applied essential addon: CoreDNS [addons] Applied essential addon: kube-proxy Your Kubernetes master 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/ You can now join any number of machines by running the following on each node as root: kubeadm join 192.168.2.211:6443 --token thmy85.7ahn8zezyt6m39yy --discovery-token-ca-cert-hash sha256:576442c07cb68e731badb48f15d28d701fa184f1dffc78556ebe834f8a651021
9,创建配置文件
上一步输出中提示创建配置文件,否则执行kubectl会报错。如:
按照提示执行
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
再次执行
[root@kube-master ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION kube-master NotReady master 3m18s v1.13.1
10, 安装Pod网络插件,使Pod可以相互通信。
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
11,启动node
切换到node服务器,重复步骤1~7。执行第七步初始化master时最后打印的命令,如
kubeadm join 192.168.2.211:6443 --token thmy85.7ahn8zezyt6m39yy --discovery-token-ca-cert-hash sha256:576442c07cb68e731badb48f15d28d701fa184f1dffc78556ebe834f8a651021
完成后,回到master执行:watch kubectl get nodes,直到状态变为Ready
NAME STATUS ROLES AGE VERSION kube-master Ready master 27m v1.13.1 kube-node1 Ready9m19s v1.13.1 kube-node2 Ready 5m36s v1.13.1
12,测试
在master上创建文件nginx.yaml,创建一个deployment并通过service发布。
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deplo spec: replicas: 2 selector: matchLabels: run: nginx-deploy template: metadata: labels: run: nginx-deploy spec: containers: - name: nginx image: nginx:1.7.9 ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: nginx-service labels: run: nginx-deploy spec: type: NodePort ports: - port: 80 targetPort: 80 nodePort: 30001 selector: run: nginx-deploy
执行
kubectl create -f service.yaml watch kubectl get deploy
等待状态Ready后,测试服务。
nginx成功返回说明服务已启动。在两个Node上执行docker ps,可以看到每个node各启动了一个nginx容器。
另外,我把镜像传到了七牛云上,可以按照命令下载使用
docker pull reg.qiniu.com/fgding/kube-proxy:v1.13.0 docker pull reg.qiniu.com/fgding/kube-controller-manager:v1.13.0 docker pull reg.qiniu.com/fgding/kube-apiserver:v1.13.0 docker pull reg.qiniu.com/fgding/kube-scheduler:v1.13.0 docker pull reg.qiniu.com/fgding/coredns:1.2.6 docker pull reg.qiniu.com/fgding/etcd:3.2.24 docker pull reg.qiniu.com/fgding/pause:3.1 docker tag reg.qiniu.com/fgding/kube-proxy:v1.13.0 k8s.gcr.io/kube-proxy:v1.13.0 docker tag reg.qiniu.com/fgding/kube-controller-manager:v1.13.0 k8s.gcr.io/kube-controller-manager:v1.13.0 docker tag reg.qiniu.com/fgding/kube-apiserver:v1.13.0 k8s.gcr.io/kube-apiserver:v1.13.0 docker tag reg.qiniu.com/fgding/kube-scheduler:v1.13.0 k8s.gcr.io/kube-scheduler:v1.13.0 docker tag reg.qiniu.com/fgding/coredns:1.2.6 k8s.gcr.io/coredns:1.2.6 docker tag reg.qiniu.com/fgding/etcd:3.2.24 k8s.gcr.io/etcd:3.2.24 docker tag reg.qiniu.com/fgding/pause:3.1 k8s.gcr.io/pause:3.1
关于kubeadm中如何安装Kubernetes集群问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
文章题目:kubeadm中如何安装Kubernetes集群
网页URL:http://hbruida.cn/article/gidphc.html