#!/bin/bashimages_name=`kubeadm config images list|awk -F '/''{print $2}'|xargs`for i in $images_namedodocker pull registry.cn-hangzhou.aliyuncs.com/google_containers/$idocker tag registry.cn-hangzhou.aliyuncs.com/google_containers/$i k8s.gcr.io/$idocker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/$idone
check 是否完成镜像的下载
命令:docker images
12345678
REPOSITORY TAG IMAGE ID CREATED SIZE
k8s.gcr.io/kube-proxy v1.13.4 fadcc5d2b066 5 days ago 80.3MB
k8s.gcr.io/kube-apiserver v1.13.4 fc3801f0fc54 5 days ago 181MB
k8s.gcr.io/kube-controller-manager v1.13.4 40a817357014 5 days ago 146MB
k8s.gcr.io/kube-scheduler v1.13.4 dd862b749309 5 days ago 79.6MB
k8s.gcr.io/coredns 1.2.6 f59dcacceff4 4 months ago 40MB
k8s.gcr.io/etcd 3.2.24 3cab8e1b9802 5 months ago 220MB
k8s.gcr.io/pause 3.1 da86e6ba6ca1 14 months ago 742kB
开机启动kubelet.service
1234
#开启服务ansible k8s_test -m command -a "systemctl enable kubelet.service"#查看服务是否已开启ansible k8s_test -m shell -a "systemctl list-unit-files|grep kubelet"
[init] Using Kubernetes version: v1.13.4
[preflight] Running pre-flight checks
[WARNING Hostname]: hostname "ktest01" could not be reached
[WARNING Hostname]: hostname "ktest01": lookup ktest01 on 10.10.9.98:53: no such host
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"[kubelet-start] Activating the kubelet service
[certs] Using certificateDir folder "/etc/kubernetes/pki"[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [ktest01 localhost] and IPs [10.10.9.89 127.0.0.1 ::1][certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [ktest01 localhost] and IPs [10.10.9.89 127.0.0.1 ::1][certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [ktest01 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 10.10.9.89][certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[control-plane] Using manifest folder "/etc/kubernetes/manifests"[control-plane] Creating static Pod manifest for"kube-apiserver"[control-plane] Creating static Pod manifest for"kube-controller-manager"[control-plane] Creating static Pod manifest for"kube-scheduler"[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 21.502598 seconds
[uploadconfig] storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.13" in namespace kube-system with the configuration for the kubelets in the cluster
[patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "ktest01" as an annotation
[mark-control-plane] Marking the node ktest01 as control-plane by adding the label "node-role.kubernetes.io/master=''"[mark-control-plane] Marking the node ktest01 as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule][bootstrap-token] Using token: iubzz7.7qmxaohjhk553dpx
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstraptoken] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstraptoken] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstraptoken] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstraptoken] creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
Your Kubernetes master 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
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/
You can now join any number of machines by running the following on each node
as root:
kubeadm join 10.10.9.89:6443 --token iubzz7.7qmxaohjhk553dpx --discovery-token-ca-cert-hash sha256:7121557de50b8d40c34b98ad3eb0b34f11444434354af494c0e63e57aafde631
podsecuritypolicy.extensions/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.extensions/kube-flannel-ds-amd64 created
daemonset.extensions/kube-flannel-ds-arm64 created
daemonset.extensions/kube-flannel-ds-arm created
daemonset.extensions/kube-flannel-ds-ppc64le created
daemonset.extensions/kube-flannel-ds-s390x created
kubectl run curl --image=radial/busyboxplus:curl -it
如出现如下情况,可通过下面命令来确认状态并选择进入方式
12345
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
Error from server (AlreadyExists): deployments.apps "curl" already exists
kubectl get pod
kubectl describe pod curl-66959f6557-4m9p5
kubectl exec -it curl-66959f6557-4m9p5 /bin/sh
NAME STATUS ROLES AGE VERSION
ktest01 Ready master 5d23h v1.13.4
ktest02 Ready <none> 4d22h v1.13.4
ktest03 Ready <none> 4d22h v1.13.4
节点移除
查看pod情况
123
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
curl-66959f6557-4m9p5 1/1 Running 0 5d22h 10.60.1.2 ktest02 <none> <none>
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
curl-66959f6557-wqqkj 1/1 Running 0 58s 10.60.2.2 ktest03 <none> <none>
kubectl get nodes
NAME STATUS ROLES AGE VERSION
ktest01 Ready master 5d23h v1.13.4
ktest02 Ready,SchedulingDisabled <none> 4d22h v1.13.4
ktest03 Ready <none> 4d22h v1.13.4
kubeadm reset
ifconfig cni0 down
ip link delete cni0
ifconfig flannel.1 down
ip link delete flannel.1
rm -rf /var/lib/cni/
重新加入该节点
如果忘记之前的token,可通过以下命令进行获取
123
kubeadm token list
TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS
iubzz7.7qmxaohjhk553dpx <forever> <never> authentication,signing The default bootstrap token generated by 'kubeadm init'. system:bootstrappers:kubeadm:default-node-token
###ktest02 nodecurl http://10.20.161.159:30001
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body { width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
###ktest03 nodecurl http://10.20.161.155:30001
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body { width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>