上一节完成了虚拟机环境的搭建。这一节开始在虚机上基于minikube搭建k8s环境,用于后面JenkinsX的环境构建。在开始之前,需要对操作系统做一些配置,以方便后续的操作。
版本清单:
Docker:Version: 18.06.1-ce
minikube: v1.4.0
kubeadm: v1.13.4
kubectl: v1.13.4
kubelet: v1.13.4
1、配置工作
图形界面
CentOS7默认启用了很耗资源的图形界面,在搭建过程中可以选择是否关闭。资源充足的可以忽略,后面jenkinx console启动浏览器时会用到图形界面。这里建议使用SecureCRT登录操作。
命令模式
systemctl set-default multi-user.target
图形模式
systemctl set-default graphical.target
配置yum源
这里将CentOS默认的yum源替换为aliyun的yum源,安装软件和依赖时会很快。执行如下命令:
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache
关闭防火墙
关闭防火墙并且禁止开启自启动
systemctl stop firewalld & systemctl disable firewalld
关闭SeLinux
执行命令:
setenforce 0
关闭Swap
在安装K8S集群时,Linux的Swap内存交换机制是一定要关闭的,否则会因为内存交换而影响性能以及稳定性,修改完成后,需要重启。
sed -i '/ swap / s/^/#/' /etc/fstab
2、安装Docker
在安装K8s之前,需要先安装Docker,这里安装的版本是版本清单里的18.06.1-ce。在安装之前先配置一下阿里云的镜像地址,防止安装过程因为网络问题中断。
配置Docker仓库
将阿里云的仓库镜像添加到yum源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache
安装Docker并设置开机自启
指定版本安装Docker
yum install docker-ce-18.06.1.ce-3.el7
systemctl start docker & systemctl enable docker
Docker的版本
docker version
Docker测试,出现“Hello from Docker!” 说明成功。
[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
[root@localhost ~]#
3、安装Kubernetes
下面就开始安装K8s相关的组件,在安装之前,先添加aliyun的yum源。
添加K8S镜像源仓库
拷贝下面的脚本直接执行
cat < /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
安装kubelet、kubectl、kubeadm
按下面顺序指定版本安装k8s的这几个组件,并设置kubelet开启自启动
yum install kubelet-1.13.4-0
yum install kubectl-1.13.4-0
yum install kubeadm-1.13.4-0
systemctl enable kubelet && systemctl start kubelet
安装minikube
参考:https://kubernetes.io/docs/tasks/tools/install-minikube/指定版本安装。
curl -Lo minikube https://storage.googleapis.com/minikube/releases/v1.4.0/minikube-linux-amd64 && chmod +x minikube && sudo cp minikube /usr/local/bin/ && rm minikube
安装k8s集群
使用minikube start启动一个K8s集群。在minikubev1.4.0的版本里,可以指定docker镜像的国别(--image-mirror-country)和镜像仓库(--image-repository),这样就不用去翻墙下载镜像了。如果你的minikube没有这两个选项,可参考之前写的一篇问题《Jenkins X--(4)不用翻墙也能下载国外的镜像》。
指定--kubernetes-version=‘v1.13.4’ 这样每次就下载跟这个版本相关的依赖组件和镜像,
指定--vm-driver=none 将K8s组件安装在宿主机里,而不是VM里,这样也就不用再虚机里再安装虚机了。
minikube start --image-mirror-country='cn' --image-repository='registry.cn-hangzhou.aliyuncs.com/google_containers' --vm-driver=none --kubernetes-version='v1.13.4'
安装过程如下:
[root@localhost ~]# minikube start --image-mirror-country='cn' --image-repository='registry.cn-hangzhou.aliyuncs.com/google_containers' --vm-driver=none --kubernetes-version='v1.13.4'
* minikube v1.4.0 on Centos 7.6.1810
* Using image repository registry.cn-hangzhou.aliyuncs.com/google_containers
* Running on localhost (CPUs=4, Memory=5948MB, Disk=26607MB) ...
* OS release is CentOS Linux 7 (Core)
* Preparing Kubernetes v1.13.4 on Docker 18.06.1-ce ...
* Downloading kubeadm v1.13.4
* Downloading kubelet v1.13.4
* 拉取镜像 ...
* 正在启动 Kubernetes ...
* Configuring local host environment ...
*
! The 'none' driver provides limited isolation and may reduce system security and reliability.
! For more information, see:
- https://minikube.sigs.k8s.io/docs/reference/drivers/none/
*
! kubectl and minikube configuration will be stored in /root
! To use kubectl or minikube commands as your own user, you may need to relocate them. For example, to overwrite your own settings, run:
*
- sudo mv /root/.kube /root/.minikube $HOME
- sudo chown -R $USER $HOME/.kube $HOME/.minikube
*
* This can also be done automatically by setting the env var CHANGE_MINIKUBE_NONE_USER=true
* Waiting for: apiserver proxy etcd scheduler controller dns
* Done! kubectl is now configured to use "minikube"
安装完成后,查看kube-system空间里的pod情况,都已经处于Running状态。
[root@localhost ~]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-89cc84847-bdpvm 1/1 Running 0 8m7s
coredns-89cc84847-nksg2 1/1 Running 0 8m7s
etcd-minikube 1/1 Running 0 7m12s
kube-addon-manager-minikube 1/1 Running 0 8m7s
kube-apiserver-minikube 1/1 Running 0 7m20s
kube-controller-manager-minikube 1/1 Running 0 7m7s
kube-proxy-2klgq 1/1 Running 0 8m7s
kube-scheduler-minikube 1/1 Running 0 7m11s
storage-provisioner 1/1 Running 0 8m4s
查看docker镜像,发现都是从配置好的aliyun镜像拉取下来的。
查看kubectl version ,Client和Server都已经启动,版本都是v1.13.4。
[root@localhost ~]# kubectl version
Client Version: version.Info{Major:"1", Minor:"13", GitVersion:"v1.13.4", GitCommit:"c27b913fddd1a6c480c229191a087698aa92f0b1", GitTreeState:"clean", BuildDate:"2019-02-28T13:37:52Z", GoVersion:"go1.11.5", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"13", GitVersion:"v1.13.4", GitCommit:"c27b913fddd1a6c480c229191a087698aa92f0b1", GitTreeState:"clean", BuildDate:"2019-02-28T13:30:26Z", GoVersion:"go1.11.5", Compiler:"gc", Platform:"linux/amd64"}
OK,基于minikube的K8s环境已经完成。如果想学习K8s的,这个环境就够了。可以在上面尽情的玩耍了。目前K8s的版本更新非常快,依赖的组件版本也在不断迭代。如果想学习最好的资源就是官方文档。可以自行百度,大多都是英文文档,是时候考验英语老师的水平了哈。
======================================
本人水平有限,难免有错误或疏漏,望大家指正,欢迎留言交流。欢迎关注本人微信公众号:DevOps亮哥