前面安裝好kubelet, kubeadm, kubectl了,接下來來實施並驗證Kubernetes功能。
swapoff -a
vim /etc/resolv.conf
nameserver 8.8.8.8
kubeadm init --pod-network-cidr=10.244.0.0/16
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
kubectl taint nodes --all node-role.kubernetes.io/master-
kubeadm join <master-ip>:<master-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/62e44c867a2846fefb68bd5f178daf4da3095ccb/Documentation/kube-flannel.yml
kubectl get node
or
watch kubectl get node
wget https://k8s.io/examples/application/deployment.yaml
kubectl apply -f deployment.yaml
kubectl describe deployment nginx-deployment
kubectl get pod
kubectl describe pod nginx-deployment-xxx-xxx
kubectl edit deployment nginx-deployment
watch kubectl get pod
kubectl edit deployment nginx-deployment
watch kubectl get pod
kubectl get pod
kubectl delete pod nginx-deployment-xxx-xxx
watch kubectl get pod
cat <<EOF > service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
type: NodePort
EOF
kubectl apply -f service.yaml
kubectl get service
http://10.27.4.x:3xxxx
kubectl exec -it nginx-deployment-xxx-xxx bash
echo 1 >> /usr/share/nginx/html/index.html
echo 2 >> /usr/share/nginx/html/index.html
echo 3 >> /usr/share/nginx/html/index.html
...
vim deployment.yaml
...
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
volumeMounts:
- name: web-persistent-storage
mountPath: /usr/share/nginx/html/
volumes:
- name: web-persistent-storage
persistentVolumeClaim:
claimName: web-pv-claim
kubectl apply -f deployment.yaml
cat <<EOF > pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: web-pv-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
EOF
cat <<EOF > pv.yaml
kind: PersistentVolume
apiVersion: v1
metadata:
name: web-pv-volume
labels:
type: local
spec:
capacity:
storage: 20Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/"
EOF
kubectl apply -f pv.yaml
kubectl apply -f pvc.yaml
cd /mnt/
cat <<EOF > index.html
<input what you want>
EOF