小丸子學Kubernetes系列之——Kubernetes安裝與使用
0.環境資訊
主機名 IP 角色
hadoop2 10.1.245.147 master
hadoop3 10.1.245.152 node
1.配置k8s的yum源(所有主機)
vi /etc/yum.repos.d/virt-testing.repo
[virt7-testing]
name=virt7-testing
baseurl=
gpgcheck=0
2.安裝kubernetes(所有主機)
yum -y install --enablerepo=virt7-testing kubernetes
3.安裝etcd(master主機)
yum install
4.配置hosts檔案(所有主機)
more /etc/hosts
10.1.245.147 hadoop2
10.1.245.152 hadoop3
5.編輯kubernetes配置檔案
vi /etc/kubernetes/config
# logging to stderr means we get it in the systemd journal
KUBE_LOGTOSTDERR="--logtostderr=true"
# journal message level, 0 is debug
KUBE_LOG_LEVEL="--v=0"
# Should this cluster be allowed to run privileged docker containers
KUBE_ALLOW_PRIV="--allow-privileged=false"
# How the controller-manager, scheduler, and proxy find the apiserver
KUBE_MASTER="--master="
6.關閉防火牆(所有主機)
systemctl disable iptables-services firewalld
systemctl stop iptables-services firewalld
7.編輯apiserver的配置檔案(master主機)
vi /etc/kubernetes/apiserver
# The address on the local server to listen to.
KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0"
# The port on the local server to listen on.
KUBE_API_PORT="--port=8080"
# Port minions listen on
KUBELET_PORT="--kubelet-port=10250"
# Comma separated list of nodes in the etcd cluster
KUBE_ETCD_SERVERS="--etcd-servers="
# Address range to use for services
KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16"
# default admission control policies
KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota"
# Add your own!
KUBE_API_ARGS=""
注:預設安裝的etcd的監聽埠在4001,所以etcd-servers必須指定埠為4001,否則apiserver服務會啟動失敗。
8.啟動k8s的服務(master主機)
more start_k8s.sh
#!/bin/bash
for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler; do
systemctl restart $SERVICES
systemctl enable $SERVICES
systemctl status $SERVICES
done
./start_k8s.sh
9.編輯kubelet配置檔案(node主機)
vi /etc/kubernetes/kubelet
# The address for the info server to serve on (set to 0.0.0.0 or "" for all interfaces)
KUBELET_ADDRESS="--address=0.0.0.0"
# The port for the info server to serve on
KUBELET_PORT="--port=10250"
# You may leave this blank to use the actual hostname
KUBELET_HOSTNAME="--hostname-override=hadoop3"
# location of the api-server
KUBELET_API_SERVER="--api-servers="
# pod infrastructure container
KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest"
# Add your own!
KUBELET_ARGS=""
10.啟動kubelet服務(node主機)
more start_kubelet.sh
#!/bin/bash
for SERVICES in kube-proxy kubelet docker; do
systemctl restart $SERVICES
systemctl enable $SERVICES
systemctl status $SERVICES
done
./start_kubelet.sh
11.使用kubectl命令來驗證安裝k8s成功(master主機)
kubectl cluster-info
kubectl get nodes
kubectl describe nodes hadoop3
12.建立一個mysql的pod
12.1 編寫pod的配置檔案
vi mysql.yaml
apiVersion: v1
kind: Pod
metadata:
name: mysql
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: mysql
ports:
- containerPort: 3306
hostPort: 3307
注:這裡需要指定環境變數MYSQL_ROOT_PASSWORD,否則建立pod會失敗,具體錯誤資訊如下:
Mar 7 16:33:57 localhost journal: error: database is uninitialized and password option is not specified
Mar 7 16:33:57 localhost journal: You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
12.2 建立mysql pod
kubectl create -f mysql.yaml
注:建立pod過程中遇到如下錯誤:
Error from server: error when creating "mysql.yaml": Pod "mysql" is forbidden: no API token found for service account default/default, retry after the token is automatically created and added to the service account
解決方法:
vi /etc/kubernetes/apiserver
KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"
修改KUBE_ADMISSION_CONTROL引數,將ServiceAccount選項去掉,然後重啟kube-apiserver
systemctl stop kube-apiserver
systemctl start kube-apiserver
kubectl create -f mysql.yaml
pod "mysql" created
13.測試mysql pod
[root@hadoop2 ~]# mysql -uroot -pmysql2 -hhadoop3 -P3307
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.11 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
至此Kubernetes初體驗成功,哈哈!
主機名 IP 角色
hadoop2 10.1.245.147 master
hadoop3 10.1.245.152 node
1.配置k8s的yum源(所有主機)
vi /etc/yum.repos.d/virt-testing.repo
[virt7-testing]
name=virt7-testing
baseurl=
gpgcheck=0
2.安裝kubernetes(所有主機)
yum -y install --enablerepo=virt7-testing kubernetes
3.安裝etcd(master主機)
yum install
4.配置hosts檔案(所有主機)
more /etc/hosts
10.1.245.147 hadoop2
10.1.245.152 hadoop3
5.編輯kubernetes配置檔案
vi /etc/kubernetes/config
# logging to stderr means we get it in the systemd journal
KUBE_LOGTOSTDERR="--logtostderr=true"
# journal message level, 0 is debug
KUBE_LOG_LEVEL="--v=0"
# Should this cluster be allowed to run privileged docker containers
KUBE_ALLOW_PRIV="--allow-privileged=false"
# How the controller-manager, scheduler, and proxy find the apiserver
KUBE_MASTER="--master="
6.關閉防火牆(所有主機)
systemctl disable iptables-services firewalld
systemctl stop iptables-services firewalld
7.編輯apiserver的配置檔案(master主機)
vi /etc/kubernetes/apiserver
# The address on the local server to listen to.
KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0"
# The port on the local server to listen on.
KUBE_API_PORT="--port=8080"
# Port minions listen on
KUBELET_PORT="--kubelet-port=10250"
# Comma separated list of nodes in the etcd cluster
KUBE_ETCD_SERVERS="--etcd-servers="
# Address range to use for services
KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16"
# default admission control policies
KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota"
# Add your own!
KUBE_API_ARGS=""
注:預設安裝的etcd的監聽埠在4001,所以etcd-servers必須指定埠為4001,否則apiserver服務會啟動失敗。
8.啟動k8s的服務(master主機)
more start_k8s.sh
#!/bin/bash
for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler; do
systemctl restart $SERVICES
systemctl enable $SERVICES
systemctl status $SERVICES
done
./start_k8s.sh
9.編輯kubelet配置檔案(node主機)
vi /etc/kubernetes/kubelet
# The address for the info server to serve on (set to 0.0.0.0 or "" for all interfaces)
KUBELET_ADDRESS="--address=0.0.0.0"
# The port for the info server to serve on
KUBELET_PORT="--port=10250"
# You may leave this blank to use the actual hostname
KUBELET_HOSTNAME="--hostname-override=hadoop3"
# location of the api-server
KUBELET_API_SERVER="--api-servers="
# pod infrastructure container
KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest"
# Add your own!
KUBELET_ARGS=""
10.啟動kubelet服務(node主機)
more start_kubelet.sh
#!/bin/bash
for SERVICES in kube-proxy kubelet docker; do
systemctl restart $SERVICES
systemctl enable $SERVICES
systemctl status $SERVICES
done
./start_kubelet.sh
11.使用kubectl命令來驗證安裝k8s成功(master主機)
kubectl cluster-info
kubectl get nodes
kubectl describe nodes hadoop3
12.建立一個mysql的pod
12.1 編寫pod的配置檔案
vi mysql.yaml
apiVersion: v1
kind: Pod
metadata:
name: mysql
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: mysql
ports:
- containerPort: 3306
hostPort: 3307
注:這裡需要指定環境變數MYSQL_ROOT_PASSWORD,否則建立pod會失敗,具體錯誤資訊如下:
Mar 7 16:33:57 localhost journal: error: database is uninitialized and password option is not specified
Mar 7 16:33:57 localhost journal: You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
12.2 建立mysql pod
kubectl create -f mysql.yaml
注:建立pod過程中遇到如下錯誤:
Error from server: error when creating "mysql.yaml": Pod "mysql" is forbidden: no API token found for service account default/default, retry after the token is automatically created and added to the service account
解決方法:
vi /etc/kubernetes/apiserver
KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"
修改KUBE_ADMISSION_CONTROL引數,將ServiceAccount選項去掉,然後重啟kube-apiserver
systemctl stop kube-apiserver
systemctl start kube-apiserver
kubectl create -f mysql.yaml
pod "mysql" created
13.測試mysql pod
[root@hadoop2 ~]# mysql -uroot -pmysql2 -hhadoop3 -P3307
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.11 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
至此Kubernetes初體驗成功,哈哈!
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/20801486/viewspace-2050906/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 從0到1使用Kubernetes系列(三)——使用Ansible安裝Kubernetes叢集
- 使用kubeadm安裝kubernetes
- 【kubernetes叢集系列(二)】Worker(node)安裝(使用kubeadm)
- kubernetes安裝配置使用vGPUGPU
- 使用 kubeadm 安裝 Kubernetes 1.16
- kubernetes 安裝
- 安裝 kubernetes
- Kubernetes 入門與安裝部署
- Kubernetes安裝之八:配置master之schedulerAST
- kubernetes系列(十六) - Helm安裝和入門
- kubernetes實戰篇之helm安裝
- GitOps實踐之kubernetes安裝argocdGitGo
- 從0到1使用Kubernetes系列(二)——安裝工具介紹
- [kubernetes]helm安裝
- Kubernetes的安裝
- Ubuntu安裝KubernetesUbuntu
- openEuler安裝Kubernetes
- Kubernetes - 安裝方法
- Kubernetes Dashboard 安裝
- CentOS 7.6 使用kubeadm安裝Kubernetes 13CentOS
- kubernetes單機安裝
- kubernetes的安裝方法
- Kubernetes: CentOS上如何安裝指定版本的KubernetesCentOS
- Kubernetes:05---安裝Kubernetes的系統要求
- 從0到1使用Kubernetes系列——Kubernetes入門
- Kubernetes安裝之六:配置master之api-serverASTAPIServer
- Kubernetes安裝之七:配置master之controller-managerASTController
- Kubernetes安裝之九:配置node節點之kubelet
- 線上學習安裝Istio和Kubernetes
- 入門系列之Kubernetes部署
- Kubernetes安裝之五:配置kubectl客戶端客戶端
- kubernetes實踐之五:Node節點安裝
- 安裝 kubernetes v1.11.1
- Kubernetes單機快速安裝
- kubernetes系列(十七) - 通過helm安裝dashboard詳細教程
- Kubernetes 網路學習之 Cilium 與 eBPFeBPF
- kubernetes系列(十三) - 儲存之Volume
- kubernetes系列(十二) - 儲存之Secret
- kubernetes系列(十四) - 儲存之PersistentVolume