microk8s使用
在使用microk8s时,可以设置一些别名,使用起来保持与k8s习惯一样,具体如下
sudo snap alias microk8s.kubectl kubectl
sudo snap alias microk8s.ctr ctr
sudo snap alias microk8s.helm helm
sudo snap alias microk8s.helm3 helm3
部署microk8s后,排查集群状态
microk8s status
microk8s.inspect
docker-registry搭建私有镜像仓库
docker pull registry:2
docker create volume registry_data
mkdir /opt/data/auth
docker run --entrypoint htpasswd registry:2 -Bbn admin Harbor@dmin.mec > auth/htpasswd
docker run -d -p 5000:5000 --restart=always --name registry \-v registry_data:/var/lib/registry \-v /opt/data/auth:/auth \-e "REGISTRY_AUTH=htpasswd" \-e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \-e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd \registry:2
docker login -u username -p userpasswd 192.168.1.33:5000
保存镜像
docker save docker.io/calico/cni:v3.25.1 docker.io/calico/kube-controllers:v3.25.1 docker.io/calico/node:v3.25.1 docker.io/cdkbot/hostpath-provisioner:1.5.0 docker.io/coredns/coredns:1.10.1 docker.io/library/busybox:1.28.4 registry.k8s.io/ingress-nginx/controller:v1.8.0 registry.k8s.io/metrics-server/metrics-server:v0.6.3 registry.k8s.io/pause:3.7 -o microk8s.tar
导入镜像
microk8s ctr i import microk8s.tar
配置集群与访问
microk8s config > ~/.kube/config
kubectl get node -o wide
kubectl get pod -A
启用插件host-path和ingress-controller,metrics-server.
microk8s status
microk8s enable hostpath-storage
microk8s enable ingress
microk8s enable metrics-server
验证
kubectl get pod -A
microk8s status
资源占用量
root@xww-nuc8i5beh:/media/xww/sda1/server/microk8s# kubectl describe node
Name: xww-nuc8i5beh
Roles: <none>
Labels: beta.kubernetes.io/arch=amd64beta.kubernetes.io/os=linuxkubernetes.io/arch=amd64kubernetes.io/hostname=xww-nuc8i5behkubernetes.io/os=linuxmicrok8s.io/cluster=truenode.kubernetes.io/microk8s-controlplane=microk8s-controlplane
Annotations: node.alpha.kubernetes.io/ttl: 0projectcalico.org/IPv4Address: 192.168.10.102/24projectcalico.org/IPv4VXLANTunnelAddr: 10.1.181.128volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp: Fri, 12 Jan 2024 20:24:04 +0800
Taints: <none>
Unschedulable: false
Lease:HolderIdentity: xww-nuc8i5behAcquireTime: <unset>RenewTime: Sat, 13 Jan 2024 11:12:38 +0800
Conditions:Type Status LastHeartbeatTime LastTransitionTime Reason Message---- ------ ----------------- ------------------ ------ -------NetworkUnavailable False Sat, 13 Jan 2024 11:01:36 +0800 Sat, 13 Jan 2024 11:01:36 +0800 CalicoIsUp Calico is running on this nodeMemoryPressure False Sat, 13 Jan 2024 11:09:49 +0800 Fri, 12 Jan 2024 20:24:04 +0800 KubeletHasSufficientMemory kubelet has sufficient memory availableDiskPressure False Sat, 13 Jan 2024 11:09:49 +0800 Fri, 12 Jan 2024 20:24:04 +0800 KubeletHasNoDiskPressure kubelet has no disk pressurePIDPressure False Sat, 13 Jan 2024 11:09:49 +0800 Fri, 12 Jan 2024 20:24:04 +0800 KubeletHasSufficientPID kubelet has sufficient PID availableReady True Sat, 13 Jan 2024 11:09:49 +0800 Sat, 13 Jan 2024 11:01:40 +0800 KubeletReady kubelet is posting ready status. AppArmor enabled
Addresses:InternalIP: 192.168.10.102Hostname: xww-nuc8i5beh
Capacity:cpu: 8ephemeral-storage: 245084444Kihugepages-1Gi: 0hugepages-2Mi: 0memory: 32867920Kipods: 110
Allocatable:cpu: 8ephemeral-storage: 244035868Kihugepages-1Gi: 0hugepages-2Mi: 0memory: 32765520Kipods: 110
System Info:Machine ID: 105c044a9dba42f5a4740ef13b5692ebSystem UUID: B17F28E3-2822-F3FD-A1F3-94C691A75966Boot ID: 6bac866c-d336-4606-9c47-6b01f110baefKernel Version: 4.15.0-109-genericOS Image: Ubuntu 18.04.4 LTSOperating System: linuxArchitecture: amd64Container Runtime Version: containerd://1.6.15Kubelet Version: v1.28.3Kube-Proxy Version: v1.28.3
Non-terminated Pods: (6 in total)Namespace Name CPU Requests CPU Limits Memory Requests Memory Limits AGE--------- ---- ------------ ---------- --------------- ------------- ---kube-system calico-node-dp5tr 250m (3%) 0 (0%) 0 (0%) 0 (0%) 14hkube-system calico-kube-controllers-77bd7c5b-fph2n 0 (0%) 0 (0%) 0 (0%) 0 (0%) 14hkube-system coredns-864597b5fd-n5pzq 100m (1%) 0 (0%) 70Mi (0%) 170Mi (0%) 14hingress nginx-ingress-microk8s-controller-z5plk 0 (0%) 0 (0%) 0 (0%) 0 (0%) 3m36skube-system hostpath-provisioner-7df77bc496-q4zp6 0 (0%) 0 (0%) 0 (0%) 0 (0%) 3m36skube-system metrics-server-848968bdcd-k5w78 100m (1%) 0 (0%) 200Mi (0%) 0 (0%) 3m35s
Allocated resources:(Total limits may be over 100 percent, i.e., overcommitted.)Resource Requests Limits-------- -------- ------cpu 450m (5%) 0 (0%)memory 270Mi (0%) 170Mi (0%)ephemeral-storage 0 (0%) 0 (0%)hugepages-1Gi 0 (0%) 0 (0%)hugepages-2Mi 0 (0%) 0 (0%)
Events: <none>
运行nginx进行测试
apiVersion: v1 #类型为Namespace
kind: Namespace #类型为Namespace
metadata:name: ns-test #命名空间名称labels:name: label-test #pod标签
---
apiVersion: apps/v1
kind: Deployment
metadata:namespace: ns-testname: nginx-deployment
spec:selector:matchLabels:app: nginxreplicas: 2template:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:alpineports:- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:namespace: ns-testname: nginx-service
spec:selector:app: nginxtype: NodePortports:- protocol: TCPport: 80targetPort: 80nodePort: 30080
部署和验证
kubectl apply -f nginx.yaml
curl http://192.168.10.102:30080
部署有状态服务
# cat pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: mypvc1namespace: ns-test
spec:accessModes:- ReadWriteOnceresources:requests:storage: 1GistorageClassName: microk8s-hostpath
有状态的nginx服务
# cat nginx.yaml
apiVersion: v1 #类型为Namespace
kind: Namespace #类型为Namespace
metadata:name: ns-test #命名空间名称labels:name: label-test #pod标签
---
apiVersion: apps/v1
kind: Deployment
metadata:namespace: ns-testname: nginx-deployment
spec:selector:matchLabels:app: nginxreplicas: 2template:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:alpineports:- containerPort: 80volumeMounts:- mountPath: "/mydata"name: mydatavolumes:- name: mydatapersistentVolumeClaim:claimName: mypvc1
---
apiVersion: v1
kind: Service
metadata:namespace: ns-testname: nginx-service
spec:selector:app: nginxtype: NodePortports:- protocol: TCPport: 80targetPort: 80nodePort: 30080
验证
kubectl apply -f pvc.yaml
kubectl apply -f nginx.yaml
kubectl get pvc -A
kubectl get pod -A