microk8s不經過虛擬機但與主機隔離方式,快速輕巧安裝Kubernetes。經過在單個快照包中打包Kubernetes,Docker.io,iptables和CNI的全部上游二進制文件來實現此隔離。 snap包是一個應用程序容器 - 您能夠將其想象爲Docker容器的輕量級版本。它使用了許多相同的底層技術進行隔離,而沒有網絡隔離的全部開銷。最終結果是一種超越任何一個發行版的包格式,由於快照能夠安裝在大多數Linux操做系統上,同時還利用原子更新,升級失敗回滾以及來自用戶主機的軟件限制級別等功能。html
microk8s能夠說是minikube在Linux平臺上的一個替代品。node
配置 | 操做系統 | ip地址 |
1核2G | ubuntu-16.04.5-server-amd64 | 192.168.0.162 |
經過 microk8s 能夠很是快速的搭建起一個 Kubernetes 單機環境,安裝極其很是方便,經過 snap
命令一鍵安裝:mysql
ubuntu 自帶 snap 命令,能夠直接執行,centos 須要先安裝 snap。nginx
apt-get install -y docker.io
apt-get clean all
注意:microk8s依賴於docker,不然執行microk8s命令,會卡頓,甚至報錯!git
sudo snap install microk8s --classic --channel=1.12/stable # 如需卸載,使用命令: # sudo snap remove microk8s
注意:安裝最新版本,使用命令:sudo snap install microk8s --classic github
可是這裏並無安裝最新版本,爲何呢?由於新版沒有microk8s.docker命令。sql
若是沒有這個命令。必須使用代理方式,下載鏡像!docker
下文,會提到,如何使用microk8s.docker命令,來避免訪問gfw問題。json
// 1. 啓動 microk8s.start // 2. 關閉 microk8s.stop // 3. kubectl 操做 // --- 查看 cluster microk8s.kubectl cluster-info // --- 查看 nodes microk8s.kubectl get nodes // --- 查看 pods microk8s.kubectl get pods // 4. docker 操做 microk8s.docker ps microk8s.docker images
能夠看到,在 microk8s 環境中執行 kubectl 時,須要在 kubectl 命令前添加前綴 microk8s.ubuntu
爲了方便,能夠爲 microk8s.kubectl
添加一個 別名:
snap alias microk8s.kubectl kubectl
echo "source <(kubectl completion bash)" >> ~/.bashrc source ~/.bashrc
microk8s.stop
microk8s.start
microk8s.status
microk8s.enable dns
查看dns運行狀態
root@ubuntu:~# kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE kube-dns-67b548dcff-75hgc 0/3 ContainerCreating 0 45
查看pod詳情
kubectl describe po kube-dns-67b548dcff-75hgc -n kube-system
會提示錯誤:
pulling image "k8s.gcr.io/pause:3.1" ...
在國內,因爲gfw的緣由, 咱們是訪問不了google的鏡像服務的, 所以不少時候咱們會碰到相似這樣的錯誤failed pulling image k8s.gcr.io/pause:3.1
。這個問題咱們能夠經過手動下載鏡像來解決。作法以下:
microk8s.docker pull mirrorgooglecontainers/pause-amd64:3.1 microk8s.docker tag mirrorgooglecontainers/pause-amd64:3.1 k8s.gcr.io/pause:3.1
microk8s會自帶一個docker, 若是你的系統裏已經安裝了docker的話, 它仍是用自帶的那個docker,也就是microk8s.docker。所以,在解決上面提到的鏡像問題的時候, 必定要使用microk8s.docker命令來操做, 使用系統原有的docker是不起做用的。可否讓microk8s使用系統的docker樓主還沒研究出來
除了 k8s.gcr.io/pause:3.1 以外,還須要其餘鏡像
microk8s.docker pull mirrorgooglecontainers/k8s-dns-kube-dns-amd64:1.14.7 microk8s.docker tag mirrorgooglecontainers/k8s-dns-kube-dns-amd64:1.14.7 gcr.io/google_containers/k8s-dns-kube-dns-amd64:1.14.7 microk8s.docker pull mirrorgooglecontainers/k8s-dns-sidecar-amd64:1.14.7 microk8s.docker tag mirrorgooglecontainers/k8s-dns-sidecar-amd64:1.14.7 gcr.io/google_containers/k8s-dns-sidecar-amd64:1.14.7 microk8s.docker pull mirrorgooglecontainers/k8s-dns-dnsmasq-nanny-amd64:1.14.7 microk8s.docker tag mirrorgooglecontainers/k8s-dns-dnsmasq-nanny-amd64:1.14.7 gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64:1.14.7
查看鏡像
root@ubuntu:~# microk8s.docker images REPOSITORY TAG IMAGE ID CREATED SIZE mirrorgooglecontainers/pause-amd64 3.1 da86e6ba6ca1 17 months ago 742kB k8s.gcr.io/pause 3.1 da86e6ba6ca1 17 months ago 742kB gcr.io/google_containers/k8s-dns-sidecar-amd64 1.14.7 db76ee297b85 19 months ago 42MB mirrorgooglecontainers/k8s-dns-sidecar-amd64 1.14.7 db76ee297b85 19 months ago 42MB mirrorgooglecontainers/k8s-dns-kube-dns-amd64 1.14.7 5d049a8c4eec 19 months ago 50.3MB gcr.io/google_containers/k8s-dns-kube-dns-amd64 1.14.7 5d049a8c4eec 19 months ago 50.3MB gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64 1.14.7 5feec37454f4 19 months ago 41MB mirrorgooglecontainers/k8s-dns-dnsmasq-nanny-amd64 1.14.7 5feec37454f4 19 months ago 41MB
刪除掉dns pod
kubectl delete po kube-dns-67b548dcff-75hgc -n kube-system --grace-period=0 --force
再次查看dns pod,狀態就正常了
root@ubuntu:~# kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE kube-dns-67b548dcff-75hgc 3/3 Running 0 45m
microk8s.docker pull nginx:1.7.9
vim nginx-service.yaml
內容以下:
apiVersion: v1 kind: Service metadata: name: nginx-deployment spec: type: NodePort ports: - port: 80 targetPort: 80 protocol: TCP name: http nodePort: 30080 selector: name: nginx
vim nginx-rc.yaml
內容以下:
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 kind: Deployment metadata: name: nginx-deployment spec: selector: matchLabels: name: nginx replicas: 1 # tells deployment to run 2 pods matching the template template: metadata: labels: name: nginx spec: containers: - name: nginx image: nginx:1.7.9 ports: - containerPort: 80
kubectl apply -f nginx-service.yaml
kubectl apply -f nginx-rc.yaml
http://192.168.0.162:30080/
效果以下:
對於非公網的鏡像,不能使用 microk8s.docker 命令來下載鏡像。這個時候,須要使用私有倉庫了!
microk8s.enable registry
訪問私有庫地址
wget http://localhost:32000/
若是輸出:
--2019-06-14 15:28:09-- http://localhost:32000/ Resolving localhost (localhost)... ::1, 127.0.0.1 Connecting to localhost (localhost)|::1|:32000... connected. HTTP request sent, awaiting response...
表示沒法鏈接!!!
那麼使用如下命令
microk8s.docker tag nginx:1.7.9 localhost:32000/nginx:1.7.9 microk8s.docker push localhost:32000/nginx:1.7.9
就會報錯:
The push refers to repository [localhost:32000/nginx]
Get http://localhost:32000/v2/: net/http: request canceled (Client.Timeout exceeded while awaiting headers)
vi /etc/hosts
註釋掉這一行
#::1 localhost ip6-localhost ip6-loopback
再次使用wget命令,就正常了!
root@ubuntu:/root/mysql# wget http://localhost:32000/ --2019-06-14 15:28:42-- http://localhost:32000/ Resolving localhost (localhost)... 127.0.0.1 Connecting to localhost (localhost)|127.0.0.1|:32000... connected. HTTP request sent, awaiting response... 200 OK Length: 0 Saving to: ‘index.html’ index.html [ <=> ] 0 --.-KB/s in 0s 2019-06-14 15:28:42 (0.00 B/s) - ‘index.html’ saved [0/0]
對於下載別的私有庫,必須修改文件
vim /var/snap/microk8s/current/args/docker-daemon.json
增長內網中的私有庫地址
{ "insecure-registries" : ["localhost:32000","192.168.0.45:5000"] }
重啓docker
/etc/init.d/docker restart
下載內網中私有庫的鏡像
root@ubuntu:/root/mysql# microk8s.docker pull 192.168.0.45:5000/mysql5.7.23 Using default tag: latest latest: Pulling from mysql5.7.23 ad965b2cd940: Downloading 20.15MB/43.02MB 08cdc197aeb5: Download complete 87da4a64f82f: Download complete 1f15cc32d2ec: Download complete 3184f9337a95: Download complete 5586ed3c006c: Downloading 11.09MB/111.7MB 6be3e1b4270e: Download complete 2eb841e4f573: Download complete 6e39824b78a8: Download complete d9c21a3025f7: Download complete 3c9f6c373477: Download complete
本文參考連接:
https://www.jianshu.com/p/2f5eb7ef1d0f
http://opentutorial.info/kubernetes/k8s/microk8s/
https://itnext.io/microk8s-docker-registry-c3f401faa760