iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 4
0

前言

前面安裝好kubelet, kubeadm, kubectl了,接下來來實施並驗證Kubernetes功能。

4. Turn off swap area
swapoff -a
change DNS server
vim /etc/resolv.conf

nameserver 8.8.8.8
5. Initializing your master and create a cluster
kubeadm init --pod-network-cidr=10.244.0.0/16

6. To start using your cluster, you need to run the following as a regular user
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
7-1. Node isolation (Single Node)
kubectl taint nodes --all node-role.kubernetes.io/master-
7-2. Join your node
kubeadm join <master-ip>:<master-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
8. Installing a pod network add-on (CNI Plugin - Flannel)
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/62e44c867a2846fefb68bd5f178daf4da3095ccb/Documentation/kube-flannel.yml
9. Check your master node is ready
kubectl get node
or 
watch kubectl get node

4. Implement and verify Kubernetes feature

1. Create a Deployment based on the YAML file
wget https://k8s.io/examples/application/deployment.yaml
kubectl apply -f deployment.yaml
2. Display information about the Deployment
kubectl describe deployment nginx-deployment
3. Display information about a pod
kubectl get pod
kubectl describe pod nginx-deployment-xxx-xxx
4. Change deployment replica
kubectl edit deployment nginx-deployment

watch kubectl get pod 
5. Change deployment image version
kubectl edit deployment nginx-deployment

watch kubectl get pod
6. Test self-heal
kubectl get pod
kubectl delete pod nginx-deployment-xxx-xxx

watch kubectl get pod
7. Create a service resource
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
8. Look your service NodePort
kubectl get service
9. Use web browser to watch your service on Kubernetes, Enter on URL :
http://10.27.4.x:3xxxx
10. Edit web file to verify that is load balance
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
...
11. Edit deployment.yaml
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
12. Add a volume on web path
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
13. Add a index.html in /mnt/
cd /mnt/

cat <<EOF > index.html 
<input what you want>
EOF

參考連結


上一篇
Day 3 - Kubernetes安裝1
下一篇
Day 5 - Kubernetes kubelet, kubeadm, kubectl 介紹
系列文
SDN/NFV 網路虛擬化調度平台30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言