Kubernetes简介
Kubernetes是一个用于管理容器化的工作负载和服务的便携式、可扩展的开源平台,有助于声明式配置和自动化。Kubernetes有一个庞大且快速生长的生态系统,服务、支持和工具已广泛应用。
谷歌于2014年将Kubernetes项目开源。通常简称k8s。
环境搭建
安装环境为 ubuntu 16.04。
docker
确保docker服务已安装并正常运行,检查方法为执行docker run hello-world
,能看到运行结果。kubectl
kubectl是kubernetes的命令行工具,通过它进行k8s集群资源监控;新建、删除、修改组件;部署和管理应用。
- 安装
# ubuntu可使用snap安装
sudo snap install kubectl
# check是否安装成功
kubectl version
- 配置
配置文件通常位置~/.kube/config
,配置信息包括:context、user、cluster、cert等。
# 查看集群信息
kubectl cluster-info
- Minikube
Minikube 是一个使我们很容易在本地运行 kubernetes 的工具,他是通过在本机 VM 里运行一个单节点 kubernetes 集群,这对于新手想了解和学习 kubernetes 提供了很大的帮助。
- Linux安装without VM
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube
curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && chmod +x kubectl
export MINIKUBE_WANTUPDATENOTIFICATION=false
export MINIKUBE_WANTREPORTERRORPROMPT=false
export MINIKUBE_HOME=$HOME
export CHANGE_MINIKUBE_NONE_USER=true
mkdir $HOME/.kube || true
touch $HOME/.kube/config
export KUBECONFIG=$HOME/.kube/config
sudo -E ./minikube start --vm-driver=none
# this for loop waits until kubectl can access the api server that Minikube has created
for i in {1..150}; do # timeout for 5 minutes
./kubectl get po &> /dev/null
if [ $? -ne 1 ]; then
break
fi
sleep 2
done
# kubectl commands are now able to interact with Minikube cluster
- 使用kubectl连接minikube并进行一些基本操作。Linux only。
$ minikube start
Starting local Kubernetes v1.7.5 cluster...
Starting VM...
SSH-ing files into VM...
Setting up certs...
Starting cluster components...
Connecting to cluster...
Setting up kubeconfig...
Kubectl is now configured to use the cluster.
$ kubectl run hello-minikube --image=k8s.gcr.io/echoserver:1.4 --port=8080
deployment "hello-minikube" created
$ kubectl expose deployment hello-minikube --type=NodePort
service "hello-minikube" exposed
# We have now launched an echoserver pod but we have to wait until the pod is up before curling/accessing it
# via the exposed service.
# To check whether the pod is up and running we can use the following:
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
hello-minikube-3383150820-vctvh 1/1 ContainerCreating 0 3s
# We can see that the pod is still being created from the ContainerCreating status
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
hello-minikube-3383150820-vctvh 1/1 Running 0 13s
# We can see that the pod is now Running and we will now be able to curl it:
$ curl $(minikube service hello-minikube --url)
CLIENT VALUES:
client_address=192.168.99.1
command=GET
real path=/
...
$ kubectl delete service hello-minikube
service "hello-minikube" deleted
$ kubectl delete deployment hello-minikube
deployment "hello-minikube" deleted
$ minikube stop
Stopping local Kubernetes cluster...
Machine stopped.
kubernetes dashboard
运行minikube dashboard
,打开kubernetes dashboard,可以看到集群的负载、任务、Pods等。
好啦,开启k8s之旅吧~
后记(踩坑记)
kubernetes的坑真的一个接一个啊,最近一周都是失足少女状态。
First of all,Never try to set up an environment in windows.
Then,下面记录了几个印象深刻的坑。
- 获取kubernetes依赖镜像
在minikube start
时运行kubeadm init
命令时,程序一直卡在那里,查询journalctl日志后发现一直在拉取国外k8s.gcr.io的镜像,但国内k8s.gcr.io镜像被墙,所以就卡在那里了。
解决方法是自己在docker hub上用docker autobuild做了镜像。当然也可以去搜索别人已经做好的镜像,找到对应版本就好了。
从别人那里fork过来的镜像库,自己改了一些docker源。https://github.com/treephie/docker-library
# 使用方法就是,先docker pull到本地,再docker tag成谷歌的地址和版本。
docker pull treeph/kube-addon-manager
docker pull treeph/k8s-dns-dnsmasq-nanny-amd64
docker pull treeph/kubernetes-dashboard-amd64
docker pull treeph/kube-proxy-amd64
docker pull treeph/k8s-dns-sidecar-amd64
docker pull treeph/k8s-dns-kube-dns-amd64
docker pull treeph/pause-amd64
docker pull treeph/etcd-amd64
docker pull treeph/kube-scheduler-amd64
docker pull treeph/kube-controller-manager-amd64
docker pull treeph/kube-apiserver-amd64
docker tag treeph/kube-addon-manager:latest k8s.gcr.io/kube-addon-manager:v8.6
docker tag treeph/k8s-dns-dnsmasq-nanny-amd64:latest k8s.gcr.io/k8s-dns-dnsmasq-nanny-amd64:1.14.4
docker tag treeph/kubernetes-dashboard-amd64:latest k8s.gcr.io/kubernetes-dashboard-amd64:v1.8.1
docker tag treeph/kube-proxy-amd64:latest k8s.gcr.io/kube-proxy-amd64:v1.10.0
docker tag treeph/k8s-dns-sidecar-amd64:latest k8s.gcr.io/k8s-dns-sidecar-amd64:1.14.4
docker tag treeph/k8s-dns-kube-dns-amd64:latest k8s.gcr.io/k8s-dns-kube-dns-amd64:1.14.4
docker tag treeph/pause-amd64:latest k8s.gcr.io/pause-amd64:3.1
docker tag treeph/etcd-amd64:latest k8s.gcr.io/etcd-amd64:3.1.12
docker tag treeph/kube-scheduler-amd64:latest k8s.gcr.io/kube-scheduler-amd64:v1.10.0
docker tag treeph/kube-controller-manager-amd64:latest k8s.gcr.io/kube-controller-manager-amd64:v1.10.0
docker tag treeph/kube-apiserver-amd64:latest k8s.gcr.io/kube-apiserver-amd64:v1.10.0
# 完成后查看本地镜像
treeph@treeph-VirtualBox:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
k8s.gcr.io/kube-addon-manager v8.6 e7ffc6793f17 7 hours ago 78.4 MB
k8s.gcr.io/k8s-dns-dnsmasq-nanny-amd64 1.14.4 a873e1b191b4 7 hours ago 41.4 MB
k8s.gcr.io/kubernetes-dashboard-amd64 v1.8.1 bb065852c246 23 hours ago 121 MB
k8s.gcr.io/kube-proxy-amd64 v1.10.0 064653de4df1 23 hours ago 97 MB
k8s.gcr.io/k8s-dns-sidecar-amd64 1.14.4 39798ae6e4ee 24 hours ago 41.8 MB
k8s.gcr.io/k8s-dns-kube-dns-amd64 1.14.4 10625de58167 24 hours ago 41.4 MB
k8s.gcr.io/pause-amd64 3.0 62eaddb725bb 24 hours ago 747 kB
k8s.gcr.io/pause-amd64 3.1 62eaddb725bb 24 hours ago 747 kB
k8s.gcr.io/etcd-amd64 3.1.12 1572aa534c99 4 days ago 193 MB
k8s.gcr.io/kube-scheduler-amd64 v1.10.0 bbd569e45bcb 4 days ago 50.4 MB
k8s.gcr.io/kube-controller-manager-amd64 v1.10.0 4afb144bd444 4 days ago 148 MB
k8s.gcr.io/kube-apiserver-amd64 v1.10.0 b59ed8fe408e 4 days ago 225 MB
更详细的步骤参考这篇博文:https://www.cnblogs.com/ericnie/p/7749588.html
- Port 10250 is already in use
可能的导致原因是kubeadm init
之前运行过,已经做了一些配置。
再次运行之前需要将之前的配置操作重置,执行kubeadm reset
即可。
重置之后再重新minikube start
或者kubeadm init
,应该就好了。
参考StackOverflow: https://stackoverflow.com/questions/41732265/how-to-use-kubeadm-to-create-kubernetest-cluster
- 执行minikube命令报错Permission denied
尝试加sudo,如果成功执行的话是权限问题,minikube安装成功后修改文件所属和组权限,从root改到普通用户。
sudo chown -R $USER $HOME/.minikube
sudo chgrp -R $USER $HOME/.minikube
- dial tcp x.x.x.x:x. connection refused
检查是否docker环境未好,检查方法是docker run hello-world
。