架构师工具-k8s安装
安装方式The Hard wayKelsey Hightower :https://github.com/kelseyhightower在线play-with-k8s网址 :https://labs.play-with-k8s.com/This is a sandbox environment. Using personal credentialsis HIGHLY! dis...
安装方式
The Hard way
Kelsey Hightower :https://github.com/kelseyhightower
在线play-with-k8s
网址 :https://labs.play-with-k8s.com/
This is a sandbox environment. Using personal credentials
is HIGHLY! discouraged. Any consequences of doing so, are
completely the user's responsibilites.
You can bootstrap a cluster as follows:
1. Initializes cluster master node:
kubeadm init --apiserver-advertise-address $(hostname -i)
2. Initialize cluster networking:
kubectl apply -n kube-system -f \
"https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 |tr -d '\n')"
3. (Optional) Create an nginx deployment:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/ application/nginx-app.yaml
Cloud上搭建
GitHub :https://github.com/kubernetes/kops
企业级解决方案CoreOS
coreos :https://coreos.com/tectonic/
Minikube
K8S单节点,适合在本地学习使用
官网 :https://kubernetes.io/docs/setup/learning-environment/minikube/
GitHub :https://github.com/kubernetes/minikube
kubeadm
本地多节点
GitHub :https://github.com/kubernetes/kubeadm
使用Minikube搭建单节点K8s
Windows
- 选择任意一种虚拟化的方式
•Hyper-V
•VirtualBox
- 安装kubectl
(1)根据官网步骤 [或] 直接下载:
https://storage.googleapis.com/kubernetes- release/release/v1.16.2/bin/windows/amd64/kubectl.exe
(2)配置kubectl.exe所在路径的环境变量,使得cmd窗口可以直接使用kubectl命令
(3)kubectl version检查是否配置成功
- 安装minikube
(1)根据官网步骤 [或] 直接下载:
https://github.com/kubernetes/minikube/releases/download/v1.5.2/minikube- windows-amd64.exe
(2)修改minikube-windows-amd64.exe名称为minikube.exe
(3)配置minikube所在路径的环境变量,使得cmd窗口可以直接使用minikube命令
(4)minikube version检查是否配置成功
- 使用minikube创建单节点的k8s
minikube start --vm-driver=virtualbox --image-repository=gcr.azk8s.cn/google- containers
- 小结
其实就是通过minikube创建一个虚拟机
这个虚拟机中安装好了单节点的K8S环境然后通过kubectl进行交互
CentOS
- 安装docker
- 安装kubectl
# 01 下载[这边我给大家下载好了,在网盘kubectl&minikube中,大家上传到自己的centos7机器中。]
# 02 授权
chmod +x ./kubectl
# 03 添加到环境变量
sudo mv ./kubectl /usr/local/bin/kubectl
# 04 检查
kubectl version
- 安装minikube
# 01 下载[这边我给大家下载好了,在网盘kubectl&minikube中,大家上传到自己的centos7机器中。]
wget https://github.com/kubernetes/minikube/releases/download/v1.5.2/minikube- linux-amd64
# 02 配置环境变量
sudo mv minikube-linux-amd64 minikube && chmod +x minikube && mv minikube /usr/local/bin/
# 03 检查
minikube version
- 使用minikube创建单节点的k8s
minikube start --vm-driver=none --image-repository=gcr.azk8s.cn/google- containers
通过minikube感受kubernetes
- 查看连接信息
kubectl config view
kubectl config get-contexts
kubectl cluster-info
- 体验Pod
(1)创建pod_nginx.yaml
resources/basic/pod_nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
2)根据pod_nginx.yaml文件创建pod
kubectl apply -f pod_nginx.yaml
(3)查看pod
kubectl get pods kubectl
get pods -o wide
kubectl describe pod nginx
(4)进入nginx容器
# kubectl进入
kubectl exec -it nginx bash
# 通过docker进入
minikube ssh
docker ps
docker exec -it containerid bash
(5)访问nginx,端口转发
# 若在minikube中,直接访问
# 若在物理主机上,要做端口转发
kubectl port-forward nginx 8080:80
(6)删除pod
kubectl delete -f pod_nginx.yaml
kubeadm
版本
- Docker 18.09.0
- kubeadm-1.14.0-0
- kubelet-1.14.0-0
- kubectl-1.14.0-0
- k8s.gcr.io/kube-apiserver:v1.14.0
- k8s.gcr.io/kube-controller-manager:v1.14.0
- k8s.gcr.io/kube-scheduler:v1.14.0
- k8s.gcr.io/kube-proxy:v1.14.0
- k8s.gcr.io/pause:3.1
- k8s.gcr.io/etcd:3.3.10
- k8s.gcr.io/coredns:1.3.1
- calico:v3.9
环境准备
01 搭建前准备
mkdir D:\vs\k8s-docker-centos7 #【创建目录】
cd D:\vs\k8s-docker-centos7 #【切换目录】
type nul>Vagrantfile #【创建Vagrantfile文件】
echo [fileContent]>Vagrantfile #【编辑文件】
boxes = [
{
:name => "master-kubeadm-k8s",
:eth1 => "10.13.11.21",
:mem => "2048",
:cpu => "2",
:sshport => 22230
},
{
:name => "worker01-kubeadm-k8s",
:eth1 => "10.13.11.22",
:mem => "2048",
:cpu => "2",
:sshport => 22231
},
{
:name => "worker02-kubeadm-k8s",
:eth1 => "10.13.11.23",
:mem => "2048",
:cpu => "2",
:sshport => 22232
}
]
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
boxes.each do |opts|
config.vm.define opts[:name] do |config|
config.vm.hostname = opts[:name]
config.vm.network :public_network, ip: opts[:eth1]
config.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh", disabled: "true"
config.vm.network "forwarded_port", guest: 22, host: opts[:sshport]
config.vm.provider "vmware_fusion" do |v|
v.vmx["memsize"] = opts[:mem]
v.vmx["numvcpus"] = opts[:cpu]
end
config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--memory", opts[:mem]]
v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
v.customize ["modifyvm", :id, "--name", opts[:name]]
end
end
end
end
02 启动之后进入到对应的centos里面,使得root账户能够登陆,从而使用XShell登陆
vagrant up #[启动虚拟机]
vagrant ssh master-kubeadm-k8s #[进入manager-node]
vagrant ssh worker01-kubeadm-k8s #[进入worker01-node]
vagrant ssh worker02-kubeadm-k8s #[进入worker02-node]
03 分别登陆三个节点,执行下面操作,改成可以密码登陆root账户
sudo -i #[进入root账户]
vi /etc/ssh/sshd_config #[设置root账户可以密码登陆]
修改PasswordAuthentication yes
passwd #[修改密码]
systemctl restart sshd #[重启sshd服务]
docker安装
- 更新并安装依赖
3台机器都需要执行
yum -y update
yum install -y conntrack ipvsadm ipset jq sysstat curl iptables libseccomp
安装Docker
3台机器都需要执行,安装版本为18.09.0
# 01 进入虚拟机
vagrant ssh [nodeName]
# 02 卸载之前安装的docker
sudo yum remove docker docker latest docker-latest-logrotate \
docker-logrotate docker-engine docker-client docker-client-latest docker-common
# 03 安装必要依赖
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# 04 添加软件源信息
sudo yum-config-manager \
--add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum list | grep docker-ce
# 05 更新yum缓存
sudo yum makecache fast
# 06 安装docker
sudo yum install -y docker-ce-18.09.0 docker-ce-cli-18.09.0 containerd.io [指定安装docker版本]
# 07 启动docker并设置开机启动
sudo systemctl start docker && sudo systemctl enable docker
# 08 测试docker安装是否成功
sudo docker run hello-world
系统配置
修改hosts文件
# 01 master
# 设置master的hostname,并且修改hosts文件
sudo hostnamectl set-hostname m
# 02 两个worker
# 设置worker01/02的hostname,并且修改hosts文件
sudo hostnamectl set-hostname w1
sudo hostnamectl set-hostname w2
# 03 三台机器
vi /etc/hosts
192.168.0.101 m
192.168.0.102 w1
192.168.0.103 w2
# ====================================================================================
# 04 使用ping测试一下
ping m
ping w1
ping w2
防火墙
# (1) 关闭防火墙
systemctl stop firewalld && systemctl disable firewalld
# (2) 关闭selinux
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
# (3) 关闭swap
swapoff -a
sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab
# (4) 配置iptables的ACCEPT规则
iptables -F && iptables -X && iptables \
-F -t nat && iptables -X -t nat && iptables -P FORWARD ACCEPT
# (5) 设置系统参数
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
Installing kubeadm, kubelet and kubectl
(1) 配置yum源
cat <<EOF > /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
(2) 安装kubeadm&kubelet&kubectl
yum install -y kubeadm-1.14.0-0 kubelet-1.14.0-0 kubectl-1.14.0-0
(3) docker和k8s设置同一个cgroup
# docker
vi /etc/docker/daemon.json
"exec-opts": ["native.cgroupdriver=systemd"],
systemctl restart docker
# kubelet,这边如果发现输出directory not exist,也说明是没问题的,大家继续往下进行即可
sed -i "s/cgroup-driver=systemd/cgroup-driver=cgroupfs/g" /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
systemctl enable kubelet && systemctl start kubelet
proxy/pause/scheduler等国内镜像
(1)查看kubeadm使用的镜像
kubeadm config images list
可以发现这里都是国外的镜像
k8s.gcr.io/kube-apiserver:v1.14.0
k8s.gcr.io/kube-controller-manager:v1.14.0
k8s.gcr.io/kube-scheduler:v1.14.0
k8s.gcr.io/kube-proxy:v1.14.0
k8s.gcr.io/pause:3.1
k8s.gcr.io/etcd:3.3.10
k8s.gcr.io/coredns:1.3.1
(2)解决国外镜像不能访问的问题
- 创建kubeadm.sh脚本,用于拉取镜像/打tag/删除原有镜像
#!/bin/bash
set -e
KUBE_VERSION=v1.14.0
KUBE_PAUSE_VERSION=3.1
ETCD_VERSION=3.3.10
CORE_DNS_VERSION=1.3.1
GCR_URL=k8s.gcr.io
ALIYUN_URL=registry.cn-hangzhou.aliyuncs.com/google_containers
images=(kube-proxy:${KUBE_VERSION}
kube-scheduler:${KUBE_VERSION}
kube-controller-manager:${KUBE_VERSION}
kube-apiserver:${KUBE_VERSION}
pause:${KUBE_PAUSE_VERSION}
etcd:${ETCD_VERSION}
coredns:${CORE_DNS_VERSION})
for imageName in ${images[@]} ; do
docker pull $ALIYUN_URL/$imageName
docker tag $ALIYUN_URL/$imageName $GCR_URL/$imageName
docker rmi $ALIYUN_URL/$imageName
done
- 运行脚本和查看镜像
# 运行脚本
sh ./kubeadm.sh
# 查看镜像
docker images
- 将这些镜像推送到自己的阿里云仓库【可选,根据自己实际的情况】
# 登录自己的阿里云仓库
docker login --username=xxx registry.cn-hangzhou.aliyuncs.com
#!/bin/bash
set -e
KUBE_VERSION=v1.14.0
KUBE_PAUSE_VERSION=3.1
ETCD_VERSION=3.3.10
CORE_DNS_VERSION=1.3.1
GCR_URL=k8s.gcr.io
ALIYUN_URL=xxx
images=(kube-proxy:${KUBE_VERSION}
kube-scheduler:${KUBE_VERSION}
kube-controller-manager:${KUBE_VERSION}
kube-apiserver:${KUBE_VERSION}
pause:${KUBE_PAUSE_VERSION}
etcd:${ETCD_VERSION}
coredns:${CORE_DNS_VERSION})
for imageName in ${images[@]} ; do
docker tag $GCR_URL/$imageName $ALIYUN_URL/$imageName
docker push $ALIYUN_URL/$imageName
docker rmi $ALIYUN_URL/$imageName
done
运行脚本 sh ./kubeadm-push-aliyun.sh
kube init初始化master
(1)kube init流程
01-进行一系列检查,以确定这台机器可以部署kubernetes
02-生成kubernetes对外提供服务所需要的各种证书可对应目录
/etc/kubernetes/pki/*
03-为其他组件生成访问kube-ApiServer所需的配置文件
ls /etc/kubernetes/
admin.conf controller-manager.conf kubelet.conf scheduler.conf
04-为 Master组件生成Pod配置文件。
ls /etc/kubernetes/manifests/*.yaml
kube-apiserver.yaml
kube-controller-manager.yaml
kube-scheduler.yaml
05-生成etcd的Pod YAML文件。
ls /etc/kubernetes/manifests/*.yaml
kube-apiserver.yaml
kube-controller-manager.yaml
kube-scheduler.yaml
etcd.yaml
06-一旦这些 YAML 文件出现在被 kubelet 监视的/etc/kubernetes/manifests/目录下,kubelet就会自动创建这些yaml文件定义的pod,即master组件的容器。master容器启动后,kubeadm会通过检查localhost:6443/healthz这个master组件的健康状态检查URL,等待master组件完全运行起来
07-为集群生成一个bootstrap token
08-将ca.crt等 Master节点的重要信息,通过ConfigMap的方式保存在etcd中,工后续部署node节点使用
09-最后一步是安装默认插件,kubernetes默认kube-proxy和DNS两个插件是必须安装的
(2)初始化master节点
官网:https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm/
注意
:此操作是在主节点上进行
# 本地有镜像
kubeadm init --kubernetes-version=1.14.0 --apiserver-advertise-address=192.168.8.51 --pod-network-cidr=10.244.0.0/16
【若要重新初始化集群状态:kubeadm reset,然后再进行上述操作】
记得保存好最后kubeadm join的信息
(3)根据日志提示
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
此时kubectl cluster-info查看一下是否成功
(4)查看pod验证一下
等待一会儿,同时可以发现像etc,controller,scheduler等组件都以pod的方式安装成功了
注意
:coredns没有启动,需要安装网络插件
kubectl get pods -n kube-system
(5)健康检查
curl -k https://localhost:6443/healthz
部署calico网络插件
选择网络插件:https://kubernetes.io/docs/concepts/cluster-administration/addons/
calico网络插件:https://docs.projectcalico.org/v3.9/getting-started/kubernetes/
calico,同样在master节点上操作
# 在k8s中安装calico
kubectl apply -f https://docs.projectcalico.org/v3.9/manifests/calico.yaml
# 确认一下calico是否安装成功
kubectl get pods --all-namespaces -w
kube join
记得保存初始化master节点的最后打印信息【注意这边大家要自己的,下面我的只是一个参考】
kubeadm join 192.168.0.51:6443 --token yu1ak0.2dcecvmpozsy8loh \
--discovery-token-ca-cert-hash sha256:5c4a69b3bb05b81b675db5559b0e4d7972f1d0a61195f217161522f464c307b0
(1)在woker01和worker02上执行上述命令
(2)在master节点上检查集群信息
kubectl get nodes
NAME STATUS ROLES AGE VERSION
master-kubeadm-k8s Ready master 19m v1.14.0
worker01-kubeadm-k8s Ready <none> 3m6s v1.14.0
worker02-kubeadm-k8s Ready <none> 2m41s v1.14.0
体验Pod
(1)定义pod.yml文件,比如pod_nginx_rs.yaml
cat > pod_nginx_rs.yaml <<EOF
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx
labels:
tier: frontend
spec:
replicas: 3
selector:
matchLabels:
tier: frontend
template:
metadata:
name: nginx
labels:
tier: frontend
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
EOF
(2)根据pod_nginx_rs.yml文件创建pod
kubectl apply -f pod_nginx_rs.yaml
(3)查看pod
kubectl get pods
kubectl get pods -o wide
kubectl describe pod nginx
(4)感受通过rs将pod扩容
kubectl scale rs nginx --replicas=5
kubectl get pods -o wide
(5)删除pod
kubectl delete -f pod_nginx_rs.yaml
更多推荐
所有评论(0)