k8s學習筆記一(搭建&部署helloworld應用)

靑い空゛發表於2022-03-23

kubernetes


虛擬機器建立三個節點

這裡略過.

  • 網路模式用的hostonly
  • 系統ubuntu 20.04

k8s install

set -x
#根據規劃設定主機名(在3臺機上分別執行)
hostnamectl set-hostname master01
hostnamectl set-hostname node01
hostnamectl set-hostname node02

#關閉防火牆
ufw disable

#關閉swap
swapoff -a && sed -ri 's/.*swap.*/#&/' /etc/fstab

#安裝 docker
sudo apt-get install -y docker
sudo apt-get install -y docker.io 
docker --version 
systemctl start docker

apt-get install -y apt-transport-https
apt-get install -y curl

# kubeadm init 預設會去google拉映象,所以換了阿里的源,前面docker是系統預設的.
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - 

cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF

apt-get update

# master上執行
apt-get install -y kubelet kubeadm kubectl

# node上執行
apt-get install -y kubelet kubeadm
apt-get install -y ethtool
apt-get install -y socat
apt-get install -y conntrack

# check
kubeadm version

# 配置docker cgroupdriver (與kubelet要一致)
cat <<EOF >/etc/docker/daemon.json
{"exec-opts": ["native.cgroupdriver=systemd"]}
EOF

systemctl restart docker

systemctl start kubelet

export KUBECONFIG=/etc/kubernetes/admin.conf
# master 上執行
kubeadm init --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16

#Your Kubernetes control-plane has initialized successfully!
#
#To start using your cluster, you need to run the following as a regular user:
#
#  mkdir -p $HOME/.kube
#  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
#  sudo chown $(id -u):$(id -g) $HOME/.kube/config
#
#Alternatively, if you are the root user, you can run:
#
#  export KUBECONFIG=/etc/kubernetes/admin.conf
#
#You should now deploy a pod network to the cluster.
#Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
#  https://kubernetes.io/docs/concepts/cluster-administration/addons/
#
#Then you can join any number of worker nodes by running the following on each as root:
#
#kubeadm join 192.168.137.6:6443 --token vzcxza.bhcs335r92677g3i \
#	--discovery-token-ca-cert-hash sha256:72b820c8f2dd7606ddbf6a155fadb57e0f266c15bb39c7f41de61cc605f6f962 

# kubeadm init 通過給的 kubeadm join 將node 加入master 節點
kubeadm join 192.168.137.6:6443 --token vzcxza.bhcs335r92677g3i \
	--discovery-token-ca-cert-hash sha256:72b820c8f2dd7606ddbf6a155fadb57e0f266c15bb39c7f41de61cc605f6f962

# K8S在kubeadm init以後查詢kubeadm join
# kubeadm token create --print-join-command

set +x


到這1個master節點2個node節點的k8s就搭好了
可以用kubect get xxx 檢查狀態,(我這裡剛搭完還是 notReady 狀態,檢查後是網路外掛沒裝,遇到的話可以參考後面issue彙總裡面)

root@master01:~/k8s# kubectl get nodes -A
NAME       STATUS   ROLES                  AGE     VERSION
master01   Ready    control-plane,master   4d17h   v1.23.5
node01     Ready    <none>                 2d      v1.23.5
node02     Ready    <none>                 2d      v1.23.5

部署hello world 應用

  • app :
package main

import (
	"net/http"
	"flag"
	log "github.com/sirupsen/logrus"
	"github.com/gin-gonic/gin"
)

func entry(c *gin.Context) {
	c.String(http.StatusOK, "hello !")
}

func main() {

	flag.Parse()

	var (
	)

	log.Info("hello serv start ...")

	engine := gin.Default()
	engine.GET("/entry", entry)

	engine.Run("0.0.0.0:8868")

}
  • dockerfile
FROM golang:1.17.2-stretch

ENV GOPROXY=http://goproxy.io

WORKDIR $GOPATH/src/
ADD ./bin $GOPATH/src/bin
ADD ./source $GOPATH/src/
RUN go env -w GOSUMDB=off

WORKDIR $GOPATH/src/
RUN go build -o /usr/local/bin/engine
CMD ["/usr/local/bin/engine"]
  • yml
    很久前玩minikube 的映象: ailumiyana/minikube-hello:latest, 以前就放在docker hub了,app 就是上面的go程式碼,有需要自取,但這裡建議開啟docker代理,不然下載很慢或者可能都下不下來
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-minikubes
spec:
  selector:
    matchLabels:
      app: myweb
  replicas: 2
  template:
    metadata:
      labels:
        app: myweb
    spec:
      containers:
        - name: hello-minikubes
          image: ailumiyana/minikube-hello:latest
          ports:
            - containerPort: 8868
              protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
  name: myweb
spec:
  type: NodePort
  ports:
    - port: 8868
      targetPort: 8868
      nodePort: 31314
  selector:
    app: myweb
kubectl apply -f hello.yml

F5幾下,檢查兩個節點,好像都有流量,還不知道什麼策略,慢慢研究~

今天先到這~

issue 彙總

node 一直處理NotReady狀態

  • kubectl describe node node01
    檢查到notready 狀態 原因:
  • untime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized

好像是沒網路外掛,搜尋嘗試手動新增 flannel 映象和 cni 配置 後解決

docker pull quay.io/coreos/flannel:v0.11.0-amd64

mkdir -p /etc/cni/net.d/

cat <<EOF> /etc/cni/net.d/10-flannel.conf
{"name":"cbr0","type":"flannel","delegate": {"isDefaultGateway": true}}
EOF

mkdir /usr/share/oci-umount/oci-umount.d -p

mkdir /run/flannel/

cat <<EOF> /run/flannel/subnet.env
FLANNEL_NETWORK=172.100.0.0/16
FLANNEL_SUBNET=172.100.1.0/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=true
EOF

https://raw.githubusercontent.com/coreos/flannel/v0.11.0/Documentation/kube-flannel.yml
然後這個yml 裡面beta版本貌似是過期的,改成v1 後好了

重啟系統後虛擬機器的網路不通

這個也是個坑,最後在網路連線裡面,重新關閉開啟vmnet1的網路共享後好了.

相關文章