目录

Docker Desktop 安装 K8s

Apple Chip M1 安装 k8s。

Docker Desktop 安装 k8s

1
2
3
4
5
cd ~/Apps/
git clone https://github.com/AliyunContainerService/k8s-for-docker-desktop.git
# 通过 docker desktop about k8s 版本
cd k8s-for-docker-desktop
git checkout v1.22.5

阿里云镜像服务加速

1
2
3
4
5
6
7
{
  "features": {
    "buildkit": true
  },
  "experimental": false,
  "registry-mirrors": ["https://xxxxxxx.mirror.aliyuncs.com"]
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
./load_images.sh
docker images

REPOSITORY                                      TAG       IMAGE ID       CREATED         SIZE
k8s.gcr.io/ingress-nginx/controller             v1.1.1    fcd5f7d32d48   3 months ago    284MB
k8s.gcr.io/kube-apiserver                       v1.22.5   66ba38aa55f7   4 months ago    119MB
k8s.gcr.io/kube-controller-manager              v1.22.5   fc9438491212   4 months ago    113MB
k8s.gcr.io/kube-proxy                           v1.22.5   d665e23ad35c   4 months ago    97.4MB
k8s.gcr.io/kube-scheduler                       v1.22.5   ecf6859f4d39   4 months ago    49.3MB
k8s.gcr.io/ingress-nginx/kube-webhook-certgen   v1.1.1    9eb277b91d6e   6 months ago    45.1MB
k8s.gcr.io/etcd                                 3.5.0-0   2252d5eb703b   10 months ago   364MB
k8s.gcr.io/coredns/coredns                      v1.8.4    6d3ffc2696ac   11 months ago   44.4MB
k8s.gcr.io/pause                                3.5       f7ff3c404263   13 months ago   484kB

Docker Desktop 开启 Kubernetes,并等待 Kubernetes 开始运行。

1
2
3
4
5
# 切换Kubernetes运行上下文至 docker-desktop
kubectl config use-context docker-desktop
# 验证 Kubernetes 集群状态
kubectl cluster-info
kubectl get nodes

dashboard

1
2
3
4
# 部署 Kubernetes dashboard
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.4.0/aio/deploy/recommended.yaml
# 检查 kubernetes-dashboard 应用状态
kubectl get pod -n kubernetes-dashboard

kubectl proxy 本地访问

kubectl proxy 的原理是将机器与kubernetes API Server之间做一个代理,默认情况下,只能从本机访问。

因为Dashboard只允许localhost和127.0.0.1使用HTTP连接进行访问,而其它地址只允许使用HTTPS。

1
2
3
4
5
6
7
8
9
# 开启 API Server 访问代理
kubectl proxy

# 配置控制台访问令牌
TOKEN=$(kubectl -n kube-system describe secret default| awk '$1=="token:"{print $2}')
kubectl config set-credentials docker-desktop --token="${TOKEN}"
echo $TOKEN

cat ~/.kube/config

通过如下 URL 访问 Kubernetes dashboard

http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 配置一个拥有完整权限的token.

# 创建一个dashboard管理用户
kubectl create serviceaccount dashboard-admin -n kube-system
# 绑定用户为集群管理用户
kubectl create clusterrolebinding dashboard-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
# 执行完以上操作后,由于管理用户的名称为dashboard-admin,生成的对应的secret的值则为dashboard-admin-token-随机字符串我的机器上完整名称为dashboard-admin-token-sg6bp

kubectl get secret -n kube-system |grep dashboard-admin-token
kubectl describe -n=kube-system  secret dashboard-admin-token-7rqzx
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
apiVersion: v1
kind: ServiceAccount
metadata:
  name: dashboard-admin
  namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: dashboard-admin
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: dashboard-admin
  namespace: kube-system
1
2
kubectl create -f dashboard-admin-user.yaml
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')

NodePort

NodePort是将节点直接暴露在外网的一种方式,只建议在开发环境,单节点的安装方式中使用。

启用NodePort很简单,只需执行在yaml配置文件中定义service时显视的定义NodePort即可,如下所示: 添加type: NodePort即可(默认是ClusterIP)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
spec:
  ports:
    - port: 443
      targetPort: 8443
  type: NodePort
  selector:
    k8s-app: kubernetes-dashboard
1
kubectl apply -f dashboard-v2.0.0-beta2.yaml

由于在正式环境中,并不推荐使用NodePort的方式来访问Dashboard。 证书的配置后续再研究

LoadBalacer

API Server 推荐

API Server访问方式和proxy方式最大的区别在于,路径开头是https的。 如果Kubernetes API服务器是公开的,并可以从外部访问,那我们可以直接使用API Server的方式来访问,也是比较推荐的方式。

Dashboard的访问地址为: https://<master-ip>:<apiserver-port>/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/

最新版的k8s默认启用了RBAC,并为未认证用户赋予了一个默认的身份:anonymous。 对于API Server来说,它是使用证书进行认证的,我们需要先创建一个证书:

首先找到kubectl命令的配置文件,默认情况下为/etc/kubernetes/admin.conf,确保已经复制到了$HOME/.kube/config中。 使用client-certificate-data和client-key-data生成一个p12文件,可使用下列命令

1
2
3
4
5
6
# 生成client-certificate-data
grep 'client-certificate-data' ~/.kube/config | head -n 1 | awk '{print $2}' | base64 -d >> kubecfg.crt
# 生成client-key-data
grep 'client-key-data' ~/.kube/config | head -n 1 | awk '{print $2}' | base64 -d >> kubecfg.key
# 生成p12
openssl pkcs12 -export -clcerts -inkey kubecfg.key -in kubecfg.crt -out kubecfg.p12 -name "kubernetes-client"

导入上面生成的p12文件,这一步自行到网上参考。最后好像还需要对浏览器清理缓存。 最后导入上面生成的p12文件,重新打开浏览器,

注:对于生产系统,我们应该为每个用户应该生成自己的证书,因为不同的用户会有不同的命名空间访问权限。

Ingress

https://github.com/kubernetes/ingress-nginx/blob/master/docs/deploy/index.md

Ingress将开源的反向代理负载均衡器(如 Nginx、Apache、Haproxy等)与k8s进行集成,并可以动态的更新Nginx配置等,是比较灵活,更为推荐的暴露服务的方式。

1
2
3
4
5
6
7
# 安装 Ingress
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.2.0/deploy/static/provider/cloud/deploy.yaml
# 验证
kubectl get pods --all-namespaces -l app.kubernetes.io/name=ingress-nginx

# 删除 Ingress
kubectl delete -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.2.0/deploy/static/provider/cloud/deploy.yaml
1
2
3
4
5
6
kubectl get role -n kubernetes-dashboard
kubectl describe role kubernetes-dashboard -n kubernetes-dashboard

kubectl get secret -n kubernetes-dashboard

kubectl get clusterrolebinding -n  kubernetes-dashboard -o wide

kubectl

如果用户已经拥有一个集群,可以使用 kubectl 与集群进行交互, 那么很可能在 $HOME/.kube 目录下有一个名为 config 的文件。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
kubectl version --client
# 将它追加到 KUBECONFIG 环境变量中
export KUBECONFIG=$KUBECONFIG:$HOME/.kube/config
# 查看当前 KUBECONFIG 环境变量中列举的所有文件合并后的配置信息
kubectl config view

kubectl get pod -n kube-system
kubectl get pod -n kube-system -o wide

# 快速地在集群之间进行切换。
kubectl config use-context <context-name>

配置对多集群的访问

`vim ~/.kube/config-exercise/config-demo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: v1
kind: Config
preferences: {}

clusters:
- cluster:
  name: development
- cluster:
  name: scratch

users:
- name: developer
- name: experimenter

contexts:
- context:
  name: dev-frontend
- context:
  name: dev-storage
- context:
  name: exp-scratch
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
cd ~/.kube/config-exercise
# 将群集详细信息添加到配置文件中
kubectl config --kubeconfig=config-demo set-cluster development --server=https://1.2.3.4 --certificate-authority=fake-ca-file
kubectl config --kubeconfig=config-demo set-cluster scratch --server=https://5.6.7.8 --insecure-skip-tls-verify
# 将用户详细信息添加到配置文件中
kubectl config --kubeconfig=config-demo set-credentials developer --client-certificate=fake-cert-file --client-key=fake-key-seefile
kubectl config --kubeconfig=config-demo set-credentials experimenter --username=exp --password=some-password
# 将上下文详细信息添加到配置文件中
kubectl config --kubeconfig=config-demo set-context dev-frontend --cluster=development --namespace=frontend --user=developer
kubectl config --kubeconfig=config-demo set-context dev-storage --cluster=development --namespace=storage --user=developer
kubectl config --kubeconfig=config-demo set-context exp-scratch --cluster=scratch --namespace=default --user=experimenter
# 打开 config-demo 文件查看添加的详细信息。 也可以使用 config view 命令进行查看
kubectl config --kubeconfig=config-demo view
# 设置当前上下文 现在当输入 kubectl 命令时,相应动作会应用于 dev-frontend 上下文中所列的集群和名字空间, 同时,命令会使用 dev-frontend 上下文中所列用户的凭证。
kubectl config --kubeconfig=config-demo use-context dev-frontend
# 使用 --minify 参数,来查看与当前上下文相关联的配置信息。
kubectl config --kubeconfig=config-demo view --minify

export  KUBECONFIG=$KUBECONFIG:config-demo:config-demo-2
kubectl config view
  • 要删除用户,可以运行 kubectl --kubeconfig=config-demo config unset users.<name>
  • 要删除集群,可以运行 kubectl --kubeconfig=config-demo config unset clusters.<name>
  • 要删除上下文,可以运行 kubectl --kubeconfig=config-demo config unset contexts.<name>

helm

1
2
3
4
5
6
7
8
# Use homebrew on Mac
brew install helm

# Add helm repo
helm repo add stable http://mirror.azure.cn/kubernetes/charts/

# Update charts repo
helm repo update

附录