15分鐘帶你體驗Kubernetes

前言

最近開始學習Kubernetes,再次記錄一些本身學習的一些心得,與你們分享,互相交流,共同進步。node

純小白一個,歡迎你們批評指正,歡迎評論,謝謝 ~~~git

準備

  • Ubuntu 18.04 Server LTS (或者 Centos)
  • 暫時不支持Ubuntu 20.04 Server LTS
  • Firefox瀏覽器

Kubeasz

github.com/easzlab/kub…github

致力於提供快速部署高可用k8s集羣的工具, 同時也努力成爲k8s實踐、使用的參考書。golang

相比於minikube在國內網絡環境下更加友好。web

能夠很是便捷的部署一套用於自學的k8s環境。docker

開始

如下內容爲Kubeasz AllinOne 文檔 AllinOne部署ubuntu

1. 下載文件
  • 下載工具腳本
export release=2.2.1
curl -C- -fLO --retry 3 https://github.com/easzlab/kubeasz/releases/download/${release}/easzup
chmod +x ./easzup
複製代碼
  • 使用工具腳本安裝k8s以及docker
./easzup -D -d 19.03.5 -k v1.18.2
複製代碼
  • 下載離線系統包
./easzup -P
複製代碼

上述腳本執行成功之後,全部所需文件均放入目錄 /etc/ansibleapi

2. 安裝集羣
  • 容器化啓動kubeasz
./easzup -S
複製代碼
  • 使用默認配置容許 AllinOne 集羣
docker exec -it kubeasz easzctl start-aio
複製代碼
3. 驗證安裝

這裏若是提示kubectl: command not found,你能夠退出機器從新ssh,或者切換到root權限便可。瀏覽器

$ kubectl version         # 驗證集羣版本 
$ kubectl get node        # 驗證節點就緒 (Ready) 狀態
$ kubectl get pod -A      # 驗證集羣pod狀態,默認已安裝網絡插件、coredns、metrics-server等
$ kubectl get svc -A      # 驗證集羣服務狀態
複製代碼
4. Dashboard
$ kubectl get svc -A

NAMESPACE     NAME                        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                  AGE
default       kubernetes                  ClusterIP   10.68.0.1       <none>        443/TCP                  8h
kube-system   dashboard-metrics-scraper   ClusterIP   10.68.117.166   <none>        8000/TCP                 15m
kube-system   kube-dns                    ClusterIP   10.68.0.2       <none>        53/UDP,53/TCP,9153/TCP   8h
kube-system   kubernetes-dashboard        NodePort    10.68.98.36     <none>        443:30001/TCP            15m
kube-system   metrics-server              ClusterIP   10.68.176.123   <none>        443/TCP                  8h
複製代碼

能夠看到Dashboard 暴露在30001端口下bash

使用Firefox瀏覽器 訪問對應IP的30001端口 https://xx.xx.xx.xx:30001

注: 這裏爲啥不用Chrome或者其餘嘞?

由於Dashboard的證書緣由,Chrome會顯示不是私密鏈接,而且徹底沒有辦法進入Dashboard,而Firefox就能夠跳過。

使用Token登陸Dashboard

# 建立Service Account 和 ClusterRoleBinding
$ kubectl apply -f /etc/ansible/manifests/dashboard/admin-user-sa-rbac.yaml
# 獲取 Bearer Token,找到輸出中 ‘token:’ 開頭那一行
$ kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')
複製代碼

k8s部署Go語言服務

下面使用kubeasz提供的實例,使用k8s部署Go應用

1. hello.go (github.com/easzlab/kub…)
package main

import (
	"fmt"
	"log"
	"math/rand"
	"net/http"
	"time"
)

var appVersion = "1.2" //Default/fallback version
var instanceNum int

func getFrontpage(w http.ResponseWriter, r *http.Request) {
	t := time.Now()
	fmt.Fprintf(w, "Hello, Go! I'm instance %d running version %s at %s\n", instanceNum, appVersion, t.Format("2019-01-02 15:04:05"))
}

func health(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
}

func getVersion(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "%s\n", appVersion)
}

func main() {
	rand.Seed(time.Now().UTC().UnixNano())
	instanceNum = rand.Intn(1000)
	http.HandleFunc("/", getFrontpage)
	http.HandleFunc("/health", health)
	http.HandleFunc("/version", getVersion)
	log.Fatal(http.ListenAndServe(":3000", nil))
}
複製代碼
2. Dockerfile
FROM golang:latest as builder

COPY *.go /app/

RUN cd /app && go build -o hellogo .

# stage 2: use alpine as base image
FROM golang:latest

COPY --from=builder /app/hellogo /hellogo

CMD ["/hellogo"]
複製代碼
3. 容器化hellogo.go
# 構建容器
docker build -t hellogo:v1.0 .
# 容器化運行hellogo服務
docker run -d --name hello -p3000:3000 hellogo:v1.0
# 測試
curl localhost:3000
Hello, Go! I'm instance 814 running version 1.2 at 21069-06-21 14:22:52 複製代碼

到此咱們已經容器化了咱們的Go Web程序,接下來使用k8s部署。

4. hellogo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
 name: hellogo-deploy
spec:
 replicas: 3
 minReadySeconds: 5 # Wait 5 seconds after each new pod comes up before marked as "ready"
 strategy:
 type: RollingUpdate # describe how we do rolling updates
 rollingUpdate:
 maxUnavailable: 1 # When updating take one pod down at a time
 maxSurge: 1
 selector:
 matchLabels:
 name: hellogo-app
 template:
 metadata:
 labels:
 name: hellogo-app
 spec:
 containers:
 - name: hellogo
 image: hellogo:v1.0
 imagePullPolicy: IfNotPresent
 resources:
 requests:
 memory: "32Mi"
 cpu: "50m"
 limits:
 memory: "64Mi"
 cpu: "100m"
 ports:
 - containerPort: 3000

---
apiVersion: v1
kind: Service
metadata:
 name: hellogo-svc
spec:
 type: NodePort
 ports:
 - name: http
 port: 80
 targetPort: 3000
 nodePort: 30002
 selector:
 name: hellogo-app
複製代碼
5. k8s部署

部署3個節點的hellogo服務

# 部署
$ kubectl apply -f hellogo.yaml

# 驗證
$ kubectl get pod

NAME                              READY   STATUS    RESTARTS   AGE
hellogo-deploy-646655bbf5-6nzdn   1/1     Running   8          17m
hellogo-deploy-646655bbf5-cjbvx   1/1     Running   8          17m
hellogo-deploy-646655bbf5-pkbxq   1/1     Running   8          17m

$kubectl get deploy
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
hellogo-deploy   3/3     3            3           20m

$kubectl get svc
NAME          TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
hellogo-svc   NodePort    10.68.199.96   <none>        80:30002/TCP   21m

複製代碼

驗證

root@ubuntu:/home/chen/hellogo# curl http://172.16.220.129:30002
Hello, Go! I'm instance 449 running version 1.2 at 21069-06-21 14:32:01 root@ubuntu:/home/chen/hellogo# curl http://172.16.220.129:30002 Hello, Go! I'm instance 867 running version 1.2 at 21069-06-21 14:32:02
root@ubuntu:/home/chen/hellogo# curl http://172.16.220.129:30002
Hello, Go! I'm instance 409 running version 1.2 at 21069-06-21 14:32:03 複製代碼

有3個不一樣id的服務返回了請求,說明部署正常。

結語

此次經過Kubeasz,咱們簡單的瞭解了k8s的基礎操做,簡單的體驗了一下k8s的基本操做,隨着學習的不斷深刻,將來我還會給你們分享一些本身學習k8s的經驗與體會。

謝謝~~~

相關文章
相關標籤/搜索